-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathscript.ts
58 lines (50 loc) · 1.55 KB
/
script.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
import type { ESLintExtendedProgram } from "."
import { analyzeScope } from "./analyze-scope"
import { traverseNodes } from "../traverse"
import type { ScriptsSourceCode } from "../context"
import { getParser } from "./resolve-parser"
/**
* Parse for script
*/
export function parseScript(
script: ScriptsSourceCode,
parserOptions: any = {},
): ESLintExtendedProgram {
const result = parseScriptWithoutAnalyzeScope(script, parserOptions)
if (!result.scopeManager) {
const scopeManager = analyzeScope(result.ast, parserOptions)
result.scopeManager = scopeManager
}
traverseNodes(result.ast, {
visitorKeys: result.visitorKeys,
enterNode(node, parent) {
;(node as any).parent = parent
if (node.type === "LabeledStatement" && node.label.name === "$") {
if (parent?.type === "Program") {
// Transform node type
node.type = "SvelteReactiveStatement" as any
}
}
},
leaveNode() {
//
},
})
return result
}
/**
* Parse for script without analyze scope
*/
function parseScriptWithoutAnalyzeScope(
{ vcode, attrs }: ScriptsSourceCode,
options: any,
): ESLintExtendedProgram {
const parser = getParser(attrs, options.parser)
const result =
parser.parseForESLint?.(vcode, options) ??
parser.parse?.(vcode, options)
if ("ast" in result && result.ast != null) {
return result
}
return { ast: result } as ESLintExtendedProgram
}