@bascik/language-server 0.1.0-rc.4 → 0.1.0-rc.6
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.
- package/README.md +72 -30
- package/dist/analyzer.d.ts +70 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +657 -77
- package/dist/analyzer.js.map +1 -1
- package/dist/api-rules.d.ts +7 -0
- package/dist/api-rules.d.ts.map +1 -1
- package/dist/api-rules.js +235 -16
- package/dist/api-rules.js.map +1 -1
- package/dist/bin.js +4 -1
- package/dist/bin.js.map +1 -1
- package/dist/check.d.ts +14 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +116 -10
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +27 -8
- package/dist/cli.js.map +1 -1
- package/dist/compatibility-rules.json +21 -3
- package/dist/component-metadata.d.ts.map +1 -1
- package/dist/component-metadata.js +3 -1
- package/dist/component-metadata.js.map +1 -1
- package/dist/component-usages.d.ts +47 -0
- package/dist/component-usages.d.ts.map +1 -0
- package/dist/component-usages.js +495 -0
- package/dist/component-usages.js.map +1 -0
- package/dist/config-reader.d.ts +41 -0
- package/dist/config-reader.d.ts.map +1 -0
- package/dist/config-reader.js +461 -0
- package/dist/config-reader.js.map +1 -0
- package/dist/config.d.ts +10 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +25 -30
- package/dist/config.js.map +1 -1
- package/dist/directive-rules.d.ts +3 -3
- package/dist/directive-rules.d.ts.map +1 -1
- package/dist/directive-rules.js +6 -6
- package/dist/directive-rules.js.map +1 -1
- package/dist/hand-written-escaper.d.ts +35 -0
- package/dist/hand-written-escaper.d.ts.map +1 -0
- package/dist/hand-written-escaper.js +739 -0
- package/dist/hand-written-escaper.js.map +1 -0
- package/dist/module-specifiers.d.ts +25 -0
- package/dist/module-specifiers.d.ts.map +1 -1
- package/dist/module-specifiers.js +234 -165
- package/dist/module-specifiers.js.map +1 -1
- package/dist/name-groups.d.ts +36 -0
- package/dist/name-groups.d.ts.map +1 -0
- package/dist/name-groups.js +198 -0
- package/dist/name-groups.js.map +1 -0
- package/dist/plain-holes.d.ts +116 -0
- package/dist/plain-holes.d.ts.map +1 -0
- package/dist/plain-holes.js +1970 -0
- package/dist/plain-holes.js.map +1 -0
- package/dist/project.d.ts +58 -1
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +146 -8
- package/dist/project.js.map +1 -1
- package/dist/references.d.ts +47 -0
- package/dist/references.d.ts.map +1 -0
- package/dist/references.js +241 -0
- package/dist/references.js.map +1 -0
- package/dist/script-modules.d.ts +60 -0
- package/dist/script-modules.d.ts.map +1 -0
- package/dist/script-modules.js +382 -0
- package/dist/script-modules.js.map +1 -0
- package/dist/server-script-rules.d.ts +94 -1
- package/dist/server-script-rules.d.ts.map +1 -1
- package/dist/server-script-rules.js +2572 -230
- package/dist/server-script-rules.js.map +1 -1
- package/dist/server-script-templates.d.ts +250 -0
- package/dist/server-script-templates.d.ts.map +1 -0
- package/dist/server-script-templates.js +1685 -0
- package/dist/server-script-templates.js.map +1 -0
- package/dist/server.d.ts +191 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +822 -50
- package/dist/server.js.map +1 -1
- package/dist/start.d.ts +10 -1
- package/dist/start.d.ts.map +1 -1
- package/dist/start.js +14 -2
- package/dist/start.js.map +1 -1
- package/dist/string-typed-flow.d.ts +26 -0
- package/dist/string-typed-flow.d.ts.map +1 -0
- package/dist/string-typed-flow.js +496 -0
- package/dist/string-typed-flow.js.map +1 -0
- package/dist/typescript-blocks.d.ts +205 -0
- package/dist/typescript-blocks.d.ts.map +1 -0
- package/dist/typescript-blocks.js +464 -0
- package/dist/typescript-blocks.js.map +1 -0
- package/dist/typescript-editor-mode.d.ts +28 -0
- package/dist/typescript-editor-mode.d.ts.map +1 -0
- package/dist/typescript-editor-mode.js +161 -0
- package/dist/typescript-editor-mode.js.map +1 -0
- package/dist/typescript-engine.d.ts +115 -0
- package/dist/typescript-engine.d.ts.map +1 -0
- package/dist/typescript-engine.js +286 -0
- package/dist/typescript-engine.js.map +1 -0
- package/dist/typescript-features.d.ts +136 -0
- package/dist/typescript-features.d.ts.map +1 -0
- package/dist/typescript-features.js +374 -0
- package/dist/typescript-features.js.map +1 -0
- package/dist/typescript-loader.d.ts +55 -0
- package/dist/typescript-loader.d.ts.map +1 -0
- package/dist/typescript-loader.js +202 -0
- package/dist/typescript-loader.js.map +1 -0
- package/dist/typescript-lsp.d.ts +8 -0
- package/dist/typescript-lsp.d.ts.map +1 -0
- package/dist/typescript-lsp.js +115 -0
- package/dist/typescript-lsp.js.map +1 -0
- package/dist/typescript-service.d.ts +350 -0
- package/dist/typescript-service.d.ts.map +1 -0
- package/dist/typescript-service.js +1465 -0
- package/dist/typescript-service.js.map +1 -0
- package/dist/typescript-worker.d.ts +11 -0
- package/dist/typescript-worker.d.ts.map +1 -0
- package/dist/typescript-worker.js +107 -0
- package/dist/typescript-worker.js.map +1 -0
- package/package.json +20 -7
|
@@ -0,0 +1,1685 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Template literals in a server or stream script, for the sink rules and
|
|
3
|
+
* server-script-unescaped-markup: every literal (nested ones inside a hole
|
|
4
|
+
* too), its tag, its static parts and its holes, in one linear pass. Strings
|
|
5
|
+
* and comments are skipped, so a backtick inside them is not a template.
|
|
6
|
+
*/
|
|
7
|
+
import { scanJavaScript } from "./module-specifiers.js";
|
|
8
|
+
/** Words before a backtick that are not tags (`return \`...\``). */
|
|
9
|
+
const NOT_TAGS = new Set([
|
|
10
|
+
"return",
|
|
11
|
+
"typeof",
|
|
12
|
+
"case",
|
|
13
|
+
"do",
|
|
14
|
+
"else",
|
|
15
|
+
"in",
|
|
16
|
+
"of",
|
|
17
|
+
"new",
|
|
18
|
+
"delete",
|
|
19
|
+
"void",
|
|
20
|
+
"throw",
|
|
21
|
+
"instanceof",
|
|
22
|
+
"yield",
|
|
23
|
+
"await",
|
|
24
|
+
"export",
|
|
25
|
+
"default",
|
|
26
|
+
]);
|
|
27
|
+
const isWord = (c) => c !== undefined && /[\w$.]/.test(c);
|
|
28
|
+
const tagBefore = (source, backtick) => {
|
|
29
|
+
let k = backtick - 1;
|
|
30
|
+
while (k >= 0 && (source[k] === " " || source[k] === "\t"))
|
|
31
|
+
k--;
|
|
32
|
+
if (k < 0)
|
|
33
|
+
return null;
|
|
34
|
+
if (k === backtick - 1 && (source[k] === ")" || source[k] === "]"))
|
|
35
|
+
return "?";
|
|
36
|
+
const end = k + 1;
|
|
37
|
+
while (k >= 0 && isWord(source[k]))
|
|
38
|
+
k--;
|
|
39
|
+
const word = source.slice(k + 1, end);
|
|
40
|
+
if (word === "" || /^\d/.test(word) || word.startsWith("."))
|
|
41
|
+
return null;
|
|
42
|
+
if (NOT_TAGS.has(word))
|
|
43
|
+
return null;
|
|
44
|
+
return word;
|
|
45
|
+
};
|
|
46
|
+
/** Keywords after which a `/` starts a regex literal rather than a division. */
|
|
47
|
+
export const REGEX_AFTER_KEYWORD = new Set([
|
|
48
|
+
"return",
|
|
49
|
+
"typeof",
|
|
50
|
+
"case",
|
|
51
|
+
"do",
|
|
52
|
+
"else",
|
|
53
|
+
"in",
|
|
54
|
+
"of",
|
|
55
|
+
"new",
|
|
56
|
+
"delete",
|
|
57
|
+
"void",
|
|
58
|
+
"throw",
|
|
59
|
+
"instanceof",
|
|
60
|
+
"yield",
|
|
61
|
+
"await",
|
|
62
|
+
]);
|
|
63
|
+
/** Punctuation after which an operand, so a regex, follows. */
|
|
64
|
+
export const OPERAND_FOLLOWS = new Set("(,=:[!&|?{};+-*%<>~^/");
|
|
65
|
+
/** The offset just past the regex literal that starts at `start` (its flags included). */
|
|
66
|
+
export function skipRegexLiteral(source, start) {
|
|
67
|
+
let i = start + 1;
|
|
68
|
+
let inClass = false;
|
|
69
|
+
while (i < source.length && source[i] !== "\n") {
|
|
70
|
+
if (source[i] === "\\")
|
|
71
|
+
i += 2;
|
|
72
|
+
else if (inClass) {
|
|
73
|
+
if (source[i] === "]")
|
|
74
|
+
inClass = false;
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
else if (source[i] === "[") {
|
|
78
|
+
inClass = true;
|
|
79
|
+
i++;
|
|
80
|
+
}
|
|
81
|
+
else if (source[i] === "/") {
|
|
82
|
+
i++;
|
|
83
|
+
while (i < source.length && /[a-z]/i.test(source[i]))
|
|
84
|
+
i++;
|
|
85
|
+
return i;
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
i++;
|
|
89
|
+
}
|
|
90
|
+
return i;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Every template literal in `source`, outer literals before the ones nested
|
|
94
|
+
* in their holes. A `/` where an operand goes starts a regex literal, which
|
|
95
|
+
* is skipped whole, so a quote, brace or backtick inside `/"/g` opens
|
|
96
|
+
* nothing. Whether an operand goes there is read from the token before it,
|
|
97
|
+
* kept as the scan goes, so the pass stays linear. The literals and holes
|
|
98
|
+
* still open are kept on an explicit stack, so templates nested thousands
|
|
99
|
+
* deep cannot overflow the call stack.
|
|
100
|
+
*/
|
|
101
|
+
export function findTemplateLiterals(source) {
|
|
102
|
+
const literals = [];
|
|
103
|
+
const skipString = (i) => {
|
|
104
|
+
const quote = source[i];
|
|
105
|
+
i++;
|
|
106
|
+
while (i < source.length) {
|
|
107
|
+
if (source[i] === "\\")
|
|
108
|
+
i += 2;
|
|
109
|
+
else if (source[i] === quote)
|
|
110
|
+
return i + 1;
|
|
111
|
+
else if (source[i] === "\n")
|
|
112
|
+
return i;
|
|
113
|
+
else
|
|
114
|
+
i++;
|
|
115
|
+
}
|
|
116
|
+
return i;
|
|
117
|
+
};
|
|
118
|
+
const code = (inHole, holeStart) => ({
|
|
119
|
+
kind: "code",
|
|
120
|
+
inHole,
|
|
121
|
+
depth: 0,
|
|
122
|
+
regexOk: true,
|
|
123
|
+
before: "",
|
|
124
|
+
holeStart,
|
|
125
|
+
});
|
|
126
|
+
const stack = [code(false, -1)];
|
|
127
|
+
let i = 0;
|
|
128
|
+
while (i < source.length) {
|
|
129
|
+
const top = stack[stack.length - 1];
|
|
130
|
+
if (top.kind === "text") {
|
|
131
|
+
const c = source[i];
|
|
132
|
+
if (c === "\\")
|
|
133
|
+
i += 2;
|
|
134
|
+
else if (c === "`") {
|
|
135
|
+
top.literal.parts.push(source.slice(top.partStart, i));
|
|
136
|
+
top.literal.end = i + 1;
|
|
137
|
+
i++;
|
|
138
|
+
stack.pop();
|
|
139
|
+
const outer = stack[stack.length - 1];
|
|
140
|
+
outer.regexOk = false;
|
|
141
|
+
outer.before = c;
|
|
142
|
+
}
|
|
143
|
+
else if (c === "$" && source[i + 1] === "{") {
|
|
144
|
+
top.literal.parts.push(source.slice(top.partStart, i));
|
|
145
|
+
stack.push(code(true, i));
|
|
146
|
+
i += 2;
|
|
147
|
+
}
|
|
148
|
+
else
|
|
149
|
+
i++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const c = source[i];
|
|
153
|
+
if (c === "'" || c === '"') {
|
|
154
|
+
i = skipString(i);
|
|
155
|
+
top.regexOk = false;
|
|
156
|
+
top.before = c;
|
|
157
|
+
}
|
|
158
|
+
else if (c === "/" && source[i + 1] === "/") {
|
|
159
|
+
while (i < source.length && source[i] !== "\n")
|
|
160
|
+
i++;
|
|
161
|
+
}
|
|
162
|
+
else if (c === "/" && source[i + 1] === "*") {
|
|
163
|
+
const close = source.indexOf("*/", i + 2);
|
|
164
|
+
i = close === -1 ? source.length : close + 2;
|
|
165
|
+
}
|
|
166
|
+
else if (c === "/" && top.regexOk) {
|
|
167
|
+
i = skipRegexLiteral(source, i);
|
|
168
|
+
top.regexOk = false;
|
|
169
|
+
top.before = "/";
|
|
170
|
+
}
|
|
171
|
+
else if (c === "`") {
|
|
172
|
+
const literal = {
|
|
173
|
+
start: i,
|
|
174
|
+
end: source.length,
|
|
175
|
+
tag: tagBefore(source, i),
|
|
176
|
+
parts: [],
|
|
177
|
+
holes: [],
|
|
178
|
+
};
|
|
179
|
+
literals.push(literal);
|
|
180
|
+
stack.push({ kind: "text", literal, partStart: i + 1 });
|
|
181
|
+
i++;
|
|
182
|
+
}
|
|
183
|
+
else if (/[\w$]/.test(c)) {
|
|
184
|
+
const wordStart = i;
|
|
185
|
+
while (i < source.length && /[\w$]/.test(source[i]))
|
|
186
|
+
i++;
|
|
187
|
+
top.regexOk =
|
|
188
|
+
top.before !== "." &&
|
|
189
|
+
REGEX_AFTER_KEYWORD.has(source.slice(wordStart, i));
|
|
190
|
+
top.before = "w";
|
|
191
|
+
}
|
|
192
|
+
else if (/\s/.test(c))
|
|
193
|
+
i++;
|
|
194
|
+
else {
|
|
195
|
+
if (c === "{")
|
|
196
|
+
top.depth++;
|
|
197
|
+
else if (c === "}") {
|
|
198
|
+
if (top.depth === 0 && top.inHole) {
|
|
199
|
+
// The `}` that closes a hole: back to its template's text.
|
|
200
|
+
stack.pop();
|
|
201
|
+
const text = stack[stack.length - 1];
|
|
202
|
+
text.literal.holes.push({
|
|
203
|
+
start: top.holeStart,
|
|
204
|
+
end: i + 1,
|
|
205
|
+
expr: source.slice(top.holeStart + 2, i),
|
|
206
|
+
index: text.literal.holes.length + 1,
|
|
207
|
+
});
|
|
208
|
+
i++;
|
|
209
|
+
text.partStart = i;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (top.depth > 0)
|
|
213
|
+
top.depth--;
|
|
214
|
+
}
|
|
215
|
+
top.regexOk = OPERAND_FOLLOWS.has(c);
|
|
216
|
+
top.before = c;
|
|
217
|
+
i++;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Holes and templates still open at the end of the source end there.
|
|
221
|
+
while (stack.length > 1) {
|
|
222
|
+
const frame = stack.pop();
|
|
223
|
+
if (frame.kind === "code") {
|
|
224
|
+
const text = stack[stack.length - 1];
|
|
225
|
+
const close = Math.min(i, source.length);
|
|
226
|
+
text.literal.holes.push({
|
|
227
|
+
start: frame.holeStart,
|
|
228
|
+
end: Math.min(close + 1, source.length),
|
|
229
|
+
expr: source.slice(frame.holeStart + 2, close),
|
|
230
|
+
index: text.literal.holes.length + 1,
|
|
231
|
+
});
|
|
232
|
+
text.partStart = close + 1;
|
|
233
|
+
}
|
|
234
|
+
else
|
|
235
|
+
frame.literal.parts.push(source.slice(frame.partStart));
|
|
236
|
+
}
|
|
237
|
+
return literals;
|
|
238
|
+
}
|
|
239
|
+
// ─── Exempt expressions ─────────────────────────────────────────────────────
|
|
240
|
+
export const HTML_MODULE = "@bascik/bascik/html";
|
|
241
|
+
const HTML_EXPORTS = new Set(["html", "raw", "esc", "safeUrl", "json"]);
|
|
242
|
+
const IDENTIFIER = /^[\w$]+$/;
|
|
243
|
+
/** `value` as a literal RegExp source: every syntax character escaped. */
|
|
244
|
+
function escapeRegExp(value) {
|
|
245
|
+
return value.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* One `{ ... }` import entry (`esc`, `html as h`) as [exported, local], or
|
|
249
|
+
* undefined when it is not `name` or `name as local` with plain identifier
|
|
250
|
+
* names (a half-typed `html as h(` binds nothing to tag with). Splits on
|
|
251
|
+
* whitespace runs, not /\s+as\s+/, which retried a long whitespace run from
|
|
252
|
+
* each of its characters.
|
|
253
|
+
*/
|
|
254
|
+
export function parseImportSpecifier(entry) {
|
|
255
|
+
const words = entry.split(/\s+/).filter(Boolean);
|
|
256
|
+
const [exported, as, local] = words.length === 1
|
|
257
|
+
? [words[0], "as", words[0]]
|
|
258
|
+
: words.length === 3
|
|
259
|
+
? words
|
|
260
|
+
: [];
|
|
261
|
+
if (as !== "as" || !exported || !local)
|
|
262
|
+
return undefined;
|
|
263
|
+
return IDENTIFIER.test(exported) && IDENTIFIER.test(local)
|
|
264
|
+
? [exported, local]
|
|
265
|
+
: undefined;
|
|
266
|
+
}
|
|
267
|
+
export function findHtmlImports(body) {
|
|
268
|
+
const named = new Map();
|
|
269
|
+
const namespaces = new Set();
|
|
270
|
+
for (const m of body.matchAll(/\bimport\s*\{([^}]*)\}\s*from\s*["']@bascik\/bascik\/html["']/g)) {
|
|
271
|
+
for (const entry of m[1].split(",")) {
|
|
272
|
+
const specifier = parseImportSpecifier(entry);
|
|
273
|
+
if (specifier && HTML_EXPORTS.has(specifier[0]))
|
|
274
|
+
named.set(specifier[1], specifier[0]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
for (const m of body.matchAll(/\bimport\s*\*\s*as\s+([\w$]+)\s*from\s*["']@bascik\/bascik\/html["']/g))
|
|
278
|
+
namespaces.add(m[1]);
|
|
279
|
+
return { named, namespaces };
|
|
280
|
+
}
|
|
281
|
+
/** The export a callee or tag names (`esc`, `b.esc`), or undefined. */
|
|
282
|
+
export function htmlExportOf(name, imports) {
|
|
283
|
+
const dot = name.indexOf(".");
|
|
284
|
+
if (dot === -1)
|
|
285
|
+
return imports.named.get(name);
|
|
286
|
+
const ns = name.slice(0, dot);
|
|
287
|
+
const member = name.slice(dot + 1);
|
|
288
|
+
return imports.namespaces.has(ns) && HTML_EXPORTS.has(member)
|
|
289
|
+
? member
|
|
290
|
+
: undefined;
|
|
291
|
+
}
|
|
292
|
+
/** The names that tag an html template in this body. */
|
|
293
|
+
export function htmlTagNames(imports) {
|
|
294
|
+
const names = [...imports.named]
|
|
295
|
+
.filter(([, exported]) => exported === "html")
|
|
296
|
+
.map(([local]) => local);
|
|
297
|
+
for (const ns of imports.namespaces)
|
|
298
|
+
names.push(`${ns}.html`);
|
|
299
|
+
return names;
|
|
300
|
+
}
|
|
301
|
+
/** Index of the bracket that closes the one at `open`, skipping strings and templates. */
|
|
302
|
+
const matchingClose = (text, open) => {
|
|
303
|
+
let depth = 0;
|
|
304
|
+
for (let i = open; i < text.length; i++) {
|
|
305
|
+
const c = text[i];
|
|
306
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
307
|
+
const quote = c;
|
|
308
|
+
i++;
|
|
309
|
+
while (i < text.length && text[i] !== quote) {
|
|
310
|
+
if (text[i] === "\\")
|
|
311
|
+
i++;
|
|
312
|
+
i++;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else if (c === "(" || c === "[" || c === "{")
|
|
316
|
+
depth++;
|
|
317
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
318
|
+
depth--;
|
|
319
|
+
if (depth === 0)
|
|
320
|
+
return i;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return -1;
|
|
324
|
+
};
|
|
325
|
+
/** Top-level operator positions in `text` (outside brackets, strings and templates). */
|
|
326
|
+
const topLevel = (text, visit) => {
|
|
327
|
+
let depth = 0;
|
|
328
|
+
for (let i = 0; i < text.length; i++) {
|
|
329
|
+
const c = text[i];
|
|
330
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
331
|
+
const quote = c;
|
|
332
|
+
i++;
|
|
333
|
+
while (i < text.length && text[i] !== quote) {
|
|
334
|
+
if (text[i] === "\\")
|
|
335
|
+
i++;
|
|
336
|
+
else if (quote === "`" && text[i] === "$" && text[i + 1] === "{") {
|
|
337
|
+
const close = matchingClose(text, i + 1);
|
|
338
|
+
if (close === -1)
|
|
339
|
+
return;
|
|
340
|
+
i = close;
|
|
341
|
+
}
|
|
342
|
+
i++;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if (c === "(" || c === "[" || c === "{")
|
|
346
|
+
depth++;
|
|
347
|
+
else if (c === ")" || c === "]" || c === "}")
|
|
348
|
+
depth--;
|
|
349
|
+
else if (depth === 0 && visit(i) === true)
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const LITERAL = /^(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|-?(?:\d[\d_]*\.?\d*(?:e[+-]?\d+)?|0x[\da-f]+)n?|true|false|null|undefined)$/i;
|
|
354
|
+
const HOLE_FREE_TEMPLATE = /^`(?:[^`\\$]|\\.|\$(?!\{))*`$/;
|
|
355
|
+
export const newNameMemo = () => ({
|
|
356
|
+
exempt: new Map(),
|
|
357
|
+
escapedJson: new Map(),
|
|
358
|
+
bound: new Map(),
|
|
359
|
+
depth: 0,
|
|
360
|
+
cut: false,
|
|
361
|
+
});
|
|
362
|
+
/**
|
|
363
|
+
* How many names deep an alias chain is followed (`const b = a; const c =
|
|
364
|
+
* b; ...`). Deeper chains count as not fine, so a generated chain of
|
|
365
|
+
* thousands of aliases cannot overflow the stack.
|
|
366
|
+
*/
|
|
367
|
+
const MAX_ALIAS_DEPTH = 200;
|
|
368
|
+
/**
|
|
369
|
+
* Whether every value bound to `name` passes `check`, read once per script
|
|
370
|
+
* through `verdicts`. A name already on the path (a cycle) or past
|
|
371
|
+
* MAX_ALIAS_DEPTH is not fine; a false verdict cut short that way is not
|
|
372
|
+
* stored, since a later read from elsewhere may reach further.
|
|
373
|
+
*/
|
|
374
|
+
function nameVerdict(name, bound, memo, verdicts, seen, check) {
|
|
375
|
+
const known = verdicts.get(name);
|
|
376
|
+
if (known !== undefined)
|
|
377
|
+
return known;
|
|
378
|
+
if (seen.has(name) || memo.depth >= MAX_ALIAS_DEPTH) {
|
|
379
|
+
memo.cut = true;
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
seen.add(name);
|
|
383
|
+
const outerCut = memo.cut;
|
|
384
|
+
memo.cut = false;
|
|
385
|
+
memo.depth++;
|
|
386
|
+
let result;
|
|
387
|
+
try {
|
|
388
|
+
result = bound.every(check);
|
|
389
|
+
}
|
|
390
|
+
finally {
|
|
391
|
+
memo.depth--;
|
|
392
|
+
}
|
|
393
|
+
if (result || !memo.cut)
|
|
394
|
+
verdicts.set(name, result);
|
|
395
|
+
memo.cut = outerCut || memo.cut;
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* `const|let|var NAME = EXPR` bindings, each EXPR up to `;`, `,` or the line
|
|
400
|
+
* end at its own depth, or the bracket that closes the one it is in. A
|
|
401
|
+
* TypeScript annotation (`const NAME: Record<string, string> = EXPR`) is
|
|
402
|
+
* skipped. A let or var name also collects every later plain assignment
|
|
403
|
+
* (`NAME = EXPR`, `NAME += EXPR`, `NAME ||= EXPR`, `NAME ??= EXPR`), so a
|
|
404
|
+
* name reassigned from request data is not trusted from its declaration.
|
|
405
|
+
* Destructured names (`const { icon } = EXPR`) are left out: EXPR is not
|
|
406
|
+
* their value. Every value's end is found in one pass (valueEnds), so
|
|
407
|
+
* nested declarations, each of which runs to the end of the ones around
|
|
408
|
+
* it, are not each scanned again.
|
|
409
|
+
*/
|
|
410
|
+
export function findBindings(body, ranges = findBindingRanges(body)) {
|
|
411
|
+
const bindings = new Map();
|
|
412
|
+
for (const { name, from, end, destructured, assigned } of ranges) {
|
|
413
|
+
// A destructuring declaration's value holds the name somewhere; a
|
|
414
|
+
// destructuring assignment stores a value this file does not follow.
|
|
415
|
+
if (destructured && !assigned)
|
|
416
|
+
continue;
|
|
417
|
+
const list = bindings.get(name) ?? [];
|
|
418
|
+
list.push(body.slice(from, end));
|
|
419
|
+
bindings.set(name, list);
|
|
420
|
+
}
|
|
421
|
+
return bindings;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* How far a type annotation or a destructuring pattern is read. Past it
|
|
425
|
+
* the declaration is not followed, so a run of unclosed annotations
|
|
426
|
+
* cannot make the scan quadratic.
|
|
427
|
+
*/
|
|
428
|
+
const MAX_DECLARATION_HEAD = 2_000;
|
|
429
|
+
/**
|
|
430
|
+
* From just after a declared name or pattern: the offset just past the
|
|
431
|
+
* `=` that starts its value (a TypeScript annotation skipped), "none" for
|
|
432
|
+
* a declaration with no value (`let out: string;`), or undefined (`for
|
|
433
|
+
* (const x of xs)`, or past MAX_DECLARATION_HEAD).
|
|
434
|
+
*/
|
|
435
|
+
function declarationValueStart(body, at) {
|
|
436
|
+
let i = at;
|
|
437
|
+
while (i < body.length && /\s/.test(body[i]))
|
|
438
|
+
i++;
|
|
439
|
+
if (body[i] === "=" && body[i + 1] !== "=" && body[i + 1] !== ">")
|
|
440
|
+
return i + 1;
|
|
441
|
+
if (body[i] === ";" || body[i] === "," || i >= body.length)
|
|
442
|
+
return "none";
|
|
443
|
+
if (body[i] !== ":") {
|
|
444
|
+
// A newline ends a declaration with no value (`let out\n`).
|
|
445
|
+
return /\n/.test(body.slice(at, i)) ? "none" : undefined;
|
|
446
|
+
}
|
|
447
|
+
let depth = 0;
|
|
448
|
+
let last = ":";
|
|
449
|
+
const limit = Math.min(body.length, i + MAX_DECLARATION_HEAD);
|
|
450
|
+
for (i++; i < limit; i++) {
|
|
451
|
+
const c = body[i];
|
|
452
|
+
if (c === '"' || c === "'") {
|
|
453
|
+
i = stringEnd(body, i) - 1;
|
|
454
|
+
last = c;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (c === "(" || c === "[" || c === "{" || c === "<")
|
|
458
|
+
depth++;
|
|
459
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
460
|
+
if (--depth < 0)
|
|
461
|
+
return undefined;
|
|
462
|
+
}
|
|
463
|
+
else if (c === ">") {
|
|
464
|
+
if (depth > 0)
|
|
465
|
+
depth--;
|
|
466
|
+
}
|
|
467
|
+
else if (c === "=") {
|
|
468
|
+
if (body[i + 1] === ">") {
|
|
469
|
+
i++;
|
|
470
|
+
last = ">";
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (body[i + 1] === "=")
|
|
474
|
+
return undefined;
|
|
475
|
+
if (depth === 0)
|
|
476
|
+
return i + 1;
|
|
477
|
+
}
|
|
478
|
+
else if (depth === 0 && (c === ";" || c === ","))
|
|
479
|
+
return "none";
|
|
480
|
+
else if (depth === 0 && c === "\n") {
|
|
481
|
+
// Prettier wraps a long union after the colon or before a `|`.
|
|
482
|
+
let k = i + 1;
|
|
483
|
+
while (k < body.length && /\s/.test(body[k]))
|
|
484
|
+
k++;
|
|
485
|
+
if (!/[:|&]/.test(last) && !/[|&=]/.test(body[k] ?? ""))
|
|
486
|
+
return "none";
|
|
487
|
+
}
|
|
488
|
+
if (!/\s/.test(c))
|
|
489
|
+
last = c;
|
|
490
|
+
}
|
|
491
|
+
return undefined;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* The names a destructuring pattern starting at `open` (`{` or `[`)
|
|
495
|
+
* declares, and the offset past its closing bracket; undefined when it
|
|
496
|
+
* does not close within MAX_DECLARATION_HEAD. Property keys (`{ a: b }`
|
|
497
|
+
* declares b) are left out; a name in a default value is kept, which
|
|
498
|
+
* only makes the fix more careful.
|
|
499
|
+
*/
|
|
500
|
+
function patternNames(body, open) {
|
|
501
|
+
let depth = 0;
|
|
502
|
+
const limit = Math.min(body.length, open + MAX_DECLARATION_HEAD);
|
|
503
|
+
for (let i = open; i < limit; i++) {
|
|
504
|
+
const c = body[i];
|
|
505
|
+
if (c === '"' || c === "'")
|
|
506
|
+
i = stringEnd(body, i) - 1;
|
|
507
|
+
else if (c === "{" || c === "[" || c === "(")
|
|
508
|
+
depth++;
|
|
509
|
+
else if (c === "}" || c === "]" || c === ")") {
|
|
510
|
+
if (--depth === 0) {
|
|
511
|
+
const pattern = body.slice(open, i + 1);
|
|
512
|
+
const names = [];
|
|
513
|
+
for (const m of pattern.matchAll(/(?<![\w$.'"])([A-Za-z_$][\w$]*)(?![\w$])(?!\s*:)/g))
|
|
514
|
+
names.push(m[1]);
|
|
515
|
+
return { names, end: i + 1 };
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
/** The bindings findBindings reads, each with its value's offsets, in no set order. */
|
|
522
|
+
export function findBindingRanges(body) {
|
|
523
|
+
const found = [];
|
|
524
|
+
const reassignable = new Set();
|
|
525
|
+
for (const m of body.matchAll(/(?<![\w$.])(const|let|var)\s+/g)) {
|
|
526
|
+
const at = (m.index ?? 0) + m[0].length;
|
|
527
|
+
let names;
|
|
528
|
+
let after;
|
|
529
|
+
let destructured = false;
|
|
530
|
+
const name = /^[A-Za-z_$][\w$]*/.exec(body.slice(at, at + 256));
|
|
531
|
+
if (name) {
|
|
532
|
+
names = [name[0]];
|
|
533
|
+
after = at + name[0].length;
|
|
534
|
+
}
|
|
535
|
+
else if (body[at] === "{" || body[at] === "[") {
|
|
536
|
+
const pattern = patternNames(body, at);
|
|
537
|
+
if (!pattern)
|
|
538
|
+
continue;
|
|
539
|
+
names = pattern.names;
|
|
540
|
+
after = pattern.end;
|
|
541
|
+
destructured = true;
|
|
542
|
+
}
|
|
543
|
+
else
|
|
544
|
+
continue;
|
|
545
|
+
const start = declarationValueStart(body, after);
|
|
546
|
+
if (start === undefined)
|
|
547
|
+
continue;
|
|
548
|
+
if (m[1] !== "const")
|
|
549
|
+
for (const n of names)
|
|
550
|
+
reassignable.add(n);
|
|
551
|
+
if (start === "none")
|
|
552
|
+
continue;
|
|
553
|
+
let from = start;
|
|
554
|
+
while (from < body.length && /[ \t\r\n]/.test(body[from]))
|
|
555
|
+
from++;
|
|
556
|
+
for (const n of names)
|
|
557
|
+
found.push(destructured ? { name: n, from, destructured } : { name: n, from });
|
|
558
|
+
}
|
|
559
|
+
if (reassignable.size > 0)
|
|
560
|
+
// Plain assignments: not after `.` (a property) or a declaration keyword
|
|
561
|
+
// (counted above); `-=`, `*=` and the like always store a number.
|
|
562
|
+
for (const m of body.matchAll(/(?<![\w$.])([\w$]+)\s*(?:\+|\|\||\?\?|&&)?=(?![=>])\s*/g)) {
|
|
563
|
+
if (!reassignable.has(m[1]))
|
|
564
|
+
continue;
|
|
565
|
+
const before = body.slice(Math.max(0, (m.index ?? 0) - 6), m.index);
|
|
566
|
+
if (/\b(?:const|let|var)\s+$/.test(before))
|
|
567
|
+
continue;
|
|
568
|
+
found.push({ name: m[1], from: (m.index ?? 0) + m[0].length });
|
|
569
|
+
}
|
|
570
|
+
if (reassignable.size > 0)
|
|
571
|
+
for (const { names, from } of patternAssignments(body))
|
|
572
|
+
for (const name of names)
|
|
573
|
+
if (reassignable.has(name))
|
|
574
|
+
found.push({ name, from, destructured: true, assigned: true });
|
|
575
|
+
const ends = valueEnds(body, [...new Set(found.map((f) => f.from))].sort((x, y) => x - y));
|
|
576
|
+
const ranges = [];
|
|
577
|
+
for (const { name, from, destructured, assigned } of found) {
|
|
578
|
+
const end = ends.get(from);
|
|
579
|
+
// Inside a string, comment or template text: not code.
|
|
580
|
+
if (end === undefined)
|
|
581
|
+
continue;
|
|
582
|
+
ranges.push(assigned
|
|
583
|
+
? { name, from, end, destructured: true, assigned }
|
|
584
|
+
: destructured
|
|
585
|
+
? { name, from, end, destructured }
|
|
586
|
+
: { name, from, end });
|
|
587
|
+
}
|
|
588
|
+
return ranges;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Destructuring assignments (`[a, b] = [b, a]`, `({ body } = o)`): the
|
|
592
|
+
* names in each pattern and where the assigned value starts. A pattern
|
|
593
|
+
* after `const`, `let` or `var` is a declaration, and `x[i] = v` an
|
|
594
|
+
* index, so neither counts. One pass over the tokens.
|
|
595
|
+
*/
|
|
596
|
+
function patternAssignments(body) {
|
|
597
|
+
const { tokens } = scanJavaScript(body);
|
|
598
|
+
const n = tokens.length;
|
|
599
|
+
const punct = (k, v) => k >= 0 &&
|
|
600
|
+
k < n &&
|
|
601
|
+
tokens[k].type === "punctuation" &&
|
|
602
|
+
tokens[k].value === v;
|
|
603
|
+
const partner = new Int32Array(n).fill(-1);
|
|
604
|
+
const stack = [];
|
|
605
|
+
for (let k = 0; k < n; k++) {
|
|
606
|
+
const v = tokens[k].type === "punctuation" ? tokens[k].value : "";
|
|
607
|
+
if (v === "(" || v === "[" || v === "{")
|
|
608
|
+
stack.push(k);
|
|
609
|
+
else if ((v === ")" || v === "]" || v === "}") && stack.length > 0)
|
|
610
|
+
partner[k] = stack.pop();
|
|
611
|
+
}
|
|
612
|
+
const out = [];
|
|
613
|
+
for (let k = 1; k < n; k++) {
|
|
614
|
+
if (!punct(k, "=") || punct(k + 1, "=") || punct(k + 1, ">"))
|
|
615
|
+
continue;
|
|
616
|
+
if (!punct(k - 1, "]") && !punct(k - 1, "}"))
|
|
617
|
+
continue;
|
|
618
|
+
if (tokens[k].start !== tokens[k - 1].end &&
|
|
619
|
+
/\S/.test(body.slice(tokens[k - 1].end, tokens[k].start)))
|
|
620
|
+
continue;
|
|
621
|
+
const open = partner[k - 1];
|
|
622
|
+
if (open === -1)
|
|
623
|
+
continue;
|
|
624
|
+
const before = open > 0 ? tokens[open - 1] : undefined;
|
|
625
|
+
if (before?.type === "identifier" &&
|
|
626
|
+
/^(?:const|let|var)$/.test(before.value))
|
|
627
|
+
continue;
|
|
628
|
+
// x[i] = v, f()[0] = v and "s"[0] = v are indexes; `) {...} =` is no pattern.
|
|
629
|
+
if (before !== undefined &&
|
|
630
|
+
(before.type === "string" ||
|
|
631
|
+
(before.type === "identifier" &&
|
|
632
|
+
!/^(?:return|typeof|void|in|of|case|await|yield|delete)$/.test(before.value)) ||
|
|
633
|
+
punct(open - 1, ")") ||
|
|
634
|
+
punct(open - 1, "]")))
|
|
635
|
+
continue;
|
|
636
|
+
const names = [];
|
|
637
|
+
for (let q = open + 1; q < k - 1; q++)
|
|
638
|
+
if (tokens[q].type === "identifier" &&
|
|
639
|
+
!punct(q - 1, ".") &&
|
|
640
|
+
!punct(q + 1, ":"))
|
|
641
|
+
names.push(tokens[q].value);
|
|
642
|
+
let from = tokens[k].end;
|
|
643
|
+
while (from < body.length && /[ \t\r\n]/.test(body[from]))
|
|
644
|
+
from++;
|
|
645
|
+
out.push({ names, from });
|
|
646
|
+
}
|
|
647
|
+
return out;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* The end of the value that starts at each offset in `starts` (ascending):
|
|
651
|
+
* the first `;`, `,` or newline at the value's own bracket depth, or the
|
|
652
|
+
* closing bracket of the one it is in, or the end of the body. One pass
|
|
653
|
+
* over the code: strings, comments, regex literals and template text are
|
|
654
|
+
* skipped (a value that starts inside one gets no end), and a template's
|
|
655
|
+
* holes are code one level deeper. Values still open are kept on a stack,
|
|
656
|
+
* shallowest first, so each one is closed once.
|
|
657
|
+
*/
|
|
658
|
+
function valueEnds(body, starts) {
|
|
659
|
+
const ends = new Map();
|
|
660
|
+
const open = [];
|
|
661
|
+
let next = 0;
|
|
662
|
+
let depth = 0;
|
|
663
|
+
/** "t" in a template's text, "h" in a hole's code; empty at the top level. */
|
|
664
|
+
const modes = [];
|
|
665
|
+
/** The depth each open hole's code starts at. */
|
|
666
|
+
const holeDepths = [];
|
|
667
|
+
let regexOk = true;
|
|
668
|
+
let before = "";
|
|
669
|
+
const close = (atDepth, at) => {
|
|
670
|
+
while (open.length > 0 && open[open.length - 1].depth >= atDepth)
|
|
671
|
+
ends.set(open.pop().from, at);
|
|
672
|
+
};
|
|
673
|
+
/** Drops starts before `to` that no code reaches (inside a skipped token). */
|
|
674
|
+
const skip = (to) => {
|
|
675
|
+
while (next < starts.length && starts[next] < to)
|
|
676
|
+
next++;
|
|
677
|
+
};
|
|
678
|
+
let i = 0;
|
|
679
|
+
while (i < body.length) {
|
|
680
|
+
if (modes[modes.length - 1] === "t") {
|
|
681
|
+
skip(i + 1);
|
|
682
|
+
const c = body[i];
|
|
683
|
+
if (c === "\\")
|
|
684
|
+
i += 2;
|
|
685
|
+
else if (c === "`") {
|
|
686
|
+
modes.pop();
|
|
687
|
+
depth--;
|
|
688
|
+
regexOk = false;
|
|
689
|
+
before = c;
|
|
690
|
+
i++;
|
|
691
|
+
}
|
|
692
|
+
else if (c === "$" && body[i + 1] === "{") {
|
|
693
|
+
modes.push("h");
|
|
694
|
+
depth++;
|
|
695
|
+
holeDepths.push(depth);
|
|
696
|
+
regexOk = true;
|
|
697
|
+
before = "{";
|
|
698
|
+
i += 2;
|
|
699
|
+
}
|
|
700
|
+
else
|
|
701
|
+
i++;
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
skip(i);
|
|
705
|
+
while (next < starts.length && starts[next] === i)
|
|
706
|
+
open.push({ from: starts[next++], depth });
|
|
707
|
+
const c = body[i];
|
|
708
|
+
if (c === "'" || c === '"') {
|
|
709
|
+
i = Math.min(stringEnd(body, i), body.length);
|
|
710
|
+
skip(i);
|
|
711
|
+
regexOk = false;
|
|
712
|
+
before = c;
|
|
713
|
+
}
|
|
714
|
+
else if (c === "/" && body[i + 1] === "/") {
|
|
715
|
+
const newline = body.indexOf("\n", i);
|
|
716
|
+
i = newline === -1 ? body.length : newline;
|
|
717
|
+
skip(i);
|
|
718
|
+
}
|
|
719
|
+
else if (c === "/" && body[i + 1] === "*") {
|
|
720
|
+
const end = body.indexOf("*/", i + 2);
|
|
721
|
+
i = end === -1 ? body.length : end + 2;
|
|
722
|
+
skip(i);
|
|
723
|
+
}
|
|
724
|
+
else if (c === "/" && regexOk) {
|
|
725
|
+
i = skipRegexLiteral(body, i);
|
|
726
|
+
skip(i);
|
|
727
|
+
regexOk = false;
|
|
728
|
+
before = "/";
|
|
729
|
+
}
|
|
730
|
+
else if (c === "`") {
|
|
731
|
+
modes.push("t");
|
|
732
|
+
depth++;
|
|
733
|
+
i++;
|
|
734
|
+
}
|
|
735
|
+
else if (/[\w$]/.test(c)) {
|
|
736
|
+
const wordStart = i;
|
|
737
|
+
while (i < body.length && /[\w$]/.test(body[i]))
|
|
738
|
+
i++;
|
|
739
|
+
regexOk =
|
|
740
|
+
before !== "." && REGEX_AFTER_KEYWORD.has(body.slice(wordStart, i));
|
|
741
|
+
before = "w";
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
if (c === "(" || c === "[" || c === "{")
|
|
745
|
+
depth++;
|
|
746
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
747
|
+
close(depth, i);
|
|
748
|
+
if (c === "}" &&
|
|
749
|
+
modes.length > 0 &&
|
|
750
|
+
depth === holeDepths[holeDepths.length - 1]) {
|
|
751
|
+
// The `}` that closes a hole: back to the template's text.
|
|
752
|
+
modes.pop();
|
|
753
|
+
holeDepths.pop();
|
|
754
|
+
}
|
|
755
|
+
depth--;
|
|
756
|
+
}
|
|
757
|
+
else if (c === ";" || c === "\n" || c === ",")
|
|
758
|
+
close(depth, i);
|
|
759
|
+
if (!/\s/.test(c)) {
|
|
760
|
+
regexOk = OPERAND_FOLLOWS.has(c);
|
|
761
|
+
before = c;
|
|
762
|
+
}
|
|
763
|
+
i++;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
close(-Infinity, body.length);
|
|
767
|
+
return ends;
|
|
768
|
+
}
|
|
769
|
+
const stringEnd = (text, i) => {
|
|
770
|
+
const quote = text[i];
|
|
771
|
+
i++;
|
|
772
|
+
while (i < text.length && text[i] !== quote && text[i] !== "\n") {
|
|
773
|
+
if (text[i] === "\\")
|
|
774
|
+
i++;
|
|
775
|
+
i++;
|
|
776
|
+
}
|
|
777
|
+
return i + 1;
|
|
778
|
+
};
|
|
779
|
+
const stripParens = (e) => {
|
|
780
|
+
while (e.startsWith("(") && matchingClose(e, 0) === e.length - 1)
|
|
781
|
+
e = e.slice(1, -1).trim();
|
|
782
|
+
return e;
|
|
783
|
+
};
|
|
784
|
+
/** Whether `e` is exactly one call of a trusted function (`esc(q)`, `b.esc(q)`). */
|
|
785
|
+
const isTrustedCall = (e, ctx) => {
|
|
786
|
+
const m = /^([\w$]+(?:\.[\w$]+)*)\s*\(/.exec(e);
|
|
787
|
+
if (!m)
|
|
788
|
+
return false;
|
|
789
|
+
const name = m[1];
|
|
790
|
+
const trusted = (htmlExportOf(name, ctx.imports) !== undefined &&
|
|
791
|
+
htmlExportOf(name, ctx.imports) !== "html") ||
|
|
792
|
+
ctx.escapeFunctions.includes(name);
|
|
793
|
+
return trusted && matchingClose(e, m[0].length - 1) === e.length - 1;
|
|
794
|
+
};
|
|
795
|
+
/** The callee of a call to `name` (local) that spans the whole expression. */
|
|
796
|
+
export const callOf = (expr) => {
|
|
797
|
+
const e = expr.trim();
|
|
798
|
+
const m = /^([\w$]+(?:\.[\w$]+)*)\s*\(/.exec(e);
|
|
799
|
+
if (!m)
|
|
800
|
+
return undefined;
|
|
801
|
+
return matchingClose(e, m[0].length - 1) === e.length - 1 ? m[1] : undefined;
|
|
802
|
+
};
|
|
803
|
+
/**
|
|
804
|
+
* What `test` says of `expr` (a label, such as the callee of `json(x)`), or,
|
|
805
|
+
* when `expr` is a name, of every value bound to it, followed through
|
|
806
|
+
* aliases (`const body = json(x)`, `const b = body`): the label of the first
|
|
807
|
+
* value when every one passes, so a `let` reassigned to anything else is not
|
|
808
|
+
* followed. `question` names the test, so each name is read once per script
|
|
809
|
+
* and question. Undefined when the expression, or any value, fails.
|
|
810
|
+
*/
|
|
811
|
+
export function boundTo(expr, ctx, question, test, seen = new Set()) {
|
|
812
|
+
const e = stripParens(expr.trim());
|
|
813
|
+
if (!/^[\w$]+$/.test(e))
|
|
814
|
+
return test(e);
|
|
815
|
+
const bound = ctx.bindings.get(e);
|
|
816
|
+
if (!bound)
|
|
817
|
+
return undefined;
|
|
818
|
+
ctx.memo ??= newNameMemo();
|
|
819
|
+
let entry = ctx.memo.bound.get(question);
|
|
820
|
+
if (entry === undefined) {
|
|
821
|
+
entry = { verdicts: new Map(), labels: new Map() };
|
|
822
|
+
ctx.memo.bound.set(question, entry);
|
|
823
|
+
}
|
|
824
|
+
const { labels } = entry;
|
|
825
|
+
let first;
|
|
826
|
+
const fine = nameVerdict(e, bound, ctx.memo, entry.verdicts, seen, (value) => {
|
|
827
|
+
const label = boundTo(value, ctx, question, test, seen);
|
|
828
|
+
first ??= label;
|
|
829
|
+
return label !== undefined;
|
|
830
|
+
});
|
|
831
|
+
if (!fine)
|
|
832
|
+
return undefined;
|
|
833
|
+
if (first !== undefined)
|
|
834
|
+
labels.set(e, first);
|
|
835
|
+
return labels.get(e);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Whether a hole's expression cannot carry markup a visitor controls:
|
|
839
|
+
* literals, numbers, calls to html/raw/esc/safeUrl/json (or a listed
|
|
840
|
+
* escaper), nested html, conditionals of those, and names bound to them.
|
|
841
|
+
*/
|
|
842
|
+
export function isExempt(expr, ctx, seen = new Set()) {
|
|
843
|
+
const e = stripParens(expr.trim());
|
|
844
|
+
if (e === "")
|
|
845
|
+
return true;
|
|
846
|
+
if (LITERAL.test(e) || HOLE_FREE_TEMPLATE.test(e))
|
|
847
|
+
return true;
|
|
848
|
+
// Numbers: .length/.size, a whole Number()/parseInt()/parseFloat()/Math.*()
|
|
849
|
+
// call, a lone toFixed() call. Arithmetic is handled after || ?? && and ?:.
|
|
850
|
+
if (/^[\w$.[\]]+\.(?:length|size)$/.test(e))
|
|
851
|
+
return true;
|
|
852
|
+
const callee = callOf(e);
|
|
853
|
+
if (callee !== undefined &&
|
|
854
|
+
/^(?:Number|parseInt|parseFloat|Math\.\w+)$/.test(callee))
|
|
855
|
+
return true;
|
|
856
|
+
if (/^[\w$.[\]]+\.toFixed\s*\([^()]*\)$/.test(e))
|
|
857
|
+
return true;
|
|
858
|
+
if (/^[A-Z][A-Z0-9_]*$/.test(e))
|
|
859
|
+
return true;
|
|
860
|
+
if (isTrustedCall(e, ctx))
|
|
861
|
+
return true;
|
|
862
|
+
for (const tag of ctx.htmlTags) {
|
|
863
|
+
if (e.startsWith(`${tag}\``))
|
|
864
|
+
return true;
|
|
865
|
+
// A callback that returns html (items.map((i) => html`...`)).
|
|
866
|
+
if (new RegExp(`=>\\s*${escapeRegExp(tag)}\``).test(e))
|
|
867
|
+
return true;
|
|
868
|
+
}
|
|
869
|
+
if (/^[\w$]+$/.test(e)) {
|
|
870
|
+
const bound = ctx.bindings.get(e);
|
|
871
|
+
if (!bound)
|
|
872
|
+
return false;
|
|
873
|
+
ctx.memo ??= newNameMemo();
|
|
874
|
+
return nameVerdict(e, bound, ctx.memo, ctx.memo.exempt, seen, (value) => isExempt(value, ctx, seen));
|
|
875
|
+
}
|
|
876
|
+
// cond ? a : b (only the rendered branches matter).
|
|
877
|
+
let question = -1;
|
|
878
|
+
topLevel(e, (i) => {
|
|
879
|
+
if (e[i] === "?" &&
|
|
880
|
+
e[i + 1] !== "?" &&
|
|
881
|
+
e[i + 1] !== "." &&
|
|
882
|
+
e[i - 1] !== "?") {
|
|
883
|
+
question = i;
|
|
884
|
+
return true;
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
if (question !== -1) {
|
|
888
|
+
let nested = 0;
|
|
889
|
+
let colon = -1;
|
|
890
|
+
topLevel(e.slice(question + 1), (i) => {
|
|
891
|
+
const c = e[question + 1 + i];
|
|
892
|
+
const next = e[question + 2 + i];
|
|
893
|
+
if (c === "?" && next !== "?" && next !== ".")
|
|
894
|
+
nested++;
|
|
895
|
+
else if (c === ":") {
|
|
896
|
+
if (nested === 0) {
|
|
897
|
+
colon = question + 1 + i;
|
|
898
|
+
return true;
|
|
899
|
+
}
|
|
900
|
+
nested--;
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
if (colon !== -1)
|
|
904
|
+
return (isExempt(e.slice(question + 1, colon), ctx, seen) &&
|
|
905
|
+
isExempt(e.slice(colon + 1), ctx, seen));
|
|
906
|
+
}
|
|
907
|
+
// a || b, a ?? b (both may render), a && b (b renders, or a falsy value).
|
|
908
|
+
let split;
|
|
909
|
+
topLevel(e, (i) => {
|
|
910
|
+
const two = e.slice(i, i + 2);
|
|
911
|
+
if (two === "||" || two === "??") {
|
|
912
|
+
split = { at: i, op: two };
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
if (!split)
|
|
917
|
+
topLevel(e, (i) => {
|
|
918
|
+
if (e.slice(i, i + 2) === "&&") {
|
|
919
|
+
split = { at: i, op: "&&" };
|
|
920
|
+
return true;
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
if (split) {
|
|
924
|
+
const left = e.slice(0, split.at);
|
|
925
|
+
const right = e.slice(split.at + 2);
|
|
926
|
+
return split.op === "&&"
|
|
927
|
+
? isExempt(right, ctx, seen)
|
|
928
|
+
: isExempt(left, ctx, seen) && isExempt(right, ctx, seen);
|
|
929
|
+
}
|
|
930
|
+
return isArithmeticExempt(e, ctx, seen);
|
|
931
|
+
}
|
|
932
|
+
const NUMBER_LITERAL = /^(?:\d[\d_]*\.?\d*(?:e[+-]?\d+)?|0x[\da-f]+)n?$/i;
|
|
933
|
+
const NAME_OR_MEMBER = /^[\w$]+(?:\??\.[\w$]+|\[[\w$]+\])*$/;
|
|
934
|
+
/**
|
|
935
|
+
* Arithmetic. `-`, `*`, `/`, `%` and unary `+`/`-` always make a number,
|
|
936
|
+
* but binary `+` joins strings, so a `+` chain is exempt only when every
|
|
937
|
+
* operand is (`count + 1`, `"Hi " + esc(q)`). One lenience keeps loop
|
|
938
|
+
* counters quiet: `i + 1` (names or members plus a number literal, with no
|
|
939
|
+
* string operand) is exempt unless a name is bound to a value that is not.
|
|
940
|
+
*/
|
|
941
|
+
function isArithmeticExempt(e, ctx, seen) {
|
|
942
|
+
const plus = [];
|
|
943
|
+
let other = false;
|
|
944
|
+
let disqualified = false;
|
|
945
|
+
topLevel(e, (i) => {
|
|
946
|
+
const c = e[i];
|
|
947
|
+
if (c === "+" || c === "-") {
|
|
948
|
+
// ++ and -- are part of their operand.
|
|
949
|
+
if (e[i + 1] === c || e[i - 1] === c)
|
|
950
|
+
return;
|
|
951
|
+
let k = i - 1;
|
|
952
|
+
while (k >= 0 && /\s/.test(e[k]))
|
|
953
|
+
k--;
|
|
954
|
+
const unary = k < 0 || /[-+*/%=<>!&|?:,~^]/.test(e[k]);
|
|
955
|
+
if (unary)
|
|
956
|
+
return;
|
|
957
|
+
if (c === "+")
|
|
958
|
+
plus.push(i);
|
|
959
|
+
else
|
|
960
|
+
other = true;
|
|
961
|
+
}
|
|
962
|
+
else if (c === "*" || c === "/" || c === "%")
|
|
963
|
+
other = true;
|
|
964
|
+
else if (c === "?" && e[i + 1] === ".")
|
|
965
|
+
return;
|
|
966
|
+
else if (/[=<>!&|?:,^~]/.test(c))
|
|
967
|
+
disqualified = true;
|
|
968
|
+
});
|
|
969
|
+
if (disqualified)
|
|
970
|
+
return false;
|
|
971
|
+
if (plus.length === 0) {
|
|
972
|
+
const unaryNumber = /^[-+]\s*[^-+]/.test(e);
|
|
973
|
+
if (!other && !unaryNumber)
|
|
974
|
+
return false;
|
|
975
|
+
// Every operand of - * / % (or a unary sign) is a number, whatever it is.
|
|
976
|
+
return true;
|
|
977
|
+
}
|
|
978
|
+
const operands = [];
|
|
979
|
+
let from = 0;
|
|
980
|
+
for (const at of plus) {
|
|
981
|
+
operands.push(e.slice(from, at).trim());
|
|
982
|
+
from = at + 1;
|
|
983
|
+
}
|
|
984
|
+
operands.push(e.slice(from).trim());
|
|
985
|
+
// Each operand gets its own copy of `seen`: `n + n` reads n twice.
|
|
986
|
+
const exempt = operands.map((o) => isExempt(o, ctx, new Set(seen)));
|
|
987
|
+
if (exempt.every(Boolean))
|
|
988
|
+
return true;
|
|
989
|
+
// i + 1: names and a number literal, no string, no call.
|
|
990
|
+
if (!operands.some((o) => NUMBER_LITERAL.test(stripParens(o))))
|
|
991
|
+
return false;
|
|
992
|
+
return operands.every((o, i) => {
|
|
993
|
+
const op = stripParens(o);
|
|
994
|
+
if (exempt[i])
|
|
995
|
+
return true;
|
|
996
|
+
if (!NAME_OR_MEMBER.test(op))
|
|
997
|
+
return false;
|
|
998
|
+
const root = /^[\w$]+/.exec(op)[0];
|
|
999
|
+
return !ctx.bindings.has(root);
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* A tag whose name is a hole: `<${tag} class="...">`, or `<${tag}>` with
|
|
1004
|
+
* an end tag `</${tag}>` in the same template. A lone `<${tag}>` is how a
|
|
1005
|
+
* message names an element (`<${tag}> needs a src attribute`), and a hole
|
|
1006
|
+
* after `<` followed by neither is a comparison or a regex source.
|
|
1007
|
+
*/
|
|
1008
|
+
const holeNamesTag = (parts, skipCodeSpans = false) => {
|
|
1009
|
+
const last = parts.length - 1;
|
|
1010
|
+
let startThenClose = false;
|
|
1011
|
+
let endTag = false;
|
|
1012
|
+
for (let p = 0; p < last; p++) {
|
|
1013
|
+
const part = parts[p];
|
|
1014
|
+
const next = parts[p + 1];
|
|
1015
|
+
// In a Markdown code span (`` `<${tag} class="..."> ``), as a fixed tag
|
|
1016
|
+
// right after a backtick is.
|
|
1017
|
+
if (skipCodeSpans && /`<\/?$/.test(part))
|
|
1018
|
+
continue;
|
|
1019
|
+
if (part.endsWith("</")) {
|
|
1020
|
+
if (/^\s*>/.test(next))
|
|
1021
|
+
endTag = true;
|
|
1022
|
+
}
|
|
1023
|
+
else if (!part.endsWith("<"))
|
|
1024
|
+
continue;
|
|
1025
|
+
else if (HOLE_THEN_ATTRIBUTE.test(next))
|
|
1026
|
+
return true;
|
|
1027
|
+
else if (/^\s*>/.test(next))
|
|
1028
|
+
startThenClose = true;
|
|
1029
|
+
}
|
|
1030
|
+
return startThenClose && endTag;
|
|
1031
|
+
};
|
|
1032
|
+
/** Static text that looks like markup: a start or end tag. */
|
|
1033
|
+
export const looksLikeMarkup = (parts) => parts.some((part) => /<\/?[a-zA-Z][\w:-]*(?=[\s/>]|$)/.test(part)) ||
|
|
1034
|
+
holeNamesTag(parts);
|
|
1035
|
+
/**
|
|
1036
|
+
* Elements whose bare start tag (`<li>`, no attribute) counts as markup in
|
|
1037
|
+
* build-time code: names that are not also CLI placeholders, TypeScript
|
|
1038
|
+
* type names or words a message names elements by (`<path>`, `<output>`,
|
|
1039
|
+
* `<title>`, `<img>`, `<script>`, `<template>` all are).
|
|
1040
|
+
*/
|
|
1041
|
+
const BARE_MARKUP_ELEMENTS = new Set("article aside blockquote br dd div dl dt em figcaption h1 h2 h3 h4 h5 h6 hr li nav ol p span strong tbody td tfoot th thead tr ul wbr".split(" "));
|
|
1042
|
+
/**
|
|
1043
|
+
* A tag in build-time text: group 1 is `/` for an end tag, group 2 the
|
|
1044
|
+
* name, group 3 the first attribute when it is written as one (`name="`,
|
|
1045
|
+
* `name='`, `name=value`, `name=` before a hole, or a lone `name>`), and
|
|
1046
|
+
* group 4 the `>` of a bare start tag. Names start with a lower-case
|
|
1047
|
+
* letter, as HTML, SVG and custom element names do in practice, so
|
|
1048
|
+
* `Array<Path>` is no tag.
|
|
1049
|
+
*/
|
|
1050
|
+
const CODE_TAG = /<(\/?)([a-z][\w:-]*)(?:(\s+[a-zA-Z_:@][-\w:.@]*(?:=(?:["']|[^\s"'=<>`]|$)|\s*\/?>))|\s*\/?(>))?/g;
|
|
1051
|
+
/** After a hole inside a start tag: the rest of the name, then `=` or an attribute. */
|
|
1052
|
+
const HOLE_THEN_ATTRIBUTE = /^[-\w:.@]*(?:=|\s+[a-zA-Z_:@][-\w:.@]*(?:=|\s*\/?>))/;
|
|
1053
|
+
/**
|
|
1054
|
+
* Static text that looks like markup in build-time code, which also holds
|
|
1055
|
+
* CLI text (`tool <command> --out <output>`), prose that names elements
|
|
1056
|
+
* (`<img> needs alt text`), Markdown code spans (`` `<nav>` ``), SQL
|
|
1057
|
+
* (`a<b AND id = 1`) and generated TypeScript (`Array<Path>`). It needs an
|
|
1058
|
+
* end tag (`</li>`), a start tag with an attribute
|
|
1059
|
+
* (`<product-card title="...">`, `<details open>`), or a bare start tag of
|
|
1060
|
+
* an element in BARE_MARKUP_ELEMENTS (`<li>`), or a tag a hole continues
|
|
1061
|
+
* with an attribute or an end tag's `>` after the hole (`<h${level} id=`,
|
|
1062
|
+
* `<meta ${attr}="...">`, `</h${level}>`). A tag right after a
|
|
1063
|
+
* backtick is in a Markdown code span and does not count. One pass per part.
|
|
1064
|
+
*/
|
|
1065
|
+
export const markupInCode = (source) => {
|
|
1066
|
+
const { parts } = markdownFences(source);
|
|
1067
|
+
return (holeNamesTag(parts, true) ||
|
|
1068
|
+
parts.some((part, p) => {
|
|
1069
|
+
for (const m of part.matchAll(CODE_TAG)) {
|
|
1070
|
+
if (m.index > 0 && part[m.index - 1] === "`")
|
|
1071
|
+
continue;
|
|
1072
|
+
if (m[3] !== undefined && m[1] !== "/")
|
|
1073
|
+
return true;
|
|
1074
|
+
if (m[4] !== undefined) {
|
|
1075
|
+
if (m[1] === "/")
|
|
1076
|
+
return true;
|
|
1077
|
+
// A bare start tag counts where markup goes: at the start of the
|
|
1078
|
+
// template or right after a hole, or after another tag. After a
|
|
1079
|
+
// word it is prose that names the element (`Found <p> inside`).
|
|
1080
|
+
if (BARE_MARKUP_ELEMENTS.has(m[2]) && afterTagOrStart(part, m.index))
|
|
1081
|
+
return true;
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
// A tag that a hole continues, with an attribute or an end tag's `>`
|
|
1085
|
+
// after the hole: `<h${level} id="`, `<meta ${attr}="`, `</h${level}>`.
|
|
1086
|
+
// A bare `<h${n}>` is how a message names a heading level.
|
|
1087
|
+
if (p < parts.length - 1 &&
|
|
1088
|
+
/^\s*$/.test(part.slice(m.index + m[0].length)) &&
|
|
1089
|
+
(m[1] === "/" ? /^\s*>/ : HOLE_THEN_ATTRIBUTE).test(parts[p + 1]))
|
|
1090
|
+
return true;
|
|
1091
|
+
}
|
|
1092
|
+
return false;
|
|
1093
|
+
}));
|
|
1094
|
+
};
|
|
1095
|
+
/** A Markdown fence line's start, as template source writes it: \`\`\` or ~~~. */
|
|
1096
|
+
const FENCE = /^[ \t]*(?:\\`\\`\\`|```|~~~)/;
|
|
1097
|
+
const NO_FENCED_HOLES = new Set();
|
|
1098
|
+
/**
|
|
1099
|
+
* Blanks each Markdown fenced block: from a line that starts with three
|
|
1100
|
+
* backticks (written \`\`\` in a template) or three tildes, through the
|
|
1101
|
+
* line that closes it. A fence that never closes opens no block: a stray
|
|
1102
|
+
* \`\`\` would otherwise hide every real markup hole after it. Code in a fence is text the
|
|
1103
|
+
* Markdown renderer escapes, not markup. A line ends at a newline or a
|
|
1104
|
+
* `\n` escape. One pass.
|
|
1105
|
+
*/
|
|
1106
|
+
export const markdownFences = (parts) => {
|
|
1107
|
+
if (!parts.some((part) => part.includes("`\\`") || part.includes("~~~")))
|
|
1108
|
+
return { parts, fencedHoles: NO_FENCED_HOLES, hasFence: false };
|
|
1109
|
+
const text = parts.join(HOLE_CHAR);
|
|
1110
|
+
// Lines, as [end, next]; a fence that never closes opens no block, so
|
|
1111
|
+
// the markup after it is still read as markup.
|
|
1112
|
+
const lines = [];
|
|
1113
|
+
for (let i = 0; i < text.length; i++) {
|
|
1114
|
+
if (text[i] === "\n")
|
|
1115
|
+
lines.push([i, i + 1]);
|
|
1116
|
+
else if (text[i] === "\\") {
|
|
1117
|
+
if (text[i + 1] === "n")
|
|
1118
|
+
lines.push([i, i + 2]);
|
|
1119
|
+
i++;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
lines.push([text.length, text.length]);
|
|
1123
|
+
let unclosed = -1;
|
|
1124
|
+
let open = false;
|
|
1125
|
+
let sawFence = false;
|
|
1126
|
+
for (let l = 0, start = 0; l < lines.length; start = lines[l][1], l++)
|
|
1127
|
+
if (FENCE.test(text.slice(start, lines[l][0]))) {
|
|
1128
|
+
sawFence = true;
|
|
1129
|
+
open = !open;
|
|
1130
|
+
if (open)
|
|
1131
|
+
unclosed = start;
|
|
1132
|
+
}
|
|
1133
|
+
if (!open)
|
|
1134
|
+
unclosed = -1;
|
|
1135
|
+
let out = "";
|
|
1136
|
+
let inFence = false;
|
|
1137
|
+
let hasFence = sawFence;
|
|
1138
|
+
let lineStart = 0;
|
|
1139
|
+
let holesBefore = 0;
|
|
1140
|
+
const fencedHoles = new Set();
|
|
1141
|
+
const endLine = (end, next) => {
|
|
1142
|
+
const line = text.slice(lineStart, end);
|
|
1143
|
+
const fence = lineStart !== unclosed && FENCE.test(line);
|
|
1144
|
+
if (inFence || fence) {
|
|
1145
|
+
hasFence = true;
|
|
1146
|
+
out += line.replace(/[^\u0000]/g, " ") + text.slice(end, next);
|
|
1147
|
+
for (let k = line.indexOf(HOLE_CHAR); k !== -1; k = line.indexOf(HOLE_CHAR, k + 1))
|
|
1148
|
+
fencedHoles.add(++holesBefore);
|
|
1149
|
+
if (fence)
|
|
1150
|
+
inFence = !inFence;
|
|
1151
|
+
}
|
|
1152
|
+
else {
|
|
1153
|
+
out += text.slice(lineStart, next);
|
|
1154
|
+
for (let k = line.indexOf(HOLE_CHAR); k !== -1; k = line.indexOf(HOLE_CHAR, k + 1))
|
|
1155
|
+
holesBefore++;
|
|
1156
|
+
}
|
|
1157
|
+
lineStart = next;
|
|
1158
|
+
};
|
|
1159
|
+
for (const [end, next] of lines)
|
|
1160
|
+
endLine(end, next);
|
|
1161
|
+
return { parts: out.split(HOLE_CHAR), fencedHoles, hasFence };
|
|
1162
|
+
};
|
|
1163
|
+
/** Whether only whitespace, or a tag's `>` then whitespace, comes before `at` in `part`. */
|
|
1164
|
+
const afterTagOrStart = (part, at) => {
|
|
1165
|
+
let k = at - 1;
|
|
1166
|
+
while (k >= 0 && /\s/.test(part[k]))
|
|
1167
|
+
k--;
|
|
1168
|
+
return k < 0 || part[k] === ">";
|
|
1169
|
+
};
|
|
1170
|
+
/** `<` written as \u003c in a replacement: `.replace(/</g, "\\u003c")`. */
|
|
1171
|
+
const LT_TO_U003C = /^\s*(?:\/<\/[a-z]*|(["'`])<\1)\s*,\s*(["'`])\\\\u003c\2\s*$/i;
|
|
1172
|
+
/**
|
|
1173
|
+
* Whether a hole is JSON.stringify(...) followed by .replace()/.replaceAll()
|
|
1174
|
+
* calls, one of which writes `<` as \u003c (or a name bound only to such
|
|
1175
|
+
* values). In a <script type="application/ld+json"> that value can never
|
|
1176
|
+
* close the script, so it is not a sink.
|
|
1177
|
+
*/
|
|
1178
|
+
export function isEscapedJson(expr, ctx, seen = new Set()) {
|
|
1179
|
+
const e = stripParens(expr.trim());
|
|
1180
|
+
if (/^[\w$]+$/.test(e)) {
|
|
1181
|
+
const bound = ctx.bindings.get(e);
|
|
1182
|
+
if (!bound)
|
|
1183
|
+
return false;
|
|
1184
|
+
ctx.memo ??= newNameMemo();
|
|
1185
|
+
return nameVerdict(e, bound, ctx.memo, ctx.memo.escapedJson, seen, (value) => isEscapedJson(value, ctx, seen));
|
|
1186
|
+
}
|
|
1187
|
+
const head = /^JSON\.stringify\s*\(/.exec(e);
|
|
1188
|
+
if (!head)
|
|
1189
|
+
return false;
|
|
1190
|
+
let i = matchingClose(e, head[0].length - 1);
|
|
1191
|
+
if (i === -1)
|
|
1192
|
+
return false;
|
|
1193
|
+
i++;
|
|
1194
|
+
let escaped = false;
|
|
1195
|
+
while (i < e.length) {
|
|
1196
|
+
const call = /^\s*\??\.\s*replace(?:All)?\s*\(/.exec(e.slice(i));
|
|
1197
|
+
if (!call)
|
|
1198
|
+
return false;
|
|
1199
|
+
const open = i + call[0].length - 1;
|
|
1200
|
+
const close = matchingClose(e, open);
|
|
1201
|
+
if (close === -1)
|
|
1202
|
+
return false;
|
|
1203
|
+
if (LT_TO_U003C.test(e.slice(open + 1, close)))
|
|
1204
|
+
escaped = true;
|
|
1205
|
+
i = close + 1;
|
|
1206
|
+
}
|
|
1207
|
+
return escaped;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Whether a literal is a message, not markup: the direct argument of
|
|
1211
|
+
* console.*(), new *Error() or throw, or the value of a `message`, `error`,
|
|
1212
|
+
* `reason` or `warning` property. `super(...)` in a class that extends an
|
|
1213
|
+
* Error is checked by the caller, which knows the class.
|
|
1214
|
+
*/
|
|
1215
|
+
export const isMessageArgument = (source, start) => {
|
|
1216
|
+
const before = source.slice(Math.max(0, start - 80), start);
|
|
1217
|
+
return (/\bthrow\s*$/.test(before) ||
|
|
1218
|
+
/\b(?:console\.[a-zA-Z]+|new\s+[\w$.]*Error)\s*\(\s*$/.test(before) ||
|
|
1219
|
+
/(?:^|[{,\s])(?:message|error|reason|warning)\s*:\s*$/.test(before));
|
|
1220
|
+
};
|
|
1221
|
+
/**
|
|
1222
|
+
* Whether a literal is a pattern that searches text rather than markup
|
|
1223
|
+
* the code writes: the needle of .includes(), .indexOf(), .lastIndexOf(),
|
|
1224
|
+
* .startsWith(), .endsWith(), .split(), .search(), .match() or
|
|
1225
|
+
* .matchAll(), the first argument of .replace() or .replaceAll(), or the
|
|
1226
|
+
* source of RegExp(). Wrapping it in html would change what it matches.
|
|
1227
|
+
*/
|
|
1228
|
+
export const isPatternArgument = (source, start) => /(?:\.\s*(?:includes|indexOf|lastIndexOf|startsWith|endsWith|split|search|match|matchAll|replace|replaceAll)|\bRegExp)\s*\(\s*(?:String\.raw\s*)?$/.test(source.slice(Math.max(0, start - 80), start));
|
|
1229
|
+
const RAW_TEXT_TAGS = new Set(["script", "style", "textarea", "title"]);
|
|
1230
|
+
const SVG_ANIMATION_TAGS = new Set([
|
|
1231
|
+
"animate",
|
|
1232
|
+
"animatemotion",
|
|
1233
|
+
"animatetransform",
|
|
1234
|
+
"animatecolor",
|
|
1235
|
+
"set",
|
|
1236
|
+
]);
|
|
1237
|
+
// ─── html's URL scheme rule (pkg/src/lib/url-safety.ts, html-template.ts) ──
|
|
1238
|
+
/** Attribute names whose whole value is one URL (url-safety.ts URL_TARGETS). */
|
|
1239
|
+
const URL_TARGETS = new Set([
|
|
1240
|
+
"href",
|
|
1241
|
+
"xlink:href",
|
|
1242
|
+
"src",
|
|
1243
|
+
"action",
|
|
1244
|
+
"formaction",
|
|
1245
|
+
"poster",
|
|
1246
|
+
"background",
|
|
1247
|
+
"cite",
|
|
1248
|
+
"codebase",
|
|
1249
|
+
"longdesc",
|
|
1250
|
+
"lowsrc",
|
|
1251
|
+
"dynsrc",
|
|
1252
|
+
"manifest",
|
|
1253
|
+
"icon",
|
|
1254
|
+
]);
|
|
1255
|
+
const IMAGE_TARGETS = new Set([
|
|
1256
|
+
"img src",
|
|
1257
|
+
"img srcset",
|
|
1258
|
+
"source srcset",
|
|
1259
|
+
"video poster",
|
|
1260
|
+
"input src",
|
|
1261
|
+
"link imagesrcset",
|
|
1262
|
+
]);
|
|
1263
|
+
const SCRIPT_SCHEMES = new Set([
|
|
1264
|
+
"javascript",
|
|
1265
|
+
"vbscript",
|
|
1266
|
+
"livescript",
|
|
1267
|
+
"mocha",
|
|
1268
|
+
"data",
|
|
1269
|
+
]);
|
|
1270
|
+
const LIST_BOUNDARY = {
|
|
1271
|
+
srcset: /[\t\n\f\r ,]/,
|
|
1272
|
+
ping: /[\t\n\f\r ]/,
|
|
1273
|
+
animation: /;/,
|
|
1274
|
+
};
|
|
1275
|
+
/** Which kind of URL `<tag attr>` holds, as html reads it, or null. */
|
|
1276
|
+
const urlSlotOf = (tag, attr) => {
|
|
1277
|
+
if (URL_TARGETS.has(attr) || (tag === "object" && attr === "data"))
|
|
1278
|
+
return "url";
|
|
1279
|
+
if (attr === "srcset" || attr === "imagesrcset")
|
|
1280
|
+
return "srcset";
|
|
1281
|
+
if (attr === "ping")
|
|
1282
|
+
return "ping";
|
|
1283
|
+
if (SVG_ANIMATION_TAGS.has(tag) &&
|
|
1284
|
+
(attr === "to" || attr === "from" || attr === "by" || attr === "values"))
|
|
1285
|
+
return "animation";
|
|
1286
|
+
return null;
|
|
1287
|
+
};
|
|
1288
|
+
/** A URL as the URL parser reads it: outer C0 controls and spaces dropped, tabs and newlines removed. */
|
|
1289
|
+
const normalizeUrl = (url) => {
|
|
1290
|
+
let start = 0;
|
|
1291
|
+
let end = url.length;
|
|
1292
|
+
while (start < end && url.charCodeAt(start) <= 0x20)
|
|
1293
|
+
start++;
|
|
1294
|
+
while (end > start && url.charCodeAt(end - 1) <= 0x20)
|
|
1295
|
+
end--;
|
|
1296
|
+
return url.slice(start, end).replace(/[\t\n\r]/g, "");
|
|
1297
|
+
};
|
|
1298
|
+
/** How the text before a URL hole settles its scheme (html's prefixVerdict). */
|
|
1299
|
+
const prefixVerdict = (prefix, image) => {
|
|
1300
|
+
const text = normalizeUrl(prefix);
|
|
1301
|
+
if (text === "")
|
|
1302
|
+
return "start";
|
|
1303
|
+
const match = /[/?#:&]/.exec(text);
|
|
1304
|
+
if (match === null || match[0] === "&")
|
|
1305
|
+
return "refused";
|
|
1306
|
+
if (match[0] !== ":")
|
|
1307
|
+
return "settled";
|
|
1308
|
+
const scheme = text.slice(0, match.index);
|
|
1309
|
+
if (!/^[a-zA-Z][a-zA-Z0-9+.-]*$/.test(scheme))
|
|
1310
|
+
return "settled";
|
|
1311
|
+
if (!SCRIPT_SCHEMES.has(scheme.toLowerCase()))
|
|
1312
|
+
return "settled";
|
|
1313
|
+
return image && /^data:image\/[a-z0-9.+-]+[;,]/i.test(text)
|
|
1314
|
+
? "settled"
|
|
1315
|
+
: "refused";
|
|
1316
|
+
};
|
|
1317
|
+
/**
|
|
1318
|
+
* The 1-based number of the first hole html refuses in a URL attribute
|
|
1319
|
+
* value, from its static text and hole numbers in order, or -1. A list
|
|
1320
|
+
* (srcset, ping, SVG animation values) is read entry by entry; one URL
|
|
1321
|
+
* whose first hole starts it must reach a /, ? or # before a : or & or a
|
|
1322
|
+
* second hole, or end there.
|
|
1323
|
+
*/
|
|
1324
|
+
const urlRefusal = (slot, image, parts) => {
|
|
1325
|
+
if (slot !== "url") {
|
|
1326
|
+
// Each entry's verdict is read once it is settled, and a blank entry
|
|
1327
|
+
// (spaces, controls, holes) starts its URL, so this stays linear in
|
|
1328
|
+
// the value however many holes it has.
|
|
1329
|
+
const boundary = LIST_BOUNDARY[slot];
|
|
1330
|
+
let token = "";
|
|
1331
|
+
let blank = true;
|
|
1332
|
+
let settled = false;
|
|
1333
|
+
for (const p of parts) {
|
|
1334
|
+
if (typeof p === "string") {
|
|
1335
|
+
for (const c of p)
|
|
1336
|
+
if (boundary.test(c)) {
|
|
1337
|
+
token = "";
|
|
1338
|
+
blank = true;
|
|
1339
|
+
settled = false;
|
|
1340
|
+
}
|
|
1341
|
+
else if (!settled) {
|
|
1342
|
+
token += c;
|
|
1343
|
+
if (c.charCodeAt(0) > 0x20)
|
|
1344
|
+
blank = false;
|
|
1345
|
+
}
|
|
1346
|
+
continue;
|
|
1347
|
+
}
|
|
1348
|
+
if (!blank && !settled) {
|
|
1349
|
+
if (prefixVerdict(token, image) === "refused")
|
|
1350
|
+
return p + 1;
|
|
1351
|
+
settled = true;
|
|
1352
|
+
}
|
|
1353
|
+
if (!settled)
|
|
1354
|
+
token += HOLE_CHAR;
|
|
1355
|
+
}
|
|
1356
|
+
return -1;
|
|
1357
|
+
}
|
|
1358
|
+
const first = parts.findIndex((p) => typeof p === "number");
|
|
1359
|
+
if (first === -1)
|
|
1360
|
+
return -1;
|
|
1361
|
+
const start = parts[first];
|
|
1362
|
+
const verdict = prefixVerdict(parts.slice(0, first).join(""), image);
|
|
1363
|
+
if (verdict === "refused")
|
|
1364
|
+
return start + 1;
|
|
1365
|
+
if (verdict === "settled")
|
|
1366
|
+
return -1;
|
|
1367
|
+
let after = "";
|
|
1368
|
+
let index = first + 1;
|
|
1369
|
+
while (index < parts.length && typeof parts[index] === "string")
|
|
1370
|
+
after += parts[index++];
|
|
1371
|
+
const match = /[/?#:&]/.exec(after);
|
|
1372
|
+
const settled = match !== null && match[0] !== ":" && match[0] !== "&";
|
|
1373
|
+
return settled || (index >= parts.length && match === null) ? -1 : start + 1;
|
|
1374
|
+
};
|
|
1375
|
+
const isAsciiAlpha = (c) => /[a-zA-Z]/.test(c);
|
|
1376
|
+
const isTagSpace = (c) => /[\t\n\f\r ]/.test(c);
|
|
1377
|
+
/** Stands for the text of a hole inside a tag or attribute name. */
|
|
1378
|
+
const HOLE_CHAR = "\u0000";
|
|
1379
|
+
/** Whether html`...` throws on a hole in this context. */
|
|
1380
|
+
const throwsOn = (context) => {
|
|
1381
|
+
switch (context.kind) {
|
|
1382
|
+
case "text":
|
|
1383
|
+
return false;
|
|
1384
|
+
case "raw":
|
|
1385
|
+
return context.tag === "script" || context.tag === "style";
|
|
1386
|
+
case "value":
|
|
1387
|
+
return (/^on/.test(context.attr) ||
|
|
1388
|
+
context.attr === "style" ||
|
|
1389
|
+
context.attr === "srcdoc" ||
|
|
1390
|
+
(context.tag === "meta" &&
|
|
1391
|
+
(context.attr === "http-equiv" || context.attr === "content")) ||
|
|
1392
|
+
SVG_ANIMATION_TAGS.has(context.tag));
|
|
1393
|
+
default:
|
|
1394
|
+
return true;
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
/**
|
|
1398
|
+
* Where each hole of a template lands, from its static parts, and where
|
|
1399
|
+
* html`...` would throw: a hole in a tag name, where an attribute name
|
|
1400
|
+
* goes, in an unquoted value, inside a comment or <!...> declaration,
|
|
1401
|
+
* inside a <script> or <style>, in an on*, style or srcdoc value, a <meta>
|
|
1402
|
+
* http-equiv or content value, any attribute of an SVG animation element,
|
|
1403
|
+
* a URL value that breaks html's scheme rule (see urlRefusal), or a
|
|
1404
|
+
* template that ends inside a tag, comment, quoted value or raw-text
|
|
1405
|
+
* element. A small version of html's own tokenizer that carries its state
|
|
1406
|
+
* across the holes, so the whole template is read once, however many holes
|
|
1407
|
+
* it has. A hole in a name position counts as part of that name, so the
|
|
1408
|
+
* text after it is read as html would read it.
|
|
1409
|
+
*/
|
|
1410
|
+
export function scanTemplateHtml(parts) {
|
|
1411
|
+
const holes = [];
|
|
1412
|
+
let throwsAt = -1;
|
|
1413
|
+
let state = 0 /* T.Data */;
|
|
1414
|
+
let tag = "";
|
|
1415
|
+
let isEnd = false;
|
|
1416
|
+
let attr = "";
|
|
1417
|
+
let rawTag = "";
|
|
1418
|
+
let sawTag = false;
|
|
1419
|
+
/** The current tag's type="..." value, for a JSON script. */
|
|
1420
|
+
let typeValue = "";
|
|
1421
|
+
let jsonScript = false;
|
|
1422
|
+
/** Only whitespace so far in the raw-text element. */
|
|
1423
|
+
let rawBlank = true;
|
|
1424
|
+
/**
|
|
1425
|
+
* The quoted value being read when it holds URLs (html's URL scheme
|
|
1426
|
+
* rule): its slot, and its static text and hole numbers so far.
|
|
1427
|
+
*/
|
|
1428
|
+
let urlSlot = null;
|
|
1429
|
+
let urlImage = false;
|
|
1430
|
+
let urlParts = [];
|
|
1431
|
+
let urlText = "";
|
|
1432
|
+
const endUrlValue = () => {
|
|
1433
|
+
if (urlSlot === null)
|
|
1434
|
+
return;
|
|
1435
|
+
urlParts.push(urlText);
|
|
1436
|
+
const refused = urlRefusal(urlSlot, urlImage, urlParts);
|
|
1437
|
+
if (refused !== -1 && (throwsAt === -1 || refused < throwsAt))
|
|
1438
|
+
throwsAt = refused;
|
|
1439
|
+
urlSlot = null;
|
|
1440
|
+
urlParts = [];
|
|
1441
|
+
urlText = "";
|
|
1442
|
+
};
|
|
1443
|
+
const startValue = () => {
|
|
1444
|
+
urlSlot = urlSlotOf(tag, attr);
|
|
1445
|
+
urlImage = IMAGE_TARGETS.has(`${tag} ${attr}`);
|
|
1446
|
+
};
|
|
1447
|
+
for (let p = 0; p < parts.length; p++) {
|
|
1448
|
+
const text = parts[p];
|
|
1449
|
+
for (let i = 0; i < text.length; i++) {
|
|
1450
|
+
const c = text[i];
|
|
1451
|
+
switch (state) {
|
|
1452
|
+
case 0 /* T.Data */:
|
|
1453
|
+
if (c === "<")
|
|
1454
|
+
state = 1 /* T.TagOpen */;
|
|
1455
|
+
break;
|
|
1456
|
+
case 1 /* T.TagOpen */:
|
|
1457
|
+
if (isAsciiAlpha(c)) {
|
|
1458
|
+
state = 3 /* T.TagName */;
|
|
1459
|
+
tag = c.toLowerCase();
|
|
1460
|
+
isEnd = false;
|
|
1461
|
+
sawTag = true;
|
|
1462
|
+
typeValue = "";
|
|
1463
|
+
}
|
|
1464
|
+
else if (c === "/")
|
|
1465
|
+
state = 2 /* T.EndTagOpen */;
|
|
1466
|
+
else if (c === "!") {
|
|
1467
|
+
sawTag = true;
|
|
1468
|
+
if (text.startsWith("--", i + 1)) {
|
|
1469
|
+
state = 13 /* T.Comment */;
|
|
1470
|
+
i += 2;
|
|
1471
|
+
}
|
|
1472
|
+
else
|
|
1473
|
+
state = 14 /* T.Bogus */;
|
|
1474
|
+
}
|
|
1475
|
+
else if (c === "?")
|
|
1476
|
+
state = 14 /* T.Bogus */;
|
|
1477
|
+
else {
|
|
1478
|
+
state = 0 /* T.Data */;
|
|
1479
|
+
i--;
|
|
1480
|
+
}
|
|
1481
|
+
break;
|
|
1482
|
+
case 2 /* T.EndTagOpen */:
|
|
1483
|
+
if (isAsciiAlpha(c)) {
|
|
1484
|
+
state = 3 /* T.TagName */;
|
|
1485
|
+
tag = c.toLowerCase();
|
|
1486
|
+
isEnd = true;
|
|
1487
|
+
sawTag = true;
|
|
1488
|
+
}
|
|
1489
|
+
else if (c === ">")
|
|
1490
|
+
state = 0 /* T.Data */;
|
|
1491
|
+
else
|
|
1492
|
+
state = 14 /* T.Bogus */;
|
|
1493
|
+
break;
|
|
1494
|
+
case 3 /* T.TagName */:
|
|
1495
|
+
case 4 /* T.BeforeAttrName */:
|
|
1496
|
+
case 5 /* T.AttrName */:
|
|
1497
|
+
case 6 /* T.AfterAttrName */:
|
|
1498
|
+
case 7 /* T.BeforeAttrValue */:
|
|
1499
|
+
case 10 /* T.ValueUq */:
|
|
1500
|
+
case 11 /* T.AfterValueQuoted */:
|
|
1501
|
+
case 12 /* T.SelfClosing */:
|
|
1502
|
+
if (c === ">") {
|
|
1503
|
+
if (!isEnd && RAW_TEXT_TAGS.has(tag)) {
|
|
1504
|
+
state = 15 /* T.RawText */;
|
|
1505
|
+
rawTag = tag;
|
|
1506
|
+
rawBlank = true;
|
|
1507
|
+
jsonScript =
|
|
1508
|
+
tag === "script" &&
|
|
1509
|
+
/^application\/(?:ld\+)?json$/i.test(typeValue.trim());
|
|
1510
|
+
}
|
|
1511
|
+
else
|
|
1512
|
+
state = 0 /* T.Data */;
|
|
1513
|
+
}
|
|
1514
|
+
else if (state === 3 /* T.TagName */) {
|
|
1515
|
+
if (isTagSpace(c))
|
|
1516
|
+
state = 4 /* T.BeforeAttrName */;
|
|
1517
|
+
else if (c === "/")
|
|
1518
|
+
state = 12 /* T.SelfClosing */;
|
|
1519
|
+
else
|
|
1520
|
+
tag += c.toLowerCase();
|
|
1521
|
+
}
|
|
1522
|
+
else if (state === 7 /* T.BeforeAttrValue */) {
|
|
1523
|
+
if (c === '"') {
|
|
1524
|
+
state = 8 /* T.ValueDq */;
|
|
1525
|
+
startValue();
|
|
1526
|
+
}
|
|
1527
|
+
else if (c === "'") {
|
|
1528
|
+
state = 9 /* T.ValueSq */;
|
|
1529
|
+
startValue();
|
|
1530
|
+
}
|
|
1531
|
+
else if (!isTagSpace(c)) {
|
|
1532
|
+
state = 10 /* T.ValueUq */;
|
|
1533
|
+
if (attr === "type")
|
|
1534
|
+
typeValue += c;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
else if (state === 10 /* T.ValueUq */) {
|
|
1538
|
+
if (isTagSpace(c))
|
|
1539
|
+
state = 4 /* T.BeforeAttrName */;
|
|
1540
|
+
else if (attr === "type")
|
|
1541
|
+
typeValue += c;
|
|
1542
|
+
}
|
|
1543
|
+
else if (state === 5 /* T.AttrName */) {
|
|
1544
|
+
if (isTagSpace(c))
|
|
1545
|
+
state = 6 /* T.AfterAttrName */;
|
|
1546
|
+
else if (c === "/")
|
|
1547
|
+
state = 12 /* T.SelfClosing */;
|
|
1548
|
+
else if (c === "=")
|
|
1549
|
+
state = 7 /* T.BeforeAttrValue */;
|
|
1550
|
+
else
|
|
1551
|
+
attr += c.toLowerCase();
|
|
1552
|
+
}
|
|
1553
|
+
else if (isTagSpace(c)) {
|
|
1554
|
+
if (state !== 6 /* T.AfterAttrName */)
|
|
1555
|
+
state = 4 /* T.BeforeAttrName */;
|
|
1556
|
+
}
|
|
1557
|
+
else if (c === "/")
|
|
1558
|
+
state = 12 /* T.SelfClosing */;
|
|
1559
|
+
else if (c === "=" && state === 6 /* T.AfterAttrName */)
|
|
1560
|
+
state = 7 /* T.BeforeAttrValue */;
|
|
1561
|
+
else {
|
|
1562
|
+
state = 5 /* T.AttrName */;
|
|
1563
|
+
attr = c.toLowerCase();
|
|
1564
|
+
}
|
|
1565
|
+
break;
|
|
1566
|
+
case 8 /* T.ValueDq */:
|
|
1567
|
+
case 9 /* T.ValueSq */:
|
|
1568
|
+
if (c === (state === 8 /* T.ValueDq */ ? '"' : "'")) {
|
|
1569
|
+
state = 11 /* T.AfterValueQuoted */;
|
|
1570
|
+
endUrlValue();
|
|
1571
|
+
}
|
|
1572
|
+
else {
|
|
1573
|
+
if (attr === "type")
|
|
1574
|
+
typeValue += c;
|
|
1575
|
+
if (urlSlot !== null)
|
|
1576
|
+
urlText += c;
|
|
1577
|
+
}
|
|
1578
|
+
break;
|
|
1579
|
+
case 13 /* T.Comment */: {
|
|
1580
|
+
const close = text.indexOf("-->", i);
|
|
1581
|
+
if (close === -1)
|
|
1582
|
+
i = text.length;
|
|
1583
|
+
else {
|
|
1584
|
+
state = 0 /* T.Data */;
|
|
1585
|
+
i = close + 2;
|
|
1586
|
+
}
|
|
1587
|
+
break;
|
|
1588
|
+
}
|
|
1589
|
+
case 14 /* T.Bogus */:
|
|
1590
|
+
if (c === ">")
|
|
1591
|
+
state = 0 /* T.Data */;
|
|
1592
|
+
break;
|
|
1593
|
+
case 15 /* T.RawText */:
|
|
1594
|
+
if (c === "<" &&
|
|
1595
|
+
text[i + 1] === "/" &&
|
|
1596
|
+
text.slice(i + 2, i + 2 + rawTag.length).toLowerCase() === rawTag &&
|
|
1597
|
+
/^[\t\n\f\r />]/.test(text[i + 2 + rawTag.length] ?? "")) {
|
|
1598
|
+
state = 3 /* T.TagName */;
|
|
1599
|
+
tag = rawTag;
|
|
1600
|
+
isEnd = true;
|
|
1601
|
+
i += 1 + rawTag.length;
|
|
1602
|
+
}
|
|
1603
|
+
else if (c === "\\" && /[ntrf]/.test(text[i + 1] ?? "")) {
|
|
1604
|
+
// `\n` in the template source is a newline in the value.
|
|
1605
|
+
i++;
|
|
1606
|
+
}
|
|
1607
|
+
else if (!isTagSpace(c))
|
|
1608
|
+
rawBlank = false;
|
|
1609
|
+
break;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
if (p === parts.length - 1)
|
|
1613
|
+
break;
|
|
1614
|
+
// A hole after parts[p]: where it lands, then the state html reads on in.
|
|
1615
|
+
let context;
|
|
1616
|
+
switch (state) {
|
|
1617
|
+
case 0 /* T.Data */:
|
|
1618
|
+
context = { kind: "text", sawTag };
|
|
1619
|
+
break;
|
|
1620
|
+
case 15 /* T.RawText */:
|
|
1621
|
+
context = {
|
|
1622
|
+
kind: "raw",
|
|
1623
|
+
tag: rawTag,
|
|
1624
|
+
jsonScript: jsonScript && rawBlank,
|
|
1625
|
+
};
|
|
1626
|
+
rawBlank = false;
|
|
1627
|
+
break;
|
|
1628
|
+
case 8 /* T.ValueDq */:
|
|
1629
|
+
case 9 /* T.ValueSq */:
|
|
1630
|
+
context = { kind: "value", tag, attr };
|
|
1631
|
+
if (attr === "type")
|
|
1632
|
+
typeValue += HOLE_CHAR;
|
|
1633
|
+
if (urlSlot !== null) {
|
|
1634
|
+
urlParts.push(urlText, holes.length);
|
|
1635
|
+
urlText = "";
|
|
1636
|
+
}
|
|
1637
|
+
break;
|
|
1638
|
+
case 7 /* T.BeforeAttrValue */:
|
|
1639
|
+
case 10 /* T.ValueUq */:
|
|
1640
|
+
context = { kind: "unquoted", tag, attr };
|
|
1641
|
+
state = 10 /* T.ValueUq */;
|
|
1642
|
+
if (attr === "type")
|
|
1643
|
+
typeValue += HOLE_CHAR;
|
|
1644
|
+
break;
|
|
1645
|
+
case 13 /* T.Comment */:
|
|
1646
|
+
context = { kind: "comment" };
|
|
1647
|
+
break;
|
|
1648
|
+
case 14 /* T.Bogus */:
|
|
1649
|
+
context = { kind: "name" };
|
|
1650
|
+
break;
|
|
1651
|
+
case 1 /* T.TagOpen */:
|
|
1652
|
+
case 2 /* T.EndTagOpen */:
|
|
1653
|
+
// `<${tag}`: the hole is the tag name.
|
|
1654
|
+
context = { kind: "name" };
|
|
1655
|
+
isEnd = state === 2 /* T.EndTagOpen */;
|
|
1656
|
+
tag = HOLE_CHAR;
|
|
1657
|
+
sawTag = true;
|
|
1658
|
+
state = 3 /* T.TagName */;
|
|
1659
|
+
break;
|
|
1660
|
+
case 3 /* T.TagName */:
|
|
1661
|
+
context = { kind: "name" };
|
|
1662
|
+
tag += HOLE_CHAR;
|
|
1663
|
+
break;
|
|
1664
|
+
case 5 /* T.AttrName */:
|
|
1665
|
+
context = { kind: "name" };
|
|
1666
|
+
attr += HOLE_CHAR;
|
|
1667
|
+
break;
|
|
1668
|
+
default:
|
|
1669
|
+
// Where an attribute name goes: the hole starts one.
|
|
1670
|
+
context = { kind: "name" };
|
|
1671
|
+
attr = HOLE_CHAR;
|
|
1672
|
+
state = 5 /* T.AttrName */;
|
|
1673
|
+
break;
|
|
1674
|
+
}
|
|
1675
|
+
holes.push(context);
|
|
1676
|
+
if (throwsAt === -1 && throwsOn(context))
|
|
1677
|
+
throwsAt = holes.length;
|
|
1678
|
+
}
|
|
1679
|
+
if (throwsAt === -1 && state !== 0 /* T.Data */)
|
|
1680
|
+
throwsAt = 0;
|
|
1681
|
+
return { holes, throwsAt };
|
|
1682
|
+
}
|
|
1683
|
+
/** Whether html`...` would throw for a template with these static parts (see scanTemplateHtml). */
|
|
1684
|
+
export const htmlWouldThrow = (parts) => scanTemplateHtml(parts).throwsAt !== -1;
|
|
1685
|
+
//# sourceMappingURL=server-script-templates.js.map
|