Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/utils/parse.js
828 views
1
'use strict';
2
exports.__esModule = true;
3
4
const moduleRequire = require('./module-require').default;
5
const extname = require('path').extname;
6
const fs = require('fs');
7
8
const log = require('debug')('eslint-plugin-import:parse');
9
10
function getBabelVisitorKeys(parserPath) {
11
if (parserPath.endsWith('index.js')) {
12
const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js');
13
if (fs.existsSync(hypotheticalLocation)) {
14
const keys = moduleRequire(hypotheticalLocation);
15
return keys.default || keys;
16
}
17
} else if (parserPath.endsWith('index.cjs')) {
18
const hypotheticalLocation = parserPath.replace('index.cjs', 'worker/ast-info.cjs');
19
if (fs.existsSync(hypotheticalLocation)) {
20
const astInfo = moduleRequire(hypotheticalLocation);
21
return astInfo.getVisitorKeys();
22
}
23
}
24
return null;
25
}
26
27
function keysFromParser(parserPath, parserInstance, parsedResult) {
28
if (/.*espree.*/.test(parserPath)) {
29
return parserInstance.VisitorKeys;
30
}
31
if (/.*(babel-eslint|@babel\/eslint-parser).*/.test(parserPath)) {
32
return getBabelVisitorKeys(parserPath);
33
}
34
if (/.*@typescript-eslint\/parser/.test(parserPath)) {
35
if (parsedResult) {
36
return parsedResult.visitorKeys;
37
}
38
}
39
return null;
40
}
41
42
exports.default = function parse(path, content, context) {
43
44
if (context == null) throw new Error('need context to parse properly');
45
46
let parserOptions = context.parserOptions;
47
const parserPath = getParserPath(path, context);
48
49
if (!parserPath) throw new Error('parserPath is required!');
50
51
// hack: espree blows up with frozen options
52
parserOptions = Object.assign({}, parserOptions);
53
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
54
55
// always include comments and tokens (for doc parsing)
56
parserOptions.comment = true;
57
parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
58
parserOptions.tokens = true;
59
60
// attach node locations
61
parserOptions.loc = true;
62
parserOptions.range = true;
63
64
// provide the `filePath` like eslint itself does, in `parserOptions`
65
// https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
66
parserOptions.filePath = path;
67
68
// @typescript-eslint/parser will parse the entire project with typechecking if you provide
69
// "project" or "projects" in parserOptions. Removing these options means the parser will
70
// only parse one file in isolate mode, which is much, much faster.
71
// https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
72
delete parserOptions.project;
73
delete parserOptions.projects;
74
75
// require the parser relative to the main module (i.e., ESLint)
76
const parser = moduleRequire(parserPath);
77
78
if (typeof parser.parseForESLint === 'function') {
79
let ast;
80
try {
81
const parserRaw = parser.parseForESLint(content, parserOptions);
82
ast = parserRaw.ast;
83
return {
84
ast,
85
visitorKeys: keysFromParser(parserPath, parser, parserRaw),
86
};
87
} catch (e) {
88
console.warn();
89
console.warn('Error while parsing ' + parserOptions.filePath);
90
console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
91
}
92
if (!ast || typeof ast !== 'object') {
93
console.warn(
94
'`parseForESLint` from parser `' +
95
parserPath +
96
'` is invalid and will just be ignored'
97
);
98
} else {
99
return {
100
ast,
101
visitorKeys: keysFromParser(parserPath, parser, undefined),
102
};
103
}
104
}
105
106
const keys = keysFromParser(parserPath, parser, undefined);
107
return {
108
ast: parser.parse(content, parserOptions),
109
visitorKeys: keys,
110
};
111
};
112
113
function getParserPath(path, context) {
114
const parsers = context.settings['import/parsers'];
115
if (parsers != null) {
116
const extension = extname(path);
117
for (const parserPath in parsers) {
118
if (parsers[parserPath].indexOf(extension) > -1) {
119
// use this alternate parser
120
log('using alt parser:', parserPath);
121
return parserPath;
122
}
123
}
124
}
125
// default to use ESLint parser
126
return context.parserPath;
127
}
128
129