-
Notifications
You must be signed in to change notification settings - Fork 393
/
parser.ts
458 lines (397 loc) · 15.7 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import {
CompilerDiagnostic,
CompilerError,
InstrumentationObject,
generateCompilerDiagnostic,
generateCompilerError,
Location,
LWCErrorInfo,
normalizeToDiagnostic,
} from '@lwc/errors';
import { APIVersion } from '@lwc/shared';
import { NormalizedConfig } from '../config';
import { isPreserveCommentsDirective, isRenderModeDirective } from '../shared/ast';
import {
Root,
SourceLocation,
ParentNode,
BaseNode,
LWCDirectiveRenderMode,
IfBlock,
ElseifBlock,
ElseBlock,
} from '../shared/types';
import { TMPL_EXPR_ECMASCRIPT_EDITION } from './constants';
import type { ecmaVersion as EcmaVersion } from 'acorn';
import type { PreparsedExpressionMap } from './expression-complex';
function normalizeLocation(location?: SourceLocation): Location {
let line = 0;
let column = 0;
let length = 0;
let start = 0;
if (location) {
line = location.startLine;
column = location.startColumn;
length = location.end - location.start;
start = location.start;
}
return { line, column, start, length };
}
interface ParentWrapper {
parent: ParentNode | null;
current: ParentNode;
}
interface IfContext {
currentNode: IfBlock | ElseifBlock | ElseBlock;
// Within a specific if-context, each set of seen slot names must be tracked separately
// because duplicate names in separate branches of the same if-block are allowed (the branching
// logic provides a compile-time guarantee that the slots will not be rendered multiple times).
// seenSlots keeps track of each set holding the slots seen in each branch of the if-block.
seenSlots: Set<string>[];
}
// A SiblingScope object keeps track of the context needed to parse a series of if-elseif-else nodes.
interface SiblingScope {
// Context for the if-elseif-else chain currently being parsed at this level. This
// IfContext keeps track of the most recently parsed node in the chain and the set of slot names we've seen in all
// previous siblings in the chain.
ifContext?: IfContext;
// Reference to the nearest ancestor IfContext. The existence of an ancestor
// IfContext means that we are currently parsing nodes nested within an if-elseif-else chain. Context from that ancestor
// is needed to track which slot names have already been seen in and only in the current scope. This reference is also needed
// so we know where to merge all visited slot names from the current IfContext.
ancestorIfContext?: IfContext;
}
export default class ParserCtx {
private readonly source: string;
readonly config: NormalizedConfig;
readonly warnings: CompilerDiagnostic[] = [];
/**
* Instrumentation object to handle gathering metrics and internal logs for everything happening
* during this context.
*/
readonly instrumentation?: InstrumentationObject;
readonly seenIds: Set<string> = new Set();
readonly seenSlots: Set<string> = new Set();
/**
* This set is not aware of if-elseif-else blocks.
*/
readonly seenScopedSlots: Set<string> = new Set();
// TODO [#3370]: remove experimental template expression flag
readonly ecmaVersion: EcmaVersion;
// TODO [#3370]: remove experimental template expression flag
/**
* Parsing of template expressions is deferred to acorn, in order to ensure that JavaScript expressions
* are parsed correctly. As such, we should cache the ESTree AST for each expression, so that the
* expression need not be re-parsed when the template compiler's AST is being created.
*/
readonly preparsedJsExpressions?: PreparsedExpressionMap;
/**
* 'elementScopes' keeps track of the hierarchy of ParentNodes as the parser
* traverses the parse5 AST. Each 'elementScope' is an array where each node in
* the array corresponds to either an IfBlock, ElseifBlock, ElseBlock, ForEach, ForOf, If, Element, Component, or Slot.
*
* Currently, each elementScope has a hierarchy of IfBlock > ForBlock > If > Element | Component | Slot.
* Note: Not all elementScopes will have all the nodes listed above, but when they do, they will appear in this order.
* We do not keep track of template nodes.
*
* Each scope corresponds to the original parse5.Element node.
*/
private readonly elementScopes: ParentNode[][] = [];
/**
* 'siblingScopes' keeps track of the context from one sibling node to another.
* This holds the info needed to properly parse lwc:if, lwc:elseif, and lwc:else directives.
*/
private readonly siblingScopes: SiblingScope[] = [];
renderMode: LWCDirectiveRenderMode;
preserveComments: boolean;
apiVersion: APIVersion;
constructor(source: string, config: NormalizedConfig) {
this.source = source;
this.config = config;
this.renderMode = LWCDirectiveRenderMode.shadow;
this.preserveComments = config.preserveHtmlComments;
// TODO [#3370]: remove experimental template expression flag
if (config.experimentalComplexExpressions) {
this.preparsedJsExpressions = new Map();
}
this.ecmaVersion = config.experimentalComplexExpressions
? TMPL_EXPR_ECMASCRIPT_EDITION
: 2020;
this.instrumentation = config.instrumentation;
this.apiVersion = config.apiVersion;
}
getSource(start: number, end: number): string {
return this.source.slice(start, end);
}
setRootDirective(root: Root): void {
this.renderMode =
root.directives.find(isRenderModeDirective)?.value.value ?? this.renderMode;
this.preserveComments =
root.directives.find(isPreserveCommentsDirective)?.value.value || this.preserveComments;
}
/**
* This method flattens the scopes into a single array for traversal.
* @param element
* @yields Each node in the scope and its parent.
*/
*ancestors(element?: ParentNode): IterableIterator<ParentWrapper> {
const ancestors = this.elementScopes.flat();
const start = element ? ancestors.indexOf(element) : ancestors.length - 1;
for (let i = start; i >= 0; i--) {
yield { current: ancestors[i], parent: ancestors[i - 1] };
}
}
/**
* This method returns an iterator over ancestor nodes, starting at the parent and ending at the root node.
*
* Note: There are instances when we want to terminate the traversal early, such as searching for a ForBlock parent.
* @param predicate This callback is called once for each ancestor until it finds one where predicate returns true.
* @param traversalCond This callback is called after predicate and will terminate the traversal if it returns false.
* traversalCond is ignored if no value is provided.
* @param startNode Starting node to begin search, defaults to the tail of the current scope.
*/
findAncestor<A extends ParentNode>(
predicate: (node: ParentNode) => node is A,
traversalCond: (nodes: ParentWrapper) => unknown = () => true,
startNode?: ParentNode
): A | null {
for (const { current, parent } of this.ancestors(startNode)) {
if (predicate(current)) {
return current;
}
if (!traversalCond({ current, parent })) {
break;
}
}
return null;
}
/**
* This method searchs the current scope and returns the value that satisfies the predicate.
* @param predicate This callback is called once for each sibling in the current scope
* until it finds one where predicate returns true.
*/
findInCurrentElementScope<A extends ParentNode>(
predicate: (node: ParentNode) => node is A
): A | null {
const currentScope = this.currentElementScope() || [];
return currentScope.find(predicate) || null;
}
beginElementScope(): void {
this.elementScopes.push([]);
}
endElementScope(): ParentNode | undefined {
const scope = this.elementScopes.pop();
return scope ? scope[0] : undefined;
}
addNodeCurrentElementScope(node: ParentNode): void {
const currentScope = this.currentElementScope();
/* istanbul ignore if */
if (!currentScope) {
throw new Error("Can't invoke addNodeCurrentElementScope if there is no current scope");
}
currentScope.push(node);
}
hasSeenSlot(name: string): boolean {
return this.seenSlotsFromAncestorIfTree().has(name);
}
addSeenSlot(name: string): void {
const currentSeenSlots = this.seenSlotsFromAncestorIfTree();
if (currentSeenSlots) {
currentSeenSlots.add(name);
} else {
this.seenSlots.add(name);
}
}
private currentElementScope(): ParentNode[] | undefined {
return this.elementScopes[this.elementScopes.length - 1];
}
beginSiblingScope() {
this.siblingScopes.push({
ancestorIfContext: this.currentIfContext() || this.ancestorIfContext(),
});
}
endSiblingScope() {
this.siblingScopes.pop();
}
beginIfChain(node: IfBlock) {
const currentSiblingContext = this.currentSiblingContext();
if (!currentSiblingContext) {
throw new Error('Cannot invoke beginIfChain if there is currently no sibling context');
}
const currentIfContext = this.currentIfContext();
if (currentIfContext) {
throw new Error(
'Should not invoke beginIfChain if an if context already exists. First end the current chain before starting a new one.'
);
}
const previouslySeenSlots = this.seenSlotsFromAncestorIfTree();
currentSiblingContext.ifContext = {
currentNode: node,
seenSlots: [new Set<string>(previouslySeenSlots)],
};
}
appendToIfChain(node: ElseifBlock | ElseBlock) {
const currentIfContext = this.currentIfContext();
if (!currentIfContext) {
throw new Error('Cannot invoke appendToIfChain without first setting the if context.');
}
currentIfContext.currentNode = node;
const previouslySeenSlots = this.seenSlotsFromAncestorIfTree();
currentIfContext.seenSlots.push(new Set<string>(previouslySeenSlots));
}
endIfChain() {
const currentIfContext = this.currentIfContext();
if (!currentIfContext) {
throw new Error('Cannot invoke endIfChain if there is currently no if context');
}
// Merge seen slot names from the current if chain into the parent scope.
const seenSlotsInAncestorIfTree = this.seenSlotsFromAncestorIfTree();
for (const seenSlots of currentIfContext.seenSlots) {
for (const name of seenSlots) {
seenSlotsInAncestorIfTree.add(name);
}
}
const currentSiblingContext = this.currentSiblingContext();
if (currentSiblingContext) {
currentSiblingContext.ifContext = undefined;
}
}
getSiblingIfNode(): IfBlock | ElseifBlock | ElseBlock | undefined {
return this.currentIfContext()?.currentNode;
}
isParsingSiblingIfBlock(): boolean {
return !!this.currentIfContext();
}
private currentSiblingContext(): SiblingScope | undefined {
return this.siblingScopes[this.siblingScopes.length - 1];
}
private currentIfContext(): IfContext | undefined {
return this.currentSiblingContext()?.ifContext;
}
private ancestorIfContext(): IfContext | undefined {
return this.currentSiblingContext()?.ancestorIfContext;
}
private seenSlotsFromAncestorIfTree(): Set<string> {
const ancestorIfContext = this.ancestorIfContext();
if (ancestorIfContext) {
return ancestorIfContext.seenSlots[ancestorIfContext.seenSlots.length - 1];
}
return this.seenSlots;
}
/**
* This method recovers from diagnostic errors that are encountered when fn is invoked.
* All other errors are considered compiler errors and can not be recovered from.
* @param fn method to be invoked.
*/
withErrorRecovery<T>(fn: () => T): T | undefined {
try {
return fn();
} catch (error) {
/* istanbul ignore else */
if (error instanceof CompilerError) {
// Diagnostic error
this.addDiagnostic(error.toDiagnostic());
} else {
// Compiler error
throw error;
}
}
}
withErrorWrapping<T>(
fn: () => T,
errorInfo: LWCErrorInfo,
location: SourceLocation,
msgFormatter?: (error: any) => string
): T {
try {
return fn();
} catch (error: any) {
if (msgFormatter) {
error.message = msgFormatter(error);
}
this.throwOnError(errorInfo, error, location);
}
}
throwOnError(errorInfo: LWCErrorInfo, error: any, location?: SourceLocation): never {
const diagnostic = normalizeToDiagnostic(errorInfo, error, {
location: normalizeLocation(location),
});
throw CompilerError.from(diagnostic);
}
/**
* This method throws a diagnostic error with the node's location.
* @param errorInfo
* @param node
* @param messageArgs
*/
throwOnNode(errorInfo: LWCErrorInfo, node: BaseNode, messageArgs?: any[]): never {
this.throw(errorInfo, messageArgs, node.location);
}
/**
* This method throws a diagnostic error with location information.
* @param errorInfo
* @param location
* @param messageArgs
*/
throwAtLocation(errorInfo: LWCErrorInfo, location: SourceLocation, messageArgs?: any[]): never {
this.throw(errorInfo, messageArgs, location);
}
/**
* This method throws a diagnostic error and will immediately exit the current routine.
* @param errorInfo
* @param messageArgs
* @param location
* @throws
*/
throw(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: SourceLocation): never {
throw generateCompilerError(errorInfo, {
messageArgs,
origin: {
location: normalizeLocation(location),
},
});
}
/**
* This method logs a diagnostic warning with the node's location.
* @param errorInfo
* @param node
* @param messageArgs
*/
warnOnNode(errorInfo: LWCErrorInfo, node: BaseNode, messageArgs?: any[]): void {
this.warn(errorInfo, messageArgs, node.location);
}
/**
* This method logs a diagnostic warning with location information.
* @param errorInfo
* @param location
* @param messageArgs
*/
warnAtLocation(errorInfo: LWCErrorInfo, location: SourceLocation, messageArgs?: any[]): void {
this.warn(errorInfo, messageArgs, location);
}
/**
* This method logs a diagnostic warning and will continue execution of the current routine.
* @param errorInfo
* @param messageArgs
* @param location
*/
warn(errorInfo: LWCErrorInfo, messageArgs?: any[], location?: SourceLocation): void {
this.addDiagnostic(
generateCompilerDiagnostic(errorInfo, {
messageArgs,
origin: {
location: normalizeLocation(location),
},
})
);
}
private addDiagnostic(diagnostic: CompilerDiagnostic): void {
this.warnings.push(diagnostic);
}
}