123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- '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 getBabelEslintVisitorKeys(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;
- }
- }
- return null;
- }
- function keysFromParser(parserPath, parserInstance, parsedResult) {
-
- if (parsedResult && parsedResult.visitorKeys) {
- return parsedResult.visitorKeys;
- }
- if (typeof parserPath === 'string' && (/.*espree.*/).test(parserPath)) {
- return parserInstance.VisitorKeys;
- }
- if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
- return getBabelEslintVisitorKeys(parserPath);
- }
- return null;
- }
- function makeParseReturn(ast, visitorKeys) {
- if (ast) {
- ast.visitorKeys = visitorKeys;
- ast.ast = ast;
- }
- return ast;
- }
- function stripUnicodeBOM(text) {
- return text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text;
- }
- function transformHashbang(text) {
- return text.replace(/^#!([^\r\n]+)/u, (_, captured) => `//${captured}`);
- }
- exports.default = function parse(path, content, context) {
- if (context == null) { throw new Error('need context to parse properly'); }
-
- let parserOptions = context.languageOptions && context.languageOptions.parserOptions || context.parserOptions;
- const parserOrPath = getParser(path, context);
- if (!parserOrPath) { throw new Error('parserPath or languageOptions.parser 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 = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;
-
-
- content = transformHashbang(stripUnicodeBOM(String(content)));
- if (typeof parser.parseForESLint === 'function') {
- let ast;
- try {
- const parserRaw = parser.parseForESLint(content, parserOptions);
- ast = parserRaw.ast;
- return makeParseReturn(ast, keysFromParser(parserOrPath, 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 `' + (typeof parserOrPath === 'string' ? parserOrPath : '`context.languageOptions.parser`') + '` is invalid and will just be ignored'
- );
- } else {
- return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
- }
- }
- const ast = parser.parse(content, parserOptions);
- return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
- };
- function getParser(path, context) {
- const parserPath = getParserPath(path, context);
- if (parserPath) {
- return parserPath;
- }
- const isFlat = context.languageOptions
- && context.languageOptions.parser
- && typeof context.languageOptions.parser !== 'string'
- && (
- typeof context.languageOptions.parser.parse === 'function'
- || typeof context.languageOptions.parser.parseForESLint === 'function'
- );
- return isFlat ? context.languageOptions.parser : null;
- }
- 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;
- }
|