Skip to content

Commit

Permalink
[utils] [Fix] parse: restore compatibility by making the return val…
Browse files Browse the repository at this point in the history
…ue `ast` again

Fixes #2350
  • Loading branch information
ljharb committed Jan 12, 2022
1 parent 68cea3e commit df8c1a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 4 additions & 0 deletions utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
- [Fix] `parse`: restore compatibility by making the return value `ast` again ([#2350], thanks [@ljharb])

## v2.7.2 - 2022-01-01

### Fixed
Expand Down Expand Up @@ -110,6 +113,7 @@ Yanked due to critical issue with cache key resulting from #839.
### Fixed
- `unambiguous.test()` regex is now properly in multiline mode

[#2350]: https://github.com/import-js/eslint-plugin-import/issues/2350
[#2343]: https://github.com/import-js/eslint-plugin-import/pull/2343
[#2261]: https://github.com/import-js/eslint-plugin-import/pull/2261
[#2212]: https://github.com/import-js/eslint-plugin-import/pull/2212
Expand Down
27 changes: 14 additions & 13 deletions utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function keysFromParser(parserPath, parserInstance, parsedResult) {
return null;
}

// this exists to smooth over the unintentional breaking change in v2.7.
// TODO, semver-major: avoid mutating `ast` and return a plain object instead.
function makeParseReturn(ast, visitorKeys) {
if (ast) {
ast.visitorKeys = visitorKeys;
ast.ast = ast;
}
return ast;
}

exports.default = function parse(path, content, context) {

if (context == null) throw new Error('need context to parse properly');
Expand Down Expand Up @@ -73,10 +83,7 @@ exports.default = function parse(path, content, context) {
try {
const parserRaw = parser.parseForESLint(content, parserOptions);
ast = parserRaw.ast;
return {
ast,
visitorKeys: keysFromParser(parserPath, parser, parserRaw),
};
return makeParseReturn(ast, keysFromParser(parserPath, parser, parserRaw));
} catch (e) {
console.warn();
console.warn('Error while parsing ' + parserOptions.filePath);
Expand All @@ -89,18 +96,12 @@ exports.default = function parse(path, content, context) {
'` is invalid and will just be ignored'
);
} else {
return {
ast,
visitorKeys: keysFromParser(parserPath, parser, undefined),
};
return makeParseReturn(ast, keysFromParser(parserPath, parser, undefined));
}
}

const keys = keysFromParser(parserPath, parser, undefined);
return {
ast: parser.parse(content, parserOptions),
visitorKeys: keys,
};
const ast = parser.parse(content, parserOptions);
return makeParseReturn(ast, keysFromParser(parserPath, parser, undefined));
};

function getParserPath(path, context) {
Expand Down

0 comments on commit df8c1a8

Please sign in to comment.