@bascik/language-server 0.1.0-rc.2 → 0.1.0-rc.4
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/dist/analyzer.d.ts +21 -5
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +702 -361
- package/dist/analyzer.js.map +1 -1
- package/dist/api-rules.d.ts +2 -0
- package/dist/api-rules.d.ts.map +1 -1
- package/dist/api-rules.js +17 -2
- package/dist/api-rules.js.map +1 -1
- package/dist/bin.js +5 -8
- package/dist/bin.js.map +1 -1
- package/dist/check.d.ts +50 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +173 -0
- package/dist/check.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +43 -100
- package/dist/cli.js.map +1 -1
- package/dist/compatibility-rules.json +41 -8
- package/dist/component-metadata.d.ts +1 -1
- package/dist/component-metadata.d.ts.map +1 -1
- package/dist/component-metadata.js +32 -26
- package/dist/component-metadata.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +10 -5
- package/dist/config.js.map +1 -1
- package/dist/directive-rules.d.ts +31 -0
- package/dist/directive-rules.d.ts.map +1 -0
- package/dist/directive-rules.js +74 -0
- package/dist/directive-rules.js.map +1 -0
- package/dist/ignore-comments.d.ts.map +1 -1
- package/dist/ignore-comments.js +13 -9
- package/dist/ignore-comments.js.map +1 -1
- package/dist/index.d.ts +13 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -11
- package/dist/index.js.map +1 -1
- package/dist/module-specifiers.d.ts.map +1 -1
- package/dist/module-specifiers.js +77 -40
- package/dist/module-specifiers.js.map +1 -1
- package/dist/project.d.ts +13 -1
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +59 -30
- package/dist/project.js.map +1 -1
- package/dist/rules.d.ts +29 -2
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +139 -1
- package/dist/rules.js.map +1 -1
- package/dist/server-script-rules.d.ts +2 -2
- package/dist/server-script-rules.d.ts.map +1 -1
- package/dist/server-script-rules.js +96 -80
- package/dist/server-script-rules.js.map +1 -1
- package/dist/server.d.ts +39 -2
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +245 -26
- package/dist/server.js.map +1 -1
- package/dist/start.d.ts +7 -0
- package/dist/start.d.ts.map +1 -0
- package/dist/start.js +12 -0
- package/dist/start.js.map +1 -0
- package/package.json +12 -8
package/dist/analyzer.js
CHANGED
|
@@ -1,160 +1,162 @@
|
|
|
1
|
-
import * as fs from
|
|
2
|
-
import * as path from
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { DiagnosticSeverity, CompletionItemKind, MarkupKind, CodeActionKind, } from "vscode-languageserver";
|
|
5
|
+
import { isInsideComponentRoots, normalizeComponentName, htmlSuppliesComponentProp, } from "./project.js";
|
|
6
|
+
import { findCompatibilityMatches } from "./rules.js";
|
|
7
|
+
import { analyzeApiRouteSource } from "./api-rules.js";
|
|
8
|
+
import { findComponentStructureProblems, isKnownBascikAttribute, isKnownBuildValue, unknownBascikAttributeMessage, unknownDirectiveValueMessage, } from "./directive-rules.js";
|
|
9
|
+
import { findModuleSpecifiers } from "./module-specifiers.js";
|
|
10
|
+
import { analyzeServerScriptSource } from "./server-script-rules.js";
|
|
11
|
+
import { analyzeComponentSource, } from "./component-metadata.js";
|
|
12
|
+
import { parseIgnoreDirectives, isDiagnosticIgnored, } from "./ignore-comments.js";
|
|
13
|
+
import { loadExtensionConfig } from "./config.js";
|
|
12
14
|
export const BUILT_IN_HTML_ELEMENTS = new Set([
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
15
|
+
"a",
|
|
16
|
+
"abbr",
|
|
17
|
+
"address",
|
|
18
|
+
"area",
|
|
19
|
+
"article",
|
|
20
|
+
"aside",
|
|
21
|
+
"audio",
|
|
22
|
+
"b",
|
|
23
|
+
"base",
|
|
24
|
+
"bdi",
|
|
25
|
+
"bdo",
|
|
26
|
+
"blockquote",
|
|
27
|
+
"body",
|
|
28
|
+
"br",
|
|
29
|
+
"button",
|
|
30
|
+
"canvas",
|
|
31
|
+
"caption",
|
|
32
|
+
"cite",
|
|
33
|
+
"code",
|
|
34
|
+
"col",
|
|
35
|
+
"colgroup",
|
|
36
|
+
"data",
|
|
37
|
+
"datalist",
|
|
38
|
+
"dd",
|
|
39
|
+
"del",
|
|
40
|
+
"details",
|
|
41
|
+
"dfn",
|
|
42
|
+
"dialog",
|
|
43
|
+
"div",
|
|
44
|
+
"dl",
|
|
45
|
+
"dt",
|
|
46
|
+
"em",
|
|
47
|
+
"embed",
|
|
48
|
+
"fieldset",
|
|
49
|
+
"figcaption",
|
|
50
|
+
"figure",
|
|
51
|
+
"footer",
|
|
52
|
+
"form",
|
|
53
|
+
"h1",
|
|
54
|
+
"h2",
|
|
55
|
+
"h3",
|
|
56
|
+
"h4",
|
|
57
|
+
"h5",
|
|
58
|
+
"h6",
|
|
59
|
+
"head",
|
|
60
|
+
"header",
|
|
61
|
+
"hgroup",
|
|
62
|
+
"hr",
|
|
63
|
+
"html",
|
|
64
|
+
"i",
|
|
65
|
+
"iframe",
|
|
66
|
+
"img",
|
|
67
|
+
"input",
|
|
68
|
+
"ins",
|
|
69
|
+
"kbd",
|
|
70
|
+
"label",
|
|
71
|
+
"legend",
|
|
72
|
+
"li",
|
|
73
|
+
"link",
|
|
74
|
+
"main",
|
|
75
|
+
"map",
|
|
76
|
+
"mark",
|
|
77
|
+
"meta",
|
|
78
|
+
"meter",
|
|
79
|
+
"nav",
|
|
80
|
+
"noscript",
|
|
81
|
+
"object",
|
|
82
|
+
"ol",
|
|
83
|
+
"optgroup",
|
|
84
|
+
"option",
|
|
85
|
+
"output",
|
|
86
|
+
"p",
|
|
87
|
+
"picture",
|
|
88
|
+
"pre",
|
|
89
|
+
"progress",
|
|
90
|
+
"q",
|
|
91
|
+
"rp",
|
|
92
|
+
"rt",
|
|
93
|
+
"ruby",
|
|
94
|
+
"s",
|
|
95
|
+
"samp",
|
|
96
|
+
"script",
|
|
97
|
+
"search",
|
|
98
|
+
"section",
|
|
99
|
+
"select",
|
|
100
|
+
"slot",
|
|
101
|
+
"small",
|
|
102
|
+
"source",
|
|
103
|
+
"span",
|
|
104
|
+
"strong",
|
|
105
|
+
"style",
|
|
106
|
+
"sub",
|
|
107
|
+
"summary",
|
|
108
|
+
"sup",
|
|
109
|
+
"table",
|
|
110
|
+
"tbody",
|
|
111
|
+
"td",
|
|
112
|
+
"template",
|
|
113
|
+
"textarea",
|
|
114
|
+
"tfoot",
|
|
115
|
+
"th",
|
|
116
|
+
"thead",
|
|
117
|
+
"time",
|
|
118
|
+
"title",
|
|
119
|
+
"tr",
|
|
120
|
+
"track",
|
|
121
|
+
"u",
|
|
122
|
+
"ul",
|
|
123
|
+
"var",
|
|
124
|
+
"video",
|
|
125
|
+
"wbr",
|
|
124
126
|
]);
|
|
125
127
|
export const SCRIPT_DIRECTIVES = [
|
|
126
128
|
{
|
|
127
|
-
name:
|
|
128
|
-
detail:
|
|
129
|
-
documentation:
|
|
129
|
+
name: "data-bascik-build",
|
|
130
|
+
detail: "Bascik build-time script",
|
|
131
|
+
documentation: "Executes in Node.js at build time. The default exported handler function returns HTML markup that replaces this script tag in the generated HTML.",
|
|
130
132
|
},
|
|
131
133
|
{
|
|
132
|
-
name:
|
|
133
|
-
detail:
|
|
134
|
-
documentation:
|
|
134
|
+
name: "data-bascik-routes",
|
|
135
|
+
detail: "Bascik dynamic routes script",
|
|
136
|
+
documentation: "Executes in Node.js at build time. The default exported handler function returns an array of dynamic route parameters to generate multiple pages from a template.",
|
|
135
137
|
},
|
|
136
138
|
{
|
|
137
|
-
name:
|
|
138
|
-
detail:
|
|
139
|
-
documentation:
|
|
139
|
+
name: "data-bascik-server",
|
|
140
|
+
detail: "Bascik server-side request script",
|
|
141
|
+
documentation: "Executes in Node.js per request on production and dev servers. The default exported handler function replaces this script tag.",
|
|
140
142
|
},
|
|
141
143
|
{
|
|
142
|
-
name:
|
|
143
|
-
detail:
|
|
144
|
-
documentation:
|
|
144
|
+
name: "data-bascik-stream",
|
|
145
|
+
detail: "Bascik server-side streaming script",
|
|
146
|
+
documentation: "Executes in Node.js per request on production servers. Streams chunked HTML responses to the client as data becomes available.",
|
|
145
147
|
},
|
|
146
148
|
];
|
|
147
|
-
export const SCRIPT_BLOCK_RE = /(<script\
|
|
149
|
+
export const SCRIPT_BLOCK_RE = /(<script(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)<\/script(?:[\t\n\f\r /][^>]*)?>/gi;
|
|
148
150
|
export function maskHtmlRawTextContents(html) {
|
|
149
|
-
return html.replace(/(<(script|style|textarea)\
|
|
151
|
+
return html.replace(/(<(script|style|textarea)(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/\2(?:[\t\n\f\r /][^>]*)?>)/gi, (_match, openTag, _tagName, content, closeTag) => `${openTag}${" ".repeat(content.length)}${closeTag}`);
|
|
150
152
|
}
|
|
151
153
|
export function escapeRegExp(value) {
|
|
152
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g,
|
|
154
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
153
155
|
}
|
|
154
156
|
export function findMatchingClose(html, tagName, contentStart) {
|
|
155
|
-
const tn = tagName.replace(/[.*+?^${}()|[\]\\]/g,
|
|
156
|
-
const openRe = new RegExp(`<${tn}[\\s>]`,
|
|
157
|
-
const closeRe = new RegExp(`<\\/${tn}>`,
|
|
157
|
+
const tn = tagName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
158
|
+
const openRe = new RegExp(`<${tn}[\\s>]`, "gi");
|
|
159
|
+
const closeRe = new RegExp(`<\\/${tn}>`, "gi");
|
|
158
160
|
let depth = 1;
|
|
159
161
|
let pos = contentStart;
|
|
160
162
|
while (pos < html.length) {
|
|
@@ -182,7 +184,7 @@ export function findMatchingClose(html, tagName, contentStart) {
|
|
|
182
184
|
else if (char === "'" && !inDoubleQuote) {
|
|
183
185
|
inSingleQuote = !inSingleQuote;
|
|
184
186
|
}
|
|
185
|
-
else if (char ===
|
|
187
|
+
else if (char === ">" && !inDoubleQuote && !inSingleQuote) {
|
|
186
188
|
tagEnd = i + 1;
|
|
187
189
|
break;
|
|
188
190
|
}
|
|
@@ -204,17 +206,90 @@ export function findMatchingClose(html, tagName, contentStart) {
|
|
|
204
206
|
}
|
|
205
207
|
return -1;
|
|
206
208
|
}
|
|
209
|
+
const isTagNameStart = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
210
|
+
const isTagNameChar = (code) => isTagNameStart(code) ||
|
|
211
|
+
(code >= 48 && code <= 57) ||
|
|
212
|
+
code === 95 ||
|
|
213
|
+
code === 45;
|
|
214
|
+
/**
|
|
215
|
+
* Tag tokens in source order, matching what
|
|
216
|
+
* `/<\/?([A-Za-z][\w-]*)(?:[^>"']|"[^"]*"|'[^']*')*>/g` finds, in linear time.
|
|
217
|
+
* The regex retries every `<` and rescans to the end on an unclosed tag, which
|
|
218
|
+
* is quadratic. The tag-body scan is deterministic (first `>` outside quotes),
|
|
219
|
+
* so its result depends only on where the scan stands; `tagEndFrom` remembers
|
|
220
|
+
* it per position and no position is scanned twice.
|
|
221
|
+
*/
|
|
222
|
+
function* scanTags(source) {
|
|
223
|
+
const length = source.length;
|
|
224
|
+
// 0 = not scanned yet, -1 = no closing `>`, otherwise the index after `>`.
|
|
225
|
+
const tagEndFrom = new Int32Array(length + 1);
|
|
226
|
+
const visited = [];
|
|
227
|
+
const findTagEnd = (from) => {
|
|
228
|
+
let position = from;
|
|
229
|
+
let end = -1;
|
|
230
|
+
visited.length = 0;
|
|
231
|
+
while (position < length) {
|
|
232
|
+
const known = tagEndFrom[position];
|
|
233
|
+
if (known !== 0) {
|
|
234
|
+
end = known;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
visited.push(position);
|
|
238
|
+
const code = source.charCodeAt(position);
|
|
239
|
+
if (code === 62 /* > */) {
|
|
240
|
+
end = position + 1;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
if (code === 34 /* " */ || code === 39 /* ' */) {
|
|
244
|
+
const close = source.indexOf(source[position], position + 1);
|
|
245
|
+
if (close < 0)
|
|
246
|
+
break;
|
|
247
|
+
position = close + 1;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
position++;
|
|
251
|
+
}
|
|
252
|
+
for (const index of visited)
|
|
253
|
+
tagEndFrom[index] = end;
|
|
254
|
+
return end;
|
|
255
|
+
};
|
|
256
|
+
let cursor = 0;
|
|
257
|
+
while (cursor < length) {
|
|
258
|
+
const open = source.indexOf("<", cursor);
|
|
259
|
+
if (open < 0)
|
|
260
|
+
return;
|
|
261
|
+
const closing = source.charCodeAt(open + 1) === 47; /* / */
|
|
262
|
+
const nameStart = open + (closing ? 2 : 1);
|
|
263
|
+
if (!isTagNameStart(source.charCodeAt(nameStart))) {
|
|
264
|
+
cursor = open + 1;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
let nameEnd = nameStart + 1;
|
|
268
|
+
while (nameEnd < length && isTagNameChar(source.charCodeAt(nameEnd)))
|
|
269
|
+
nameEnd++;
|
|
270
|
+
const end = findTagEnd(nameEnd);
|
|
271
|
+
if (end < 0) {
|
|
272
|
+
cursor = open + 1;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
yield {
|
|
276
|
+
name: source.slice(nameStart, nameEnd),
|
|
277
|
+
closing,
|
|
278
|
+
text: source.slice(open, end),
|
|
279
|
+
};
|
|
280
|
+
cursor = end;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
207
283
|
export function findNearestParentComponent(source, componentMap) {
|
|
208
284
|
const stack = [];
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (match[0].startsWith('</')) {
|
|
285
|
+
for (const tag of scanTags(source)) {
|
|
286
|
+
const name = tag.name.toLowerCase();
|
|
287
|
+
if (tag.closing) {
|
|
213
288
|
const matchingIndex = stack.lastIndexOf(name);
|
|
214
289
|
if (matchingIndex >= 0)
|
|
215
290
|
stack.splice(matchingIndex);
|
|
216
291
|
}
|
|
217
|
-
else if (!/\/\s*>$/.test(
|
|
292
|
+
else if (!/\/\s*>$/.test(tag.text)) {
|
|
218
293
|
stack.push(name);
|
|
219
294
|
}
|
|
220
295
|
}
|
|
@@ -222,7 +297,7 @@ export function findNearestParentComponent(source, componentMap) {
|
|
|
222
297
|
}
|
|
223
298
|
export function parseScriptOpenTagAttributes(openTag) {
|
|
224
299
|
const attrs = new Map();
|
|
225
|
-
const insideTag = openTag.replace(/^<script\b/i,
|
|
300
|
+
const insideTag = openTag.replace(/^<script\b/i, "").replace(/>$/, "");
|
|
226
301
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
227
302
|
let match;
|
|
228
303
|
while ((match = attrRe.exec(insideTag)) !== null) {
|
|
@@ -234,14 +309,32 @@ export function parseScriptOpenTagAttributes(openTag) {
|
|
|
234
309
|
}
|
|
235
310
|
return attrs;
|
|
236
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Attribute values of any open tag, keyed by lowercased name. A bare attribute
|
|
314
|
+
* maps to `""`, as the DOM reports it. The first occurrence of a name wins, as
|
|
315
|
+
* in the HTML tokenizer, and text inside a quoted value is never read as an
|
|
316
|
+
* attribute. An unquoted value keeps its slashes; a trailing `/>` ends it.
|
|
317
|
+
*/
|
|
318
|
+
function readOpenTagAttributes(openTag) {
|
|
319
|
+
const attrs = new Map();
|
|
320
|
+
const insideTag = openTag.replace(/^<[^\s/>]+/, "").replace(/\/?>$/, "");
|
|
321
|
+
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
322
|
+
let match;
|
|
323
|
+
while ((match = attrRe.exec(insideTag)) !== null) {
|
|
324
|
+
const name = match[1].toLowerCase();
|
|
325
|
+
if (!attrs.has(name))
|
|
326
|
+
attrs.set(name, match[2] ?? match[3] ?? match[4] ?? "");
|
|
327
|
+
}
|
|
328
|
+
return attrs;
|
|
329
|
+
}
|
|
237
330
|
export function resolveScriptTarget(value, documentDir, importRootAbs, kind) {
|
|
238
|
-
if (value.startsWith(
|
|
331
|
+
if (value.startsWith("./") || value.startsWith("../"))
|
|
239
332
|
return path.resolve(documentDir, value);
|
|
240
|
-
if (value.startsWith(
|
|
333
|
+
if (value.startsWith("@/"))
|
|
241
334
|
return path.resolve(importRootAbs, value.slice(2));
|
|
242
|
-
if (value.startsWith(
|
|
335
|
+
if (value.startsWith("/"))
|
|
243
336
|
return undefined;
|
|
244
|
-
if (kind ===
|
|
337
|
+
if (kind === "src") {
|
|
245
338
|
if (/^[a-z][a-z\d+.-]*:/i.test(value))
|
|
246
339
|
return undefined;
|
|
247
340
|
return path.resolve(documentDir, value);
|
|
@@ -249,17 +342,17 @@ export function resolveScriptTarget(value, documentDir, importRootAbs, kind) {
|
|
|
249
342
|
return undefined;
|
|
250
343
|
}
|
|
251
344
|
export function leadingSlashSpecifierMessage(specifier) {
|
|
252
|
-
const rest = specifier.replace(/^\/+/,
|
|
345
|
+
const rest = specifier.replace(/^\/+/, "");
|
|
253
346
|
return (`Leading-slash specifier '${specifier}' is not supported in Bascik scripts. ` +
|
|
254
347
|
`A bare '/' is ambiguous (filesystem root vs. site root). ` +
|
|
255
348
|
`Use '@/${rest}' to resolve against scripts.importRoot, ` +
|
|
256
349
|
`or './${rest}' to resolve relative to this file.`);
|
|
257
350
|
}
|
|
258
351
|
export function collectLeadingSlashDiagnostics(document, openTag, scriptBody, blockStart, attrs) {
|
|
259
|
-
if (!attrs.has(
|
|
260
|
-
!attrs.has(
|
|
261
|
-
!attrs.has(
|
|
262
|
-
!attrs.has(
|
|
352
|
+
if (!attrs.has("data-bascik-build") &&
|
|
353
|
+
!attrs.has("data-bascik-server") &&
|
|
354
|
+
!attrs.has("data-bascik-routes") &&
|
|
355
|
+
!attrs.has("data-bascik-stream")) {
|
|
263
356
|
return [];
|
|
264
357
|
}
|
|
265
358
|
const out = [];
|
|
@@ -271,40 +364,53 @@ export function collectLeadingSlashDiagnostics(document, openTag, scriptBody, bl
|
|
|
271
364
|
},
|
|
272
365
|
message: leadingSlashSpecifierMessage(specifier),
|
|
273
366
|
severity: DiagnosticSeverity.Error,
|
|
274
|
-
source:
|
|
275
|
-
code:
|
|
367
|
+
source: "bascik",
|
|
368
|
+
code: "leading-slash-specifier",
|
|
276
369
|
});
|
|
277
370
|
};
|
|
278
371
|
const srcMatch = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(openTag);
|
|
279
372
|
if (srcMatch) {
|
|
280
|
-
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ??
|
|
281
|
-
if (srcValue.startsWith(
|
|
373
|
+
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? "";
|
|
374
|
+
if (srcValue.startsWith("/")) {
|
|
282
375
|
const valueStart = blockStart + (srcMatch.index ?? 0) + srcMatch[0].indexOf(srcValue);
|
|
283
376
|
push(valueStart, valueStart + srcValue.length, srcValue);
|
|
284
377
|
}
|
|
285
378
|
}
|
|
286
379
|
const bodyStart = blockStart + openTag.length;
|
|
287
380
|
for (const { start, end, value } of findModuleSpecifiers(scriptBody)) {
|
|
288
|
-
if (value.startsWith(
|
|
381
|
+
if (value.startsWith("/"))
|
|
289
382
|
push(bodyStart + start, bodyStart + end, value);
|
|
290
383
|
}
|
|
291
384
|
return out;
|
|
292
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Escapes plain text so it renders as typed inside markdown: syntax characters are backslash-escaped, runs of
|
|
388
|
+
* spaces become ` `, `>` is escaped, and a newline becomes a hard break. Descriptions come from a
|
|
389
|
+
* component's `<!-- @bascik -->` header and are prose, not markdown, so `<unsafe>` or `*not bold*` stay literal.
|
|
390
|
+
* Matches VS Code's `MarkdownString.appendText`, so the text looks the same in every editor.
|
|
391
|
+
*/
|
|
392
|
+
export function escapeMarkdownText(text) {
|
|
393
|
+
return text
|
|
394
|
+
.replace(/[\\`*_{}[\]()#+\-!~]/g, "\\$&")
|
|
395
|
+
.replace(/([ \t]+)/g, (_match, spaces) => " ".repeat(spaces.length))
|
|
396
|
+
.replace(/>/g, "\\>")
|
|
397
|
+
.replace(/\n/g, "\\\n");
|
|
398
|
+
}
|
|
293
399
|
export function createComponentDocumentationMarkdown(relativePath, metadata) {
|
|
294
400
|
const parts = [];
|
|
295
401
|
if (metadata?.description) {
|
|
296
|
-
parts.push(metadata.description,
|
|
402
|
+
parts.push(escapeMarkdownText(metadata.description), "\n\n");
|
|
297
403
|
}
|
|
298
404
|
parts.push(`Bascik component from \`${relativePath}\`.`);
|
|
299
405
|
if (metadata) {
|
|
300
|
-
parts.push(
|
|
301
|
-
appendMetadataMembersMarkdown(parts,
|
|
302
|
-
appendMetadataMembersMarkdown(parts,
|
|
406
|
+
parts.push("\n\n");
|
|
407
|
+
appendMetadataMembersMarkdown(parts, "Props", metadata.props);
|
|
408
|
+
appendMetadataMembersMarkdown(parts, "Slots", [
|
|
303
409
|
...metadata.slots,
|
|
304
410
|
...(metadata.defaultSlot ? [metadata.defaultSlot] : []),
|
|
305
411
|
]);
|
|
306
412
|
}
|
|
307
|
-
return parts.join(
|
|
413
|
+
return parts.join("");
|
|
308
414
|
}
|
|
309
415
|
function appendMetadataMembersMarkdown(parts, label, members) {
|
|
310
416
|
if (members.length === 0)
|
|
@@ -313,15 +419,15 @@ function appendMetadataMembersMarkdown(parts, label, members) {
|
|
|
313
419
|
for (const member of members) {
|
|
314
420
|
parts.push(`- \`${member.name}\``);
|
|
315
421
|
if (member.description) {
|
|
316
|
-
parts.push(`: ${member.description}`);
|
|
422
|
+
parts.push(`: ${escapeMarkdownText(member.description)}`);
|
|
317
423
|
}
|
|
318
|
-
parts.push(
|
|
424
|
+
parts.push("\n");
|
|
319
425
|
}
|
|
320
|
-
parts.push(
|
|
426
|
+
parts.push("\n");
|
|
321
427
|
}
|
|
322
428
|
export function completionReplacementRange(document, position) {
|
|
323
429
|
const offset = document.offsetAt(position);
|
|
324
|
-
const prefix = /[^\s<>"'=]*$/.exec(document.getText().slice(0, offset))?.[0] ??
|
|
430
|
+
const prefix = /[^\s<>"'=]*$/.exec(document.getText().slice(0, offset))?.[0] ?? "";
|
|
325
431
|
return {
|
|
326
432
|
start: document.positionAt(offset - prefix.length),
|
|
327
433
|
end: position,
|
|
@@ -329,13 +435,13 @@ export function completionReplacementRange(document, position) {
|
|
|
329
435
|
}
|
|
330
436
|
export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts = []) {
|
|
331
437
|
const { languageId } = document;
|
|
332
|
-
if (languageId !==
|
|
333
|
-
languageId !==
|
|
334
|
-
languageId !==
|
|
335
|
-
languageId !==
|
|
438
|
+
if (languageId !== "css" &&
|
|
439
|
+
languageId !== "javascript" &&
|
|
440
|
+
languageId !== "typescript" &&
|
|
441
|
+
languageId !== "html") {
|
|
336
442
|
return [];
|
|
337
443
|
}
|
|
338
|
-
const normalizedDocumentPath = fsPath.replace(/\\/g,
|
|
444
|
+
const normalizedDocumentPath = fsPath.replace(/\\/g, "/");
|
|
339
445
|
if (/\.(test|spec)\.[a-z0-9]+$/i.test(normalizedDocumentPath)) {
|
|
340
446
|
return [];
|
|
341
447
|
}
|
|
@@ -343,16 +449,33 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
343
449
|
const diagnostics = [];
|
|
344
450
|
const isComponentDocument = snapshot !== undefined &&
|
|
345
451
|
isInsideComponentRoots(normalizedDocumentPath, snapshot.componentRoots);
|
|
346
|
-
|
|
347
|
-
|
|
452
|
+
// The API folder is `directory.api`, so it is read from the project when there is one. Without a project
|
|
453
|
+
// (a file opened on its own), the default folder name is the best guess.
|
|
454
|
+
const isInsideApiRoot = snapshot?.apiRoot
|
|
455
|
+
? isInsideComponentRoots(normalizedDocumentPath, [snapshot.apiRoot])
|
|
456
|
+
: normalizedDocumentPath.includes("/src/api/");
|
|
457
|
+
const isApiRouteDocument = isInsideApiRoot &&
|
|
458
|
+
(languageId === "typescript" || languageId === "javascript");
|
|
348
459
|
if (isApiRouteDocument) {
|
|
460
|
+
if (path.basename(normalizedDocumentPath).startsWith("_middleware.")) {
|
|
461
|
+
diagnostics.push({
|
|
462
|
+
range: {
|
|
463
|
+
start: { line: 0, character: 0 },
|
|
464
|
+
end: { line: 0, character: Math.min(text.length, 10) },
|
|
465
|
+
},
|
|
466
|
+
message: "Middleware chains (_middleware.ts) are not supported by design in Bascik API routes. Compose plain functions directly in handler files.",
|
|
467
|
+
severity: DiagnosticSeverity.Error,
|
|
468
|
+
source: "bascik",
|
|
469
|
+
code: "middleware-unsupported",
|
|
470
|
+
});
|
|
471
|
+
}
|
|
349
472
|
const apiDiags = analyzeApiRouteSource(text);
|
|
350
473
|
for (const diag of apiDiags) {
|
|
351
474
|
let severity = DiagnosticSeverity.Warning;
|
|
352
|
-
if (diag.severity ===
|
|
475
|
+
if (diag.severity === "error") {
|
|
353
476
|
severity = DiagnosticSeverity.Error;
|
|
354
477
|
}
|
|
355
|
-
else if (diag.severity ===
|
|
478
|
+
else if (diag.severity === "info") {
|
|
356
479
|
severity = DiagnosticSeverity.Information;
|
|
357
480
|
}
|
|
358
481
|
diagnostics.push({
|
|
@@ -362,65 +485,70 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
362
485
|
},
|
|
363
486
|
message: diag.message,
|
|
364
487
|
severity,
|
|
365
|
-
source:
|
|
488
|
+
source: "bascik",
|
|
489
|
+
code: diag.code,
|
|
366
490
|
});
|
|
367
491
|
}
|
|
368
492
|
}
|
|
369
|
-
if (languageId ===
|
|
370
|
-
if (snapshot &&
|
|
493
|
+
if (languageId === "html" && fsPath.startsWith("/")) {
|
|
494
|
+
if (snapshot &&
|
|
495
|
+
isInsideComponentRoots(normalizedDocumentPath, snapshot.componentRoots)) {
|
|
371
496
|
const fileName = path.basename(normalizedDocumentPath);
|
|
372
|
-
const nameWithoutExt = fileName.replace(/\.html$/i,
|
|
373
|
-
if (!nameWithoutExt.includes(
|
|
497
|
+
const nameWithoutExt = fileName.replace(/\.html$/i, "").toLowerCase();
|
|
498
|
+
if (!nameWithoutExt.includes("-") &&
|
|
374
499
|
!BUILT_IN_HTML_ELEMENTS.has(nameWithoutExt)) {
|
|
375
500
|
diagnostics.push({
|
|
376
501
|
range: {
|
|
377
502
|
start: { line: 0, character: 0 },
|
|
378
503
|
end: { line: 0, character: Math.min(text.length, 10) },
|
|
379
504
|
},
|
|
505
|
+
code: "component-name-not-hyphenated",
|
|
380
506
|
message: `Component "${nameWithoutExt}" is not hyphenated. Under WHATWG HTML §4.13, custom elements should include a hyphen (e.g. "my-${nameWithoutExt}") to avoid collisions with future HTML standards.`,
|
|
381
507
|
severity: DiagnosticSeverity.Warning,
|
|
382
|
-
source:
|
|
508
|
+
source: "bascik",
|
|
383
509
|
});
|
|
384
510
|
}
|
|
385
511
|
}
|
|
386
512
|
}
|
|
513
|
+
// Scoping rules describe what Bascik cannot rewrite, so they apply to component files, the only code it scopes.
|
|
514
|
+
// Pages and API routes are not scoped. Without a project there is no way to tell, so every file is checked.
|
|
515
|
+
const appliesScopingRules = snapshot === undefined || isComponentDocument;
|
|
387
516
|
const addCompatibilityDiagnostics = (sourceText, kind, offset) => {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const start = document.positionAt(offset + match.index);
|
|
397
|
-
const end = document.positionAt(offset + match.index + Math.max(match[0].length, 1));
|
|
517
|
+
if (!appliesScopingRules)
|
|
518
|
+
return;
|
|
519
|
+
for (const { rule, index, length } of findCompatibilityMatches(sourceText, kind)) {
|
|
520
|
+
const start = document.positionAt(offset + index);
|
|
521
|
+
const end = document.positionAt(offset + index + length);
|
|
522
|
+
const severity = rule.severity === "warning"
|
|
523
|
+
? DiagnosticSeverity.Warning
|
|
524
|
+
: DiagnosticSeverity.Error;
|
|
398
525
|
diagnostics.push({
|
|
399
526
|
range: { start, end },
|
|
400
527
|
message: `${rule.message} ${rule.suggestion}`,
|
|
401
|
-
severity
|
|
402
|
-
source:
|
|
528
|
+
severity,
|
|
529
|
+
source: "bascik",
|
|
530
|
+
code: rule.id,
|
|
403
531
|
});
|
|
404
532
|
}
|
|
405
533
|
};
|
|
406
534
|
const isJavaScriptScriptTag = (openTag) => {
|
|
407
535
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
408
|
-
const typeValue = attrs.get(
|
|
536
|
+
const typeValue = attrs.get("type");
|
|
409
537
|
if (!typeValue || typeValue === true)
|
|
410
538
|
return true;
|
|
411
539
|
const normalized = String(typeValue).trim().toLowerCase();
|
|
412
|
-
return (normalized ===
|
|
413
|
-
normalized ===
|
|
414
|
-
normalized ===
|
|
415
|
-
normalized ===
|
|
416
|
-
normalized ===
|
|
540
|
+
return (normalized === "module" ||
|
|
541
|
+
normalized === "text/javascript" ||
|
|
542
|
+
normalized === "application/javascript" ||
|
|
543
|
+
normalized === "text/ecmascript" ||
|
|
544
|
+
normalized === "application/ecmascript");
|
|
417
545
|
};
|
|
418
546
|
const scriptBlockRe = new RegExp(SCRIPT_BLOCK_RE.source, SCRIPT_BLOCK_RE.flags);
|
|
419
|
-
const styleBlockRe = /(<style\
|
|
420
|
-
if (languageId ===
|
|
547
|
+
const styleBlockRe = /(<style(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)<\/style(?:[\t\n\f\r /][^>]*)?>/gi;
|
|
548
|
+
if (languageId === "html") {
|
|
421
549
|
if (isComponentDocument) {
|
|
422
550
|
const metadata = analyzeComponentSource(text, {
|
|
423
|
-
hasCompanionStyles: fs.existsSync(fsPath.replace(/\.html$/i,
|
|
551
|
+
hasCompanionStyles: fs.existsSync(fsPath.replace(/\.html$/i, ".css")),
|
|
424
552
|
});
|
|
425
553
|
for (const metadataDiagnostic of metadata.diagnostics) {
|
|
426
554
|
diagnostics.push({
|
|
@@ -430,19 +558,22 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
430
558
|
},
|
|
431
559
|
message: metadataDiagnostic.message,
|
|
432
560
|
severity: DiagnosticSeverity.Warning,
|
|
433
|
-
source:
|
|
561
|
+
source: "bascik",
|
|
434
562
|
code: metadataDiagnostic.code,
|
|
435
563
|
});
|
|
436
564
|
}
|
|
437
|
-
const referenceScanText = maskHtmlRawTextContents(text).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) =>
|
|
438
|
-
const declaredIds = new Set(Array.from(referenceScanText.matchAll(/\sid\s*=\s*(?:"([^"]+)"|'([^']+)')/gi))
|
|
439
|
-
.map((match) => match[1] ?? match[2])
|
|
565
|
+
const referenceScanText = maskHtmlRawTextContents(text).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) => " ".repeat(comment.length));
|
|
566
|
+
const declaredIds = new Set(Array.from(referenceScanText.matchAll(/\sid\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s"'=<>`/>]+))/gi))
|
|
567
|
+
.map((match) => match[1] ?? match[2] ?? match[3])
|
|
440
568
|
.filter((id) => Boolean(id)));
|
|
441
|
-
const idReferenceAttributeRegex = /\s(for|itemref|aria-activedescendant|aria-details|aria-errormessage|aria-labelledby|aria-describedby|aria-controls|aria-owns|aria-flowto)\s*=\s*(?:"([^"]*)"|'([^']*)')/gi;
|
|
569
|
+
const idReferenceAttributeRegex = /\s(for|itemref|aria-activedescendant|aria-details|aria-errormessage|aria-labelledby|aria-describedby|aria-controls|aria-owns|aria-flowto)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`/>]+))/gi;
|
|
442
570
|
let idReferenceMatch;
|
|
443
571
|
while ((idReferenceMatch =
|
|
444
572
|
idReferenceAttributeRegex.exec(referenceScanText)) !== null) {
|
|
445
|
-
const value = idReferenceMatch[2] ??
|
|
573
|
+
const value = idReferenceMatch[2] ??
|
|
574
|
+
idReferenceMatch[3] ??
|
|
575
|
+
idReferenceMatch[4] ??
|
|
576
|
+
"";
|
|
446
577
|
for (const id of value.trim().split(/\s+/).filter(Boolean)) {
|
|
447
578
|
if (declaredIds.has(id))
|
|
448
579
|
continue;
|
|
@@ -451,17 +582,20 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
451
582
|
const end = document.positionAt(idReferenceMatch.index + Math.max(valueOffset, 0) + id.length);
|
|
452
583
|
diagnostics.push({
|
|
453
584
|
range: { start, end },
|
|
585
|
+
code: "id-reference-undeclared",
|
|
454
586
|
message: `ID reference "${id}" is not declared in this component and will be left unscoped.`,
|
|
455
587
|
severity: DiagnosticSeverity.Information,
|
|
456
|
-
source:
|
|
588
|
+
source: "bascik",
|
|
457
589
|
});
|
|
458
590
|
}
|
|
459
591
|
}
|
|
460
|
-
const fragmentReferenceRegex = /\shref\s*=\s*(?:"#([^"]+)"|'#([^']+)')/gi;
|
|
592
|
+
const fragmentReferenceRegex = /\shref\s*=\s*(?:"#([^"]+)"|'#([^']+)'|#([^\s"'=<>`/>]+))/gi;
|
|
461
593
|
let fragmentReferenceMatch;
|
|
462
594
|
while ((fragmentReferenceMatch =
|
|
463
595
|
fragmentReferenceRegex.exec(referenceScanText)) !== null) {
|
|
464
|
-
const id = fragmentReferenceMatch[1] ??
|
|
596
|
+
const id = fragmentReferenceMatch[1] ??
|
|
597
|
+
fragmentReferenceMatch[2] ??
|
|
598
|
+
fragmentReferenceMatch[3];
|
|
465
599
|
if (!id || declaredIds.has(id))
|
|
466
600
|
continue;
|
|
467
601
|
const idOffset = fragmentReferenceMatch[0].indexOf(id);
|
|
@@ -469,31 +603,33 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
469
603
|
const end = document.positionAt(fragmentReferenceMatch.index + Math.max(idOffset, 0) + id.length);
|
|
470
604
|
diagnostics.push({
|
|
471
605
|
range: { start, end },
|
|
606
|
+
code: "id-reference-undeclared",
|
|
472
607
|
message: `ID reference "${id}" is not declared in this component and will be left unscoped.`,
|
|
473
608
|
severity: DiagnosticSeverity.Information,
|
|
474
|
-
source:
|
|
609
|
+
source: "bascik",
|
|
475
610
|
});
|
|
476
611
|
}
|
|
477
612
|
}
|
|
478
|
-
const preserveDirectiveRegex = /data-bascik-preserve(?:\s*=\s*("([^"]*)"|'([^']*)'))?/gi;
|
|
613
|
+
const preserveDirectiveRegex = /data-bascik-preserve(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s"'=<>`/>]+)))?/gi;
|
|
479
614
|
let preserveMatch;
|
|
480
615
|
while ((preserveMatch = preserveDirectiveRegex.exec(text)) !== null) {
|
|
481
616
|
if (preserveMatch[1] === undefined)
|
|
482
617
|
continue;
|
|
483
|
-
const value = preserveMatch[2] ?? preserveMatch[3] ??
|
|
618
|
+
const value = preserveMatch[2] ?? preserveMatch[3] ?? preserveMatch[4] ?? "";
|
|
484
619
|
for (const preserveToken of value.trim().split(/\s+/).filter(Boolean)) {
|
|
485
|
-
if (preserveToken ===
|
|
486
|
-
preserveToken ===
|
|
487
|
-
preserveToken ===
|
|
620
|
+
if (preserveToken === "id" ||
|
|
621
|
+
preserveToken === "name" ||
|
|
622
|
+
preserveToken === "class")
|
|
488
623
|
continue;
|
|
489
624
|
const tokenOffset = preserveMatch[0].indexOf(preserveToken);
|
|
490
625
|
const start = document.positionAt(preserveMatch.index + Math.max(tokenOffset, 0));
|
|
491
626
|
const end = document.positionAt(preserveMatch.index + Math.max(tokenOffset, 0) + preserveToken.length);
|
|
492
627
|
diagnostics.push({
|
|
493
628
|
range: { start, end },
|
|
629
|
+
code: "unknown-preserve-token",
|
|
494
630
|
message: `Unknown data-bascik-preserve token "${preserveToken}". Valid tokens are id, name, and class.`,
|
|
495
631
|
severity: DiagnosticSeverity.Warning,
|
|
496
|
-
source:
|
|
632
|
+
source: "bascik",
|
|
497
633
|
});
|
|
498
634
|
}
|
|
499
635
|
}
|
|
@@ -501,35 +637,38 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
501
637
|
let formMatch;
|
|
502
638
|
while (isComponentDocument &&
|
|
503
639
|
(formMatch = formOpenTagRegex.exec(text)) !== null) {
|
|
504
|
-
const
|
|
505
|
-
const action =
|
|
640
|
+
const formAttributes = readOpenTagAttributes(formMatch[0]);
|
|
641
|
+
const action = formAttributes.get("action");
|
|
506
642
|
if (!action || !/^(?:https?:)?\/\//i.test(action))
|
|
507
643
|
continue;
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
644
|
+
const preserveValue = formAttributes.get("data-bascik-preserve");
|
|
645
|
+
// A bare directive, or an empty value (the bare form as serializers
|
|
646
|
+
// write it), preserves every attribute, `name` included.
|
|
647
|
+
const preserveTokens = (preserveValue ?? "")
|
|
648
|
+
.trim()
|
|
649
|
+
.split(/\s+/)
|
|
650
|
+
.filter(Boolean);
|
|
651
|
+
const preservesName = preserveValue !== undefined &&
|
|
652
|
+
(preserveTokens.length === 0 || preserveTokens.includes("name"));
|
|
515
653
|
if (preservesName)
|
|
516
654
|
continue;
|
|
517
655
|
const start = document.positionAt(formMatch.index);
|
|
518
656
|
const end = document.positionAt(formMatch.index + formMatch[0].length);
|
|
519
657
|
diagnostics.push({
|
|
520
658
|
range: { start, end },
|
|
659
|
+
code: "external-form-missing-preserve-name",
|
|
521
660
|
message: 'External form actions require data-bascik-preserve="name" so submitted field names remain literal.',
|
|
522
661
|
severity: DiagnosticSeverity.Warning,
|
|
523
|
-
source:
|
|
662
|
+
source: "bascik",
|
|
524
663
|
});
|
|
525
664
|
}
|
|
526
665
|
if (snapshot && isComponentDocument) {
|
|
527
666
|
const componentName = normalizeComponentName(fsPath);
|
|
528
|
-
const directiveRegex = /data-bascik-attr-([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([\w-]+)"|'([\w-]+)')/gi;
|
|
667
|
+
const directiveRegex = /data-bascik-attr-([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([\w-]+)"|'([\w-]+)'|([\w-]+))/gi;
|
|
529
668
|
let directiveMatch;
|
|
530
669
|
while ((directiveMatch = directiveRegex.exec(text)) !== null) {
|
|
531
670
|
const targetName = directiveMatch[1];
|
|
532
|
-
const propName = directiveMatch[2] ?? directiveMatch[3];
|
|
671
|
+
const propName = directiveMatch[2] ?? directiveMatch[3] ?? directiveMatch[4];
|
|
533
672
|
const suppliedInOpen = openDocumentTexts.some(({ text }) => htmlSuppliesComponentProp(text, componentName, propName));
|
|
534
673
|
const suppliedInDisk = Array.from(snapshot.htmlUsageByFile.values()).some((usageHtml) => htmlSuppliesComponentProp(usageHtml, componentName, propName));
|
|
535
674
|
if (suppliedInOpen || suppliedInDisk)
|
|
@@ -538,23 +677,26 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
538
677
|
const end = document.positionAt(directiveMatch.index + directiveMatch[0].length);
|
|
539
678
|
diagnostics.push({
|
|
540
679
|
range: { start, end },
|
|
680
|
+
// A hint, not a warning. Whether anything supplies the prop is a question about every page, and the
|
|
681
|
+
// snapshot only reads src/**/*.html, so usage from Markdown or a build script is invisible to it.
|
|
682
|
+
code: "attr-prop-unsupplied",
|
|
541
683
|
message: `data-bascik-attr-${targetName} references prop "${propName}", but no <${componentName}> usage supplies data-bascik-prop-${propName}.`,
|
|
542
|
-
severity: DiagnosticSeverity.
|
|
543
|
-
source:
|
|
684
|
+
severity: DiagnosticSeverity.Information,
|
|
685
|
+
source: "bascik",
|
|
544
686
|
});
|
|
545
687
|
}
|
|
546
688
|
}
|
|
547
689
|
let scriptMatch;
|
|
548
690
|
while ((scriptMatch = scriptBlockRe.exec(text)) !== null) {
|
|
549
691
|
const openTag = scriptMatch[1];
|
|
550
|
-
const scriptBody = scriptMatch[2] ??
|
|
692
|
+
const scriptBody = scriptMatch[2] ?? "";
|
|
551
693
|
const scriptBodyOffset = (scriptMatch.index ?? 0) + openTag.length;
|
|
552
694
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
553
695
|
const directiveAttrs = [
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
696
|
+
"data-bascik-build",
|
|
697
|
+
"data-bascik-server",
|
|
698
|
+
"data-bascik-routes",
|
|
699
|
+
"data-bascik-stream",
|
|
558
700
|
];
|
|
559
701
|
const presentDirectives = directiveAttrs.filter((d) => attrs.has(d));
|
|
560
702
|
for (let i = 0; i < presentDirectives.length; i++) {
|
|
@@ -562,20 +704,20 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
562
704
|
const d1 = presentDirectives[i];
|
|
563
705
|
const d2 = presentDirectives[j];
|
|
564
706
|
let message;
|
|
565
|
-
if ((d1 ===
|
|
566
|
-
(d1 ===
|
|
707
|
+
if ((d1 === "data-bascik-build" && d2 === "data-bascik-server") ||
|
|
708
|
+
(d1 === "data-bascik-server" && d2 === "data-bascik-build")) {
|
|
567
709
|
message =
|
|
568
|
-
|
|
710
|
+
"data-bascik-build and data-bascik-server cannot both appear on the same <script> tag. Remove one - a script runs at build time or at request time, not both.";
|
|
569
711
|
}
|
|
570
|
-
else if ((d1 ===
|
|
571
|
-
(d1 ===
|
|
712
|
+
else if ((d1 === "data-bascik-routes" && d2 === "data-bascik-server") ||
|
|
713
|
+
(d1 === "data-bascik-server" && d2 === "data-bascik-routes")) {
|
|
572
714
|
message =
|
|
573
|
-
|
|
715
|
+
"data-bascik-routes and data-bascik-server cannot both appear on the same <script> tag. Remove one - a routes script runs at build time, while a server script runs at request time.";
|
|
574
716
|
}
|
|
575
|
-
else if ((d1 ===
|
|
576
|
-
(d1 ===
|
|
717
|
+
else if ((d1 === "data-bascik-routes" && d2 === "data-bascik-build") ||
|
|
718
|
+
(d1 === "data-bascik-build" && d2 === "data-bascik-routes")) {
|
|
577
719
|
message =
|
|
578
|
-
|
|
720
|
+
"data-bascik-routes and data-bascik-build cannot both appear on the same <script> tag. Remove one.";
|
|
579
721
|
}
|
|
580
722
|
else {
|
|
581
723
|
message = `${d1} and ${d2} cannot both appear on the same <script> tag. Remove one.`;
|
|
@@ -586,22 +728,36 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
586
728
|
range: { start, end },
|
|
587
729
|
message,
|
|
588
730
|
severity: DiagnosticSeverity.Error,
|
|
589
|
-
source:
|
|
731
|
+
source: "bascik",
|
|
732
|
+
code: "script-mode-conflict",
|
|
590
733
|
});
|
|
591
734
|
}
|
|
592
735
|
}
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
736
|
+
const buildValue = attrs.get("data-bascik-build");
|
|
737
|
+
if (typeof buildValue === "string" && !isKnownBuildValue(buildValue)) {
|
|
738
|
+
diagnostics.push({
|
|
739
|
+
range: {
|
|
740
|
+
start: document.positionAt(scriptMatch.index ?? 0),
|
|
741
|
+
end: document.positionAt((scriptMatch.index ?? 0) + openTag.length),
|
|
742
|
+
},
|
|
743
|
+
message: unknownDirectiveValueMessage(buildValue.trim()),
|
|
744
|
+
severity: DiagnosticSeverity.Warning,
|
|
745
|
+
source: "bascik",
|
|
746
|
+
code: "unknown-directive-value",
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
const hasServerDirective = attrs.has("data-bascik-server") ||
|
|
750
|
+
attrs.has("data-bascik-stream") ||
|
|
751
|
+
attrs.has("data-bascik-build") ||
|
|
752
|
+
attrs.has("data-bascik-routes");
|
|
597
753
|
if (hasServerDirective) {
|
|
598
|
-
const directive = attrs.has(
|
|
599
|
-
?
|
|
600
|
-
: attrs.has(
|
|
601
|
-
?
|
|
602
|
-
: attrs.has(
|
|
603
|
-
?
|
|
604
|
-
:
|
|
754
|
+
const directive = attrs.has("data-bascik-stream")
|
|
755
|
+
? "stream"
|
|
756
|
+
: attrs.has("data-bascik-server")
|
|
757
|
+
? "server"
|
|
758
|
+
: attrs.has("data-bascik-build")
|
|
759
|
+
? "build"
|
|
760
|
+
: "routes";
|
|
605
761
|
const hasSrcAttribute = /\ssrc\s*=/i.test(openTag);
|
|
606
762
|
const serverDiags = analyzeServerScriptSource(scriptBody, {
|
|
607
763
|
hasSrcAttribute,
|
|
@@ -611,53 +767,101 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
611
767
|
const start = document.positionAt(scriptBodyOffset + sd.start);
|
|
612
768
|
const end = document.positionAt(scriptBodyOffset + sd.end);
|
|
613
769
|
let severity = DiagnosticSeverity.Error;
|
|
614
|
-
if (sd.severity ===
|
|
770
|
+
if (sd.severity === "warning") {
|
|
615
771
|
severity = DiagnosticSeverity.Warning;
|
|
616
772
|
}
|
|
617
|
-
else if (sd.severity ===
|
|
773
|
+
else if (sd.severity === "info") {
|
|
618
774
|
severity = DiagnosticSeverity.Information;
|
|
619
775
|
}
|
|
620
776
|
diagnostics.push({
|
|
621
777
|
range: { start, end },
|
|
622
778
|
message: sd.message,
|
|
623
779
|
severity,
|
|
624
|
-
source:
|
|
780
|
+
source: "bascik",
|
|
625
781
|
code: sd.code,
|
|
626
782
|
});
|
|
627
783
|
}
|
|
628
784
|
}
|
|
629
785
|
diagnostics.push(...collectLeadingSlashDiagnostics(document, openTag, scriptBody, scriptMatch.index ?? 0, attrs));
|
|
630
|
-
|
|
631
|
-
|
|
786
|
+
// A directive script runs in Node, at build time or per request, and never in the browser Bascik scopes.
|
|
787
|
+
if (isJavaScriptScriptTag(openTag) && !hasServerDirective) {
|
|
788
|
+
addCompatibilityDiagnostics(scriptBody, "js", scriptBodyOffset);
|
|
632
789
|
}
|
|
633
790
|
}
|
|
634
791
|
let styleMatch;
|
|
635
|
-
const hasCompanionCss = fsPath.toLowerCase().endsWith(
|
|
636
|
-
fs.existsSync(fsPath.replace(/\.html$/i,
|
|
792
|
+
const hasCompanionCss = fsPath.toLowerCase().endsWith(".html") &&
|
|
793
|
+
fs.existsSync(fsPath.replace(/\.html$/i, ".css"));
|
|
637
794
|
const maskedText = text
|
|
638
|
-
.replace(/(<(style|textarea|script)\
|
|
639
|
-
.replace(/<!--([\s\S]*?)-->/g, (_m, content) =>
|
|
795
|
+
.replace(/(<(style|textarea|script)(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/\2(?:[\t\n\f\r /][^>]*)?>)/gi, (_m, open, _tag, content, close) => open + " ".repeat(content.length) + close)
|
|
796
|
+
.replace(/<!--([\s\S]*?)-->/g, (_m, content) => "<!--" + " ".repeat(content.length) + "-->");
|
|
640
797
|
const componentMap = snapshot?.componentMap ?? new Map();
|
|
641
798
|
const componentNames = Array.from(componentMap.keys());
|
|
799
|
+
// Typos in Bascik's own attributes. Bascik ignores an attribute it does not know, so nothing else reports
|
|
800
|
+
// them. `<code>` contents are shown as text by default (`scoping.preserve`), so they are not scanned.
|
|
801
|
+
const attributeScanText = maskedText.replace(/(<code(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/code(?:[\t\n\f\r /][^>]*)?>)/gi, (_m, open, content, close) => open + content.replace(/[^\n]/g, " ") + close);
|
|
802
|
+
const anyOpenTagRe = /<([A-Za-z][\w-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
|
|
803
|
+
let anyOpenTag;
|
|
804
|
+
while ((anyOpenTag = anyOpenTagRe.exec(attributeScanText)) !== null) {
|
|
805
|
+
const attributesOffset = anyOpenTag.index + 1 + anyOpenTag[1].length;
|
|
806
|
+
const attributeNameRe = /(?:^|\s)([^\s"'=<>`/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/g;
|
|
807
|
+
let attributeMatch;
|
|
808
|
+
while ((attributeMatch = attributeNameRe.exec(anyOpenTag[2])) !== null) {
|
|
809
|
+
const attributeName = attributeMatch[1].toLowerCase();
|
|
810
|
+
if (!attributeName.startsWith("data-bascik-") ||
|
|
811
|
+
isKnownBascikAttribute(attributeName))
|
|
812
|
+
continue;
|
|
813
|
+
const nameStart = attributesOffset +
|
|
814
|
+
attributeMatch.index +
|
|
815
|
+
attributeMatch[0].indexOf(attributeMatch[1]);
|
|
816
|
+
diagnostics.push({
|
|
817
|
+
range: {
|
|
818
|
+
start: document.positionAt(nameStart),
|
|
819
|
+
end: document.positionAt(nameStart + attributeMatch[1].length),
|
|
820
|
+
},
|
|
821
|
+
message: unknownBascikAttributeMessage(attributeName),
|
|
822
|
+
severity: DiagnosticSeverity.Warning,
|
|
823
|
+
source: "bascik",
|
|
824
|
+
code: "unknown-bascik-attribute",
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
// Component layout convention: styles above the markup, scripts below it.
|
|
829
|
+
if (isComponentDocument) {
|
|
830
|
+
for (const problem of findComponentStructureProblems(maskedText)) {
|
|
831
|
+
const tagLength = problem.kind === "script-before-markup"
|
|
832
|
+
? "<script".length
|
|
833
|
+
: "<style".length;
|
|
834
|
+
diagnostics.push({
|
|
835
|
+
range: {
|
|
836
|
+
start: document.positionAt(problem.index),
|
|
837
|
+
end: document.positionAt(problem.index + tagLength),
|
|
838
|
+
},
|
|
839
|
+
message: problem.message,
|
|
840
|
+
severity: DiagnosticSeverity.Warning,
|
|
841
|
+
source: "bascik",
|
|
842
|
+
code: "component-structure-order",
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
}
|
|
642
846
|
// 1. Diagnose script directives on non-script tags
|
|
643
847
|
const elementOpenTagRe = /<([A-Za-z][\w-]*)(?:\s+(?:[^>"']|"[^"]*"|'[^']*')*?)?\/?>/gi;
|
|
644
848
|
let elementMatch;
|
|
645
849
|
while ((elementMatch = elementOpenTagRe.exec(maskedText)) !== null) {
|
|
646
850
|
const tagName = elementMatch[1].toLowerCase();
|
|
647
|
-
if (tagName ===
|
|
851
|
+
if (tagName === "script")
|
|
648
852
|
continue;
|
|
649
853
|
const openTag = elementMatch[0];
|
|
650
854
|
const matchStart = elementMatch.index;
|
|
651
855
|
const scriptDirectives = new Set([
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
856
|
+
"data-bascik-build",
|
|
857
|
+
"data-bascik-server",
|
|
858
|
+
"data-bascik-routes",
|
|
859
|
+
"data-bascik-stream",
|
|
656
860
|
]);
|
|
657
861
|
const insideTag = openTag
|
|
658
|
-
.replace(/^<[A-Za-z][\w-]*\s*/i,
|
|
659
|
-
.replace(/\/?>$/,
|
|
660
|
-
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith(
|
|
862
|
+
.replace(/^<[A-Za-z][\w-]*\s*/i, "")
|
|
863
|
+
.replace(/\/?>$/, "");
|
|
864
|
+
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith("/>") ? 2 : 1);
|
|
661
865
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
662
866
|
let attrMatch;
|
|
663
867
|
while ((attrMatch = attrRe.exec(insideTag)) !== null) {
|
|
@@ -667,9 +871,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
667
871
|
const end = document.positionAt(matchStart + baseOffset + attrMatch.index + attrMatch[1].length);
|
|
668
872
|
diagnostics.push({
|
|
669
873
|
range: { start, end },
|
|
874
|
+
code: "directive-on-non-script",
|
|
670
875
|
message: `\`${attrMatch[1]}\` is only valid on <script> tags. It has no effect on <${tagName}>.`,
|
|
671
876
|
severity: DiagnosticSeverity.Error,
|
|
672
|
-
source:
|
|
877
|
+
source: "bascik",
|
|
673
878
|
});
|
|
674
879
|
}
|
|
675
880
|
}
|
|
@@ -681,14 +886,14 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
681
886
|
const openTag = slotMatch[0];
|
|
682
887
|
const matchStart = slotMatch.index;
|
|
683
888
|
const insideTag = openTag
|
|
684
|
-
.replace(/^<[A-Za-z][\w-]*\s*/i,
|
|
685
|
-
.replace(/\/?>$/,
|
|
686
|
-
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith(
|
|
889
|
+
.replace(/^<[A-Za-z][\w-]*\s*/i, "")
|
|
890
|
+
.replace(/\/?>$/, "");
|
|
891
|
+
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith("/>") ? 2 : 1);
|
|
687
892
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
688
893
|
let attrMatch;
|
|
689
894
|
let slotAttr = null;
|
|
690
895
|
while ((attrMatch = attrRe.exec(insideTag)) !== null) {
|
|
691
|
-
if (attrMatch[1].toLowerCase() ===
|
|
896
|
+
if (attrMatch[1].toLowerCase() === "data-bascik-slot") {
|
|
692
897
|
slotAttr = {
|
|
693
898
|
name: attrMatch[1],
|
|
694
899
|
value: attrMatch[2] ?? attrMatch[3] ?? attrMatch[4],
|
|
@@ -708,9 +913,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
708
913
|
const end = document.positionAt(matchStart + baseOffset + slotAttr.index + slotAttr.name.length);
|
|
709
914
|
diagnostics.push({
|
|
710
915
|
range: { start, end },
|
|
711
|
-
|
|
916
|
+
code: "slot-outside-component",
|
|
917
|
+
message: "`data-bascik-slot` is only valid inside a Bascik component body.",
|
|
712
918
|
severity: DiagnosticSeverity.Error,
|
|
713
|
-
source:
|
|
919
|
+
source: "bascik",
|
|
714
920
|
});
|
|
715
921
|
}
|
|
716
922
|
else if (parentName) {
|
|
@@ -722,9 +928,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
722
928
|
const end = document.positionAt(matchStart + baseOffset + slotAttr.index + slotAttr.name.length);
|
|
723
929
|
diagnostics.push({
|
|
724
930
|
range: { start, end },
|
|
931
|
+
code: "undeclared-slot",
|
|
725
932
|
message: `Component <${parentName}> does not declare slot "${slotName}".`,
|
|
726
933
|
severity: DiagnosticSeverity.Error,
|
|
727
|
-
source:
|
|
934
|
+
source: "bascik",
|
|
728
935
|
});
|
|
729
936
|
}
|
|
730
937
|
}
|
|
@@ -732,8 +939,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
732
939
|
}
|
|
733
940
|
if (componentNames.length > 0) {
|
|
734
941
|
componentNames.sort((a, b) => b.length - a.length);
|
|
735
|
-
const escapedNames = componentNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g,
|
|
736
|
-
const componentTagRe = new RegExp(`<(${escapedNames.join(
|
|
942
|
+
const escapedNames = componentNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
943
|
+
const componentTagRe = new RegExp(`<(${escapedNames.join("|")})\\b`, "gi");
|
|
737
944
|
let compMatch;
|
|
738
945
|
while ((compMatch = componentTagRe.exec(maskedText)) !== null) {
|
|
739
946
|
const tagStartIndex = compMatch.index;
|
|
@@ -749,7 +956,7 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
749
956
|
else if (char === "'" && !inDoubleQuote) {
|
|
750
957
|
inSingleQuote = !inSingleQuote;
|
|
751
958
|
}
|
|
752
|
-
else if (char ===
|
|
959
|
+
else if (char === ">" && !inDoubleQuote && !inSingleQuote) {
|
|
753
960
|
openTagEndIndex = i + 1;
|
|
754
961
|
break;
|
|
755
962
|
}
|
|
@@ -771,7 +978,38 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
771
978
|
range: { start, end },
|
|
772
979
|
message: `Component <${tagName}> does not declare prop "${propName}".`,
|
|
773
980
|
severity: DiagnosticSeverity.Warning,
|
|
774
|
-
source:
|
|
981
|
+
source: "bascik",
|
|
982
|
+
code: "undeclared-prop",
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
if (compMeta.props.length > 0) {
|
|
987
|
+
const insideOpenTag = openTagText
|
|
988
|
+
.replace(new RegExp(`^<${escapeRegExp(tagName)}\\b`, "i"), "")
|
|
989
|
+
.replace(/\/?>$/, "");
|
|
990
|
+
const baseAttrOffset = openTagText.indexOf(insideOpenTag);
|
|
991
|
+
const bareAttrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/gi;
|
|
992
|
+
let bareMatch;
|
|
993
|
+
while ((bareMatch = bareAttrRe.exec(insideOpenTag)) !== null) {
|
|
994
|
+
const attrName = bareMatch[1];
|
|
995
|
+
const lowerAttr = attrName.toLowerCase();
|
|
996
|
+
if (lowerAttr.startsWith("data-bascik-"))
|
|
997
|
+
continue;
|
|
998
|
+
const declaredProp = compMeta.props.find((p) => p.name.toLowerCase() === lowerAttr);
|
|
999
|
+
if (!declaredProp)
|
|
1000
|
+
continue;
|
|
1001
|
+
const alreadySupplied = new RegExp(`\\bdata-bascik-prop-${escapeRegExp(declaredProp.name)}\\b`, "i").test(openTagText);
|
|
1002
|
+
if (alreadySupplied)
|
|
1003
|
+
continue;
|
|
1004
|
+
const attrOffset = tagStartIndex + baseAttrOffset + bareMatch.index;
|
|
1005
|
+
const start = document.positionAt(attrOffset);
|
|
1006
|
+
const end = document.positionAt(attrOffset + attrName.length);
|
|
1007
|
+
diagnostics.push({
|
|
1008
|
+
range: { start, end },
|
|
1009
|
+
message: `Component <${tagName}> declares prop "${declaredProp.name}". Pass it with the data-bascik-prop-${declaredProp.name} prefix instead of "${attrName}".`,
|
|
1010
|
+
severity: DiagnosticSeverity.Warning,
|
|
1011
|
+
source: "bascik",
|
|
1012
|
+
code: "missing-prop-prefix",
|
|
775
1013
|
});
|
|
776
1014
|
}
|
|
777
1015
|
}
|
|
@@ -786,8 +1024,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
786
1024
|
range: { start, end },
|
|
787
1025
|
message: `Component tag <${tagName}> is unclosed. It will be treated as self-closing (<${tagName}/>), but an explicit closing tag is recommended to avoid layout or scoping issues.`,
|
|
788
1026
|
severity: DiagnosticSeverity.Warning,
|
|
789
|
-
source:
|
|
790
|
-
code:
|
|
1027
|
+
source: "bascik",
|
|
1028
|
+
code: "unclosed-component-tag",
|
|
791
1029
|
});
|
|
792
1030
|
}
|
|
793
1031
|
else if (compMeta) {
|
|
@@ -799,8 +1037,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
799
1037
|
range: { start, end },
|
|
800
1038
|
message: `Component <${tagName}> defines no slots and accepts no children. Use self-closing tag <${tagName} /> instead of paired <${tagName}></${tagName}>.`,
|
|
801
1039
|
severity: DiagnosticSeverity.Warning,
|
|
802
|
-
source:
|
|
803
|
-
code:
|
|
1040
|
+
source: "bascik",
|
|
1041
|
+
code: "prefer-self-closing-component",
|
|
804
1042
|
});
|
|
805
1043
|
}
|
|
806
1044
|
}
|
|
@@ -814,26 +1052,28 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
814
1052
|
}
|
|
815
1053
|
for (const match of styleMatches) {
|
|
816
1054
|
const openTag = match[1];
|
|
817
|
-
const styleBody = match[2] ?? '';
|
|
818
1055
|
const styleBodyOffset = (match.index ?? 0) + openTag.length;
|
|
1056
|
+
// maskedText blanks style bodies (same length), so read the real body from text.
|
|
1057
|
+
const styleBody = text.slice(styleBodyOffset, styleBodyOffset + (match[2] ?? "").length);
|
|
819
1058
|
if (hasCompanionCss) {
|
|
820
1059
|
const start = document.positionAt(match.index ?? 0);
|
|
821
1060
|
const end = document.positionAt((match.index ?? 0) + openTag.length);
|
|
822
1061
|
diagnostics.push({
|
|
823
1062
|
range: { start, end },
|
|
824
|
-
|
|
1063
|
+
code: "inline-and-companion-styles",
|
|
1064
|
+
message: "Component has both a companion .css file and an inline <style> tag. They will be combined at build time, but mixing both is not recommended for readability and maintainability.",
|
|
825
1065
|
severity: DiagnosticSeverity.Warning,
|
|
826
|
-
source:
|
|
1066
|
+
source: "bascik",
|
|
827
1067
|
});
|
|
828
1068
|
}
|
|
829
|
-
addCompatibilityDiagnostics(styleBody,
|
|
1069
|
+
addCompatibilityDiagnostics(styleBody, "css", styleBodyOffset);
|
|
830
1070
|
}
|
|
831
1071
|
}
|
|
832
|
-
else if (languageId ===
|
|
833
|
-
addCompatibilityDiagnostics(text,
|
|
1072
|
+
else if (languageId === "css") {
|
|
1073
|
+
addCompatibilityDiagnostics(text, "css", 0);
|
|
834
1074
|
}
|
|
835
1075
|
else {
|
|
836
|
-
addCompatibilityDiagnostics(text,
|
|
1076
|
+
addCompatibilityDiagnostics(text, "js", 0);
|
|
837
1077
|
}
|
|
838
1078
|
const ignoreRanges = parseIgnoreDirectives(text, languageId);
|
|
839
1079
|
const extConfig = snapshot ? loadExtensionConfig(snapshot.projectRoot) : {};
|
|
@@ -843,15 +1083,15 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
843
1083
|
}
|
|
844
1084
|
return diagnostics.filter((diag) => {
|
|
845
1085
|
const line = diag.range.start.line;
|
|
846
|
-
const code = typeof diag.code ===
|
|
1086
|
+
const code = typeof diag.code === "string" ? diag.code : undefined;
|
|
847
1087
|
if (code && disabledRules.has(code.toLowerCase())) {
|
|
848
1088
|
return false;
|
|
849
1089
|
}
|
|
850
|
-
if (code ===
|
|
1090
|
+
if (code === "prefer-self-closing-component" &&
|
|
851
1091
|
extConfig.diagnostics?.selfClosingComponents === false) {
|
|
852
1092
|
return false;
|
|
853
1093
|
}
|
|
854
|
-
if (code ===
|
|
1094
|
+
if (code === "unclosed-component-tag" &&
|
|
855
1095
|
extConfig.diagnostics?.unclosedComponents === false) {
|
|
856
1096
|
return false;
|
|
857
1097
|
}
|
|
@@ -859,10 +1099,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
859
1099
|
});
|
|
860
1100
|
}
|
|
861
1101
|
export function createCompletions(document, position, snapshot) {
|
|
862
|
-
if (document.languageId !==
|
|
1102
|
+
if (document.languageId !== "html")
|
|
863
1103
|
return undefined;
|
|
864
1104
|
const offset = document.offsetAt(position);
|
|
865
|
-
const maskedSource = maskHtmlRawTextContents(document.getText()).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) =>
|
|
1105
|
+
const maskedSource = maskHtmlRawTextContents(document.getText()).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) => " ".repeat(comment.length));
|
|
866
1106
|
const sourceBeforeCursor = maskedSource.slice(0, offset);
|
|
867
1107
|
const tagMatch = /<([A-Za-z][\w-]*)?$/.exec(sourceBeforeCursor);
|
|
868
1108
|
if (!tagMatch) {
|
|
@@ -872,8 +1112,8 @@ export function createCompletions(document, position, snapshot) {
|
|
|
872
1112
|
const currentTagName = openTagMatch[1].toLowerCase();
|
|
873
1113
|
const openTagText = openTagMatch[0];
|
|
874
1114
|
const currentTagStart = offset - openTagText.length;
|
|
875
|
-
if (currentTagName ===
|
|
876
|
-
const hasAnyDirective = SCRIPT_DIRECTIVES.some((dir) => new RegExp(`\\b${dir.name}\\b`,
|
|
1115
|
+
if (currentTagName === "script") {
|
|
1116
|
+
const hasAnyDirective = SCRIPT_DIRECTIVES.some((dir) => new RegExp(`\\b${dir.name}\\b`, "i").test(openTagText));
|
|
877
1117
|
if (hasAnyDirective)
|
|
878
1118
|
return [];
|
|
879
1119
|
const range = completionReplacementRange(document, position);
|
|
@@ -896,7 +1136,7 @@ export function createCompletions(document, position, snapshot) {
|
|
|
896
1136
|
if (currentComponentMetadata) {
|
|
897
1137
|
const range = completionReplacementRange(document, position);
|
|
898
1138
|
return currentComponentMetadata.props
|
|
899
|
-
.filter(({ name }) => !new RegExp(`\\bdata-bascik-prop-${escapeRegExp(name)}\\s*=`,
|
|
1139
|
+
.filter(({ name }) => !new RegExp(`\\bdata-bascik-prop-${escapeRegExp(name)}\\s*=`, "i").test(openTagText))
|
|
900
1140
|
.map((prop) => {
|
|
901
1141
|
const attribute = `data-bascik-prop-${prop.name}`;
|
|
902
1142
|
const item = {
|
|
@@ -908,12 +1148,12 @@ export function createCompletions(document, position, snapshot) {
|
|
|
908
1148
|
},
|
|
909
1149
|
insertTextFormat: 2, // Snippet
|
|
910
1150
|
sortText: `0_${attribute}`,
|
|
911
|
-
detail:
|
|
1151
|
+
detail: "Bascik component prop",
|
|
912
1152
|
};
|
|
913
1153
|
if (prop.description) {
|
|
914
1154
|
item.documentation = {
|
|
915
1155
|
kind: MarkupKind.Markdown,
|
|
916
|
-
value: prop.description,
|
|
1156
|
+
value: escapeMarkdownText(prop.description),
|
|
917
1157
|
};
|
|
918
1158
|
}
|
|
919
1159
|
return item;
|
|
@@ -938,12 +1178,12 @@ export function createCompletions(document, position, snapshot) {
|
|
|
938
1178
|
},
|
|
939
1179
|
filterText: `data-bascik-slot ${slot.name}`,
|
|
940
1180
|
sortText: `0_data-bascik-slot_${slot.name}`,
|
|
941
|
-
detail:
|
|
1181
|
+
detail: "Bascik named slot",
|
|
942
1182
|
};
|
|
943
1183
|
if (slot.description) {
|
|
944
1184
|
item.documentation = {
|
|
945
1185
|
kind: MarkupKind.Markdown,
|
|
946
|
-
value: slot.description,
|
|
1186
|
+
value: escapeMarkdownText(slot.description),
|
|
947
1187
|
};
|
|
948
1188
|
}
|
|
949
1189
|
return item;
|
|
@@ -951,52 +1191,54 @@ export function createCompletions(document, position, snapshot) {
|
|
|
951
1191
|
}
|
|
952
1192
|
const tagStart = offset - tagMatch[0].length;
|
|
953
1193
|
const sourceBeforeTag = sourceBeforeCursor.slice(0, tagStart);
|
|
954
|
-
const previousTagStart = sourceBeforeTag.lastIndexOf(
|
|
955
|
-
const previousTagEnd = sourceBeforeTag.lastIndexOf(
|
|
1194
|
+
const previousTagStart = sourceBeforeTag.lastIndexOf("<");
|
|
1195
|
+
const previousTagEnd = sourceBeforeTag.lastIndexOf(">");
|
|
956
1196
|
if (previousTagStart > previousTagEnd)
|
|
957
1197
|
return undefined;
|
|
958
|
-
const prefix = (tagMatch[1] ??
|
|
1198
|
+
const prefix = (tagMatch[1] ?? "").toLowerCase();
|
|
959
1199
|
const start = document.positionAt(offset - prefix.length);
|
|
960
1200
|
const replacementRange = { start, end: position };
|
|
961
1201
|
const scriptShorthands = [
|
|
962
1202
|
{
|
|
963
|
-
label:
|
|
964
|
-
detail:
|
|
965
|
-
documentation:
|
|
1203
|
+
label: "bascik-build",
|
|
1204
|
+
detail: "Bascik build-time script block",
|
|
1205
|
+
documentation: "Executes in Node.js at build time. The default exported handler function returns HTML markup that replaces this script tag in the generated HTML.",
|
|
966
1206
|
insertText: `<script data-bascik-build>\n export default async function () {\n return \`$0\`;\n }\n</script>`,
|
|
967
1207
|
},
|
|
968
1208
|
{
|
|
969
|
-
label:
|
|
970
|
-
detail:
|
|
971
|
-
documentation:
|
|
1209
|
+
label: "bascik-routes",
|
|
1210
|
+
detail: "Bascik dynamic routes script block",
|
|
1211
|
+
documentation: "Executes in Node.js at build time. The default exported handler function returns an array of dynamic route parameters to generate multiple pages from a template.",
|
|
972
1212
|
insertText: `<script data-bascik-routes>\n export default async function () {\n return [\n $0\n ];\n }\n</script>`,
|
|
973
1213
|
},
|
|
974
1214
|
{
|
|
975
|
-
label:
|
|
976
|
-
detail:
|
|
977
|
-
documentation:
|
|
1215
|
+
label: "bascik-server",
|
|
1216
|
+
detail: "Bascik server-side request script block",
|
|
1217
|
+
documentation: "Executes in Node.js per request on production and dev servers. The default exported handler function replaces this script tag.",
|
|
978
1218
|
insertText: `<script data-bascik-server>\n export default async function (request, context, { signal }) {\n return \`$0\`;\n }\n</script>`,
|
|
979
1219
|
},
|
|
980
1220
|
{
|
|
981
|
-
label:
|
|
982
|
-
detail:
|
|
983
|
-
documentation:
|
|
1221
|
+
label: "bascik-stream",
|
|
1222
|
+
detail: "Bascik server-side streaming script block",
|
|
1223
|
+
documentation: "Executes in Node.js per request on production servers. Streams chunked HTML responses to the client as data becomes available.",
|
|
984
1224
|
insertText: `<script data-bascik-stream>\n export default async function (request, context, { signal }) {\n return \`$0\`;\n }\n</script>`,
|
|
985
1225
|
},
|
|
986
1226
|
];
|
|
1227
|
+
// The replacement starts at the "<" already typed, so the filter text must start there too, or the editor
|
|
1228
|
+
// compares what was typed ("<my-b") against a label without it and hides every item.
|
|
1229
|
+
const tagRange = {
|
|
1230
|
+
start: document.positionAt(tagStart),
|
|
1231
|
+
end: replacementRange.end,
|
|
1232
|
+
};
|
|
987
1233
|
const shorthandItems = scriptShorthands
|
|
988
1234
|
.filter((shorthand) => shorthand.label.startsWith(prefix))
|
|
989
1235
|
.map((shorthand) => ({
|
|
990
1236
|
label: shorthand.label,
|
|
991
1237
|
kind: CompletionItemKind.Snippet,
|
|
992
1238
|
sortText: `0_${shorthand.label}`,
|
|
993
|
-
range:
|
|
994
|
-
start: document.positionAt(tagStart),
|
|
995
|
-
end: replacementRange.end,
|
|
996
|
-
},
|
|
997
|
-
insertText: shorthand.insertText,
|
|
1239
|
+
textEdit: { range: tagRange, newText: shorthand.insertText },
|
|
998
1240
|
insertTextFormat: 2, // Snippet
|
|
999
|
-
filterText: shorthand.label
|
|
1241
|
+
filterText: `<${shorthand.label}`,
|
|
1000
1242
|
detail: shorthand.detail,
|
|
1001
1243
|
documentation: {
|
|
1002
1244
|
kind: MarkupKind.Markdown,
|
|
@@ -1010,21 +1252,20 @@ export function createCompletions(document, position, snapshot) {
|
|
|
1010
1252
|
const metadata = snapshot.componentMetadata.get(componentName);
|
|
1011
1253
|
const relativePath = path
|
|
1012
1254
|
.relative(snapshot.projectRoot, componentPath)
|
|
1013
|
-
.replace(/\\/g,
|
|
1255
|
+
.replace(/\\/g, "/");
|
|
1014
1256
|
return {
|
|
1015
1257
|
label: componentName,
|
|
1016
1258
|
kind: CompletionItemKind.Class,
|
|
1017
1259
|
sortText: `0_${componentName}`,
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1260
|
+
textEdit: {
|
|
1261
|
+
range: tagRange,
|
|
1262
|
+
newText: metadata?.defaultSlot
|
|
1263
|
+
? `<${componentName}>$0</${componentName}>`
|
|
1264
|
+
: `<${componentName} />$0`,
|
|
1021
1265
|
},
|
|
1022
|
-
insertText: metadata?.defaultSlot
|
|
1023
|
-
? `<${componentName}>$0</${componentName}>`
|
|
1024
|
-
: `<${componentName} />$0`,
|
|
1025
1266
|
insertTextFormat: 2, // Snippet
|
|
1026
|
-
filterText: componentName
|
|
1027
|
-
detail:
|
|
1267
|
+
filterText: `<${componentName}`,
|
|
1268
|
+
detail: "Bascik component",
|
|
1028
1269
|
documentation: {
|
|
1029
1270
|
kind: MarkupKind.Markdown,
|
|
1030
1271
|
value: createComponentDocumentationMarkdown(relativePath, metadata),
|
|
@@ -1067,34 +1308,34 @@ export function createHover(document, position, snapshot) {
|
|
|
1067
1308
|
return undefined;
|
|
1068
1309
|
const relativePath = path
|
|
1069
1310
|
.relative(snapshot.projectRoot, componentPath)
|
|
1070
|
-
.replace(/\\/g,
|
|
1311
|
+
.replace(/\\/g, "/");
|
|
1071
1312
|
const details = [`### \`<${componentName}>\`\n\n`];
|
|
1072
1313
|
if (metadata.description) {
|
|
1073
|
-
details.push(metadata.description,
|
|
1314
|
+
details.push(escapeMarkdownText(metadata.description), "\n\n");
|
|
1074
1315
|
}
|
|
1075
|
-
details.push(`**Source:** [\`${relativePath}\`](
|
|
1076
|
-
appendMetadataMembersMarkdown(details,
|
|
1077
|
-
appendMetadataMembersMarkdown(details,
|
|
1316
|
+
details.push(`**Source:** [\`${relativePath}\`](${pathToFileURL(componentPath).toString()})\n\n`);
|
|
1317
|
+
appendMetadataMembersMarkdown(details, "Props", metadata.props);
|
|
1318
|
+
appendMetadataMembersMarkdown(details, "Slots", [
|
|
1078
1319
|
...metadata.slots,
|
|
1079
1320
|
...(metadata.defaultSlot ? [metadata.defaultSlot] : []),
|
|
1080
1321
|
]);
|
|
1081
1322
|
const features = [
|
|
1082
|
-
metadata.hasStyles ?
|
|
1083
|
-
metadata.hasScripts ?
|
|
1323
|
+
metadata.hasStyles ? "styles" : "",
|
|
1324
|
+
metadata.hasScripts ? "scripts" : "",
|
|
1084
1325
|
].filter(Boolean);
|
|
1085
1326
|
if (features.length > 0) {
|
|
1086
|
-
details.push(`**Includes:** ${features.join(
|
|
1327
|
+
details.push(`**Includes:** ${features.join(", ")}`);
|
|
1087
1328
|
}
|
|
1088
1329
|
return {
|
|
1089
1330
|
contents: {
|
|
1090
1331
|
kind: MarkupKind.Markdown,
|
|
1091
|
-
value: details.join(
|
|
1332
|
+
value: details.join(""),
|
|
1092
1333
|
},
|
|
1093
1334
|
range: wordRange,
|
|
1094
1335
|
};
|
|
1095
1336
|
}
|
|
1096
1337
|
export function createDefinition(document, position, snapshot, fsPath) {
|
|
1097
|
-
if (document.languageId !==
|
|
1338
|
+
if (document.languageId !== "html")
|
|
1098
1339
|
return undefined;
|
|
1099
1340
|
// 1. Check for script import definition
|
|
1100
1341
|
const text = document.getText();
|
|
@@ -1103,16 +1344,16 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1103
1344
|
let scriptMatch;
|
|
1104
1345
|
while ((scriptMatch = SCRIPT_BLOCK_RE.exec(text)) !== null) {
|
|
1105
1346
|
const openTag = scriptMatch[1];
|
|
1106
|
-
const scriptBody = scriptMatch[2] ??
|
|
1347
|
+
const scriptBody = scriptMatch[2] ?? "";
|
|
1107
1348
|
const blockStart = scriptMatch.index ?? 0;
|
|
1108
1349
|
const openTagEnd = blockStart + openTag.length;
|
|
1109
1350
|
const blockEnd = blockStart + scriptMatch[0].length;
|
|
1110
1351
|
if (offset < blockStart || offset > blockEnd)
|
|
1111
1352
|
continue;
|
|
1112
1353
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
1113
|
-
if (!attrs.has(
|
|
1114
|
-
!attrs.has(
|
|
1115
|
-
!attrs.has(
|
|
1354
|
+
if (!attrs.has("data-bascik-build") &&
|
|
1355
|
+
!attrs.has("data-bascik-server") &&
|
|
1356
|
+
!attrs.has("data-bascik-routes")) {
|
|
1116
1357
|
return undefined;
|
|
1117
1358
|
}
|
|
1118
1359
|
const baseDir = path.dirname(fsPath);
|
|
@@ -1121,14 +1362,14 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1121
1362
|
const srcMatch = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(openTag);
|
|
1122
1363
|
if (!srcMatch)
|
|
1123
1364
|
return undefined;
|
|
1124
|
-
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ??
|
|
1365
|
+
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? "";
|
|
1125
1366
|
if (!srcValue)
|
|
1126
1367
|
return undefined;
|
|
1127
1368
|
const valueStart = blockStart + (srcMatch.index ?? 0) + srcMatch[0].indexOf(srcValue);
|
|
1128
1369
|
const valueEnd = valueStart + srcValue.length;
|
|
1129
1370
|
if (offset < valueStart || offset > valueEnd)
|
|
1130
1371
|
return undefined;
|
|
1131
|
-
const resolved = resolveScriptTarget(srcValue, baseDir, importRootAbs,
|
|
1372
|
+
const resolved = resolveScriptTarget(srcValue, baseDir, importRootAbs, "src");
|
|
1132
1373
|
if (!resolved || !fs.existsSync(resolved))
|
|
1133
1374
|
return undefined;
|
|
1134
1375
|
return [
|
|
@@ -1153,7 +1394,7 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1153
1394
|
for (const { start, end, value: specifier } of findModuleSpecifiers(scriptBody)) {
|
|
1154
1395
|
if (bodyOffset < start || bodyOffset > end)
|
|
1155
1396
|
continue;
|
|
1156
|
-
const resolved = resolveScriptTarget(specifier, baseDir, importRootAbs,
|
|
1397
|
+
const resolved = resolveScriptTarget(specifier, baseDir, importRootAbs, "specifier");
|
|
1157
1398
|
if (!resolved || !fs.existsSync(resolved))
|
|
1158
1399
|
return undefined;
|
|
1159
1400
|
return [
|
|
@@ -1213,4 +1454,104 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1213
1454
|
}
|
|
1214
1455
|
return undefined;
|
|
1215
1456
|
}
|
|
1457
|
+
function rangesOverlap(a, b) {
|
|
1458
|
+
if (a.end.line < b.start.line || b.end.line < a.start.line)
|
|
1459
|
+
return false;
|
|
1460
|
+
if (a.end.line === b.start.line && a.end.character < b.start.character)
|
|
1461
|
+
return false;
|
|
1462
|
+
if (b.end.line === a.start.line && b.end.character < a.start.character)
|
|
1463
|
+
return false;
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
export function buildAutoFixEdit(document, diag) {
|
|
1467
|
+
const code = typeof diag.code === "string" ? diag.code : undefined;
|
|
1468
|
+
if (code === "missing-prop-prefix") {
|
|
1469
|
+
const rawAttr = document.getText(diag.range);
|
|
1470
|
+
if (!rawAttr || rawAttr.toLowerCase().startsWith("data-bascik-prop-")) {
|
|
1471
|
+
return undefined;
|
|
1472
|
+
}
|
|
1473
|
+
const newText = `data-bascik-prop-${rawAttr}`;
|
|
1474
|
+
return {
|
|
1475
|
+
title: `Prefix prop with data-bascik-prop- (${newText})`,
|
|
1476
|
+
edit: {
|
|
1477
|
+
range: diag.range,
|
|
1478
|
+
newText,
|
|
1479
|
+
},
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
if (code === "prefer-self-closing-component") {
|
|
1483
|
+
const fullText = document.getText(diag.range);
|
|
1484
|
+
const match = /^<([A-Za-z][\w-]*)(\s+(?:[^>"']|"[^"]*"|'[^']*')*?)?>\s*<\/\1\s*>$/is.exec(fullText);
|
|
1485
|
+
if (!match)
|
|
1486
|
+
return undefined;
|
|
1487
|
+
const tagName = match[1];
|
|
1488
|
+
const attrs = (match[2] ?? "").trimEnd();
|
|
1489
|
+
const newText = `<${tagName}${attrs} />`;
|
|
1490
|
+
return {
|
|
1491
|
+
title: `Convert <${tagName}> to self-closing tag`,
|
|
1492
|
+
edit: {
|
|
1493
|
+
range: diag.range,
|
|
1494
|
+
newText,
|
|
1495
|
+
},
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
return undefined;
|
|
1499
|
+
}
|
|
1500
|
+
export function createCodeActions(document, range, diagnostics, snapshot, fsPath) {
|
|
1501
|
+
const activeDiagnostics = diagnostics.length > 0
|
|
1502
|
+
? diagnostics
|
|
1503
|
+
: fsPath
|
|
1504
|
+
? createDiagnostics(document, fsPath, snapshot)
|
|
1505
|
+
: [];
|
|
1506
|
+
const actions = [];
|
|
1507
|
+
for (const diag of activeDiagnostics) {
|
|
1508
|
+
if (!rangesOverlap(diag.range, range))
|
|
1509
|
+
continue;
|
|
1510
|
+
const fix = buildAutoFixEdit(document, diag);
|
|
1511
|
+
if (!fix)
|
|
1512
|
+
continue;
|
|
1513
|
+
actions.push({
|
|
1514
|
+
title: fix.title,
|
|
1515
|
+
kind: CodeActionKind.QuickFix,
|
|
1516
|
+
diagnostics: [diag],
|
|
1517
|
+
isPreferred: true,
|
|
1518
|
+
edit: {
|
|
1519
|
+
changes: {
|
|
1520
|
+
[document.uri]: [fix.edit],
|
|
1521
|
+
},
|
|
1522
|
+
},
|
|
1523
|
+
});
|
|
1524
|
+
}
|
|
1525
|
+
return actions;
|
|
1526
|
+
}
|
|
1527
|
+
export function applyAutoFixes(document, snapshot, fsPath = "/virtual.html") {
|
|
1528
|
+
const diags = createDiagnostics(document, fsPath, snapshot);
|
|
1529
|
+
const edits = [];
|
|
1530
|
+
for (const diag of diags) {
|
|
1531
|
+
const fix = buildAutoFixEdit(document, diag);
|
|
1532
|
+
if (!fix)
|
|
1533
|
+
continue;
|
|
1534
|
+
const startOffset = document.offsetAt(fix.edit.range.start);
|
|
1535
|
+
const endOffset = document.offsetAt(fix.edit.range.end);
|
|
1536
|
+
edits.push({ startOffset, endOffset, newText: fix.edit.newText });
|
|
1537
|
+
}
|
|
1538
|
+
if (edits.length === 0) {
|
|
1539
|
+
return { text: document.getText(), fixedCount: 0 };
|
|
1540
|
+
}
|
|
1541
|
+
edits.sort((a, b) => b.startOffset - a.startOffset);
|
|
1542
|
+
let text = document.getText();
|
|
1543
|
+
let fixedCount = 0;
|
|
1544
|
+
let lastStart = Number.POSITIVE_INFINITY;
|
|
1545
|
+
for (const edit of edits) {
|
|
1546
|
+
if (edit.endOffset > lastStart)
|
|
1547
|
+
continue;
|
|
1548
|
+
text =
|
|
1549
|
+
text.slice(0, edit.startOffset) +
|
|
1550
|
+
edit.newText +
|
|
1551
|
+
text.slice(edit.endOffset);
|
|
1552
|
+
lastStart = edit.startOffset;
|
|
1553
|
+
fixedCount++;
|
|
1554
|
+
}
|
|
1555
|
+
return { text, fixedCount };
|
|
1556
|
+
}
|
|
1216
1557
|
//# sourceMappingURL=analyzer.js.map
|