Skip to content

Commit

Permalink
feat: support parseForESLint from custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 3, 2019
1 parent 989f6cc commit b3cb239
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
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: {},
}
},
}
12 changes: 12 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,16 @@ 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.parseForESLint = parseForESLintSpy
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
31 changes: 24 additions & 7 deletions utils/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict"
'use strict'
exports.__esModule = true

const moduleRequire = require('./module-require').default
Expand All @@ -7,7 +7,6 @@ const extname = require('path').extname
const log = require('debug')('eslint-plugin-import:parse')

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

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

let parserOptions = context.parserOptions
Expand All @@ -21,7 +20,7 @@ exports.default = function parse(path, content, context) {

// always include comments and tokens (for doc parsing)
parserOptions.comment = true
parserOptions.attachComment = true // keeping this for backward-compat with older parsers
parserOptions.attachComment = true // keeping this for backward-compat with older parsers
parserOptions.tokens = true

// attach node locations
Expand All @@ -31,17 +30,35 @@ exports.default = function parse(path, content, context) {
// provide the `filePath` like eslint itself does, in `parserOptions`
// https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
parserOptions.filePath = path

// @typescript-eslint/parser will parse the entire project with typechecking if you provide
// "project" or "projects" in parserOptions. Removing these options means the parser will
// only parse one file in isolate mode, which is much, much faster.
// https://github.com/benmosher/eslint-plugin-import/issues/1408#issuecomment-509298962
delete parserOptions.project;
delete parserOptions.projects;
delete parserOptions.project
delete parserOptions.projects

// 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 b3cb239

Please sign in to comment.