Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support parseForESLint from custom parser #1435

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ljharb marked this conversation as resolved.
Show resolved Hide resolved
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