-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcontext.ts
More file actions
104 lines (96 loc) · 3.5 KB
/
context.ts
File metadata and controls
104 lines (96 loc) · 3.5 KB
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
/*
* Copyright (c) 2024, 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 { ImportManager } from '../imports';
import type { ImportDeclaration as EsImportDeclaration, Statement as EsStatement } from 'estree';
import type { TemplateOpts, TransformerContext } from './types';
export function createNewContext(templateOptions: TemplateOpts): {
getImports: () => EsImportDeclaration[];
cxt: TransformerContext;
} {
const importManager = new ImportManager();
const localVarStack: Set<string>[] = [];
const pushLocalVars = (vars: string[]) => {
localVarStack.push(new Set(vars));
};
const popLocalVars = () => {
localVarStack.pop();
};
const isLocalVar = (varName: string | null | undefined) => {
if (!varName) {
return false;
}
for (const stackFrame of localVarStack) {
if (stackFrame.has(varName)) {
return true;
}
}
return false;
};
const getLocalVars = () => localVarStack.flatMap((stackFrameVars) => [...stackFrameVars]);
const hoistedStatements = {
module: [] as EsStatement[],
templateFn: [] as EsStatement[],
};
const previouslyHoistedStatementKeysMod = new Set<unknown>();
const previouslyHoistedStatementKeysTmpl = new Set<unknown>();
const hoist = {
module(stmt: EsStatement, optionalDedupeKey?: unknown) {
if (optionalDedupeKey) {
if (previouslyHoistedStatementKeysMod.has(optionalDedupeKey)) {
return;
}
previouslyHoistedStatementKeysMod.add(optionalDedupeKey);
}
hoistedStatements.module.push(stmt);
},
templateFn(stmt: EsStatement, optionalDedupeKey?: unknown) {
if (optionalDedupeKey) {
if (previouslyHoistedStatementKeysTmpl.has(optionalDedupeKey)) {
return;
}
previouslyHoistedStatementKeysTmpl.add(optionalDedupeKey);
}
hoistedStatements.templateFn.push(stmt);
},
};
const shadowSlotToFnName = new Map<string, string>();
let fnNameUniqueId = 0;
const slots = {
shadow: {
isDuplicate(uniqueNodeId: string) {
return shadowSlotToFnName.has(uniqueNodeId);
},
register(uniqueNodeId: string, kebabCmpName: string) {
if (slots.shadow.isDuplicate(uniqueNodeId)) {
return shadowSlotToFnName.get(uniqueNodeId)!;
}
const shadowSlotContentFnName = `__lwcGenerateShadowSlottedContent_${kebabCmpName}_${fnNameUniqueId++}`;
shadowSlotToFnName.set(uniqueNodeId, shadowSlotContentFnName);
return shadowSlotContentFnName;
},
getFnName(uniqueNodeId: string) {
return shadowSlotToFnName.get(uniqueNodeId) ?? null;
},
},
};
return {
getImports: () => importManager.getImportDeclarations(),
cxt: {
pushLocalVars,
popLocalVars,
isLocalVar,
getLocalVars,
templateOptions,
hoist,
hoistedStatements,
slots,
import: importManager.add.bind(importManager),
siblings: undefined,
currentNodeIndex: undefined,
},
};
}