Skip to content

Commit

Permalink
refactor(babel-eslint-parser): use fallback options instead of typeer…
Browse files Browse the repository at this point in the history
…ror protections
  • Loading branch information
François Servant authored and nicolo-ribaudo committed May 19, 2021
1 parent b019157 commit 5e4243b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
9 changes: 0 additions & 9 deletions eslint/babel-eslint-parser/src/convert/convertAST.cjs
Expand Up @@ -77,17 +77,12 @@ const convertNodesVisitor = {
if (node.type === "TemplateLiteral") {
for (let i = 0; i < node.quasis.length; i++) {
const q = node.quasis[i];

if (!q.range) {
q.range = [];
}
q.range[0] -= 1;
if (q.tail) {
q.range[1] += 1;
} else {
q.range[1] += 2;
}

q.loc.start.column -= 1;
if (q.tail) {
q.loc.end.column += 1;
Expand All @@ -110,10 +105,6 @@ function convertProgramNode(ast) {
delete ast.program;
delete ast.errors;

if (!ast.range) {
ast.range = [];
}

if (ast.comments.length) {
const lastComment = ast.comments[ast.comments.length - 1];

Expand Down
2 changes: 1 addition & 1 deletion eslint/babel-eslint-parser/src/convert/convertTokens.cjs
Expand Up @@ -31,7 +31,7 @@ function convertTemplateType(tokens, tl) {
templateTokens = [];
}

(tokens || []).forEach(token => {
tokens.forEach(token => {
switch (token.type.label) {
case tl.backQuote:
if (curlyBrace) {
Expand Down
26 changes: 24 additions & 2 deletions eslint/babel-eslint-parser/src/worker/configuration.cjs
Expand Up @@ -58,15 +58,37 @@ function validateResolvedConfig(config, options) {
}
}

function getDefaultParserOptions(options) {
return {
plugins: [],
...options,
babelrc: false,
configFile: false,
browserslistConfigFile: false,
ignore: null,
only: null,
};
}

module.exports = function normalizeBabelParseConfig(options) {
const parseOptions = normalizeParserOptions(options);

if (process.env.BABEL_8_BREAKING) {
return babel
.loadPartialConfigAsync(parseOptions)
.then(config => validateResolvedConfig(config, options) || parseOptions);
.then(config => validateConfigWithFallback(config));
} else {
const config = babel.loadPartialConfigSync(parseOptions);
return validateResolvedConfig(config, options) || parseOptions;
return validateConfigWithFallback(config);
}

function validateConfigWithFallback(inputConfig) {
const result = validateResolvedConfig(inputConfig, options);
if (result) {
return result;
} else {
// Fallback when `loadPartialConfig` returns `null` (e.g.: when the file is ignored)
return getDefaultParserOptions(parseOptions);
}
}
};
5 changes: 0 additions & 5 deletions eslint/babel-eslint-parser/src/worker/maybeParse.cjs
Expand Up @@ -15,11 +15,6 @@ module.exports = function maybeParse(code, options) {
{ dirname: __dirname, type: "plugin" },
);
}

if (!options.plugins) {
// May happen when `loadPartialConfigSync` returns `null` (e.g.: when the file is ignored)
options.plugins = [];
}
options.plugins.push(extractParserOptionsConfigItem);

try {
Expand Down

0 comments on commit 5e4243b

Please sign in to comment.