forked from babel/babel
/
index.js
59 lines (48 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import semver from "semver";
import {
version as babelCoreVersion,
parseSync as babelParse,
} from "@babel/core";
import packageJson from "../package.json";
import {
normalizeBabelParseConfig,
normalizeESLintConfig,
} from "./configuration";
import convert from "./convert";
import analyzeScope from "./analyze-scope";
import visitorKeys from "./visitor-keys";
let isRunningSupportedVersion;
function baseParse(code, options) {
if (typeof isRunningSupportedVersion !== "boolean") {
isRunningSupportedVersion = semver.satisfies(
babelCoreVersion,
packageJson.peerDependencies["@babel/core"],
);
}
if (!isRunningSupportedVersion) {
throw new Error(
`@babel/eslint-parser@${packageJson.version} does not support @babel/core@${babelCoreVersion}. Please upgrade to @babel/core@${packageJson.peerDependencies["@babel/core"]}`,
);
}
let ast;
try {
ast = babelParse(code, normalizeBabelParseConfig(options));
} catch (err) {
if (err instanceof SyntaxError) {
err.lineNumber = err.loc.line;
err.column = err.loc.column;
}
throw err;
}
convert(ast, code);
return ast;
}
export function parse(code, options = {}) {
return baseParse(code, normalizeESLintConfig(options));
}
export function parseForESLint(code, options = {}) {
const normalizedOptions = normalizeESLintConfig(options);
const ast = baseParse(code, normalizedOptions);
const scopeManager = analyzeScope(ast, normalizedOptions);
return { ast, scopeManager, visitorKeys };
}