Skip to content

Commit

Permalink
feat: support flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach committed Sep 24, 2022
1 parent 753505d commit 05a9115
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
22 changes: 15 additions & 7 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,21 @@ export function recursivePatternCapture(pattern, callback) {
* don't hold full context object in memory, just grab what we need.
*/
function childContext(path, context) {
const { settings, parserOptions, parserPath } = context;
return {
settings,
parserOptions,
parserPath,
path,
};
const { settings, parserOptions, parserPath, languageOptions } = context;

const childContext = { settings, parserPath, path };

if (languageOptions) {
childContext.parserOptions = {
ecmaVersion: 'latest',
...languageOptions.parserOptions,
};
childContext.parser = languageOptions.parser;
} else {
childContext.parserOptions = parserOptions;
}

return childContext;
}


Expand Down
22 changes: 12 additions & 10 deletions utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ exports.default = function parse(path, content, context) {

if (context == null) throw new Error('need context to parse properly');

let parserOptions = context.parserOptions;
const parserPath = getParserPath(path, context);
let { parserOptions } = context;
const { parserPath, parser } = getParser(path, context);

if (!parserPath) throw new Error('parserPath is required!');
if (!parser) throw new Error('parserPath or parser is required!');

// hack: espree blows up with frozen options
parserOptions = Object.assign({}, parserOptions);
Expand All @@ -83,9 +83,6 @@ exports.default = function parse(path, content, context) {
delete parserOptions.project;
delete parserOptions.projects;

// require the parser relative to the main module (i.e., ESLint)
const parser = moduleRequire(parserPath);

// replicate bom strip and hashbang transform of ESLint
// https://github.com/eslint/eslint/blob/b93af98b3c417225a027cabc964c38e779adb945/lib/linter/linter.js#L779
content = transformHashbang(stripUnicodeBOM(String(content)));
Expand All @@ -104,7 +101,7 @@ exports.default = function parse(path, content, context) {
if (!ast || typeof ast !== 'object') {
console.warn(
'`parseForESLint` from parser `' +
parserPath +
(parserPath || 'unknown') +
'` is invalid and will just be ignored'
);
} else {
Expand All @@ -116,18 +113,23 @@ exports.default = function parse(path, content, context) {
return makeParseReturn(ast, keysFromParser(parserPath, parser, undefined));
};

function getParserPath(path, context) {
function getParser(path, context) {
const parsers = context.settings['import/parsers'];
if (parsers != null) {
const extension = extname(path);
for (const parserPath in parsers) {
if (parsers[parserPath].indexOf(extension) > -1) {
// use this alternate parser
log('using alt parser:', parserPath);
return parserPath;
return { parserPath, parser: parserPath && moduleRequire(parserPath) };
}
}
}
// default to use ESLint parser
return context.parserPath;
return {
parserPath: context.parserPath,
parser: context.parserPath
? moduleRequire(context.parserPath)
: context.parser,
};
}

0 comments on commit 05a9115

Please sign in to comment.