ventojs 0.12.1 → 0.12.2

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,183 +0,0 @@
1
- import * as dntShim from "../_dnt.shims.js";
2
- import { astring, meriyah, walker } from "../deps.js";
3
- // List of identifiers that are in globalThis
4
- // but should be accessed as templateState.identifier
5
- const INCLUDE_GLOBAL = [
6
- "name",
7
- ];
8
- // List of identifiers that should be ignored
9
- // when transforming the code
10
- const DEFAULT_EXCLUDES = [
11
- "globalThis",
12
- "self",
13
- "global",
14
- "this",
15
- "undefined",
16
- "null",
17
- ];
18
- // Tracks the scope of the code
19
- // and the variables that should be ignored
20
- class ScopeTracker {
21
- scopes = [];
22
- // The index of the global/function scope
23
- globalScope = 0;
24
- includes(val) {
25
- for (let i = this.scopes.length - 1; i >= 0; i--) {
26
- if (this.scopes[i].stack.includes(val)) {
27
- return true;
28
- }
29
- }
30
- return false;
31
- }
32
- pushScope(global) {
33
- if (global) {
34
- this.globalScope = this.scopes.length;
35
- }
36
- const newScope = {
37
- globalScope: this.globalScope,
38
- stack: [],
39
- };
40
- this.scopes.push(newScope);
41
- }
42
- popScope() {
43
- this.globalScope = this.scopes[this.scopes.length - 1].globalScope;
44
- this.scopes.pop();
45
- }
46
- pushBinding(val, global) {
47
- if (this.scopes.length === 0) {
48
- this.scopes.push({ globalScope: this.globalScope, stack: [] });
49
- }
50
- if (global) {
51
- this.scopes[this.globalScope].stack.push(val);
52
- }
53
- else {
54
- this.scopes[this.scopes.length - 1].stack.push(val);
55
- }
56
- }
57
- pushPatternBinding(pattern, global) {
58
- switch (pattern.type) {
59
- case "Identifier":
60
- this.pushBinding(pattern.name, global);
61
- break;
62
- case "RestElement":
63
- this.pushPatternBinding(pattern.argument, global);
64
- break;
65
- case "ArrayPattern":
66
- for (const element of pattern.elements) {
67
- if (element) {
68
- this.pushPatternBinding(element, global);
69
- }
70
- }
71
- break;
72
- case "ObjectPattern":
73
- for (const prop of pattern.properties) {
74
- if (prop.type === "RestElement") {
75
- this.pushPatternBinding(prop.argument, global);
76
- }
77
- else {
78
- this.pushPatternBinding(prop.value, global);
79
- }
80
- }
81
- break;
82
- case "AssignmentPattern":
83
- this.pushPatternBinding(pattern.left, global);
84
- break;
85
- }
86
- }
87
- pushPatternBindings(patterns, global) {
88
- for (const pattern of patterns) {
89
- this.pushPatternBinding(pattern, global);
90
- }
91
- }
92
- }
93
- export function transformTemplateCode(code, templateState) {
94
- if (!code.trim()) {
95
- return code;
96
- }
97
- const parsed = meriyah.parseScript(code, { module: true });
98
- const tracker = new ScopeTracker();
99
- const exclude = [
100
- templateState,
101
- ...DEFAULT_EXCLUDES,
102
- ];
103
- if (parsed.type !== "Program") {
104
- throw new Error("Expected a program");
105
- }
106
- if (parsed.body.length === 0) {
107
- throw new Error("Empty program");
108
- }
109
- // Transforms an identifier to a MemberExpression
110
- // if it's not in the exclude list
111
- //
112
- // Example:
113
- // Transforms {{ name }} to {{ id.name }}
114
- function transformIdentifier(id) {
115
- if ((!INCLUDE_GLOBAL.includes(id.name) &&
116
- dntShim.dntGlobalThis[id.name] !== undefined) ||
117
- exclude.includes(id.name) ||
118
- tracker.includes(id.name) ||
119
- id.name.startsWith("__")) {
120
- return id;
121
- }
122
- return {
123
- type: "MemberExpression",
124
- object: {
125
- type: "Identifier",
126
- name: templateState,
127
- },
128
- optional: false,
129
- computed: false,
130
- property: id,
131
- };
132
- }
133
- walker.walk(parsed, {
134
- enter(node) {
135
- switch (node.type) {
136
- // Track variable declarations
137
- case "VariableDeclaration":
138
- // "var" declarations are scoped to the function/global scope.
139
- tracker.pushPatternBindings(node.declarations.map((d) => d.id), node.kind === "var");
140
- break;
141
- // Track function declarations, and
142
- // function parameters.
143
- // Also track the scope.
144
- case "FunctionDeclaration":
145
- case "FunctionExpression":
146
- if (node.id) {
147
- tracker.pushBinding(node.id.name);
148
- }
149
- tracker.pushScope(true);
150
- tracker.pushPatternBindings(node.params);
151
- break;
152
- case "ArrowFunctionExpression":
153
- tracker.pushScope();
154
- tracker.pushPatternBindings(node.params);
155
- break;
156
- }
157
- },
158
- leave(node, parent) {
159
- switch (node.type) {
160
- // Pop the scope when leaving a function
161
- case "FunctionDeclaration":
162
- case "FunctionExpression":
163
- case "ArrowFunctionExpression":
164
- tracker.popScope();
165
- break;
166
- case "Identifier":
167
- // Don't transform identifiers that aren't at the start of a MemberExpression
168
- // ie. don't transform `bar` or `baz` in `foo.bar.baz`
169
- if (parent?.type === "MemberExpression" && parent.property === node) {
170
- return;
171
- }
172
- // Don't transform identifiers that are keys in an object
173
- if (parent?.type === "Property" && parent.key === node) {
174
- return;
175
- }
176
- this.replace(transformIdentifier(node));
177
- break;
178
- }
179
- },
180
- });
181
- const generated = astring.generate(parsed);
182
- return generated;
183
- }