Skip to content

Commit

Permalink
[New] support parseForESLint from custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored and ljharb committed Aug 3, 2019
1 parent ebcf17c commit 35a12f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions tests/src/core/eslintParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
parseForESLint: function() {
return {
ast: {},
}
},
}
11 changes: 11 additions & 0 deletions tests/src/core/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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)
})
Expand Down
18 changes: 18 additions & 0 deletions utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 35a12f9

Please sign in to comment.