forked from vuejs/vue-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
164 lines (153 loc) · 5.57 KB
/
index.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import * as path from "path"
import * as AST from "./ast"
import { LocationCalculatorForHtml } from "./common/location-calculator"
import { HTMLParser, HTMLTokenizer } from "./html"
import { PugParser, PugTokenizer } from "./pug"
import { parseScript, parseScriptElement } from "./script"
import * as services from "./parser-services"
import type { ParserOptions } from "./common/parser-options"
import { getScriptParser } from "./common/parser-options"
import { parseScriptSetupElements } from "./script-setup"
import { LinesAndColumns } from "./common/lines-and-columns"
import type { VElement } from "./ast"
import { DEFAULT_ECMA_VERSION } from "./script-setup/parser-options"
import {
getLang,
isScriptElement,
isScriptSetupElement,
isStyleElement,
isTemplateElement,
} from "./common/ast-utils"
import { parseStyleElements } from "./style"
const STARTS_WITH_LT = /^\s*</u
/**
* Check whether the code is a Vue.js component.
* @param code The source code to check.
* @param options The parser options.
* @returns `true` if the source code is a Vue.js component.
*/
function isVueFile(code: string, options: ParserOptions): boolean {
const filePath = options.filePath || "unknown.js"
return path.extname(filePath) === ".vue" || STARTS_WITH_LT.test(code)
}
/**
* Parse the given source code.
* @param code The source code to parse.
* @param parserOptions The parser options.
* @returns The parsing result.
*/
export function parseForESLint(
code: string,
parserOptions: any,
): AST.ESLintExtendedProgram {
const options: ParserOptions = Object.assign(
{
comment: true,
loc: true,
range: true,
tokens: true,
},
parserOptions || {},
)
let result: AST.ESLintExtendedProgram
let document: AST.VDocumentFragment | null
let locationCalculator: LocationCalculatorForHtml | null
if (!isVueFile(code, options)) {
result = parseScript(code, {
...options,
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
parser: getScriptParser(options.parser, null, "script"),
})
document = null
locationCalculator = null
} else {
const optionsForTemplate = {
...options,
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
}
const skipParsingScript = options.parser === false
let tokenizer: HTMLTokenizer | PugTokenizer
let rootAST: AST.VDocumentFragment
if (/^\<template\s+lang\=["']pug["']\>/i.test(code)) {
tokenizer = new PugTokenizer(code, optionsForTemplate)
rootAST = new PugParser(tokenizer, optionsForTemplate).parse()
} else {
tokenizer = new HTMLTokenizer(code, optionsForTemplate)
rootAST = new HTMLParser(tokenizer as HTMLTokenizer, optionsForTemplate).parse()
}
locationCalculator = new LocationCalculatorForHtml(
tokenizer.gaps,
tokenizer.lineTerminators,
)
const scripts = rootAST.children.filter(isScriptElement)
const template = rootAST.children.find(isTemplateElement)
const templateLang = getLang(template) || "html"
const concreteInfo: AST.HasConcreteInfo = {
tokens: rootAST.tokens,
comments: rootAST.comments,
errors: rootAST.errors,
}
const templateBody =
template != null && (templateLang === "html" || templateLang === "pug")
? Object.assign(template, concreteInfo)
: undefined
const scriptParser = getScriptParser(options.parser, rootAST, "script")
let scriptSetup: VElement | undefined
if (skipParsingScript || !scripts.length) {
result = parseScript("", {
...options,
ecmaVersion: options.ecmaVersion || DEFAULT_ECMA_VERSION,
})
} else if (
scripts.length === 2 &&
(scriptSetup = scripts.find(isScriptSetupElement))
) {
result = parseScriptSetupElements(
scriptSetup,
scripts.find((e) => e !== scriptSetup)!,
code,
new LinesAndColumns(tokenizer.lineTerminators),
{
...options,
parser: scriptParser,
},
)
} else {
result = parseScriptElement(scripts[0], locationCalculator, {
...options,
parser: scriptParser,
})
}
if (options.vueFeatures?.styleCSSVariableInjection ?? true) {
const styles = rootAST.children.filter(isStyleElement)
parseStyleElements(styles, locationCalculator, {
...options,
parser: getScriptParser(options.parser, rootAST, "template"),
})
}
result.ast.templateBody = templateBody
document = rootAST
}
result.services = Object.assign(
result.services || {},
services.define(code, result.ast, document, locationCalculator, {
parserOptions: options,
}),
)
return result
}
/**
* Parse the given source code.
* @param code The source code to parse.
* @param options The parser options.
* @returns The parsing result.
*/
export function parse(code: string, options: any): AST.ESLintProgram {
return parseForESLint(code, options).ast
}
export { AST }