2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.parseForESLint = exports.parse = void 0;
4 const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
5 const analyze_scope_1 = require("./analyze-scope");
6 function validateBoolean(value, fallback = false) {
7 if (typeof value !== 'boolean') {
12 function parse(code, options) {
13 return parseForESLint(code, options).ast;
15 exports.parse = parse;
16 function parseForESLint(code, options) {
17 if (!options || typeof options !== 'object') {
20 // https://eslint.org/docs/user-guide/configuring#specifying-parser-options
21 // if sourceType is not provided by default eslint expect that it will be set to "script"
22 if (options.sourceType !== 'module' && options.sourceType !== 'script') {
23 options.sourceType = 'script';
25 if (typeof options.ecmaFeatures !== 'object') {
26 options.ecmaFeatures = {};
28 const parserOptions = {};
29 Object.assign(parserOptions, options, {
30 useJSXTextNode: validateBoolean(options.useJSXTextNode, true),
31 jsx: validateBoolean(options.ecmaFeatures.jsx),
33 if (typeof options.filePath === 'string') {
34 const tsx = options.filePath.endsWith('.tsx');
35 if (tsx || options.filePath.endsWith('.ts')) {
36 parserOptions.jsx = tsx;
40 * Allow the user to suppress the warning from typescript-estree if they are using an unsupported
41 * version of TypeScript
43 const warnOnUnsupportedTypeScriptVersion = validateBoolean(options.warnOnUnsupportedTypeScriptVersion, true);
44 if (!warnOnUnsupportedTypeScriptVersion) {
45 parserOptions.loggerFn = false;
47 const { ast, services } = typescript_estree_1.parseAndGenerateServices(code, parserOptions);
48 ast.sourceType = options.sourceType;
49 const scopeManager = analyze_scope_1.analyzeScope(ast, options);
50 return { ast, services, scopeManager, visitorKeys: typescript_estree_1.visitorKeys };
52 exports.parseForESLint = parseForESLint;
53 //# sourceMappingURL=parser.js.map