@fluixi/ts-plugin 0.1.0-alpha.8 → 1.0.0-alpha.99
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/LICENSE +1 -1
- package/README.md +45 -14
- package/dist/auto-import.d.ts +43 -0
- package/dist/auto-import.d.ts.map +1 -0
- package/dist/cache.d.ts +29 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/collect.d.ts +33 -0
- package/dist/collect.d.ts.map +1 -0
- package/dist/completion.d.ts +11 -0
- package/dist/completion.d.ts.map +1 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/elements.d.ts +21 -0
- package/dist/elements.d.ts.map +1 -0
- package/dist/fixes.d.ts +19 -0
- package/dist/fixes.d.ts.map +1 -0
- package/dist/hover.d.ts +11 -0
- package/dist/hover.d.ts.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1231 -385
- package/dist/infer.d.ts +45 -0
- package/dist/infer.d.ts.map +1 -0
- package/dist/navigation.d.ts +48 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/pieces.d.ts +20 -0
- package/dist/pieces.d.ts.map +1 -0
- package/dist/props.d.ts +19 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/rules.d.ts +49 -0
- package/dist/rules.d.ts.map +1 -0
- package/dist/rules.js +417 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +16 -7
package/dist/index.js
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
// ../template-parser/dist/index.mjs
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
for (
|
|
4
|
+
function setParents(root) {
|
|
5
|
+
const visit = (node, parent) => {
|
|
6
|
+
node.parent = parent;
|
|
7
|
+
const kids = childrenOf(node);
|
|
8
|
+
for (const child of kids) visit(child, node);
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
visit(root, null);
|
|
11
|
+
return root;
|
|
11
12
|
}
|
|
12
|
-
function
|
|
13
|
-
switch (
|
|
13
|
+
function childrenOf(node) {
|
|
14
|
+
switch (node.kind) {
|
|
14
15
|
case "Root":
|
|
15
16
|
case "Element":
|
|
16
17
|
case "Component":
|
|
17
18
|
case "Fragment":
|
|
18
|
-
return
|
|
19
|
+
return node.children;
|
|
19
20
|
default:
|
|
20
21
|
return [];
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
var Reader = class {
|
|
25
|
+
// offset within the current static piece
|
|
26
|
+
constructor(pieces) {
|
|
27
|
+
this.pieces = pieces;
|
|
26
28
|
this.pi = 0;
|
|
27
29
|
this.oi = 0;
|
|
28
30
|
this.normalize();
|
|
29
31
|
}
|
|
32
|
+
/** Skip fully-consumed static pieces so `current()` is live text or a hole. */
|
|
30
33
|
normalize() {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (
|
|
34
|
-
this.pi
|
|
34
|
+
while (this.pi < this.pieces.length) {
|
|
35
|
+
const p = this.pieces[this.pi];
|
|
36
|
+
if (p.kind === "static" && this.oi >= p.text.length) {
|
|
37
|
+
this.pi++;
|
|
38
|
+
this.oi = 0;
|
|
35
39
|
continue;
|
|
36
40
|
}
|
|
37
41
|
break;
|
|
@@ -40,174 +44,449 @@ var m = class {
|
|
|
40
44
|
eof() {
|
|
41
45
|
return this.pi >= this.pieces.length;
|
|
42
46
|
}
|
|
47
|
+
/** Absolute source offset of the cursor. */
|
|
43
48
|
pos() {
|
|
44
49
|
if (this.eof()) {
|
|
45
|
-
|
|
46
|
-
return
|
|
50
|
+
const last = this.pieces[this.pieces.length - 1];
|
|
51
|
+
return last ? last.kind === "static" ? last.start + last.text.length : last.end : 0;
|
|
47
52
|
}
|
|
48
|
-
|
|
49
|
-
return
|
|
53
|
+
const p = this.pieces[this.pi];
|
|
54
|
+
return p.kind === "static" ? p.start + this.oi : p.start;
|
|
50
55
|
}
|
|
56
|
+
/** True when the cursor sits exactly on a hole marker. */
|
|
51
57
|
atHole() {
|
|
52
58
|
return !this.eof() && this.pieces[this.pi].kind === "hole";
|
|
53
59
|
}
|
|
60
|
+
/** True when advancing one char would land on a hole (e.g. the `<` in `<${Tag}`). */
|
|
54
61
|
holeFollows() {
|
|
55
62
|
if (this.eof()) return false;
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
const p = this.pieces[this.pi];
|
|
64
|
+
if (p.kind !== "static") return false;
|
|
65
|
+
if (this.oi + 1 < p.text.length) return false;
|
|
66
|
+
const next = this.pieces[this.pi + 1];
|
|
67
|
+
return next !== void 0 && next.kind === "hole";
|
|
60
68
|
}
|
|
69
|
+
/** Consume the hole at the cursor. Caller must have checked {@link atHole}. */
|
|
61
70
|
takeHole() {
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
71
|
+
const p = this.pieces[this.pi];
|
|
72
|
+
if (p.kind !== "hole") throw new Error("takeHole called off a hole");
|
|
73
|
+
this.pi++;
|
|
74
|
+
this.oi = 0;
|
|
75
|
+
this.normalize();
|
|
76
|
+
return { index: p.index, loc: { start: p.start, end: p.end } };
|
|
65
77
|
}
|
|
66
|
-
|
|
78
|
+
/** Char `k` ahead within the current static segment, or '' at a boundary/hole. */
|
|
79
|
+
peek(k = 0) {
|
|
67
80
|
if (this.eof()) return "";
|
|
68
|
-
|
|
69
|
-
|
|
81
|
+
const p = this.pieces[this.pi];
|
|
82
|
+
if (p.kind !== "static") return "";
|
|
83
|
+
return p.text[this.oi + k] ?? "";
|
|
70
84
|
}
|
|
71
|
-
|
|
85
|
+
/** Whether the current static segment starts with `s` at the cursor. */
|
|
86
|
+
startsWith(s) {
|
|
72
87
|
if (this.eof()) return false;
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
const p = this.pieces[this.pi];
|
|
89
|
+
if (p.kind !== "static") return false;
|
|
90
|
+
return p.text.startsWith(s, this.oi);
|
|
75
91
|
}
|
|
92
|
+
/** Advance one character inside the current static segment. */
|
|
76
93
|
advance() {
|
|
77
94
|
if (this.eof()) return "";
|
|
78
|
-
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
const p = this.pieces[this.pi];
|
|
96
|
+
if (p.kind !== "static") return "";
|
|
97
|
+
const ch = p.text[this.oi++] ?? "";
|
|
98
|
+
this.normalize();
|
|
99
|
+
return ch;
|
|
100
|
+
}
|
|
101
|
+
/** Advance while `pred` holds and we stay inside one static segment. */
|
|
102
|
+
readWhile(pred) {
|
|
103
|
+
let out = "";
|
|
104
|
+
while (!this.eof() && !this.atHole()) {
|
|
105
|
+
const ch = this.peek();
|
|
106
|
+
if (ch === "" || !pred(ch)) break;
|
|
107
|
+
out += ch;
|
|
108
|
+
this.oi++;
|
|
89
109
|
}
|
|
90
|
-
|
|
110
|
+
this.normalize();
|
|
111
|
+
return out;
|
|
91
112
|
}
|
|
92
113
|
skipWhitespace() {
|
|
93
|
-
this.readWhile((
|
|
114
|
+
this.readWhile((ch) => /\s/.test(ch));
|
|
94
115
|
}
|
|
95
|
-
span(
|
|
96
|
-
return { start
|
|
116
|
+
span(start) {
|
|
117
|
+
return { start, end: this.pos() };
|
|
97
118
|
}
|
|
98
119
|
};
|
|
99
|
-
var
|
|
100
|
-
function
|
|
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
|
-
|
|
120
|
+
var EVENT_MODIFIERS = /* @__PURE__ */ new Set(["capture", "once", "passive", "prevent", "stop", "self"]);
|
|
121
|
+
function requireHole(raw, what) {
|
|
122
|
+
if (raw.value && raw.value.kind === "hole") return raw.value.hole;
|
|
123
|
+
raw.report("invalid-binding", `${what} requires an expression value: ${raw.name}=\${\u2026}`);
|
|
124
|
+
return -1;
|
|
125
|
+
}
|
|
126
|
+
var attribute = (raw, boolean) => ({
|
|
127
|
+
kind: "Attribute",
|
|
128
|
+
name: boolean ? raw.name.slice(1) : raw.name,
|
|
129
|
+
value: raw.value,
|
|
130
|
+
boolean,
|
|
131
|
+
loc: raw.loc
|
|
132
|
+
});
|
|
133
|
+
var CORE_DIRECTIVES = [
|
|
134
|
+
// Events: `@click=${h}`, `on:click=${h}` (native), `onClick=${h}`, each with
|
|
135
|
+
// optional dotted modifiers (`@click.capture.once`, `on:input.passive`).
|
|
136
|
+
{
|
|
137
|
+
id: "event",
|
|
138
|
+
match: (n) => n[0] === "@" || n.startsWith("on:") || /^on[A-Z]/.test(n),
|
|
139
|
+
build: (raw) => {
|
|
140
|
+
let syntax;
|
|
141
|
+
let rest;
|
|
142
|
+
if (raw.name[0] === "@") {
|
|
143
|
+
syntax = "at";
|
|
144
|
+
rest = raw.name.slice(1);
|
|
145
|
+
} else if (raw.name.startsWith("on:")) {
|
|
146
|
+
syntax = "colon";
|
|
147
|
+
rest = raw.name.slice(3);
|
|
148
|
+
} else {
|
|
149
|
+
syntax = "on";
|
|
150
|
+
rest = raw.name.slice(2);
|
|
151
|
+
}
|
|
152
|
+
const [raw_name, ...modifiers] = rest.split(".");
|
|
153
|
+
const name = raw_name.toLowerCase();
|
|
154
|
+
for (const m of modifiers) {
|
|
155
|
+
if (!EVENT_MODIFIERS.has(m)) {
|
|
156
|
+
raw.report("invalid-directive", `Unknown event modifier ".${m}" \u2014 expected one of ${[...EVENT_MODIFIERS].join(", ")}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
kind: "EventBinding",
|
|
161
|
+
name,
|
|
162
|
+
syntax,
|
|
163
|
+
modifiers,
|
|
164
|
+
hole: requireHole(raw, "event binding"),
|
|
165
|
+
loc: raw.loc
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
// Two-way binding: `bind:value=${signal}`.
|
|
170
|
+
{
|
|
171
|
+
id: "bind",
|
|
172
|
+
elementOnly: true,
|
|
173
|
+
match: (n) => n.startsWith("bind:"),
|
|
174
|
+
build: (raw) => ({
|
|
175
|
+
kind: "BindDirective",
|
|
176
|
+
name: raw.name.slice("bind:".length),
|
|
177
|
+
hole: requireHole(raw, "two-way binding"),
|
|
178
|
+
loc: raw.loc
|
|
179
|
+
})
|
|
180
|
+
},
|
|
181
|
+
// Dynamic class toggle: `class:active=${flag}`.
|
|
182
|
+
{
|
|
183
|
+
id: "class-directive",
|
|
184
|
+
elementOnly: true,
|
|
185
|
+
match: (n) => n.startsWith("class:"),
|
|
186
|
+
build: (raw) => ({
|
|
187
|
+
kind: "ClassDirective",
|
|
188
|
+
name: raw.name.slice("class:".length),
|
|
189
|
+
hole: requireHole(raw, "class directive"),
|
|
190
|
+
loc: raw.loc
|
|
191
|
+
})
|
|
192
|
+
},
|
|
193
|
+
// Dynamic style: `style:color=${c}`.
|
|
194
|
+
{
|
|
195
|
+
id: "style-directive",
|
|
196
|
+
elementOnly: true,
|
|
197
|
+
match: (n) => n.startsWith("style:"),
|
|
198
|
+
build: (raw) => ({
|
|
199
|
+
kind: "StyleDirective",
|
|
200
|
+
name: raw.name.slice("style:".length),
|
|
201
|
+
hole: requireHole(raw, "style directive"),
|
|
202
|
+
loc: raw.loc
|
|
203
|
+
})
|
|
204
|
+
},
|
|
205
|
+
// Load strategy: `load:visible[="200px"]`. Components only, an intrinsic element
|
|
206
|
+
// has no module to defer.
|
|
207
|
+
{
|
|
208
|
+
id: "load",
|
|
209
|
+
match: (n) => n.startsWith("load:"),
|
|
210
|
+
build: (raw) => ({
|
|
211
|
+
kind: "LoadDirective",
|
|
212
|
+
strategy: raw.name.slice("load:".length),
|
|
213
|
+
modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
|
|
214
|
+
loc: raw.loc
|
|
215
|
+
})
|
|
216
|
+
},
|
|
217
|
+
// Custom directive: `use=${d}` or `use:tooltip[=${opts}]`.
|
|
218
|
+
{
|
|
219
|
+
id: "use",
|
|
220
|
+
match: (n) => n === "use" || n.startsWith("use:"),
|
|
221
|
+
build: (raw) => {
|
|
222
|
+
const named = raw.name.startsWith("use:");
|
|
223
|
+
return {
|
|
224
|
+
kind: "UseDirective",
|
|
225
|
+
name: named ? raw.name.slice("use:".length) : null,
|
|
226
|
+
hole: raw.value && raw.value.kind === "hole" ? raw.value.hole : null,
|
|
227
|
+
loc: raw.loc
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
// Conditional rendering: `if=${cond}`.
|
|
232
|
+
{
|
|
233
|
+
id: "if",
|
|
234
|
+
elementOnly: true,
|
|
235
|
+
match: (n) => n === "if",
|
|
236
|
+
build: (raw) => ({
|
|
237
|
+
kind: "IfDirective",
|
|
238
|
+
hole: requireHole(raw, "if directive"),
|
|
239
|
+
loc: raw.loc
|
|
240
|
+
})
|
|
241
|
+
},
|
|
242
|
+
// Loop rendering: `each=${items}` (a sibling `key` folds in at parse time).
|
|
243
|
+
{
|
|
244
|
+
id: "each",
|
|
245
|
+
elementOnly: true,
|
|
246
|
+
match: (n) => n === "each",
|
|
247
|
+
build: (raw) => ({
|
|
248
|
+
kind: "EachDirective",
|
|
249
|
+
hole: requireHole(raw, "each directive"),
|
|
250
|
+
key: null,
|
|
251
|
+
loc: raw.loc
|
|
252
|
+
})
|
|
253
|
+
},
|
|
254
|
+
// `else` (valueless).
|
|
255
|
+
{
|
|
256
|
+
id: "else",
|
|
257
|
+
elementOnly: true,
|
|
258
|
+
match: (n) => n === "else",
|
|
259
|
+
build: (raw) => {
|
|
260
|
+
if (raw.value !== null) raw.report("invalid-directive", "`else` takes no value");
|
|
261
|
+
return { kind: "ElseDirective", loc: raw.loc };
|
|
150
262
|
}
|
|
263
|
+
},
|
|
264
|
+
// Ref binding: `ref=${el}`.
|
|
265
|
+
{
|
|
266
|
+
id: "ref",
|
|
267
|
+
match: (n) => n === "ref",
|
|
268
|
+
build: (raw) => ({
|
|
269
|
+
kind: "RefBinding",
|
|
270
|
+
hole: requireHole(raw, "ref binding"),
|
|
271
|
+
loc: raw.loc
|
|
272
|
+
})
|
|
273
|
+
},
|
|
274
|
+
// Property binding: `.currentTime=${t}`.
|
|
275
|
+
{
|
|
276
|
+
id: "property",
|
|
277
|
+
match: (n) => n[0] === ".",
|
|
278
|
+
build: (raw) => ({
|
|
279
|
+
kind: "PropertyBinding",
|
|
280
|
+
name: raw.name.slice(1),
|
|
281
|
+
hole: requireHole(raw, "property binding"),
|
|
282
|
+
loc: raw.loc
|
|
283
|
+
})
|
|
284
|
+
},
|
|
285
|
+
// Boolean attribute (lit `?disabled`): a plain attribute flagged boolean.
|
|
286
|
+
{
|
|
287
|
+
id: "boolean",
|
|
288
|
+
match: (n) => n[0] === "?",
|
|
289
|
+
build: (raw) => attribute(raw, true)
|
|
290
|
+
}
|
|
291
|
+
];
|
|
292
|
+
function resolveAttribute(raw, directives = CORE_DIRECTIVES, ctx = { component: false }) {
|
|
293
|
+
for (const d of directives) {
|
|
294
|
+
if (d.elementOnly && ctx.component) continue;
|
|
295
|
+
if (d.match(raw.name)) return d.build(raw);
|
|
296
|
+
}
|
|
297
|
+
return attribute(raw, false);
|
|
298
|
+
}
|
|
299
|
+
var SVG_TAGS = /* @__PURE__ */ new Set([
|
|
300
|
+
"svg",
|
|
301
|
+
"path",
|
|
302
|
+
"circle",
|
|
303
|
+
"rect",
|
|
304
|
+
"line",
|
|
305
|
+
"polygon",
|
|
306
|
+
"polyline",
|
|
307
|
+
"ellipse",
|
|
308
|
+
"g",
|
|
309
|
+
"defs",
|
|
310
|
+
"clipPath",
|
|
311
|
+
"text"
|
|
312
|
+
]);
|
|
313
|
+
var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
|
|
314
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
315
|
+
"area",
|
|
316
|
+
"base",
|
|
317
|
+
"br",
|
|
318
|
+
"col",
|
|
319
|
+
"embed",
|
|
320
|
+
"hr",
|
|
321
|
+
"img",
|
|
322
|
+
"input",
|
|
323
|
+
"link",
|
|
324
|
+
"meta",
|
|
325
|
+
"param",
|
|
326
|
+
"source",
|
|
327
|
+
"track",
|
|
328
|
+
"wbr"
|
|
329
|
+
]);
|
|
330
|
+
function isComponentTag(tag) {
|
|
331
|
+
return tag.length > 0 && (tag[0] !== tag[0].toLowerCase() || tag.includes("."));
|
|
332
|
+
}
|
|
333
|
+
var INTERACTIVE = /* @__PURE__ */ new Set([
|
|
334
|
+
"a",
|
|
335
|
+
"button",
|
|
336
|
+
"input",
|
|
337
|
+
"select",
|
|
338
|
+
"textarea",
|
|
339
|
+
"summary",
|
|
340
|
+
"details",
|
|
341
|
+
"option",
|
|
342
|
+
"label"
|
|
343
|
+
]);
|
|
344
|
+
var KEY_EVENTS = /* @__PURE__ */ new Set(["keydown", "keyup", "keypress"]);
|
|
345
|
+
function attributeNamed(node, name) {
|
|
346
|
+
return node.attributes.find((a) => "name" in a && a.name === name);
|
|
347
|
+
}
|
|
348
|
+
function hasSpread(node) {
|
|
349
|
+
return node.attributes.some((a) => a.kind === "Spread");
|
|
350
|
+
}
|
|
351
|
+
function staticValue(node, name) {
|
|
352
|
+
const attribute2 = attributeNamed(node, name);
|
|
353
|
+
if (!attribute2 || attribute2.kind !== "Attribute" || !attribute2.value) return void 0;
|
|
354
|
+
const value = attribute2.value;
|
|
355
|
+
return value.kind === "static" ? value.value : void 0;
|
|
356
|
+
}
|
|
357
|
+
function isPresentational(node) {
|
|
358
|
+
const role = staticValue(node, "role");
|
|
359
|
+
if (role === "presentation" || role === "none") return true;
|
|
360
|
+
return staticValue(node, "aria-hidden") === "true";
|
|
361
|
+
}
|
|
362
|
+
function hasEvent(node, predicate) {
|
|
363
|
+
return node.attributes.some((a) => a.kind === "EventBinding" && predicate(a.name));
|
|
364
|
+
}
|
|
365
|
+
function checkAccessibility(root) {
|
|
366
|
+
const out = [];
|
|
367
|
+
const report = (node, code, message) => {
|
|
368
|
+
out.push({ code, message, severity: "warning", loc: node.loc });
|
|
151
369
|
};
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
370
|
+
const visit = (nodes) => {
|
|
371
|
+
for (const node of nodes) {
|
|
372
|
+
if (node.kind === "Element") check(node, report);
|
|
373
|
+
const children = "children" in node ? node.children : void 0;
|
|
374
|
+
if (children) visit(children);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
visit(root.children);
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
function check(node, report) {
|
|
381
|
+
if (hasSpread(node)) return;
|
|
382
|
+
const tag = node.tag.toLowerCase();
|
|
383
|
+
if (tag === "img" && !attributeNamed(node, "alt") && !isPresentational(node)) {
|
|
384
|
+
report(node, "a11y-img-alt", 'An `img` needs an `alt`. Use `alt=""` when the image is decorative.');
|
|
385
|
+
}
|
|
386
|
+
if (tag === "a" && !attributeNamed(node, "href") && !attributeNamed(node, "role")) {
|
|
387
|
+
report(node, "a11y-anchor-href", "An `a` without `href` is not a link. Use a `button` for an action.");
|
|
388
|
+
}
|
|
389
|
+
if (tag === "iframe" && !attributeNamed(node, "title")) {
|
|
390
|
+
report(node, "a11y-iframe-title", "An `iframe` needs a `title` describing its content.");
|
|
391
|
+
}
|
|
392
|
+
const tabindex = staticValue(node, "tabindex") ?? staticValue(node, "tabIndex");
|
|
393
|
+
if (tabindex !== void 0 && Number(tabindex) > 0) {
|
|
394
|
+
report(
|
|
395
|
+
node,
|
|
396
|
+
"a11y-positive-tabindex",
|
|
397
|
+
"A positive `tabindex` reorders tab navigation for the entire page. Use `0`, or restructure the markup."
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (!INTERACTIVE.has(tag) && hasEvent(node, (name) => name === "click") && !attributeNamed(node, "role") && !hasEvent(node, (name) => KEY_EVENTS.has(name))) {
|
|
401
|
+
report(
|
|
402
|
+
node,
|
|
403
|
+
"a11y-click-without-keyboard",
|
|
404
|
+
`A click handler on \`${tag}\` is unreachable by keyboard. Use a \`button\`, or add a \`role\` and a key handler.`
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
var NAME_CHAR = /[A-Za-z0-9\-_:@.?]/;
|
|
409
|
+
var Parser = class {
|
|
410
|
+
constructor(pieces, opts) {
|
|
164
411
|
this.diagnostics = [];
|
|
165
|
-
this.r = new
|
|
412
|
+
this.r = new Reader(pieces);
|
|
413
|
+
this.svg = opts.svg ?? false;
|
|
414
|
+
this.directives = opts.directives ?? CORE_DIRECTIVES;
|
|
415
|
+
this.accessibility = opts.accessibility ?? true;
|
|
166
416
|
}
|
|
167
417
|
parse() {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
418
|
+
const start = this.r.pos();
|
|
419
|
+
const children = [];
|
|
420
|
+
while (!this.r.eof()) {
|
|
421
|
+
children.push(...this.parseChildren());
|
|
422
|
+
if (this.r.eof()) break;
|
|
423
|
+
const s = this.r.pos();
|
|
424
|
+
this.consumeCloseTag();
|
|
425
|
+
this.report("stray-close-tag", "error", "Close tag without a matching open tag", this.r.span(s));
|
|
172
426
|
}
|
|
173
|
-
|
|
174
|
-
|
|
427
|
+
const root = { kind: "Root", children, loc: this.r.span(start) };
|
|
428
|
+
setParents(root);
|
|
429
|
+
this.validateTree(root);
|
|
430
|
+
if (this.accessibility) {
|
|
431
|
+
this.diagnostics.push(...checkAccessibility(root));
|
|
432
|
+
}
|
|
433
|
+
return { root, diagnostics: this.diagnostics };
|
|
175
434
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
435
|
+
/** Cross-node validation that needs the built tree (sibling relationships). */
|
|
436
|
+
validateTree(root) {
|
|
437
|
+
const visit = (nodes) => {
|
|
438
|
+
let prevBranch = null;
|
|
439
|
+
for (const node of nodes) {
|
|
440
|
+
if (node.kind === "Text" && /^\s*$/.test(node.value)) continue;
|
|
441
|
+
if (node.kind === "Element" || node.kind === "Component") {
|
|
442
|
+
const hasElse = node.attributes.some((a) => a.kind === "ElseDirective");
|
|
443
|
+
if (hasElse) {
|
|
444
|
+
const prevHasIf = prevBranch != null && (prevBranch.kind === "Element" || prevBranch.kind === "Component") && prevBranch.attributes.some((a) => a.kind === "IfDirective");
|
|
445
|
+
if (!prevHasIf) {
|
|
446
|
+
this.report("else-without-if", "error", "`else` must immediately follow an element with `if`", node.loc);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
prevBranch = node;
|
|
450
|
+
} else {
|
|
451
|
+
prevBranch = null;
|
|
452
|
+
}
|
|
453
|
+
if ("children" in node && node.children) visit(node.children);
|
|
454
|
+
}
|
|
180
455
|
};
|
|
181
|
-
|
|
456
|
+
visit(root.children);
|
|
182
457
|
}
|
|
458
|
+
// --- content ------------------------------------------------------------
|
|
459
|
+
/** Parse sibling nodes until EOF or a `</` close the caller must consume. */
|
|
183
460
|
parseChildren() {
|
|
184
|
-
|
|
185
|
-
|
|
461
|
+
const out = [];
|
|
462
|
+
while (!this.r.eof()) {
|
|
463
|
+
if (this.r.startsWith("</")) break;
|
|
186
464
|
if (this.r.startsWith("<!--")) {
|
|
187
|
-
|
|
465
|
+
out.push(this.parseComment());
|
|
188
466
|
continue;
|
|
189
467
|
}
|
|
190
468
|
if (this.isFragmentStart()) {
|
|
191
|
-
|
|
469
|
+
out.push(this.parseFragment());
|
|
192
470
|
continue;
|
|
193
471
|
}
|
|
194
472
|
if (this.isDynamicTagStart()) {
|
|
195
|
-
|
|
473
|
+
out.push(this.parseDynamicComponent());
|
|
196
474
|
continue;
|
|
197
475
|
}
|
|
198
476
|
if (this.isTagStart()) {
|
|
199
|
-
|
|
477
|
+
out.push(this.parseElement());
|
|
200
478
|
continue;
|
|
201
479
|
}
|
|
202
480
|
if (this.r.atHole()) {
|
|
203
|
-
|
|
204
|
-
|
|
481
|
+
const h = this.r.takeHole();
|
|
482
|
+
const node = { kind: "Expression", hole: h.index, loc: h.loc };
|
|
483
|
+
out.push(node);
|
|
205
484
|
continue;
|
|
206
485
|
}
|
|
207
|
-
|
|
208
|
-
|
|
486
|
+
const text = this.readText();
|
|
487
|
+
if (text) out.push(text);
|
|
209
488
|
}
|
|
210
|
-
return
|
|
489
|
+
return out;
|
|
211
490
|
}
|
|
212
491
|
isTagStart() {
|
|
213
492
|
return this.r.peek() === "<" && /[A-Za-z]/.test(this.r.peek(1));
|
|
@@ -215,239 +494,306 @@ var N = class {
|
|
|
215
494
|
isFragmentStart() {
|
|
216
495
|
return this.r.peek() === "<" && this.r.peek(1) === ">";
|
|
217
496
|
}
|
|
497
|
+
/** `<${Comp} …>`: a component whose tag is an interpolated expression. */
|
|
218
498
|
isDynamicTagStart() {
|
|
219
499
|
return this.r.peek() === "<" && this.r.holeFollows();
|
|
220
500
|
}
|
|
501
|
+
/** Parse `<${Comp} …/>` or `<${Comp} …>…</${Comp}>` into a dynamic ComponentNode. */
|
|
221
502
|
parseDynamicComponent() {
|
|
222
|
-
|
|
503
|
+
const start = this.r.pos();
|
|
223
504
|
this.r.advance();
|
|
224
|
-
|
|
505
|
+
const tagHole = this.r.takeHole().index;
|
|
506
|
+
const attributes = this.parseAttributes(true);
|
|
225
507
|
this.r.skipWhitespace();
|
|
226
|
-
let
|
|
227
|
-
|
|
508
|
+
let selfClosing = false;
|
|
509
|
+
let children = [];
|
|
510
|
+
if (this.r.startsWith("/>")) {
|
|
511
|
+
this.r.advance();
|
|
512
|
+
this.r.advance();
|
|
513
|
+
selfClosing = true;
|
|
514
|
+
} else {
|
|
515
|
+
if (this.r.peek() === ">") this.r.advance();
|
|
516
|
+
children = this.parseChildren();
|
|
517
|
+
if (this.r.startsWith("</")) {
|
|
518
|
+
this.r.advance();
|
|
519
|
+
this.r.advance();
|
|
520
|
+
if (this.r.atHole()) this.r.takeHole();
|
|
521
|
+
else this.readName();
|
|
522
|
+
this.r.skipWhitespace();
|
|
523
|
+
if (this.r.peek() === ">") this.r.advance();
|
|
524
|
+
} else {
|
|
525
|
+
this.report("unclosed-tag", "error", "Unclosed dynamic component", this.r.span(start));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return { kind: "Component", tag: "", tagHole, attributes, children, selfClosing, loc: this.r.span(start) };
|
|
228
529
|
}
|
|
530
|
+
/** A raw text run (preserved verbatim; whitespace is normalized at lowering). */
|
|
229
531
|
readText() {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
532
|
+
const start = this.r.pos();
|
|
533
|
+
let value = "";
|
|
534
|
+
while (!this.r.eof() && !this.r.atHole()) {
|
|
535
|
+
if (this.r.startsWith("</") || this.r.startsWith("<!--") || this.isTagStart() || this.isFragmentStart() || this.isDynamicTagStart()) break;
|
|
536
|
+
value += this.r.advance();
|
|
537
|
+
}
|
|
538
|
+
if (!value) return null;
|
|
539
|
+
return { kind: "Text", value, raw: false, loc: this.r.span(start) };
|
|
233
540
|
}
|
|
234
541
|
parseComment() {
|
|
235
|
-
|
|
236
|
-
this.r.advance()
|
|
237
|
-
|
|
238
|
-
|
|
542
|
+
const start = this.r.pos();
|
|
543
|
+
this.r.advance();
|
|
544
|
+
this.r.advance();
|
|
545
|
+
this.r.advance();
|
|
546
|
+
this.r.advance();
|
|
547
|
+
let value = "";
|
|
548
|
+
while (!this.r.eof() && !this.r.startsWith("-->")) {
|
|
239
549
|
if (this.r.atHole()) {
|
|
240
|
-
this.r.takeHole()
|
|
550
|
+
this.r.takeHole();
|
|
551
|
+
value += "\0";
|
|
241
552
|
continue;
|
|
242
553
|
}
|
|
243
|
-
|
|
554
|
+
value += this.r.advance();
|
|
244
555
|
}
|
|
245
|
-
|
|
556
|
+
if (this.r.startsWith("-->")) {
|
|
557
|
+
this.r.advance();
|
|
558
|
+
this.r.advance();
|
|
559
|
+
this.r.advance();
|
|
560
|
+
} else {
|
|
561
|
+
this.report("unclosed-comment", "warning", "Unterminated comment", this.r.span(start));
|
|
562
|
+
}
|
|
563
|
+
return { kind: "Comment", value, loc: this.r.span(start) };
|
|
246
564
|
}
|
|
247
565
|
parseFragment() {
|
|
248
|
-
|
|
249
|
-
this.r.advance()
|
|
250
|
-
|
|
566
|
+
const start = this.r.pos();
|
|
567
|
+
this.r.advance();
|
|
568
|
+
this.r.advance();
|
|
569
|
+
const children = this.parseChildren();
|
|
251
570
|
if (this.r.startsWith("</")) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
} else
|
|
255
|
-
|
|
571
|
+
const name = this.consumeCloseTag();
|
|
572
|
+
if (name) this.report("mismatched-close-tag", "error", `Expected </> to close fragment, got </${name}>`, this.r.span(start));
|
|
573
|
+
} else {
|
|
574
|
+
this.report("unclosed-tag", "error", "Unterminated fragment", this.r.span(start));
|
|
575
|
+
}
|
|
576
|
+
return { kind: "Fragment", children, loc: this.r.span(start) };
|
|
256
577
|
}
|
|
257
578
|
parseElement() {
|
|
258
|
-
|
|
579
|
+
const start = this.r.pos();
|
|
259
580
|
this.r.advance();
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
let
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
581
|
+
const tag = this.readName();
|
|
582
|
+
const component = isComponentTag(tag);
|
|
583
|
+
const attributes = this.parseAttributes(component);
|
|
584
|
+
this.foldEachKey(attributes, start);
|
|
585
|
+
this.r.skipWhitespace();
|
|
586
|
+
const lower = tag.toLowerCase();
|
|
587
|
+
const rawText = !component && RAW_TEXT_ELEMENTS.has(lower);
|
|
588
|
+
const isVoid = !component && VOID_ELEMENTS.has(lower);
|
|
589
|
+
let selfClosing = false;
|
|
590
|
+
let children = [];
|
|
591
|
+
if (this.r.startsWith("/>")) {
|
|
592
|
+
this.r.advance();
|
|
593
|
+
this.r.advance();
|
|
594
|
+
selfClosing = true;
|
|
595
|
+
} else {
|
|
596
|
+
if (this.r.peek() === ">") this.r.advance();
|
|
597
|
+
if (!isVoid) {
|
|
598
|
+
children = rawText ? this.parseRawText(tag) : this.parseChildren();
|
|
599
|
+
if (this.r.startsWith("</")) {
|
|
600
|
+
const name = this.consumeCloseTag();
|
|
601
|
+
if (name && name !== tag) {
|
|
602
|
+
this.report("mismatched-close-tag", "error", `Expected </${tag}>, got </${name}>`, this.r.span(start));
|
|
603
|
+
}
|
|
604
|
+
} else {
|
|
605
|
+
this.report("unclosed-tag", "error", `Unclosed <${tag}>`, this.r.span(start));
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const loc = this.r.span(start);
|
|
610
|
+
if (component) {
|
|
611
|
+
return { kind: "Component", tag, tagHole: null, attributes, children, selfClosing, loc };
|
|
612
|
+
}
|
|
613
|
+
const namespace = this.svg || SVG_TAGS.has(tag) ? "svg" : "html";
|
|
614
|
+
return { kind: "Element", tag, namespace, attributes, children, selfClosing, rawText, loc };
|
|
615
|
+
}
|
|
616
|
+
/** Read a raw-text element body: only holes break the text run. */
|
|
617
|
+
parseRawText(tag) {
|
|
618
|
+
const out = [];
|
|
619
|
+
const close = "</" + tag.toLowerCase();
|
|
620
|
+
let text = "";
|
|
621
|
+
let start = this.r.pos();
|
|
622
|
+
const flush = () => {
|
|
623
|
+
if (text) out.push({ kind: "Text", value: text, raw: true, loc: this.r.span(start) });
|
|
624
|
+
text = "";
|
|
276
625
|
};
|
|
277
|
-
|
|
626
|
+
while (!this.r.eof()) {
|
|
627
|
+
if (this.startsWithCloseFor(close)) break;
|
|
278
628
|
if (this.r.atHole()) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
629
|
+
flush();
|
|
630
|
+
const h = this.r.takeHole();
|
|
631
|
+
out.push({ kind: "Expression", hole: h.index, loc: h.loc });
|
|
632
|
+
start = this.r.pos();
|
|
282
633
|
continue;
|
|
283
634
|
}
|
|
284
|
-
|
|
635
|
+
if (!text) start = this.r.pos();
|
|
636
|
+
text += this.r.advance();
|
|
285
637
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
638
|
+
flush();
|
|
639
|
+
return out;
|
|
640
|
+
}
|
|
641
|
+
startsWithCloseFor(close) {
|
|
642
|
+
let i = 0;
|
|
643
|
+
while (i < close.length) {
|
|
644
|
+
const ch = this.r.peek(i);
|
|
645
|
+
if (ch === "" || ch.toLowerCase() !== close[i]) return false;
|
|
646
|
+
i++;
|
|
294
647
|
}
|
|
295
648
|
return true;
|
|
296
649
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
650
|
+
// --- attributes ---------------------------------------------------------
|
|
651
|
+
parseAttributes(component) {
|
|
652
|
+
const props = [];
|
|
653
|
+
while (!this.r.eof()) {
|
|
654
|
+
this.r.skipWhitespace();
|
|
655
|
+
if (this.r.peek() === ">" || this.r.startsWith("/>")) break;
|
|
300
656
|
if (this.r.atHole()) {
|
|
301
|
-
|
|
302
|
-
|
|
657
|
+
const h = this.r.takeHole();
|
|
658
|
+
props.push({ kind: "Spread", hole: h.index, loc: h.loc });
|
|
303
659
|
continue;
|
|
304
660
|
}
|
|
305
661
|
if (this.r.startsWith("...")) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
662
|
+
const s = this.r.pos();
|
|
663
|
+
this.r.advance();
|
|
664
|
+
this.r.advance();
|
|
665
|
+
this.r.advance();
|
|
666
|
+
if (this.r.atHole()) {
|
|
667
|
+
const h = this.r.takeHole();
|
|
668
|
+
props.push({ kind: "Spread", hole: h.index, loc: this.r.span(s) });
|
|
669
|
+
} else {
|
|
670
|
+
this.report("invalid-binding", "error", "Spread `...` must be followed by ${\u2026}", this.r.span(s));
|
|
671
|
+
}
|
|
311
672
|
continue;
|
|
312
673
|
}
|
|
313
|
-
|
|
314
|
-
|
|
674
|
+
const nameStart = this.r.pos();
|
|
675
|
+
const name = this.readName();
|
|
676
|
+
if (!name) {
|
|
315
677
|
this.r.advance();
|
|
316
678
|
continue;
|
|
317
679
|
}
|
|
318
|
-
|
|
680
|
+
const nameLoc = this.r.span(nameStart);
|
|
319
681
|
this.r.skipWhitespace();
|
|
320
|
-
let
|
|
321
|
-
this.r.peek() === "="
|
|
322
|
-
|
|
323
|
-
|
|
682
|
+
let value = null;
|
|
683
|
+
if (this.r.peek() === "=") {
|
|
684
|
+
this.r.advance();
|
|
685
|
+
this.r.skipWhitespace();
|
|
686
|
+
value = this.parseAttrValue();
|
|
687
|
+
}
|
|
688
|
+
const raw = {
|
|
689
|
+
name,
|
|
690
|
+
nameLoc,
|
|
691
|
+
value,
|
|
692
|
+
loc: this.r.span(nameStart),
|
|
693
|
+
report: (code, message) => this.report(code, "error", message, this.r.span(nameStart))
|
|
694
|
+
};
|
|
695
|
+
props.push(resolveAttribute(raw, this.directives, { component }));
|
|
324
696
|
}
|
|
325
|
-
return
|
|
697
|
+
return props;
|
|
326
698
|
}
|
|
327
699
|
parseAttrValue() {
|
|
328
|
-
if (this.r.atHole())
|
|
329
|
-
|
|
330
|
-
|
|
700
|
+
if (this.r.atHole()) {
|
|
701
|
+
const h = this.r.takeHole();
|
|
702
|
+
return { kind: "hole", hole: h.index };
|
|
703
|
+
}
|
|
704
|
+
const quote = this.r.peek();
|
|
705
|
+
if (quote === '"' || quote === "'") {
|
|
706
|
+
return this.classifyParts(this.readQuotedParts(quote));
|
|
707
|
+
}
|
|
708
|
+
const value = this.r.readWhile((ch) => !/[\s>]/.test(ch));
|
|
709
|
+
return { kind: "static", value };
|
|
331
710
|
}
|
|
332
|
-
readQuotedParts(
|
|
711
|
+
readQuotedParts(quote) {
|
|
333
712
|
this.r.advance();
|
|
334
|
-
|
|
335
|
-
|
|
713
|
+
const parts = [];
|
|
714
|
+
let text = "";
|
|
715
|
+
while (!this.r.eof() && this.r.peek() !== quote) {
|
|
336
716
|
if (this.r.atHole()) {
|
|
337
|
-
|
|
717
|
+
if (text) {
|
|
718
|
+
parts.push({ text });
|
|
719
|
+
text = "";
|
|
720
|
+
}
|
|
721
|
+
parts.push({ hole: this.r.takeHole().index });
|
|
338
722
|
continue;
|
|
339
723
|
}
|
|
340
|
-
|
|
341
|
-
if (
|
|
342
|
-
|
|
724
|
+
const ch = this.r.advance();
|
|
725
|
+
if (ch === "") break;
|
|
726
|
+
text += ch;
|
|
343
727
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
728
|
+
if (text) parts.push({ text });
|
|
729
|
+
if (this.r.peek() === quote) this.r.advance();
|
|
730
|
+
return parts;
|
|
731
|
+
}
|
|
732
|
+
classifyParts(parts) {
|
|
733
|
+
const holes = parts.filter((p) => "hole" in p);
|
|
734
|
+
if (holes.length === 0) {
|
|
735
|
+
return { kind: "static", value: parts.map((p) => "text" in p ? p.text : "").join("") };
|
|
736
|
+
}
|
|
737
|
+
if (parts.length === 1) return { kind: "hole", hole: holes[0].hole };
|
|
738
|
+
return { kind: "mixed", parts };
|
|
739
|
+
}
|
|
740
|
+
/** Fold `key` into `each`, and validate an element's directive combination. */
|
|
741
|
+
foldEachKey(attrs, elLoc) {
|
|
742
|
+
let each = null;
|
|
743
|
+
let ifCount = 0;
|
|
744
|
+
let eachCount = 0;
|
|
745
|
+
let elseCount = 0;
|
|
746
|
+
let refCount = 0;
|
|
747
|
+
for (const a of attrs) {
|
|
748
|
+
if (a.kind === "EachDirective") {
|
|
749
|
+
each = a;
|
|
750
|
+
eachCount++;
|
|
751
|
+
} else if (a.kind === "IfDirective") ifCount++;
|
|
752
|
+
else if (a.kind === "ElseDirective") elseCount++;
|
|
753
|
+
else if (a.kind === "RefBinding") refCount++;
|
|
754
|
+
}
|
|
755
|
+
const span = this.r.span(elLoc);
|
|
756
|
+
if (eachCount > 1) this.report("duplicate-directive", "error", "Multiple `each` directives on one element", span);
|
|
757
|
+
if (ifCount > 1) this.report("duplicate-directive", "error", "Multiple `if` directives on one element", span);
|
|
758
|
+
if (refCount > 1) this.report("duplicate-directive", "error", "Multiple `ref` bindings on one element", span);
|
|
759
|
+
if (ifCount > 0 && eachCount > 0) {
|
|
760
|
+
this.report("unsupported-combination", "error", "`if` and `each` cannot both apply to one element \u2014 wrap one in another element", span);
|
|
761
|
+
}
|
|
762
|
+
if (ifCount > 0 && elseCount > 0) {
|
|
763
|
+
this.report("invalid-directive", "error", "`if` and `else` cannot be on the same element", span);
|
|
764
|
+
}
|
|
765
|
+
const keyIdx = attrs.findIndex((a) => a.kind === "Attribute" && a.name === "key");
|
|
766
|
+
if (!each) {
|
|
767
|
+
if (keyIdx !== -1) this.report("invalid-directive", "error", "`key` requires an `each` directive on the same element", span);
|
|
358
768
|
return;
|
|
359
769
|
}
|
|
360
|
-
if (
|
|
361
|
-
|
|
362
|
-
|
|
770
|
+
if (keyIdx === -1) return;
|
|
771
|
+
const key = attrs[keyIdx];
|
|
772
|
+
if (key.kind === "Attribute" && key.value) {
|
|
773
|
+
if (key.value.kind === "static") each.key = { static: key.value.value };
|
|
774
|
+
else if (key.value.kind === "hole") each.key = { hole: key.value.hole };
|
|
775
|
+
}
|
|
776
|
+
attrs.splice(keyIdx, 1);
|
|
363
777
|
}
|
|
778
|
+
// --- low level ----------------------------------------------------------
|
|
364
779
|
readName() {
|
|
365
|
-
return this.r.readWhile((
|
|
780
|
+
return this.r.readWhile((ch) => NAME_CHAR.test(ch));
|
|
366
781
|
}
|
|
782
|
+
/** Consume `</name>` (or `</>`), returning the close-tag name (may be ''). */
|
|
367
783
|
consumeCloseTag() {
|
|
368
|
-
this.r.advance()
|
|
369
|
-
|
|
370
|
-
|
|
784
|
+
this.r.advance();
|
|
785
|
+
this.r.advance();
|
|
786
|
+
const name = this.readName();
|
|
787
|
+
this.r.skipWhitespace();
|
|
788
|
+
if (this.r.peek() === ">") this.r.advance();
|
|
789
|
+
return name;
|
|
371
790
|
}
|
|
372
|
-
report(
|
|
373
|
-
this.diagnostics.push({ code
|
|
791
|
+
report(code, severity, message, loc) {
|
|
792
|
+
this.diagnostics.push({ code, severity, message, loc });
|
|
374
793
|
}
|
|
375
794
|
};
|
|
376
|
-
function
|
|
377
|
-
return new
|
|
378
|
-
}
|
|
379
|
-
function I(e) {
|
|
380
|
-
let t = [], i2 = 0;
|
|
381
|
-
return e.forEach((n, r) => {
|
|
382
|
-
t.push({ kind: "static", text: n, start: i2 }), i2 += n.length, r < e.length - 1 && t.push({ kind: "hole", index: r, start: i2, end: i2 });
|
|
383
|
-
}), t;
|
|
384
|
-
}
|
|
385
|
-
function Q(e, t = {}) {
|
|
386
|
-
return b(I(e), t);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// src/collect.ts
|
|
390
|
-
var UNUSED_CODES = /* @__PURE__ */ new Set([6133, 6196, 6198, 6199, 6205, 6138, 6192]);
|
|
391
|
-
function isUnusedCode(code) {
|
|
392
|
-
return UNUSED_CODES.has(code);
|
|
393
|
-
}
|
|
394
|
-
function usedComponentTags(ts, sourceFile) {
|
|
395
|
-
const tags = /* @__PURE__ */ new Set();
|
|
396
|
-
const visit = (node) => {
|
|
397
|
-
if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && (node.tag.text === "html" || node.tag.text === "svg")) {
|
|
398
|
-
collectTags(Q(templateStatics(ts, node.template)).root.children, tags);
|
|
399
|
-
}
|
|
400
|
-
ts.forEachChild(node, visit);
|
|
401
|
-
};
|
|
402
|
-
visit(sourceFile);
|
|
403
|
-
return tags;
|
|
404
|
-
}
|
|
405
|
-
function templateStatics(ts, tpl) {
|
|
406
|
-
if (ts.isNoSubstitutionTemplateLiteral(tpl)) return [tpl.text];
|
|
407
|
-
const out = [tpl.head.text];
|
|
408
|
-
for (const span of tpl.templateSpans) out.push(span.literal.text);
|
|
409
|
-
return out;
|
|
410
|
-
}
|
|
411
|
-
function collectTags(nodes, out) {
|
|
412
|
-
for (const node of nodes) {
|
|
413
|
-
if (node.kind === "Component" && node.tag) out.add(node.tag.split(".")[0]);
|
|
414
|
-
if ("children" in node && node.children) collectTags(node.children, out);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
function shouldSuppress(ts, sourceFile, diagnostic, used) {
|
|
418
|
-
if (diagnostic.start === void 0 || !isUnusedCode(diagnostic.code)) return false;
|
|
419
|
-
const spanText = sourceFile.text.substr(diagnostic.start, diagnostic.length ?? 0);
|
|
420
|
-
const decl = importDeclarationAt(ts, sourceFile, diagnostic.start);
|
|
421
|
-
if (decl) {
|
|
422
|
-
const names = importedNames(ts, decl);
|
|
423
|
-
if (names.includes(spanText)) return used.has(spanText);
|
|
424
|
-
return names.some((n) => used.has(n));
|
|
425
|
-
}
|
|
426
|
-
return used.has(spanText);
|
|
427
|
-
}
|
|
428
|
-
function importDeclarationAt(ts, sourceFile, pos) {
|
|
429
|
-
let found;
|
|
430
|
-
const visit = (node) => {
|
|
431
|
-
if (found) return;
|
|
432
|
-
if (ts.isImportDeclaration(node) && node.getStart(sourceFile) <= pos && node.getEnd() >= pos) {
|
|
433
|
-
found = node;
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
ts.forEachChild(node, visit);
|
|
437
|
-
};
|
|
438
|
-
visit(sourceFile);
|
|
439
|
-
return found;
|
|
440
|
-
}
|
|
441
|
-
function importedNames(ts, decl) {
|
|
442
|
-
const bindings = decl.importClause?.namedBindings;
|
|
443
|
-
const names = [];
|
|
444
|
-
if (decl.importClause?.name) names.push(decl.importClause.name.text);
|
|
445
|
-
if (bindings && ts.isNamedImports(bindings)) {
|
|
446
|
-
for (const el of bindings.elements) names.push(el.name.text);
|
|
447
|
-
} else if (bindings && ts.isNamespaceImport(bindings)) {
|
|
448
|
-
names.push(bindings.name.text);
|
|
449
|
-
}
|
|
450
|
-
return names;
|
|
795
|
+
function parseTemplate(pieces, opts = {}) {
|
|
796
|
+
return new Parser(pieces, opts).parse();
|
|
451
797
|
}
|
|
452
798
|
|
|
453
799
|
// src/pieces.ts
|
|
@@ -463,9 +809,9 @@ function templatePieces(ts, tpl, sf) {
|
|
|
463
809
|
const pieces = [
|
|
464
810
|
{ kind: "static", text: rawContent(ts, tpl.head, sf), start: tpl.head.getStart(sf) + 1 }
|
|
465
811
|
];
|
|
466
|
-
tpl.templateSpans.forEach((span,
|
|
812
|
+
tpl.templateSpans.forEach((span, i) => {
|
|
467
813
|
const e = span.expression;
|
|
468
|
-
pieces.push({ kind: "hole", index:
|
|
814
|
+
pieces.push({ kind: "hole", index: i, start: e.getStart(sf), end: e.getEnd() });
|
|
469
815
|
pieces.push({
|
|
470
816
|
kind: "static",
|
|
471
817
|
text: rawContent(ts, span.literal, sf),
|
|
@@ -486,14 +832,22 @@ function parseCached(ts, sf, tpl) {
|
|
|
486
832
|
const key = tpl.getStart(sf);
|
|
487
833
|
const hit = byTemplate.get(key);
|
|
488
834
|
if (hit) return hit;
|
|
489
|
-
const result =
|
|
835
|
+
const result = parseTemplate(templatePieces(ts, tpl, sf));
|
|
490
836
|
byTemplate.set(key, result);
|
|
491
837
|
return result;
|
|
492
838
|
}
|
|
493
839
|
var templates = /* @__PURE__ */ new WeakMap();
|
|
840
|
+
function mayHaveTemplate(text) {
|
|
841
|
+
return text.includes("html`") || text.includes("svg`");
|
|
842
|
+
}
|
|
843
|
+
var NONE = [];
|
|
494
844
|
function templatesIn(ts, sf) {
|
|
495
845
|
const hit = templates.get(sf);
|
|
496
846
|
if (hit) return hit;
|
|
847
|
+
if (!mayHaveTemplate(sf.text)) {
|
|
848
|
+
templates.set(sf, NONE);
|
|
849
|
+
return NONE;
|
|
850
|
+
}
|
|
497
851
|
const found = [];
|
|
498
852
|
const visit = (node) => {
|
|
499
853
|
if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && (node.tag.text === "html" || node.tag.text === "svg")) {
|
|
@@ -502,7 +856,7 @@ function templatesIn(ts, sf) {
|
|
|
502
856
|
ts.forEachChild(node, visit);
|
|
503
857
|
};
|
|
504
858
|
visit(sf);
|
|
505
|
-
found.sort((
|
|
859
|
+
found.sort((a, b) => a.getStart(sf) - b.getStart(sf));
|
|
506
860
|
templates.set(sf, found);
|
|
507
861
|
return found;
|
|
508
862
|
}
|
|
@@ -516,22 +870,101 @@ function templateAtCached(ts, sf, pos) {
|
|
|
516
870
|
return match;
|
|
517
871
|
}
|
|
518
872
|
|
|
873
|
+
// src/collect.ts
|
|
874
|
+
var UNUSED_CODES = /* @__PURE__ */ new Set([6133, 6196, 6198, 6199, 6205, 6138, 6192]);
|
|
875
|
+
function isUnusedCode(code) {
|
|
876
|
+
return UNUSED_CODES.has(code);
|
|
877
|
+
}
|
|
878
|
+
var tagCache = /* @__PURE__ */ new WeakMap();
|
|
879
|
+
function usedComponentTags(ts, sourceFile) {
|
|
880
|
+
const hit = tagCache.get(sourceFile);
|
|
881
|
+
if (hit) return hit;
|
|
882
|
+
const tags = /* @__PURE__ */ new Set();
|
|
883
|
+
for (const template of templatesIn(ts, sourceFile)) {
|
|
884
|
+
collectTags(parseCached(ts, sourceFile, template.template).root.children, tags);
|
|
885
|
+
}
|
|
886
|
+
tagCache.set(sourceFile, tags);
|
|
887
|
+
return tags;
|
|
888
|
+
}
|
|
889
|
+
function collectTags(nodes, out) {
|
|
890
|
+
for (const node of nodes) {
|
|
891
|
+
if (node.kind === "Component" && node.tag) out.add(node.tag.split(".")[0]);
|
|
892
|
+
if ("children" in node && node.children) collectTags(node.children, out);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
function shouldSuppress(ts, sourceFile, diagnostic, used) {
|
|
896
|
+
if (diagnostic.start === void 0 || !isUnusedCode(diagnostic.code)) return false;
|
|
897
|
+
const spanText = sourceFile.text.substr(diagnostic.start, diagnostic.length ?? 0);
|
|
898
|
+
const decl = importDeclarationAt(ts, sourceFile, diagnostic.start);
|
|
899
|
+
if (decl) {
|
|
900
|
+
const names = importedNames(ts, decl);
|
|
901
|
+
if (names.includes(spanText)) return used.has(spanText);
|
|
902
|
+
return names.some((n) => used.has(n));
|
|
903
|
+
}
|
|
904
|
+
return used.has(spanText);
|
|
905
|
+
}
|
|
906
|
+
function importDeclarationAt(ts, sourceFile, pos) {
|
|
907
|
+
let found;
|
|
908
|
+
const visit = (node) => {
|
|
909
|
+
if (found) return;
|
|
910
|
+
if (ts.isImportDeclaration(node) && node.getStart(sourceFile) <= pos && node.getEnd() >= pos) {
|
|
911
|
+
found = node;
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
ts.forEachChild(node, visit);
|
|
915
|
+
};
|
|
916
|
+
visit(sourceFile);
|
|
917
|
+
return found;
|
|
918
|
+
}
|
|
919
|
+
function importedNames(ts, decl) {
|
|
920
|
+
const bindings = decl.importClause?.namedBindings;
|
|
921
|
+
const names = [];
|
|
922
|
+
if (decl.importClause?.name) names.push(decl.importClause.name.text);
|
|
923
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
924
|
+
for (const el of bindings.elements) names.push(el.name.text);
|
|
925
|
+
} else if (bindings && ts.isNamespaceImport(bindings)) {
|
|
926
|
+
names.push(bindings.name.text);
|
|
927
|
+
}
|
|
928
|
+
return names;
|
|
929
|
+
}
|
|
930
|
+
|
|
519
931
|
// ../compiler/dist/resolve/builtins.mjs
|
|
520
|
-
var
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
932
|
+
var DOM_CONTROL_FLOW = [
|
|
933
|
+
"Show",
|
|
934
|
+
"For",
|
|
935
|
+
"Index",
|
|
936
|
+
"Switch",
|
|
937
|
+
"Match",
|
|
938
|
+
"Portal",
|
|
939
|
+
"Dynamic",
|
|
940
|
+
"ErrorBoundary"
|
|
941
|
+
];
|
|
942
|
+
var CORE_CONTROL_FLOW = ["Suspense", "SuspenseList", "Await"];
|
|
943
|
+
var ROUTER_COMPONENTS = ["Router", "Outlet", "Redirect", "Link"];
|
|
944
|
+
var BUILTIN_COMPONENTS = [
|
|
945
|
+
...DOM_CONTROL_FLOW,
|
|
946
|
+
...CORE_CONTROL_FLOW,
|
|
947
|
+
...ROUTER_COMPONENTS
|
|
948
|
+
];
|
|
949
|
+
var RESERVED_COMPONENTS = /* @__PURE__ */ new Set([
|
|
950
|
+
...DOM_CONTROL_FLOW,
|
|
951
|
+
...CORE_CONTROL_FLOW
|
|
952
|
+
]);
|
|
953
|
+
function builtinComponentMap(modules = {}) {
|
|
954
|
+
const {
|
|
955
|
+
controlFlowModule = "@fluixi/dom",
|
|
956
|
+
coreModule = "@fluixi/core",
|
|
957
|
+
routerModule = "@fluixi/core/router"
|
|
958
|
+
} = modules;
|
|
959
|
+
const map = {};
|
|
960
|
+
for (const name of DOM_CONTROL_FLOW) map[name] = controlFlowModule;
|
|
961
|
+
for (const name of CORE_CONTROL_FLOW) map[name] = coreModule;
|
|
962
|
+
for (const name of ROUTER_COMPONENTS) map[name] = routerModule;
|
|
963
|
+
return map;
|
|
531
964
|
}
|
|
532
965
|
|
|
533
966
|
// src/auto-import.ts
|
|
534
|
-
var AUTO_IMPORTED =
|
|
967
|
+
var AUTO_IMPORTED = builtinComponentMap();
|
|
535
968
|
var exportsByProgram = /* @__PURE__ */ new WeakMap();
|
|
536
969
|
function moduleExports(ts, program, host, from, moduleName) {
|
|
537
970
|
let cache2 = exportsByProgram.get(program);
|
|
@@ -604,7 +1037,7 @@ function findExport(ts, host, options, fileName, name, depth) {
|
|
|
604
1037
|
return void 0;
|
|
605
1038
|
}
|
|
606
1039
|
function declaredName(ts, statement, name) {
|
|
607
|
-
const exported = ts.canHaveModifiers(statement) ? ts.getModifiers(statement)?.some((
|
|
1040
|
+
const exported = ts.canHaveModifiers(statement) ? ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) : false;
|
|
608
1041
|
if (!exported) return void 0;
|
|
609
1042
|
if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name?.text === name) {
|
|
610
1043
|
return statement.name;
|
|
@@ -627,7 +1060,7 @@ function resolveRelative(ts, host, options, from, specifier) {
|
|
|
627
1060
|
function autoImportedSymbol(ts, program, host, from, name) {
|
|
628
1061
|
const moduleName = AUTO_IMPORTED[name];
|
|
629
1062
|
if (!moduleName) return void 0;
|
|
630
|
-
const symbol = moduleExports(ts, program, host, from, moduleName)?.find((
|
|
1063
|
+
const symbol = moduleExports(ts, program, host, from, moduleName)?.find((s) => s.name === name);
|
|
631
1064
|
if (!symbol) return void 0;
|
|
632
1065
|
if (!(symbol.flags & ts.SymbolFlags.Alias)) return symbol;
|
|
633
1066
|
try {
|
|
@@ -648,7 +1081,7 @@ function componentQuickInfo(ts, ls, host, fileName, position) {
|
|
|
648
1081
|
if (!hit) return void 0;
|
|
649
1082
|
const checker = program.getTypeChecker();
|
|
650
1083
|
const flags = ts.SymbolFlags.Value | ts.SymbolFlags.Function | ts.SymbolFlags.Class | ts.SymbolFlags.Alias;
|
|
651
|
-
const symbol = checker.getSymbolsInScope(template, flags).find((
|
|
1084
|
+
const symbol = checker.getSymbolsInScope(template, flags).find((s) => s.name === hit.name) ?? autoImportedSymbol(ts, program, host, sf, hit.name);
|
|
652
1085
|
const decl = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
|
|
653
1086
|
if (!decl) return void 0;
|
|
654
1087
|
const declFile = decl.getSourceFile();
|
|
@@ -785,21 +1218,21 @@ function holeRoles(nodes, out) {
|
|
|
785
1218
|
};
|
|
786
1219
|
for (const node of nodes) {
|
|
787
1220
|
if (node.kind === "Element" || node.kind === "Component") {
|
|
788
|
-
for (const
|
|
789
|
-
if (
|
|
790
|
-
if (node.kind === "Element" &&
|
|
791
|
-
if (node.kind === "Element" &&
|
|
1221
|
+
for (const a of node.attributes) {
|
|
1222
|
+
if (a.kind === "EventBinding") out.set(a.hole, { kind: "event", event: a.name.toLowerCase() });
|
|
1223
|
+
if (node.kind === "Element" && a.kind === "RefBinding") out.set(a.hole, { kind: "refEl", tag: node.tag });
|
|
1224
|
+
if (node.kind === "Element" && a.kind === "UseDirective" && a.hole != null) out.set(a.hole, { kind: "refEl", tag: node.tag });
|
|
792
1225
|
}
|
|
793
|
-
const eachDir = node.attributes.find((
|
|
1226
|
+
const eachDir = node.attributes.find((a) => a.kind === "EachDirective");
|
|
794
1227
|
if (eachDir && eachDir.kind === "EachDirective") {
|
|
795
1228
|
renderChildren(node, { kind: "each", eachHole: eachDir.hole });
|
|
796
1229
|
}
|
|
797
1230
|
if (node.kind === "Component") {
|
|
798
1231
|
const propHole = (name) => {
|
|
799
|
-
const
|
|
800
|
-
(
|
|
1232
|
+
const a = node.attributes.find(
|
|
1233
|
+
(x) => x.kind === "Attribute" && x.name === name && x.value != null && x.value.kind === "hole"
|
|
801
1234
|
);
|
|
802
|
-
return
|
|
1235
|
+
return a && a.kind === "Attribute" && a.value && a.value.kind === "hole" ? a.value.hole : void 0;
|
|
803
1236
|
};
|
|
804
1237
|
const eachHole = propHole("each");
|
|
805
1238
|
if (node.tag === "For" && eachHole !== void 0) renderChildren(node, { kind: "each", eachHole });
|
|
@@ -812,7 +1245,7 @@ function holeRoles(nodes, out) {
|
|
|
812
1245
|
}
|
|
813
1246
|
}
|
|
814
1247
|
function resolveGlobalType(ts, checker, location, name) {
|
|
815
|
-
const sym = checker.getSymbolsInScope(location, ts.SymbolFlags.Type).find((
|
|
1248
|
+
const sym = checker.getSymbolsInScope(location, ts.SymbolFlags.Type).find((s) => s.getName() === name);
|
|
816
1249
|
return sym ? checker.getDeclaredTypeOfSymbol(sym) : void 0;
|
|
817
1250
|
}
|
|
818
1251
|
function resolveElementType(ts, checker, location, tag) {
|
|
@@ -825,7 +1258,7 @@ function inferParamAt(ts, program, sf, position) {
|
|
|
825
1258
|
const template = templateAtCached(ts, sf, position);
|
|
826
1259
|
if (!template || ts.isNoSubstitutionTemplateLiteral(template.template)) return void 0;
|
|
827
1260
|
const spans = template.template.templateSpans;
|
|
828
|
-
const holeExprs = spans.map((
|
|
1261
|
+
const holeExprs = spans.map((s) => s.expression);
|
|
829
1262
|
const holeIndex = holeExprs.findIndex((e) => position >= e.getStart(sf) && position <= e.getEnd());
|
|
830
1263
|
if (holeIndex === -1) return void 0;
|
|
831
1264
|
const roles = /* @__PURE__ */ new Map();
|
|
@@ -874,7 +1307,7 @@ function inferParamsIn(ts, program, sf) {
|
|
|
874
1307
|
for (const template of templatesIn(ts, sf)) {
|
|
875
1308
|
const tpl = template.template;
|
|
876
1309
|
if (ts.isNoSubstitutionTemplateLiteral(tpl)) continue;
|
|
877
|
-
const holeExprs = tpl.templateSpans.map((
|
|
1310
|
+
const holeExprs = tpl.templateSpans.map((s) => s.expression);
|
|
878
1311
|
const roles = /* @__PURE__ */ new Map();
|
|
879
1312
|
holeRoles(parseCached(ts, sf, tpl).root.children, roles);
|
|
880
1313
|
for (const [holeIndex, role] of roles) {
|
|
@@ -949,8 +1382,8 @@ function componentPropCompletions(ts, ls, host, fileName, position) {
|
|
|
949
1382
|
if (!propsType) return void 0;
|
|
950
1383
|
const already = new Set(comp.attributeNames);
|
|
951
1384
|
const entries = [];
|
|
952
|
-
for (const
|
|
953
|
-
const name =
|
|
1385
|
+
for (const p of propsType.getProperties()) {
|
|
1386
|
+
const name = p.getName();
|
|
954
1387
|
if (name === "children" || already.has(name)) continue;
|
|
955
1388
|
entries.push(propEntry(ts, name, span));
|
|
956
1389
|
}
|
|
@@ -1004,10 +1437,10 @@ function propNameAt(ts, sf, tpl, position) {
|
|
|
1004
1437
|
for (const node of nodes) {
|
|
1005
1438
|
if (node.kind === "Component") {
|
|
1006
1439
|
const c = node;
|
|
1007
|
-
for (const
|
|
1008
|
-
if ((
|
|
1009
|
-
const start =
|
|
1010
|
-
if (position >= start && position <= start +
|
|
1440
|
+
for (const a of c.attributes) {
|
|
1441
|
+
if ((a.kind === "Attribute" || a.kind === "PropertyBinding" || a.kind === "BindDirective") && "loc" in a) {
|
|
1442
|
+
const start = a.loc.start;
|
|
1443
|
+
if (position >= start && position <= start + a.name.length) hit = { tag: c.tag, prop: a.name, start };
|
|
1011
1444
|
}
|
|
1012
1445
|
}
|
|
1013
1446
|
}
|
|
@@ -1052,7 +1485,7 @@ function propEntry(ts, name, replacementSpan) {
|
|
|
1052
1485
|
sortText: "0_" + name,
|
|
1053
1486
|
insertText: name,
|
|
1054
1487
|
// Without a span the editor filters these against whatever word *it* computes at the
|
|
1055
|
-
// cursor, and inside a template literal that is not the attribute name
|
|
1488
|
+
// cursor, and inside a template literal that is not the attribute name, so the list
|
|
1056
1489
|
// showed in full until the first keystroke and then emptied. Saying exactly which
|
|
1057
1490
|
// characters are being replaced makes filtering match what is typed.
|
|
1058
1491
|
replacementSpan
|
|
@@ -1068,7 +1501,7 @@ function attrNameSpan(text, position) {
|
|
|
1068
1501
|
}
|
|
1069
1502
|
function componentTypeOf(ts, program, checker, host, sf, template, name) {
|
|
1070
1503
|
const flags = ts.SymbolFlags.Value | ts.SymbolFlags.Function | ts.SymbolFlags.Class | ts.SymbolFlags.Alias;
|
|
1071
|
-
const symbol = checker.getSymbolsInScope(template, flags).find((
|
|
1504
|
+
const symbol = checker.getSymbolsInScope(template, flags).find((s) => s.name === name) ?? autoImportedSymbol(ts, program, host, sf, name);
|
|
1072
1505
|
if (!symbol) return void 0;
|
|
1073
1506
|
const resolved = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
1074
1507
|
const decl = resolved.valueDeclaration ?? resolved.declarations?.[0];
|
|
@@ -1076,8 +1509,8 @@ function componentTypeOf(ts, program, checker, host, sf, template, name) {
|
|
|
1076
1509
|
}
|
|
1077
1510
|
function firstParamType(ts, checker, type) {
|
|
1078
1511
|
for (const sig of checker.getSignaturesOfType(type, ts.SignatureKind.Call)) {
|
|
1079
|
-
const
|
|
1080
|
-
if (
|
|
1512
|
+
const p = sig.getParameters()[0];
|
|
1513
|
+
if (p) return checker.getTypeOfSymbolAtLocation(p, p.valueDeclaration ?? sig.getDeclaration());
|
|
1081
1514
|
}
|
|
1082
1515
|
return void 0;
|
|
1083
1516
|
}
|
|
@@ -1108,8 +1541,8 @@ function firstChildOrCloseStart(c) {
|
|
|
1108
1541
|
}
|
|
1109
1542
|
function attributeNames(c) {
|
|
1110
1543
|
const out = [];
|
|
1111
|
-
for (const
|
|
1112
|
-
if (
|
|
1544
|
+
for (const a of c.attributes) {
|
|
1545
|
+
if (a.kind === "Attribute" || a.kind === "PropertyBinding" || a.kind === "BindDirective") out.push(a.name);
|
|
1113
1546
|
}
|
|
1114
1547
|
return out;
|
|
1115
1548
|
}
|
|
@@ -1126,13 +1559,13 @@ function paramMemberCompletions(ts, ls, fileName, position) {
|
|
|
1126
1559
|
const checker = program.getTypeChecker();
|
|
1127
1560
|
const already = /* @__PURE__ */ new Set();
|
|
1128
1561
|
const entries = [];
|
|
1129
|
-
for (const
|
|
1130
|
-
const name =
|
|
1562
|
+
for (const p of checker.getApparentType(info.type).getProperties()) {
|
|
1563
|
+
const name = p.getName();
|
|
1131
1564
|
if (already.has(name) || name.startsWith("__")) continue;
|
|
1132
1565
|
already.add(name);
|
|
1133
1566
|
entries.push({
|
|
1134
1567
|
name,
|
|
1135
|
-
kind: memberKind(ts,
|
|
1568
|
+
kind: memberKind(ts, p),
|
|
1136
1569
|
kindModifiers: "",
|
|
1137
1570
|
sortText: "0_" + name,
|
|
1138
1571
|
insertText: name
|
|
@@ -1220,8 +1653,8 @@ function elementAtAttributeArea(ts, sf, tpl, position) {
|
|
|
1220
1653
|
const openEnd = firstAttributeEnd(el);
|
|
1221
1654
|
if (position > openStart && position <= openEnd) {
|
|
1222
1655
|
const present = /* @__PURE__ */ new Set();
|
|
1223
|
-
for (const
|
|
1224
|
-
if ("loc" in
|
|
1656
|
+
for (const a of el.attributes) {
|
|
1657
|
+
if ("loc" in a && a.loc) present.add(writtenName(sf.text, a.loc.start));
|
|
1225
1658
|
}
|
|
1226
1659
|
hit = { tag: el.tag, present };
|
|
1227
1660
|
}
|
|
@@ -1240,8 +1673,8 @@ function writtenName(text, start) {
|
|
|
1240
1673
|
}
|
|
1241
1674
|
function firstAttributeEnd(el) {
|
|
1242
1675
|
let end = el.loc.start + 1 + el.tag.length;
|
|
1243
|
-
for (const
|
|
1244
|
-
if ("loc" in
|
|
1676
|
+
for (const a of el.attributes) {
|
|
1677
|
+
if ("loc" in a && a.loc) end = Math.max(end, a.loc.end);
|
|
1245
1678
|
}
|
|
1246
1679
|
return end + 1;
|
|
1247
1680
|
}
|
|
@@ -1256,8 +1689,8 @@ function elementAttributeCompletions(ts, ls, host, fileName, position) {
|
|
|
1256
1689
|
const props = attributesFor(ts, program, host, sf, hit.tag);
|
|
1257
1690
|
if (!props) return void 0;
|
|
1258
1691
|
const entries = [];
|
|
1259
|
-
for (const
|
|
1260
|
-
const name =
|
|
1692
|
+
for (const p of props) {
|
|
1693
|
+
const name = p.getName();
|
|
1261
1694
|
if (name === "children" || hit.present.has(name)) continue;
|
|
1262
1695
|
entries.push({
|
|
1263
1696
|
name,
|
|
@@ -1275,9 +1708,9 @@ function attributeNameAt(ts, sf, tpl, position) {
|
|
|
1275
1708
|
for (const node of nodes) {
|
|
1276
1709
|
if (node.kind === "Element") {
|
|
1277
1710
|
const el = node;
|
|
1278
|
-
for (const
|
|
1279
|
-
if (!("loc" in
|
|
1280
|
-
const start =
|
|
1711
|
+
for (const a of el.attributes) {
|
|
1712
|
+
if (!("loc" in a) || !a.loc) continue;
|
|
1713
|
+
const start = a.loc.start;
|
|
1281
1714
|
const name = writtenName(sf.text, start);
|
|
1282
1715
|
if (name && position >= start && position <= start + name.length) {
|
|
1283
1716
|
hit = { tag: el.tag, name, start };
|
|
@@ -1300,7 +1733,7 @@ function elementAttributeHover(ts, ls, host, fileName, position) {
|
|
|
1300
1733
|
const hit = attributeNameAt(ts, sf, tpl.template, position);
|
|
1301
1734
|
if (!hit) return void 0;
|
|
1302
1735
|
const props = attributesFor(ts, program, host, sf, hit.tag);
|
|
1303
|
-
const symbol = props?.find((
|
|
1736
|
+
const symbol = props?.find((p) => p.getName() === hit.name);
|
|
1304
1737
|
if (!symbol) return void 0;
|
|
1305
1738
|
const decl = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
1306
1739
|
if (!decl) return void 0;
|
|
@@ -1340,7 +1773,7 @@ function accessorDiagnostics(ts, program, sf) {
|
|
|
1340
1773
|
for (const template of templatesIn(ts, sf)) {
|
|
1341
1774
|
const tpl = template.template;
|
|
1342
1775
|
if (ts.isNoSubstitutionTemplateLiteral(tpl)) continue;
|
|
1343
|
-
const holes = tpl.templateSpans.map((
|
|
1776
|
+
const holes = tpl.templateSpans.map((s) => s.expression);
|
|
1344
1777
|
const visit = (nodes, parent) => {
|
|
1345
1778
|
for (const node of nodes) {
|
|
1346
1779
|
if (node.kind === "Expression" && parent?.kind === "Element") {
|
|
@@ -1373,7 +1806,7 @@ function isAccessor(ts, checker, node) {
|
|
|
1373
1806
|
const signatures = checker.getTypeAtLocation(node).getCallSignatures();
|
|
1374
1807
|
if (signatures.length !== 1) return false;
|
|
1375
1808
|
const signature = signatures[0];
|
|
1376
|
-
if (signature.getParameters().some((
|
|
1809
|
+
if (signature.getParameters().some((p) => !isOptional(ts, p))) return false;
|
|
1377
1810
|
const returned = signature.getReturnType();
|
|
1378
1811
|
return !(returned.flags & (ts.TypeFlags.Void | ts.TypeFlags.Never));
|
|
1379
1812
|
}
|
|
@@ -1450,10 +1883,392 @@ function templateFixAll(ts, ls, fileName, errorCodes) {
|
|
|
1450
1883
|
textChanges.push({ span: { start: at, length: 0 }, newText: "()" });
|
|
1451
1884
|
}
|
|
1452
1885
|
if (textChanges.length === 0) return void 0;
|
|
1453
|
-
textChanges.sort((
|
|
1886
|
+
textChanges.sort((a, b) => b.span.start - a.span.start);
|
|
1454
1887
|
return { changes: [{ fileName, textChanges }], commands: void 0 };
|
|
1455
1888
|
}
|
|
1456
1889
|
|
|
1890
|
+
// src/rules.ts
|
|
1891
|
+
var RULE = {
|
|
1892
|
+
envNotPublic: 9101,
|
|
1893
|
+
serverReadInBrowser: 9102,
|
|
1894
|
+
importMetaEnv: 9103,
|
|
1895
|
+
secretInEnvPublic: 9104,
|
|
1896
|
+
secretInBrowser: 9105,
|
|
1897
|
+
audienceUnchecked: 9106,
|
|
1898
|
+
weakAlgorithm: 9107,
|
|
1899
|
+
dpopBareVerify: 9108,
|
|
1900
|
+
guardWithoutRoles: 9109,
|
|
1901
|
+
weakCookie: 9110,
|
|
1902
|
+
localStorageTokens: 9111
|
|
1903
|
+
};
|
|
1904
|
+
var SECRETISH = /(SECRET|PASSWORD|PASSWD|PRIVATE_KEY|CLIENT_SECRET|CREDENTIALS?)($|_)/;
|
|
1905
|
+
var VITE_BUILTINS = /* @__PURE__ */ new Set(["DEV", "PROD", "SSR", "MODE", "BASE_URL"]);
|
|
1906
|
+
function isServerFile(fileName) {
|
|
1907
|
+
const f = fileName.replace(/\\/g, "/");
|
|
1908
|
+
return /\.server\.[cm]?[jt]sx?$/.test(f) || /\/src\/api\//.test(f) || /\/src\/middleware\.[cm]?[jt]s$/.test(f) || /\/(fluixi|vite)\.config\.[cm]?[jt]s$/.test(f) || /\/src\/entry-server\.[cm]?[jt]sx?$/.test(f) || // Tests run under Node, never in a page.
|
|
1909
|
+
/\.(spec|test)\.[cm]?[jt]sx?$/.test(f) || /\/(tests?|__tests__)\//.test(f);
|
|
1910
|
+
}
|
|
1911
|
+
function importsOf(ts, sf) {
|
|
1912
|
+
const out = /* @__PURE__ */ new Map();
|
|
1913
|
+
for (const stmt of sf.statements) {
|
|
1914
|
+
if (!ts.isImportDeclaration(stmt) || !ts.isStringLiteral(stmt.moduleSpecifier)) continue;
|
|
1915
|
+
const bindings = stmt.importClause?.namedBindings;
|
|
1916
|
+
if (!bindings || !ts.isNamedImports(bindings)) continue;
|
|
1917
|
+
for (const el of bindings.elements) {
|
|
1918
|
+
out.set(el.name.text, { module: stmt.moduleSpecifier.text, name: (el.propertyName ?? el.name).text });
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
return out;
|
|
1922
|
+
}
|
|
1923
|
+
function sideOf(ts, test) {
|
|
1924
|
+
if (ts.isParenthesizedExpression(test)) return sideOf(ts, test.expression);
|
|
1925
|
+
if (ts.isPrefixUnaryExpression(test) && test.operator === ts.SyntaxKind.ExclamationToken) {
|
|
1926
|
+
const inner = sideOf(ts, test.operand);
|
|
1927
|
+
return inner === "server" ? "browser" : inner === "browser" ? "server" : null;
|
|
1928
|
+
}
|
|
1929
|
+
if (ts.isPropertyAccessExpression(test) && test.name.text === "SSR" && ts.isPropertyAccessExpression(test.expression) && test.expression.name.text === "env" && ts.isMetaProperty(test.expression.expression)) {
|
|
1930
|
+
return "server";
|
|
1931
|
+
}
|
|
1932
|
+
if (ts.isBinaryExpression(test)) {
|
|
1933
|
+
const op = test.operatorToken.kind;
|
|
1934
|
+
const eq = op === ts.SyntaxKind.EqualsEqualsEqualsToken || op === ts.SyntaxKind.EqualsEqualsToken;
|
|
1935
|
+
const ne = op === ts.SyntaxKind.ExclamationEqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken;
|
|
1936
|
+
if (!eq && !ne) return null;
|
|
1937
|
+
const typeofGlobal = (n) => ts.isTypeOfExpression(n) && ts.isIdentifier(n.expression) && ["window", "document"].includes(n.expression.text);
|
|
1938
|
+
const undef = (n) => ts.isStringLiteral(n) && n.text === "undefined";
|
|
1939
|
+
if (typeofGlobal(test.left) && undef(test.right) || typeofGlobal(test.right) && undef(test.left)) {
|
|
1940
|
+
return eq ? "server" : "browser";
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
return null;
|
|
1944
|
+
}
|
|
1945
|
+
var isUseServer = (ts, body) => {
|
|
1946
|
+
if (!body || !ts.isBlock(body)) return false;
|
|
1947
|
+
const first = body.statements[0];
|
|
1948
|
+
return !!first && ts.isExpressionStatement(first) && ts.isStringLiteral(first.expression) && first.expression.text === "use server";
|
|
1949
|
+
};
|
|
1950
|
+
function inServerContext(ts, node, sf) {
|
|
1951
|
+
const first = sf.statements[0];
|
|
1952
|
+
if (first && ts.isExpressionStatement(first) && ts.isStringLiteral(first.expression) && first.expression.text === "use server") {
|
|
1953
|
+
return true;
|
|
1954
|
+
}
|
|
1955
|
+
for (let child = node, parent = node.parent; parent; child = parent, parent = parent.parent) {
|
|
1956
|
+
if ((ts.isFunctionDeclaration(parent) || ts.isFunctionExpression(parent) || ts.isArrowFunction(parent)) && isUseServer(ts, parent.body)) {
|
|
1957
|
+
return true;
|
|
1958
|
+
}
|
|
1959
|
+
if (ts.isIfStatement(parent) || ts.isConditionalExpression(parent)) {
|
|
1960
|
+
const side = sideOf(ts, ts.isIfStatement(parent) ? parent.expression : parent.condition);
|
|
1961
|
+
const whenTrue = ts.isIfStatement(parent) ? parent.thenStatement : parent.whenTrue;
|
|
1962
|
+
const whenFalse = ts.isIfStatement(parent) ? parent.elseStatement : parent.whenFalse;
|
|
1963
|
+
if (side === "server" && child === whenTrue) return true;
|
|
1964
|
+
if (side === "browser" && child === whenFalse) return true;
|
|
1965
|
+
}
|
|
1966
|
+
if (ts.isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken && child === parent.right && sideOf(ts, parent.left) === "server") {
|
|
1967
|
+
return true;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
return false;
|
|
1971
|
+
}
|
|
1972
|
+
function publicEnvNames(ts, program, fileName, host) {
|
|
1973
|
+
const checker = program.getTypeChecker();
|
|
1974
|
+
let moduleSymbol;
|
|
1975
|
+
if (host) {
|
|
1976
|
+
const resolved = ts.resolveModuleName("@fluixi/start/env", fileName, program.getCompilerOptions(), host).resolvedModule;
|
|
1977
|
+
const target = resolved && program.getSourceFile(resolved.resolvedFileName);
|
|
1978
|
+
if (target) moduleSymbol = checker.getSymbolAtLocation(target);
|
|
1979
|
+
}
|
|
1980
|
+
moduleSymbol ??= checker.getAmbientModules().find((m) => m.name.replace(/"/g, "") === "@fluixi/start/env");
|
|
1981
|
+
const iface = moduleSymbol && checker.getExportsOfModule(moduleSymbol).find((s) => s.name === "PublicEnv");
|
|
1982
|
+
if (!iface) return null;
|
|
1983
|
+
const names = checker.getPropertiesOfType(checker.getDeclaredTypeOfSymbol(iface)).map((p) => p.name);
|
|
1984
|
+
return names.length ? new Set(names) : null;
|
|
1985
|
+
}
|
|
1986
|
+
var propsOf = (ts, obj) => {
|
|
1987
|
+
const map = /* @__PURE__ */ new Map();
|
|
1988
|
+
for (const p of obj.properties) {
|
|
1989
|
+
if (ts.isPropertyAssignment(p) && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name))) map.set(p.name.text, p);
|
|
1990
|
+
}
|
|
1991
|
+
return map;
|
|
1992
|
+
};
|
|
1993
|
+
var isFalse = (ts, e) => !!e && e.kind === ts.SyntaxKind.FalseKeyword;
|
|
1994
|
+
var OAUTH_CONFIG_CALLS = /* @__PURE__ */ new Set([
|
|
1995
|
+
"createAuthService",
|
|
1996
|
+
"createBrowserClient",
|
|
1997
|
+
"createServerClient",
|
|
1998
|
+
"createServiceClient",
|
|
1999
|
+
"createTokenIntrospector",
|
|
2000
|
+
"oauthRoutes",
|
|
2001
|
+
"authServerModule",
|
|
2002
|
+
"sessionLocals"
|
|
2003
|
+
]);
|
|
2004
|
+
function envAuthDiagnostics(ts, program, fileName, host) {
|
|
2005
|
+
const sf = program.getSourceFile(fileName);
|
|
2006
|
+
if (!sf || sf.isDeclarationFile || /[\\/]node_modules[\\/]/.test(fileName)) return [];
|
|
2007
|
+
const text = sf.text;
|
|
2008
|
+
const isConfig = /[\\/]fluixi\.config\.[cm]?[jt]s$/.test(fileName);
|
|
2009
|
+
if (!isConfig && !/fluixi|\$env|import\.meta\.env|process\.env/.test(text)) return [];
|
|
2010
|
+
const out = [];
|
|
2011
|
+
const add = (node, code, category, messageText) => out.push({ start: node.getStart(sf), length: node.getWidth(sf), code, category, messageText, source: "fluixi" });
|
|
2012
|
+
const imports = importsOf(ts, sf);
|
|
2013
|
+
const serverFile = isServerFile(fileName);
|
|
2014
|
+
const browserSide = (node) => !serverFile && !inServerContext(ts, node, sf);
|
|
2015
|
+
const fromModule = (callee, prefix) => {
|
|
2016
|
+
if (!ts.isIdentifier(callee)) return null;
|
|
2017
|
+
const hit = imports.get(callee.text);
|
|
2018
|
+
return hit && (hit.module === prefix || hit.module.startsWith(prefix + "/")) ? hit.name : null;
|
|
2019
|
+
};
|
|
2020
|
+
let publicNames;
|
|
2021
|
+
const isPublic = (key) => {
|
|
2022
|
+
if (publicNames === void 0) publicNames = publicEnvNames(ts, program, fileName, host);
|
|
2023
|
+
return publicNames === null || publicNames.has(key) || key.startsWith("VITE_");
|
|
2024
|
+
};
|
|
2025
|
+
const envNotPublic = (lit) => {
|
|
2026
|
+
if (!browserSide(lit) || isPublic(lit.text)) return;
|
|
2027
|
+
add(
|
|
2028
|
+
lit,
|
|
2029
|
+
RULE.envNotPublic,
|
|
2030
|
+
ts.DiagnosticCategory.Warning,
|
|
2031
|
+
`${lit.text} is not in env.public, so the browser has no value for it, and the build refuses this read in browser code. If it is public, add it to env.public in fluixi.config.ts. If it is a secret, read it on the server and pass the page what it needs.`
|
|
2032
|
+
);
|
|
2033
|
+
};
|
|
2034
|
+
const dpopVerifiers = /* @__PURE__ */ new Set();
|
|
2035
|
+
const guardsWithoutRoles = /* @__PURE__ */ new Set();
|
|
2036
|
+
const visit = (node) => {
|
|
2037
|
+
if (ts.isCallExpression(node)) {
|
|
2038
|
+
const callee = node.expression;
|
|
2039
|
+
const envFn = ts.isIdentifier(callee) && callee.text === "$env" ? "env" : fromModule(callee, "@fluixi/start");
|
|
2040
|
+
const oauthFn = fromModule(callee, "@fluixi/oauth2");
|
|
2041
|
+
const sessionFn = fromModule(callee, "@fluixi/session");
|
|
2042
|
+
const arg0 = node.arguments[0];
|
|
2043
|
+
if (envFn === "env" && arg0 && ts.isStringLiteral(arg0)) envNotPublic(arg0);
|
|
2044
|
+
if (envFn === "pickEnv" && arg0 && ts.isObjectLiteralExpression(arg0)) {
|
|
2045
|
+
for (const p of arg0.properties) {
|
|
2046
|
+
if (!ts.isPropertyAssignment(p)) continue;
|
|
2047
|
+
if (ts.isStringLiteral(p.initializer)) envNotPublic(p.initializer);
|
|
2048
|
+
else if (ts.isObjectLiteralExpression(p.initializer)) {
|
|
2049
|
+
const key = propsOf(ts, p.initializer).get("key")?.initializer;
|
|
2050
|
+
if (key && ts.isStringLiteral(key)) envNotPublic(key);
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
if (envFn === "serverEnv" && browserSide(node)) {
|
|
2055
|
+
add(
|
|
2056
|
+
callee,
|
|
2057
|
+
RULE.serverReadInBrowser,
|
|
2058
|
+
ts.DiagnosticCategory.Warning,
|
|
2059
|
+
'serverEnv() in code the browser runs throws there: server-only values are not in the bundle. Read it in an API route, middleware or a "use server" function.'
|
|
2060
|
+
);
|
|
2061
|
+
}
|
|
2062
|
+
if (ts.isIdentifier(callee) && dpopVerifiers.has(callee.text)) {
|
|
2063
|
+
add(
|
|
2064
|
+
callee,
|
|
2065
|
+
RULE.dpopBareVerify,
|
|
2066
|
+
ts.DiagnosticCategory.Error,
|
|
2067
|
+
`${callee.text} requires DPoP, and a bare token carries no proof, so every call is refused. Use ${callee.text}.fromRequest(request).`
|
|
2068
|
+
);
|
|
2069
|
+
}
|
|
2070
|
+
if (ts.isIdentifier(callee) && guardsWithoutRoles.has(callee.text)) {
|
|
2071
|
+
const req = node.arguments[1];
|
|
2072
|
+
if (req && ts.isObjectLiteralExpression(req)) {
|
|
2073
|
+
const props = propsOf(ts, req);
|
|
2074
|
+
const role = props.get("role") ?? props.get("allRoles");
|
|
2075
|
+
if (role) {
|
|
2076
|
+
add(
|
|
2077
|
+
role.name,
|
|
2078
|
+
RULE.guardWithoutRoles,
|
|
2079
|
+
ts.DiagnosticCategory.Error,
|
|
2080
|
+
`${callee.text} was built without \`roles\`, so a role requirement throws on the first request. Pass one of rolesFrom to createApiGuard.`
|
|
2081
|
+
);
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
const configs = [];
|
|
2086
|
+
if (oauthFn && OAUTH_CONFIG_CALLS.has(oauthFn) || oauthFn === "createTokenVerifier" || sessionFn === "createSessionCookie") {
|
|
2087
|
+
if (arg0 && ts.isObjectLiteralExpression(arg0)) {
|
|
2088
|
+
configs.push(arg0);
|
|
2089
|
+
const nested = propsOf(ts, arg0).get("config")?.initializer;
|
|
2090
|
+
if (oauthFn === "createAuthService" && nested && ts.isObjectLiteralExpression(nested)) configs.push(nested);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
for (const obj of configs) {
|
|
2094
|
+
const props = propsOf(ts, obj);
|
|
2095
|
+
const tokens = props.get("tokens")?.initializer;
|
|
2096
|
+
const browserTokens = tokens && ts.isStringLiteral(tokens) && tokens.text === "browser" || oauthFn === "createBrowserClient";
|
|
2097
|
+
for (const secret of ["clientSecret", "privateKey"]) {
|
|
2098
|
+
const p = props.get(secret);
|
|
2099
|
+
if (p && (browserTokens || browserSide(p) && oauthFn !== "createTokenVerifier")) {
|
|
2100
|
+
add(
|
|
2101
|
+
p.name,
|
|
2102
|
+
RULE.secretInBrowser,
|
|
2103
|
+
ts.DiagnosticCategory.Error,
|
|
2104
|
+
`${secret} in ${browserTokens ? "a tokens: 'browser' client" : "code the browser runs"} ships to every visitor, and anything shipped to a browser is not a secret. A browser client uses PKCE without one; keep ${secret} on the server.`
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
if (oauthFn === "createTokenVerifier") {
|
|
2109
|
+
const audience = props.get("audience");
|
|
2110
|
+
if (audience && isFalse(ts, audience.initializer) && !props.has("authorizedParty") && !props.has("require")) {
|
|
2111
|
+
add(
|
|
2112
|
+
audience,
|
|
2113
|
+
RULE.audienceUnchecked,
|
|
2114
|
+
ts.DiagnosticCategory.Warning,
|
|
2115
|
+
"audience: false with no authorizedParty or require accepts a token minted for any client of the same issuer. Pin authorizedParty to your client id, or check a claim in require."
|
|
2116
|
+
);
|
|
2117
|
+
}
|
|
2118
|
+
const algs = props.get("algorithms")?.initializer;
|
|
2119
|
+
if (algs && ts.isArrayLiteralExpression(algs)) {
|
|
2120
|
+
for (const el of algs.elements) {
|
|
2121
|
+
if (ts.isStringLiteral(el) && (/^HS/.test(el.text) || el.text === "none")) {
|
|
2122
|
+
add(
|
|
2123
|
+
el,
|
|
2124
|
+
RULE.weakAlgorithm,
|
|
2125
|
+
ts.DiagnosticCategory.Error,
|
|
2126
|
+
el.text === "none" ? '"none" accepts an unsigned token as valid.' : `${el.text} verifies with a shared secret; with a provider's public key it lets anyone who has that key sign tokens. Use RS256 or ES256.`
|
|
2127
|
+
);
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
}
|
|
2132
|
+
const cookieKey = props.get(sessionFn === "createSessionCookie" ? "secret" : "cookieSecret");
|
|
2133
|
+
if (cookieKey && ts.isStringLiteral(cookieKey.initializer) && cookieKey.initializer.text.length < 32) {
|
|
2134
|
+
add(
|
|
2135
|
+
cookieKey.initializer,
|
|
2136
|
+
RULE.weakCookie,
|
|
2137
|
+
ts.DiagnosticCategory.Error,
|
|
2138
|
+
"The session cookie is encrypted with a key derived from this, and under 32 characters it is short enough to guess. Read a long random value with serverEnv."
|
|
2139
|
+
);
|
|
2140
|
+
}
|
|
2141
|
+
const secure = props.get(sessionFn === "createSessionCookie" ? "secure" : "secureCookie");
|
|
2142
|
+
if (secure && isFalse(ts, secure.initializer)) {
|
|
2143
|
+
add(
|
|
2144
|
+
secure,
|
|
2145
|
+
RULE.weakCookie,
|
|
2146
|
+
ts.DiagnosticCategory.Warning,
|
|
2147
|
+
"The session cookie will also be sent over plain http. Keep this for local work only: derive it, e.g. from whether the app URL is https."
|
|
2148
|
+
);
|
|
2149
|
+
}
|
|
2150
|
+
const storage = props.get("storage")?.initializer;
|
|
2151
|
+
if (storage && ts.isStringLiteral(storage) && storage.text === "local" && browserTokens) {
|
|
2152
|
+
add(
|
|
2153
|
+
storage,
|
|
2154
|
+
RULE.localStorageTokens,
|
|
2155
|
+
ts.DiagnosticCategory.Suggestion,
|
|
2156
|
+
"storage: 'local' keeps the refresh token after the tab closes, so injected script can reach it for longer. 'session', the default, ends with the tab."
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
2162
|
+
const obj = node.expression;
|
|
2163
|
+
if (ts.isIdentifier(obj) && obj.text === "process" && node.name.text === "env" && browserSide(node)) {
|
|
2164
|
+
add(
|
|
2165
|
+
node,
|
|
2166
|
+
RULE.serverReadInBrowser,
|
|
2167
|
+
ts.DiagnosticCategory.Warning,
|
|
2168
|
+
"process.env does not exist in the browser. Use $env('NAME'), which reads the public set there and anything on the server."
|
|
2169
|
+
);
|
|
2170
|
+
}
|
|
2171
|
+
if (ts.isPropertyAccessExpression(obj) && obj.name.text === "env" && ts.isMetaProperty(obj.expression) && !VITE_BUILTINS.has(node.name.text) && !node.name.text.startsWith("VITE_")) {
|
|
2172
|
+
add(
|
|
2173
|
+
node,
|
|
2174
|
+
RULE.importMetaEnv,
|
|
2175
|
+
ts.DiagnosticCategory.Warning,
|
|
2176
|
+
`import.meta.env.${node.name.text} is undefined in the browser: Vite only defines VITE_ names there. Use $env('${node.name.text}') and declare it in env.public if it is public.`
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
if (/[\\/]fluixi\.config\.[cm]?[jt]s$/.test(fileName) && ts.isPropertyAssignment(node) && ts.isIdentifier(node.name) && node.name.text === "public" && ts.isArrayLiteralExpression(node.initializer) && ts.isObjectLiteralExpression(node.parent) && ts.isPropertyAssignment(node.parent.parent) && ts.isIdentifier(node.parent.parent.name) && node.parent.parent.name.text === "env") {
|
|
2181
|
+
for (const el of node.initializer.elements) {
|
|
2182
|
+
if (ts.isStringLiteral(el) && SECRETISH.test(el.text)) {
|
|
2183
|
+
add(
|
|
2184
|
+
el,
|
|
2185
|
+
RULE.secretInEnvPublic,
|
|
2186
|
+
ts.DiagnosticCategory.Error,
|
|
2187
|
+
`${el.text} reads like a secret, and env.public puts its value in the browser bundle; the build refuses it. Remove it and read it with serverEnv() on the server. If the name only looks like a secret, set FLUIXI_ALLOW_PUBLIC_SECRETS=true.`
|
|
2188
|
+
);
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
ts.forEachChild(node, visit);
|
|
2193
|
+
};
|
|
2194
|
+
const collect = (node) => {
|
|
2195
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer && ts.isCallExpression(node.initializer)) {
|
|
2196
|
+
const fn = fromModule(node.initializer.expression, "@fluixi/oauth2");
|
|
2197
|
+
const opts = node.initializer.arguments[0];
|
|
2198
|
+
if (opts && ts.isObjectLiteralExpression(opts)) {
|
|
2199
|
+
const props = propsOf(ts, opts);
|
|
2200
|
+
if (fn === "createTokenVerifier" && props.get("dpop")?.initializer.kind === ts.SyntaxKind.TrueKeyword) dpopVerifiers.add(node.name.text);
|
|
2201
|
+
if (fn === "createApiGuard" && !props.has("roles") && !opts.properties.some((p) => ts.isSpreadAssignment(p))) {
|
|
2202
|
+
guardsWithoutRoles.add(node.name.text);
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
ts.forEachChild(node, collect);
|
|
2207
|
+
};
|
|
2208
|
+
collect(sf);
|
|
2209
|
+
visit(sf);
|
|
2210
|
+
return out.map((d) => ({ ...d, file: sf }));
|
|
2211
|
+
}
|
|
2212
|
+
var CONFIG_NAMES = ["fluixi.config.ts", "fluixi.config.mts", "fluixi.config.js", "fluixi.config.mjs"];
|
|
2213
|
+
function findConfig(fileName, host) {
|
|
2214
|
+
let dir = fileName.replace(/\\/g, "/").replace(/\/[^/]*$/, "");
|
|
2215
|
+
for (; ; ) {
|
|
2216
|
+
for (const name of CONFIG_NAMES) if (host.fileExists(`${dir}/${name}`)) return `${dir}/${name}`;
|
|
2217
|
+
const up = dir.replace(/\/[^/]*$/, "");
|
|
2218
|
+
if (!up || up === dir) return null;
|
|
2219
|
+
dir = up;
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
function envPublicFix(ts, program, fileName, position, host) {
|
|
2223
|
+
const sf = program.getSourceFile(fileName);
|
|
2224
|
+
if (!sf) return null;
|
|
2225
|
+
let literal;
|
|
2226
|
+
const find = (node) => {
|
|
2227
|
+
if (literal || position < node.getFullStart() || position > node.getEnd()) return;
|
|
2228
|
+
if (ts.isStringLiteral(node) && position >= node.getStart(sf) && position <= node.getEnd()) literal = node;
|
|
2229
|
+
else ts.forEachChild(node, find);
|
|
2230
|
+
};
|
|
2231
|
+
find(sf);
|
|
2232
|
+
if (!literal || SECRETISH.test(literal.text)) return null;
|
|
2233
|
+
const configPath = findConfig(fileName, host);
|
|
2234
|
+
const configText = configPath && host.readFile(configPath);
|
|
2235
|
+
if (!configPath || !configText) return null;
|
|
2236
|
+
const config = ts.createSourceFile(configPath, configText, ts.ScriptTarget.Latest, true);
|
|
2237
|
+
let list;
|
|
2238
|
+
const search = (node) => {
|
|
2239
|
+
if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name) && node.name.text === "public" && ts.isArrayLiteralExpression(node.initializer) && ts.isPropertyAssignment(node.parent.parent) && ts.isIdentifier(node.parent.parent.name) && node.parent.parent.name.text === "env") {
|
|
2240
|
+
list = node.initializer;
|
|
2241
|
+
}
|
|
2242
|
+
if (!list) ts.forEachChild(node, search);
|
|
2243
|
+
};
|
|
2244
|
+
search(config);
|
|
2245
|
+
if (!list) return null;
|
|
2246
|
+
const quote = list.elements.find(ts.isStringLiteral)?.getText(config).startsWith('"') ? '"' : "'";
|
|
2247
|
+
const entry = `${quote}${literal.text}${quote}`;
|
|
2248
|
+
const last = list.elements[list.elements.length - 1];
|
|
2249
|
+
let change;
|
|
2250
|
+
if (!last) {
|
|
2251
|
+
change = { span: { start: list.getStart(config) + 1, length: 0 }, newText: entry };
|
|
2252
|
+
} else {
|
|
2253
|
+
const multiline = configText.slice(list.getStart(config), last.getStart(config)).includes("\n");
|
|
2254
|
+
const lineStart = configText.lastIndexOf("\n", last.getStart(config)) + 1;
|
|
2255
|
+
const indent = configText.slice(lineStart, last.getStart(config));
|
|
2256
|
+
const trailingComma = list.elements.hasTrailingComma;
|
|
2257
|
+
const at = trailingComma ? configText.indexOf(",", last.getEnd()) + 1 : last.getEnd();
|
|
2258
|
+
const lead = trailingComma ? "" : ",";
|
|
2259
|
+
change = {
|
|
2260
|
+
span: { start: at, length: 0 },
|
|
2261
|
+
newText: multiline ? `${lead}
|
|
2262
|
+
${indent}${entry}${trailingComma ? "," : ""}` : `${lead} ${entry}`
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
return {
|
|
2266
|
+
fixName: "fluixiAddEnvPublic",
|
|
2267
|
+
description: `Add ${literal.text} to env.public`,
|
|
2268
|
+
changes: [{ fileName: configPath, textChanges: [change] }]
|
|
2269
|
+
};
|
|
2270
|
+
}
|
|
2271
|
+
|
|
1457
2272
|
// src/navigation.ts
|
|
1458
2273
|
var spansByFile = /* @__PURE__ */ new WeakMap();
|
|
1459
2274
|
function tagSpans(ts, sf) {
|
|
@@ -1489,12 +2304,12 @@ function closingTagName(text, end, tag) {
|
|
|
1489
2304
|
}
|
|
1490
2305
|
function tagAt(ts, sf, position) {
|
|
1491
2306
|
if (!templateAtCached(ts, sf, position)) return void 0;
|
|
1492
|
-
return tagSpans(ts, sf).find((
|
|
2307
|
+
return tagSpans(ts, sf).find((s) => position >= s.start && position <= s.start + s.length);
|
|
1493
2308
|
}
|
|
1494
2309
|
function symbolFor(ts, program, host, location, name) {
|
|
1495
2310
|
const checker = program.getTypeChecker();
|
|
1496
2311
|
const flags = ts.SymbolFlags.Value | ts.SymbolFlags.Function | ts.SymbolFlags.Class | ts.SymbolFlags.Alias;
|
|
1497
|
-
const symbol = checker.getSymbolsInScope(location, flags).find((
|
|
2312
|
+
const symbol = checker.getSymbolsInScope(location, flags).find((s) => s.name === name);
|
|
1498
2313
|
if (!symbol) return autoImportedSymbol(ts, program, host, location.getSourceFile(), name);
|
|
1499
2314
|
return symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
1500
2315
|
}
|
|
@@ -1584,8 +2399,8 @@ function templateReferences(ts, ls, host, fileName, position) {
|
|
|
1584
2399
|
if (!program || !sf) return [];
|
|
1585
2400
|
const hit = symbolAt(ts, program, host, sf, position);
|
|
1586
2401
|
if (!hit) return [];
|
|
1587
|
-
return tagOccurrences(ts, program, host, hit.symbol, hit.name).map(({ fileName:
|
|
1588
|
-
fileName:
|
|
2402
|
+
return tagOccurrences(ts, program, host, hit.symbol, hit.name).map(({ fileName: f, span }) => ({
|
|
2403
|
+
fileName: f,
|
|
1589
2404
|
textSpan: span,
|
|
1590
2405
|
isWriteAccess: false,
|
|
1591
2406
|
isDefinition: false
|
|
@@ -1597,8 +2412,8 @@ function templateRenameLocations(ts, ls, host, fileName, position) {
|
|
|
1597
2412
|
if (!program || !sf) return [];
|
|
1598
2413
|
const hit = symbolAt(ts, program, host, sf, position);
|
|
1599
2414
|
if (!hit) return [];
|
|
1600
|
-
return tagOccurrences(ts, program, host, hit.symbol, hit.name).map(({ fileName:
|
|
1601
|
-
fileName:
|
|
2415
|
+
return tagOccurrences(ts, program, host, hit.symbol, hit.name).map(({ fileName: f, span }) => ({
|
|
2416
|
+
fileName: f,
|
|
1602
2417
|
textSpan: span
|
|
1603
2418
|
}));
|
|
1604
2419
|
}
|
|
@@ -1610,6 +2425,22 @@ function init(modules) {
|
|
|
1610
2425
|
create(info) {
|
|
1611
2426
|
const ls = info.languageService;
|
|
1612
2427
|
const host = info.languageServiceHost;
|
|
2428
|
+
const logged = /* @__PURE__ */ new Set();
|
|
2429
|
+
const report = (what, error) => {
|
|
2430
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2431
|
+
if (logged.has(`${what}: ${message}`)) return;
|
|
2432
|
+
logged.add(`${what}: ${message}`);
|
|
2433
|
+
const text = error instanceof Error ? error.stack ?? message : message;
|
|
2434
|
+
info.project?.projectService?.logger?.info(`[fluixi] ${what} failed: ${text}`);
|
|
2435
|
+
};
|
|
2436
|
+
const guarded = (what, run) => {
|
|
2437
|
+
try {
|
|
2438
|
+
return run();
|
|
2439
|
+
} catch (error) {
|
|
2440
|
+
report(what, error);
|
|
2441
|
+
return [];
|
|
2442
|
+
}
|
|
2443
|
+
};
|
|
1613
2444
|
const proxy = /* @__PURE__ */ Object.create(null);
|
|
1614
2445
|
for (const key of Object.keys(ls)) {
|
|
1615
2446
|
const member = ls[key];
|
|
@@ -1620,8 +2451,8 @@ function init(modules) {
|
|
|
1620
2451
|
isMemberCompletion: false,
|
|
1621
2452
|
isNewIdentifierLocation: true,
|
|
1622
2453
|
// Re-ask on every keystroke instead of letting the editor filter what it already
|
|
1623
|
-
// has. Inside a template the valid set changes with the cursor
|
|
1624
|
-
// that tag's props
|
|
2454
|
+
// has. Inside a template the valid set changes with the cursor, a tag name, then
|
|
2455
|
+
// that tag's props: and a list cached from one position is wrong at the next.
|
|
1625
2456
|
// Without this the props appear only after enough typing to force a fresh
|
|
1626
2457
|
// request, which reads as completion working intermittently.
|
|
1627
2458
|
isIncomplete: true,
|
|
@@ -1630,8 +2461,8 @@ function init(modules) {
|
|
|
1630
2461
|
const IMPLICIT_ANY = /* @__PURE__ */ new Set([7006, 7044]);
|
|
1631
2462
|
const dedupe = (items) => {
|
|
1632
2463
|
const seen = /* @__PURE__ */ new Set();
|
|
1633
|
-
return items.filter((
|
|
1634
|
-
const key = `${
|
|
2464
|
+
return items.filter((i) => {
|
|
2465
|
+
const key = `${i.fileName}:${i.textSpan.start}:${i.textSpan.length}`;
|
|
1635
2466
|
if (seen.has(key)) return false;
|
|
1636
2467
|
seen.add(key);
|
|
1637
2468
|
return true;
|
|
@@ -1650,13 +2481,21 @@ function init(modules) {
|
|
|
1650
2481
|
};
|
|
1651
2482
|
proxy.getSemanticDiagnostics = (fileName) => {
|
|
1652
2483
|
const base = reliableFilter(fileName, ls.getSemanticDiagnostics(fileName));
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
2484
|
+
return base.concat(
|
|
2485
|
+
guarded("template diagnostics", () => templateDiagnostics(ts, ls, fileName)),
|
|
2486
|
+
guarded("env and auth rules", () => rulesFor(fileName, false))
|
|
2487
|
+
);
|
|
2488
|
+
};
|
|
2489
|
+
proxy.getSuggestionDiagnostics = (fileName) => reliableFilter(fileName, ls.getSuggestionDiagnostics(fileName)).concat(
|
|
2490
|
+
guarded("env and auth rules", () => rulesFor(fileName, true))
|
|
2491
|
+
);
|
|
2492
|
+
const rulesFor = (fileName, suggestions) => {
|
|
2493
|
+
const program = ls.getProgram();
|
|
2494
|
+
if (!program) return [];
|
|
2495
|
+
return envAuthDiagnostics(ts, program, fileName, host).filter(
|
|
2496
|
+
(d) => d.category === ts.DiagnosticCategory.Suggestion === suggestions
|
|
2497
|
+
);
|
|
1658
2498
|
};
|
|
1659
|
-
proxy.getSuggestionDiagnostics = (fileName) => reliableFilter(fileName, ls.getSuggestionDiagnostics(fileName));
|
|
1660
2499
|
proxy.getQuickInfoAtPosition = (fileName, position) => {
|
|
1661
2500
|
try {
|
|
1662
2501
|
const prop = componentPropHover(ts, ls, host, fileName, position);
|
|
@@ -1750,6 +2589,11 @@ function init(modules) {
|
|
|
1750
2589
|
);
|
|
1751
2590
|
try {
|
|
1752
2591
|
const own = templateCodeFixes(ts, ls, fileName, start, end, errorCodes);
|
|
2592
|
+
const program = ls.getProgram();
|
|
2593
|
+
if (program && errorCodes.includes(RULE.envNotPublic)) {
|
|
2594
|
+
const fix = envPublicFix(ts, program, fileName, start, host);
|
|
2595
|
+
if (fix) own.push(fix);
|
|
2596
|
+
}
|
|
1753
2597
|
return own.length ? [...base, ...own] : base;
|
|
1754
2598
|
} catch {
|
|
1755
2599
|
return base;
|
|
@@ -1768,3 +2612,5 @@ function init(modules) {
|
|
|
1768
2612
|
};
|
|
1769
2613
|
}
|
|
1770
2614
|
module.exports = init;
|
|
2615
|
+
/*! @fluixi/template-parser v1.0.0-alpha.99 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2616
|
+
/*! @fluixi/compiler v1.0.0-alpha.99 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|