-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathindex.ts
376 lines (349 loc) · 11.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import type { Rule, SourceCode } from "eslint"
import type { ScopeManager, Scope } from "eslint-scope"
import type {
ESLintExtendedProgram,
Node,
OffsetRange,
VDocumentFragment,
VElement,
VExpressionContainer,
VText,
} from "../../ast"
import { getFallbackKeys, ParseError } from "../../ast"
import { getEslintScope } from "../../common/eslint-scope"
import { getEcmaVersionIfUseEspree } from "../../common/espree"
import { fixErrorLocation, fixLocations } from "../../common/fix-locations"
import type { LocationCalculatorForHtml } from "../../common/location-calculator"
import type { ParserObject } from "../../common/parser-object"
import { isEnhancedParserObject } from "../../common/parser-object"
import type { ParserOptions } from "../../common/parser-options"
import { DEFAULT_ECMA_VERSION } from "../../script-setup/parser-options"
export type ESLintCustomBlockParser = ParserObject<any, any>
export type CustomBlockContext = {
getSourceCode(): SourceCode
parserServices: any
getAncestors(): any[]
getDeclaredVariables(node: any): any[]
getScope(): any
markVariableAsUsed(name: string): boolean
// Same as the original context.
id: string
options: any[]
settings: { [name: string]: any }
parserPath: string
parserOptions: any
getFilename(): string
report(descriptor: Rule.ReportDescriptor): void
}
/**
* Checks whether the given node is VElement.
*/
function isVElement(
node: VElement | VExpressionContainer | VText,
): node is VElement {
return node.type === "VElement"
}
/**
* Get the all custom blocks from given document
* @param document
*/
export function getCustomBlocks(
document: VDocumentFragment | null,
): VElement[] {
return document
? document.children
.filter(isVElement)
.filter(
(block) =>
block.name !== "script" &&
block.name !== "template" &&
block.name !== "style",
)
: []
}
/**
* Parse the source code of the given custom block element.
* @param node The custom block element to parse.
* @param parser The custom parser.
* @param globalLocationCalculator The location calculator for fixLocations.
* @param parserOptions The parser options.
* @returns The result of parsing.
*/
export function parseCustomBlockElement(
node: VElement,
parser: ESLintCustomBlockParser,
globalLocationCalculator: LocationCalculatorForHtml,
parserOptions: ParserOptions,
): ESLintExtendedProgram & { error?: ParseError | Error } {
const text = node.children[0]
const { code, range, loc } =
text != null && text.type === "VText"
? {
code: text.value,
range: text.range,
loc: text.loc,
}
: {
code: "",
range: [
node.startTag.range[1],
node.endTag!.range[0],
] as OffsetRange,
loc: {
start: node.startTag.loc.end,
end: node.endTag!.loc.start,
},
}
const locationCalculator = globalLocationCalculator.getSubCalculatorAfter(
range[0],
)
try {
return parseCustomBlockFragment(
code,
parser,
locationCalculator,
parserOptions,
)
} catch (e) {
if (!(e instanceof Error)) {
throw e
}
return {
error: e,
ast: {
type: "Program",
sourceType: "module",
loc: {
start: {
...loc.start,
},
end: {
...loc.end,
},
},
range: [...range],
body: [],
tokens: [],
comments: [],
},
}
}
}
/**
* Parse the given source code.
*
* @param code The source code to parse.
* @param parser The custom parser.
* @param locationCalculator The location calculator for fixLocations.
* @param parserOptions The parser options.
* @returns The result of parsing.
*/
function parseCustomBlockFragment(
code: string,
parser: ESLintCustomBlockParser,
locationCalculator: LocationCalculatorForHtml,
parserOptions: ParserOptions,
): ESLintExtendedProgram {
try {
const result = parseBlock(code, parser, {
ecmaVersion: DEFAULT_ECMA_VERSION,
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
...parserOptions,
})
fixLocations(result, locationCalculator)
return result
} catch (err) {
const perr = ParseError.normalize(err)
if (perr) {
fixErrorLocation(perr, locationCalculator)
throw perr
}
throw err
}
}
function parseBlock(
code: string,
parser: ESLintCustomBlockParser,
parserOptions: any,
): any {
const result = isEnhancedParserObject(parser)
? parser.parseForESLint(code, parserOptions)
: parser.parse(code, parserOptions)
if (result.ast != null) {
return result
}
return { ast: result }
}
/**
* Create shared context.
*
* @param text The source code of SFC.
* @param customBlock The custom block node.
* @param parsedResult The parse result data
* @param parserOptions The parser options.
*/
export function createCustomBlockSharedContext({
text,
customBlock,
parsedResult,
globalLocationCalculator,
parserOptions,
}: {
text: string
customBlock: VElement
parsedResult: ESLintExtendedProgram & { error?: ParseError | Error }
globalLocationCalculator: LocationCalculatorForHtml
parserOptions: any
}) {
let sourceCode: SourceCode
let scopeManager: ScopeManager
let currentNode: any
return {
serCurrentNode(node: any) {
currentNode = node
},
context: {
getAncestors: () => getAncestors(currentNode),
getDeclaredVariables: (...args: any[]) =>
// @ts-expect-error -- ignore
getScopeManager().getDeclaredVariables(...args),
getScope: () => getScope(getScopeManager(), currentNode),
markVariableAsUsed: (name: string) =>
markVariableAsUsed(
getScopeManager(),
currentNode,
parserOptions,
name,
),
parserServices: {
customBlock,
parseCustomBlockElement(
parser: ESLintCustomBlockParser,
options: any,
) {
return parseCustomBlockElement(
customBlock,
parser,
globalLocationCalculator,
{ ...parserOptions, ...options },
)
},
...(parsedResult.services || {}),
...(parsedResult.error
? { parseError: parsedResult.error }
: {}),
},
getSourceCode,
},
}
function getSourceCode() {
return (
sourceCode ||
// eslint-disable-next-line @typescript-eslint/no-require-imports
(sourceCode = new (require("eslint").SourceCode)({
text,
ast: parsedResult.ast,
parserServices: parsedResult.services,
scopeManager: getScopeManager(),
visitorKeys: parsedResult.visitorKeys,
}))
)
}
function getScopeManager() {
if (parsedResult.scopeManager || scopeManager) {
return parsedResult.scopeManager || scopeManager
}
const ecmaVersion = getEcmaVersionIfUseEspree(parserOptions) || 2022
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const sourceType = parserOptions.sourceType || "script"
scopeManager = getEslintScope().analyze(parsedResult.ast, {
ignoreEval: true,
nodejsScope: false,
impliedStrict: ecmaFeatures.impliedStrict,
ecmaVersion,
sourceType,
fallback: getFallbackKeys,
})
return scopeManager
}
}
/* The following source code is copied from `eslint/lib/linter/linter.js` */
/**
* Gets all the ancestors of a given node
* @param {ASTNode} node The node
* @returns {ASTNode[]} All the ancestor nodes in the AST, not including the provided node, starting
* from the root node and going inwards to the parent node.
*/
function getAncestors(node: Node) {
const ancestorsStartingAtParent = []
for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
ancestorsStartingAtParent.push(ancestor)
}
return ancestorsStartingAtParent.reverse()
}
/**
* Gets the scope for the current node
* @param {ScopeManager} scopeManager The scope manager for this AST
* @param {ASTNode} currentNode The node to get the scope of
* @returns {eslint-scope.Scope} The scope information for this node
*/
function getScope(scopeManager: ScopeManager, currentNode: Node) {
// On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
const inner = currentNode.type !== "Program"
for (
let node: Node | null = currentNode;
node;
node = node.parent || null
) {
const scope = scopeManager.acquire(node as any, inner)
if (scope) {
if (scope.type === "function-expression-name") {
return scope.childScopes[0]
}
return scope
}
}
return scopeManager.scopes[0]
}
/**
* Marks a variable as used in the current scope
* @param {ScopeManager} scopeManager The scope manager for this AST. The scope may be mutated by this function.
* @param {ASTNode} currentNode The node currently being traversed
* @param {Object} parserOptions The options used to parse this text
* @param {string} name The name of the variable that should be marked as used.
* @returns {boolean} True if the variable was found and marked as used, false if not.
*/
function markVariableAsUsed(
scopeManager: ScopeManager,
currentNode: Node,
parserOptions: any,
name: string,
) {
const hasGlobalReturn =
parserOptions.ecmaFeatures && parserOptions.ecmaFeatures.globalReturn
const specialScope =
hasGlobalReturn || parserOptions.sourceType === "module"
const currentScope = getScope(scopeManager, currentNode)
// Special Node.js scope means we need to start one level deeper
const initialScope =
currentScope.type === "global" && specialScope
? currentScope.childScopes[0]
: currentScope
for (let scope: Scope | null = initialScope; scope; scope = scope.upper) {
const variable = scope.variables.find(
(scopeVar) => scopeVar.name === name,
)
if (variable) {
// @ts-expect-error -- ignore
variable.eslintUsed = true
return true
}
}
return false
}