poe-code 6.0.0 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,386 +0,0 @@
1
- import { parseModule } from "../../parse/parser.js";
2
- const MESSAGE = "Await is only allowed at the script top level or inside async arrow functions.";
3
- export function AS008(source, options = {}) {
4
- return new AS008Scanner(options.filename ?? "<input>").scan(source);
5
- }
6
- class AS008Scanner {
7
- filename;
8
- diagnostics = [];
9
- functionStack = [];
10
- scopeDepth = 0;
11
- constructor(filename) {
12
- this.filename = filename;
13
- }
14
- scan(source) {
15
- const module = parseModule(source, this.filename);
16
- this.visitModule(module);
17
- return this.diagnostics;
18
- }
19
- visitModule(node) {
20
- for (const statement of node.body) {
21
- this.visitStatement(statement);
22
- }
23
- }
24
- visitStatement(node) {
25
- switch (node.type) {
26
- case "FunctionDeclaration":
27
- this.visitArrowFunction(node);
28
- return;
29
- case "BlockStatement":
30
- this.withNestedScope(() => {
31
- this.visitBlock(node);
32
- });
33
- return;
34
- case "ExpressionStatement":
35
- this.visitExpression(node.expression);
36
- return;
37
- case "IfStatement":
38
- this.visitIfStatement(node);
39
- return;
40
- case "ForStatement":
41
- this.visitForStatement(node);
42
- return;
43
- case "ForInStatement":
44
- case "ForOfStatement":
45
- this.visitForOfStatement(node);
46
- return;
47
- case "WhileStatement":
48
- case "DoWhileStatement":
49
- this.visitWhileStatement(node);
50
- return;
51
- case "TryStatement":
52
- this.visitTryStatement(node);
53
- return;
54
- case "VariableDeclaration":
55
- this.visitVariableDeclaration(node);
56
- return;
57
- case "ReturnStatement":
58
- if (node.argument !== undefined) {
59
- this.visitExpression(node.argument);
60
- }
61
- return;
62
- case "ThrowStatement":
63
- this.visitThrowStatement(node);
64
- return;
65
- case "ImportDeclaration":
66
- case "BreakStatement":
67
- case "ContinueStatement":
68
- return;
69
- }
70
- }
71
- visitBlock(node) {
72
- for (const statement of node.body) {
73
- this.visitStatement(statement);
74
- }
75
- }
76
- visitIfStatement(node) {
77
- this.visitExpression(node.test);
78
- this.visitStatement(node.consequent);
79
- if (node.alternate !== undefined) {
80
- this.visitStatement(node.alternate);
81
- }
82
- }
83
- visitForStatement(node) {
84
- if (node.init !== undefined) {
85
- if (node.init.type === "VariableDeclaration") {
86
- this.visitVariableDeclaration(node.init);
87
- }
88
- else {
89
- this.visitExpression(node.init);
90
- }
91
- }
92
- if (node.test !== undefined) {
93
- this.visitExpression(node.test);
94
- }
95
- if (node.update !== undefined) {
96
- this.visitExpression(node.update);
97
- }
98
- this.visitStatement(node.body);
99
- }
100
- visitForOfStatement(node) {
101
- if (node.left.type === "VariableDeclaration") {
102
- this.visitVariableDeclaration(node.left);
103
- }
104
- else {
105
- this.visitAssignmentTarget(node.left);
106
- }
107
- this.visitExpression(node.right);
108
- this.visitStatement(node.body);
109
- }
110
- visitWhileStatement(node) {
111
- this.visitExpression(node.test);
112
- this.visitStatement(node.body);
113
- }
114
- visitTryStatement(node) {
115
- this.visitStatement(node.block);
116
- if (node.handler !== undefined) {
117
- this.visitCatchClause(node.handler);
118
- }
119
- if (node.finalizer !== undefined) {
120
- this.visitStatement(node.finalizer);
121
- }
122
- }
123
- visitCatchClause(node) {
124
- this.withNestedScope(() => {
125
- if (node.param !== undefined) {
126
- this.visitBindingPattern(node.param);
127
- }
128
- this.visitBlock(node.body);
129
- });
130
- }
131
- visitThrowStatement(node) {
132
- this.visitExpression(node.argument);
133
- }
134
- visitVariableDeclaration(node) {
135
- for (const declarator of node.declarations) {
136
- this.visitVariableDeclarator(declarator);
137
- }
138
- }
139
- visitVariableDeclarator(node) {
140
- this.visitBindingPattern(node.id);
141
- if (node.init !== undefined) {
142
- this.visitExpression(node.init);
143
- }
144
- }
145
- visitExpression(node) {
146
- switch (node.type) {
147
- case "YieldExpression":
148
- if (node.argument !== undefined) {
149
- this.visitExpression(node.argument);
150
- }
151
- return;
152
- case "FunctionExpression":
153
- case "ArrowFunctionExpression":
154
- this.visitArrowFunction(node);
155
- return;
156
- case "AwaitExpression":
157
- this.visitAwaitExpression(node);
158
- return;
159
- case "ArrayExpression":
160
- this.visitArrayExpression(node);
161
- return;
162
- case "ObjectExpression":
163
- this.visitObjectExpression(node);
164
- return;
165
- case "UnaryExpression":
166
- this.visitUnaryExpression(node);
167
- return;
168
- case "BinaryExpression":
169
- this.visitBinaryExpression(node);
170
- return;
171
- case "LogicalExpression":
172
- this.visitLogicalExpression(node);
173
- return;
174
- case "ConditionalExpression":
175
- this.visitConditionalExpression(node);
176
- return;
177
- case "MemberExpression":
178
- this.visitMemberExpression(node);
179
- return;
180
- case "AssignmentExpression":
181
- this.visitAssignmentExpression(node);
182
- return;
183
- case "CallExpression":
184
- this.visitCallExpression(node);
185
- return;
186
- case "TemplateLiteral":
187
- this.visitTemplateLiteral(node);
188
- return;
189
- case "Identifier":
190
- case "BooleanLiteral":
191
- case "NullLiteral":
192
- case "NumericLiteral":
193
- case "StringLiteral":
194
- case "UndefinedLiteral":
195
- return;
196
- }
197
- }
198
- visitArrowFunction(node) {
199
- this.functionStack.push(node.async);
200
- for (const parameter of node.params) {
201
- this.visitBindingElement(parameter);
202
- }
203
- const body = node.body;
204
- if (body.type === "BlockStatement") {
205
- this.withNestedScope(() => {
206
- this.visitBlock(body);
207
- });
208
- }
209
- else {
210
- this.visitExpression(body);
211
- }
212
- this.functionStack.pop();
213
- }
214
- visitAwaitExpression(node) {
215
- if (!this.isAwaitAllowed()) {
216
- this.diagnostics.push({
217
- code: "AS008",
218
- severity: "error",
219
- message: MESSAGE,
220
- filename: this.filename,
221
- line: node.span.start.line,
222
- column: node.span.start.column,
223
- span: node.span
224
- });
225
- }
226
- this.visitExpression(node.argument);
227
- }
228
- visitArrayExpression(node) {
229
- for (const element of node.elements) {
230
- if (element?.type === "SpreadElement") {
231
- this.visitSpreadElement(element);
232
- continue;
233
- }
234
- if (element !== undefined) {
235
- this.visitExpression(element);
236
- }
237
- }
238
- }
239
- visitObjectExpression(node) {
240
- for (const property of node.properties) {
241
- if (property.type === "SpreadElement") {
242
- this.visitSpreadElement(property);
243
- continue;
244
- }
245
- this.visitProperty(property);
246
- }
247
- }
248
- visitUnaryExpression(node) {
249
- this.visitExpression(node.argument);
250
- }
251
- visitBinaryExpression(node) {
252
- this.visitExpression(node.left);
253
- this.visitExpression(node.right);
254
- }
255
- visitLogicalExpression(node) {
256
- this.visitExpression(node.left);
257
- this.visitExpression(node.right);
258
- }
259
- visitConditionalExpression(node) {
260
- this.visitExpression(node.test);
261
- this.visitExpression(node.consequent);
262
- this.visitExpression(node.alternate);
263
- }
264
- visitMemberExpression(node) {
265
- this.visitExpression(node.object);
266
- if (node.computed) {
267
- this.visitExpression(node.property);
268
- }
269
- }
270
- visitAssignmentExpression(node) {
271
- this.visitAssignmentTarget(node.left);
272
- this.visitExpression(node.right);
273
- }
274
- visitCallExpression(node) {
275
- this.visitExpression(node.callee);
276
- for (const argument of node.arguments) {
277
- if (argument.type === "SpreadElement") {
278
- this.visitSpreadElement(argument);
279
- continue;
280
- }
281
- this.visitExpression(argument);
282
- }
283
- }
284
- visitTemplateLiteral(node) {
285
- for (const expression of node.expressions) {
286
- this.visitExpression(expression);
287
- }
288
- }
289
- visitProperty(node) {
290
- if (node.computed) {
291
- this.visitExpression(node.key);
292
- }
293
- this.visitExpression(node.value);
294
- }
295
- visitSpreadElement(node) {
296
- this.visitExpression(node.argument);
297
- }
298
- visitAssignmentTarget(node) {
299
- if (node.type === "Identifier") {
300
- return;
301
- }
302
- if (node.type === "MetaProperty") {
303
- return;
304
- }
305
- if (node.type === "MemberExpression") {
306
- this.visitMemberExpression(node);
307
- return;
308
- }
309
- this.visitBindingPattern(node);
310
- }
311
- visitBindingElement(node) {
312
- if (node.type === "RestElement") {
313
- this.visitRestElement(node);
314
- return;
315
- }
316
- this.visitBindingPattern(node);
317
- }
318
- visitBindingPattern(node) {
319
- switch (node.type) {
320
- case "Identifier":
321
- return;
322
- case "MemberExpression":
323
- this.visitMemberExpression(node);
324
- return;
325
- case "AssignmentPattern":
326
- this.visitAssignmentPattern(node);
327
- return;
328
- case "ArrayPattern":
329
- this.visitArrayPattern(node);
330
- return;
331
- case "ObjectPattern":
332
- this.visitObjectPattern(node);
333
- return;
334
- }
335
- }
336
- visitAssignmentPattern(node) {
337
- this.visitBindingPattern(node.left);
338
- this.visitExpression(node.right);
339
- }
340
- visitArrayPattern(node) {
341
- for (const element of node.elements) {
342
- if (element === null) {
343
- continue;
344
- }
345
- if (element.type === "RestElement") {
346
- this.visitRestElement(element);
347
- continue;
348
- }
349
- this.visitBindingPattern(element);
350
- }
351
- }
352
- visitObjectPattern(node) {
353
- for (const property of node.properties) {
354
- if (property.type === "RestElement") {
355
- this.visitRestElement(property);
356
- continue;
357
- }
358
- this.visitAssignmentProperty(property);
359
- }
360
- }
361
- visitAssignmentProperty(node) {
362
- if (node.computed) {
363
- this.visitExpression(node.key);
364
- }
365
- this.visitBindingPattern(node.value);
366
- }
367
- visitRestElement(node) {
368
- this.visitAssignmentTarget(node.argument);
369
- }
370
- isAwaitAllowed() {
371
- const currentFunction = this.functionStack.at(-1);
372
- if (currentFunction !== undefined) {
373
- return true;
374
- }
375
- return this.scopeDepth === 0;
376
- }
377
- withNestedScope(callback) {
378
- this.scopeDepth += 1;
379
- try {
380
- callback();
381
- }
382
- finally {
383
- this.scopeDepth -= 1;
384
- }
385
- }
386
- }