Skip to content

Commit

Permalink
refactoring: Babel's error recovery superseded option combinations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 authored and lydell committed Nov 11, 2019
1 parent 4e49ff9 commit f99e3d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
40 changes: 4 additions & 36 deletions src/language-js/parser-babylon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const hasPragma = require("./pragma").hasPragma;
const locFns = require("./loc");
const postprocess = require("./postprocess");

function babelOptions(extraOptions, extraPlugins) {
function babelOptions(extraOptions, extraPlugins = []) {
return Object.assign(
{
sourceType: "module",
Expand Down Expand Up @@ -41,7 +41,8 @@ function babelOptions(extraOptions, extraPlugins) {
"logicalAssignment",
"classPrivateMethods",
"v8intrinsic",
"partialApplication"
"partialApplication",
["decorators", { decoratorsBeforeExport: false }]
].concat(extraPlugins)
},
extraOptions
Expand All @@ -53,28 +54,9 @@ function createParse(parseMethod, extraPlugins) {
// Inline the require to avoid loading all the JS if we don't use it
const babel = require("@babel/parser");

const combinations = [
babelOptions(
{ strictMode: true },
["decorators-legacy"].concat(extraPlugins)
),
babelOptions(
{ strictMode: false },
["decorators-legacy"].concat(extraPlugins)
),
babelOptions(
{ strictMode: true },
[["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins)
),
babelOptions(
{ strictMode: false },
[["decorators", { decoratorsBeforeExport: false }]].concat(extraPlugins)
)
];

let ast;
try {
ast = tryCombinations(babel[parseMethod].bind(null, text), combinations);
ast = babel[parseMethod](text, babelOptions({}, extraPlugins));
} catch (error) {
throw createError(
// babel error prints (l:c) with cols that are zero indexed
Expand All @@ -97,20 +79,6 @@ const parse = createParse("parse", ["flow"]);
const parseFlow = createParse("parse", [["flow", { all: true, enums: true }]]);
const parseExpression = createParse("parseExpression");

function tryCombinations(fn, combinations) {
let error;
for (let i = 0; i < combinations.length; i++) {
try {
return fn(combinations[i]);
} catch (_error) {
if (!error) {
error = _error;
}
}
}
throw error;
}

function parseJson(text, parsers, opts) {
const ast = parseExpression(text, parsers, opts);

Expand Down
26 changes: 26 additions & 0 deletions tests/decorators/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ class Yo {
================================================================================
`;

exports[`mixed.js 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
// https://github.com/prettier/prettier/issues/6747
@foo
export default class MyComponent {
@task
*foo() {
}
}
=====================================output=====================================
// https://github.com/prettier/prettier/issues/6747
@foo
export default class MyComponent {
@task
*foo() {}
}
================================================================================
`;

exports[`mobx.js 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down
8 changes: 8 additions & 0 deletions tests/decorators/mixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://github.com/prettier/prettier/issues/6747

@foo
export default class MyComponent {
@task
*foo() {
}
}

0 comments on commit f99e3d9

Please sign in to comment.