Files
codeql-action/node_modules/eslint-module-utils/parse.js
T

79 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-07-27 16:54:26 +00:00
'use strict';
exports.__esModule = true;
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
const moduleRequire = require('./module-require').default;
const extname = require('path').extname;
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
const log = require('debug')('eslint-plugin-import:parse');
2020-09-14 10:42:37 +01:00
exports.default = function parse(path, content, context) {
2021-07-27 16:54:26 +00:00
if (context == null) throw new Error('need context to parse properly');
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
let parserOptions = context.parserOptions;
const parserPath = getParserPath(path, context);
2020-09-14 10:42:37 +01:00
2021-07-27 16:54:26 +00:00
if (!parserPath) throw new Error('parserPath is required!');
2020-09-14 10:42:37 +01:00
// hack: espree blows up with frozen options
2021-07-27 16:54:26 +00:00
parserOptions = Object.assign({}, parserOptions);
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
2020-09-14 10:42:37 +01:00
// always include comments and tokens (for doc parsing)
2021-07-27 16:54:26 +00:00
parserOptions.comment = true;
parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
parserOptions.tokens = true;
2020-09-14 10:42:37 +01:00
// attach node locations
2021-07-27 16:54:26 +00:00
parserOptions.loc = true;
parserOptions.range = true;
2020-09-14 10:42:37 +01:00
// provide the `filePath` like eslint itself does, in `parserOptions`
// https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
2021-07-27 16:54:26 +00:00
parserOptions.filePath = path;
2020-09-14 10:42:37 +01:00
// @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.
2021-08-09 19:49:16 +00:00
// https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
2021-07-27 16:54:26 +00:00
delete parserOptions.project;
delete parserOptions.projects;
2020-09-14 10:42:37 +01:00
// require the parser relative to the main module (i.e., ESLint)
2021-07-27 16:54:26 +00:00
const parser = moduleRequire(parserPath);
2020-09-14 10:42:37 +01:00
if (typeof parser.parseForESLint === 'function') {
2021-07-27 16:54:26 +00:00
let ast;
2020-09-14 10:42:37 +01:00
try {
2021-07-27 16:54:26 +00:00
ast = parser.parseForESLint(content, parserOptions).ast;
2020-09-14 10:42:37 +01:00
} catch (e) {
2021-07-27 16:54:26 +00:00
console.warn();
console.warn('Error while parsing ' + parserOptions.filePath);
console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
2020-09-14 10:42:37 +01:00
}
if (!ast || typeof ast !== 'object') {
2021-08-09 19:49:16 +00:00
console.warn('`parseForESLint` from parser `' + parserPath + '` is invalid and will just be ignored');
2020-09-14 10:42:37 +01:00
} else {
2021-07-27 16:54:26 +00:00
return ast;
2020-09-14 10:42:37 +01:00
}
}
2021-07-27 16:54:26 +00:00
return parser.parse(content, parserOptions);
};
2020-09-14 10:42:37 +01:00
function getParserPath(path, context) {
2021-07-27 16:54:26 +00:00
const parsers = context.settings['import/parsers'];
2020-09-14 10:42:37 +01:00
if (parsers != null) {
2021-07-27 16:54:26 +00:00
const extension = extname(path);
for (const parserPath in parsers) {
2020-09-14 10:42:37 +01:00
if (parsers[parserPath].indexOf(extension) > -1) {
// use this alternate parser
2021-07-27 16:54:26 +00:00
log('using alt parser:', parserPath);
return parserPath;
2020-09-14 10:42:37 +01:00
}
}
}
// default to use ESLint parser
2021-07-27 16:54:26 +00:00
return context.parserPath;
2020-09-14 10:42:37 +01:00
}