-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparser.ts
243 lines (225 loc) · 7.55 KB
/
parser.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
/* global require -- node */
import assert from "assert";
import fs from "fs";
import semver from "semver";
import { traverseNodes } from "../../../src/traverse";
import { parseForESLint } from "../../../src";
import {
generateParserOptions,
listupFixtures,
astToJson,
scopeToJSON,
} from "./test-utils";
import type { Comment, SvelteProgram, Token } from "../../../src/ast";
import { sortNodes } from "../../../src/parser/sort";
import { sortJson } from "./test-utils";
function parse(code: string, filePath: string, config: any) {
return parseForESLint(code, generateParserOptions({ filePath }, config));
}
describe("Check for AST.", () => {
for (const {
input,
inputFileName,
outputFileName,
config,
getScopeFile,
meetRequirements,
} of listupFixtures()) {
if (!meetRequirements("parse")) {
continue;
}
describe(inputFileName, () => {
let result: any;
it("most to generate the expected AST.", () => {
result = parse(input, inputFileName, config);
if (!meetRequirements("test")) {
return;
}
const astJson = astToJson(result.ast);
const output = fs.readFileSync(outputFileName, "utf8");
assert.strictEqual(astJson, output);
});
if (meetRequirements("scope"))
it("most to generate the expected scope.", () => {
let json: any = scopeToJSON(result.scopeManager);
let output: any = getScopeFile();
if (
result.services?.program // use ts parser
) {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- ignore
const pkg = require("@typescript-eslint/parser/package.json");
if (!semver.satisfies(pkg.version, "^5.6.0")) {
// adjust global scope
json = JSON.parse(json);
output = JSON.parse(output);
json.variables = output.variables;
}
}
assert.deepStrictEqual(sortJson(json), sortJson(output));
});
it("location must be correct.", () => {
// check tokens
checkTokens(result.ast, input);
checkLoc(result.ast, inputFileName, input);
});
it("even if Win, it must be correct.", () => {
const inputForWin = input.replace(/\n/g, "\r\n");
// check
const astForWin = parse(inputForWin, inputFileName, config).ast;
// check tokens
checkTokens(astForWin, inputForWin);
});
});
}
});
function checkTokens(ast: SvelteProgram, input: string) {
const allTokens = sortNodes([...ast.tokens, ...ast.comments]);
// check loc
for (const token of allTokens) {
const value = getText(token);
assert.strictEqual(value, input.slice(...token.range));
}
assert.strictEqual(
input
.replace(/\s/gu, "")
.split(/(.{0,20})/)
.filter((s) => s)
.join("\n"),
allTokens
.map(getText)
.join("")
.replace(/\s/gu, "")
.split(/(.{0,20})/)
.filter((s) => s)
.join("\n"),
);
function getText(token: Token | Comment) {
return token.type === "Block"
? `/*${token.value}*/`
: token.type === "Line"
? `//${token.value}`
: token.value;
}
}
function checkLoc(ast: SvelteProgram, fileName: string, code: string) {
for (const token of ast.tokens) {
assert.ok(
token.range[0] < token.range[1],
`No range on "${token.type} line:${token.loc.start.line} col:${token.loc.start.column}" in ${fileName}`,
);
}
for (const token of ast.comments) {
assert.ok(
token.range[0] < token.range[1],
`No range on "${token.type} line:${token.loc.start.line} col:${token.loc.start.column}" in ${fileName}`,
);
}
const set = new Set<any>();
traverseNodes(ast, {
enterNode(node, parent) {
if (
(node.type.startsWith("Svelte") || parent?.type.startsWith("Svelte")) &&
set.has(node)
) {
assert.fail(
`Duplicate node @parent: ${parent?.type}, ${astToJson(node)}`,
);
}
set.add(node);
const nodeParent = (node as any).parent;
if (parent?.type.startsWith("Svelte")) {
assert.ok(
nodeParent?.type === parent?.type,
`Parent type mismatch [${nodeParent?.type} : ${parent?.type}] @${astToJson(
node,
)}`,
);
}
if (nodeParent) {
assert.ok(
nodeParent.range?.[0] === parent?.range![0],
`Parent range mismatch [${
nodeParent?.range?.[0]
} : ${parent?.range![0]}] @${astToJson(node)}`,
);
assert.ok(
nodeParent.range?.[1] === parent?.range![1],
`Parent range mismatch [${
nodeParent?.range?.[1]
} : ${parent?.range![1]}] @${astToJson(node)}`,
);
}
assert.ok(
node.range![0] < node.range![1],
`No range on "${node.type} line:${node.loc!.start.line} col:${
node.loc!.start.column
}" in ${fileName}`,
);
if (parent) {
assert.ok(
parent.range![0] <= node.range![0],
`overlap range[0] on "${parent.type} line:${
parent.loc!.start.line
} col:${parent.loc!.start.column}" > "${node.type} line:${
node.loc!.start.line
} col:${node.loc!.start.column}" in ${fileName}`,
);
assert.ok(
node.range![1] <= parent.range![1],
`overlap range[1] on "${parent.type} line:${
parent.loc!.end.line
} col:${parent.loc!.end.column}" > "${node.type} line:${
node.loc!.end.line
} col:${node.loc!.end.column}" in ${fileName}`,
);
assert.ok(
parent.loc!.start.line <= node.loc!.start.line,
`overlap loc.start.line on "${parent.type} line:${
parent.loc!.start.line
} col:${parent.loc!.start.column}" > "${node.type} line:${
node.loc!.start.line
} col:${node.loc!.start.column}" in ${fileName}`,
);
if (parent.loc!.start.line === node.loc!.start.line) {
assert.ok(
parent.loc!.start.column <= node.loc!.start.column,
`overlap loc.start.column on "${parent.type} line:${
parent.loc!.start.line
} col:${parent.loc!.start.column}" > "${node.type} line:${
node.loc!.start.line
} col:${node.loc!.start.column}" in ${fileName}`,
);
}
assert.ok(
node.loc!.end.line <= parent.loc!.end.line,
`overlap loc.end.line on "${parent.type} line:${
parent.loc!.end.line
} col:${parent.loc!.end.column}" > "${node.type} line:${
node.loc!.end.line
} col:${node.loc!.end.column}" in ${fileName}`,
);
if (parent.loc!.end.line === node.loc!.end.line) {
assert.ok(
node.loc!.end.column <= parent.loc!.end.column,
`overlap loc.end.column on "${parent.type} line:${
parent.loc!.end.line
} col:${parent.loc!.end.column}" > "${node.type} line:${
node.loc!.end.line
} col:${node.loc!.end.column}" in ${fileName}`,
);
}
}
if (node.type === "SvelteStartTag") {
assert.strictEqual(code[node.range[0]], "<");
assert.strictEqual(code[node.range[1] - 1], ">");
}
if (node.type === "SvelteEndTag") {
assert.strictEqual(code.slice(node.range[0], node.range[0] + 2), "</");
assert.strictEqual(code[node.range[1] - 1], ">");
}
},
leaveNode() {
// noop
},
});
}