| 1 |
const babel = require("./babel-core.cjs"); |
| 2 |
const ESLINT_VERSION = require("../utils/eslint-version.cjs"); |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
function getParserPlugins(babelOptions) { |
| 11 |
const babelParserPlugins = babelOptions.parserOpts?.plugins ?? []; |
| 12 |
const estreeOptions = { classFeatures: ESLINT_VERSION >= 8 }; |
| 13 |
for (const plugin of babelParserPlugins) { |
| 14 |
if (Array.isArray(plugin) && plugin[0] === "estree") { |
| 15 |
Object.assign(estreeOptions, plugin[1]); |
| 16 |
break; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
return [["estree", estreeOptions], ...babelParserPlugins]; |
| 21 |
} |
| 22 |
|
| 23 |
function normalizeParserOptions(options) { |
| 24 |
return { |
| 25 |
sourceType: options.sourceType, |
| 26 |
filename: options.filePath, |
| 27 |
...options.babelOptions, |
| 28 |
parserOpts: { |
| 29 |
...(process.env.BABEL_8_BREAKING |
| 30 |
? {} |
| 31 |
: { |
| 32 |
allowImportExportEverywhere: |
| 33 |
options.allowImportExportEverywhere ?? false, |
| 34 |
allowSuperOutsideMethod: true, |
| 35 |
}), |
| 36 |
allowReturnOutsideFunction: |
| 37 |
options.ecmaFeatures?.globalReturn ?? |
| 38 |
(process.env.BABEL_8_BREAKING ? false : true), |
| 39 |
...options.babelOptions.parserOpts, |
| 40 |
plugins: getParserPlugins(options.babelOptions), |
| 41 |
|
| 42 |
attachComment: false, |
| 43 |
ranges: true, |
| 44 |
tokens: true, |
| 45 |
}, |
| 46 |
caller: { |
| 47 |
name: "@babel/eslint-parser", |
| 48 |
...options.babelOptions.caller, |
| 49 |
}, |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
function validateResolvedConfig(config, options, parseOptions) { |
| 54 |
if (config !== null) { |
| 55 |
if (options.requireConfigFile !== false) { |
| 56 |
if (!config.hasFilesystemConfig()) { |
| 57 |
let error = `No Babel config file detected for ${config.options.filename}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`; |
| 58 |
|
| 59 |
if (config.options.filename.includes("node_modules")) { |
| 60 |
error += `\nIf you have a .babelrc.js file or use package.json#babel, keep in mind that it's not used when parsing dependencies. If you want your config to be applied to your whole app, consider using babel.config.js or babel.config.json instead.`; |
| 61 |
} |
| 62 |
|
| 63 |
throw new Error(error); |
| 64 |
} |
| 65 |
} |
| 66 |
if (config.options) return config.options; |
| 67 |
} |
| 68 |
|
| 69 |
return getDefaultParserOptions(parseOptions); |
| 70 |
} |
| 71 |
|
| 72 |
function getDefaultParserOptions(options) { |
| 73 |
return { |
| 74 |
plugins: [], |
| 75 |
...options, |
| 76 |
babelrc: false, |
| 77 |
configFile: false, |
| 78 |
browserslistConfigFile: false, |
| 79 |
ignore: null, |
| 80 |
only: null, |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
exports.normalizeBabelParseConfig = async function (options) { |
| 85 |
const parseOptions = normalizeParserOptions(options); |
| 86 |
const config = await babel.loadPartialConfigAsync(parseOptions); |
| 87 |
return validateResolvedConfig(config, options, parseOptions); |
| 88 |
}; |
| 89 |
|
| 90 |
exports.normalizeBabelParseConfigSync = function (options) { |
| 91 |
const parseOptions = normalizeParserOptions(options); |
| 92 |
const config = babel.loadPartialConfigSync(parseOptions); |
| 93 |
return validateResolvedConfig(config, options, parseOptions); |
| 94 |
}; |