I'm trying to make a conditional eslint parser which decides based on filename which parser to execute: Babel or TypeScript (because my project will soon contain both types of files which need to live together under a single eslint config for some time).
The native eslint parse call provides filePath in the parserOptions, which turned out the only and the very useful way to determine the file name:
https://github.com/eslint/eslint/blob/3ec436eeed0b0271e2ed0d0cb22e4246eb15f137/lib/linter.js#L637
function parse(text, config, filePath, messages) {
let parser,
parserOptions = {
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
filePath
};
But when eslint-plugin-import parses its dependencies, it calls the custom parser on its own, and does not provide filePath in parserOptions:
https://github.com/benmosher/eslint-plugin-import/blob/90ef48b3ade57c77526b285f75dc0cfc41537831/utils/parse.js#L28
exports.default = function parse(path, content, context) {
if (context == null) throw new Error('need context to parse properly')
let parserOptions = context.parserOptions
const parserPath = getParserPath(path, context)
if (!parserPath) throw new Error('parserPath is required!')
// hack: espree blows up with frozen options
parserOptions = Object.assign({}, parserOptions)
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)
// always attach comments
parserOptions.attachComment = true
// require the parser relative to the main module (i.e., ESLint)
const parser = moduleRequire(parserPath)
return parser.parse(content, parserOptions)
}
This should not be a big change to add that, I'll propose a PR soon.
I'm trying to make a conditional eslint parser which decides based on filename which parser to execute: Babel or TypeScript (because my project will soon contain both types of files which need to live together under a single eslint config for some time).
The native eslint parse call provides
filePathin theparserOptions, which turned out the only and the very useful way to determine the file name:https://github.com/eslint/eslint/blob/3ec436eeed0b0271e2ed0d0cb22e4246eb15f137/lib/linter.js#L637
But when
eslint-plugin-importparses its dependencies, it calls the custom parser on its own, and does not providefilePathinparserOptions:https://github.com/benmosher/eslint-plugin-import/blob/90ef48b3ade57c77526b285f75dc0cfc41537831/utils/parse.js#L28
This should not be a big change to add that, I'll propose a PR soon.