'use strict';
exports.__esModule = true;
const moduleRequire = require('./module-require').default;
const extname = require('path').extname;
const fs = require('fs');
const log = require('debug')('eslint-plugin-import:parse');
function getBabelVisitorKeys(parserPath) {
if (parserPath.endsWith('index.js')) {
const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js');
if (fs.existsSync(hypotheticalLocation)) {
const keys = moduleRequire(hypotheticalLocation);
return keys.default || keys;
}
} else if (parserPath.endsWith('index.cjs')) {
const hypotheticalLocation = parserPath.replace('index.cjs', 'worker/ast-info.cjs');
if (fs.existsSync(hypotheticalLocation)) {
const astInfo = moduleRequire(hypotheticalLocation);
return astInfo.getVisitorKeys();
}
}
return null;
}
function keysFromParser(parserPath, parserInstance, parsedResult) {
if (/.*espree.*/.test(parserPath)) {
return parserInstance.VisitorKeys;
}
if (/.*(babel-eslint|@babel\/eslint-parser).*/.test(parserPath)) {
return getBabelVisitorKeys(parserPath);
}
if (/.*@typescript-eslint\/parser/.test(parserPath)) {
if (parsedResult) {
return parsedResult.visitorKeys;
}
}
return null;
}
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!');
parserOptions = Object.assign({}, parserOptions);
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
parserOptions.comment = true;
parserOptions.attachComment = true;
parserOptions.tokens = true;
parserOptions.loc = true;
parserOptions.range = true;
parserOptions.filePath = path;
delete parserOptions.project;
delete parserOptions.projects;
const parser = moduleRequire(parserPath);
if (typeof parser.parseForESLint === 'function') {
let ast;
try {
const parserRaw = parser.parseForESLint(content, parserOptions);
ast = parserRaw.ast;
return {
ast,
visitorKeys: keysFromParser(parserPath, parser, parserRaw),
};
} catch (e) {
console.warn();
console.warn('Error while parsing ' + parserOptions.filePath);
console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
}
if (!ast || typeof ast !== 'object') {
console.warn(
'`parseForESLint` from parser `' +
parserPath +
'` is invalid and will just be ignored'
);
} else {
return {
ast,
visitorKeys: keysFromParser(parserPath, parser, undefined),
};
}
}
const keys = keysFromParser(parserPath, parser, undefined);
return {
ast: parser.parse(content, parserOptions),
visitorKeys: keys,
};
};
function getParserPath(path, context) {
const parsers = context.settings['import/parsers'];
if (parsers != null) {
const extension = extname(path);
for (const parserPath in parsers) {
if (parsers[parserPath].indexOf(extension) > -1) {
log('using alt parser:', parserPath);
return parserPath;
}
}
}
return context.parserPath;
}