-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcontext.ts
More file actions
48 lines (44 loc) · 1.38 KB
/
context.ts
File metadata and controls
48 lines (44 loc) · 1.38 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
/*
* 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 } 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;
};
return {
getImports: () => importManager.getImportDeclarations(),
cxt: {
pushLocalVars,
popLocalVars,
isLocalVar,
templateOptions,
import: importManager.add.bind(importManager),
bufferedTextNodeValues: [],
},
};
}