diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d75cca7f..23424cad3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,16 +5,19 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## [Unreleased] +### Added +- support `parseForESLint` from custom parser ([#1435], thanks [@JounQin]) + ## [2.18.2] - 2019-07-19 - - Skip warning on type interfaces ([#1425], thanks [@lencioni]) +- Skip warning on type interfaces ([#1425], thanks [@lencioni]) ## [2.18.1] - 2019-07-18 ### Fixed - - Improve parse perf when using `@typescript-eslint/parser` ([#1409], thanks [@bradzacher]) - - [`prefer-default-export`]: don't warn on TypeAlias & TSTypeAliasDeclaration ([#1377], thanks [@sharmilajesupaul]) - - [`no-unused-modules`]: Exclude package "main"/"bin"/"browser" entry points ([#1404], thanks [@rfermann]) - - [`export`]: false positive for typescript overloads ([#1412], thanks [@golopot]) +- Improve parse perf when using `@typescript-eslint/parser` ([#1409], thanks [@bradzacher]) +- [`prefer-default-export`]: don't warn on TypeAlias & TSTypeAliasDeclaration ([#1377], thanks [@sharmilajesupaul]) +- [`no-unused-modules`]: Exclude package "main"/"bin"/"browser" entry points ([#1404], thanks [@rfermann]) +- [`export`]: false positive for typescript overloads ([#1412], thanks [@golopot]) ### Refactors - [`no-extraneous-dependencies`], `importType`: remove lodash ([#1419], thanks [@ljharb]) @@ -598,6 +601,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435 [#1425]: https://github.com/benmosher/eslint-plugin-import/pull/1425 [#1419]: https://github.com/benmosher/eslint-plugin-import/pull/1419 [#1412]: https://github.com/benmosher/eslint-plugin-import/pull/1412 @@ -972,3 +976,4 @@ for info on changes for earlier releases. [@sheepsteak]: https://github.com/sheepsteak [@sharmilajesupaul]: https://github.com/sharmilajesupaul [@lencioni]: https://github.com/lencioni +[@JounQin]: https://github.com/JounQin diff --git a/tests/src/core/eslintParser.js b/tests/src/core/eslintParser.js new file mode 100644 index 000000000..3870ccc6e --- /dev/null +++ b/tests/src/core/eslintParser.js @@ -0,0 +1,7 @@ +module.exports = { + parseForESLint: function() { + return { + ast: {}, + } + }, +} diff --git a/tests/src/core/parse.js b/tests/src/core/parse.js index 4d8b5ab58..72d232730 100644 --- a/tests/src/core/parse.js +++ b/tests/src/core/parse.js @@ -9,6 +9,8 @@ describe('parse(content, { settings, ecmaFeatures })', function () { const path = getFilename('jsx.js') const parseStubParser = require('./parseStubParser') const parseStubParserPath = require.resolve('./parseStubParser') + const eslintParser = require('./eslintParser') + const eslintParserPath = require.resolve('./eslintParser') let content before((done) => @@ -43,6 +45,15 @@ describe('parse(content, { settings, ecmaFeatures })', function () { expect(parseSpy.args[0][1], 'custom parser to get parserOptions.filePath equal to the full path of the source file').to.have.property('filePath', path) }) + it('passes with custom `parseForESLint` parser', function () { + const parseForESLintSpy = sinon.spy(eslintParser, 'parseForESLint') + const parseSpy = sinon.spy() + eslintParser.parse = parseSpy + parse(path, content, { settings: {}, parserPath: eslintParserPath }) + expect(parseForESLintSpy.callCount, 'custom `parseForESLint` parser to be called once').to.equal(1) + expect(parseSpy.callCount, '`parseForESLint` takes higher priority than `parse`').to.equal(0) + }) + it('throws on context == null', function () { expect(parse.bind(null, path, content, null)).to.throw(Error) }) diff --git a/utils/parse.js b/utils/parse.js index 99e5a9334..955159904 100644 --- a/utils/parse.js +++ b/utils/parse.js @@ -42,6 +42,24 @@ exports.default = function parse(path, content, context) { // require the parser relative to the main module (i.e., ESLint) const parser = moduleRequire(parserPath) + if (typeof parser.parseForESLint === 'function') { + let ast + try { + ast = parser.parseForESLint(content, parserOptions).ast + } catch (e) { + // + } + if (!ast || typeof ast !== 'object') { + console.warn( + '`parseForESLint` from parser `' + + parserPath + + '` is invalid and will just be ignored' + ) + } else { + return ast + } + } + return parser.parse(content, parserOptions) }