@bascik/language-server 0.1.0-rc.3 → 0.1.0-rc.5
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 +649 -360
- 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 +36 -129
- package/dist/cli.js.map +1 -1
- package/dist/compatibility-rules.json +11 -2
- package/dist/component-metadata.d.ts +1 -1
- package/dist/component-metadata.d.ts.map +1 -1
- package/dist/component-metadata.js +31 -25
- 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 +52 -30
- package/dist/project.js.map +1 -1
- package/dist/rules.d.ts +29 -3
- 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 +5 -1
package/dist/analyzer.js
CHANGED
|
@@ -1,160 +1,167 @@
|
|
|
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. In a component, it runs for each use with that page's context. 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-build-once",
|
|
135
|
+
detail: "Bascik build-time script, run once",
|
|
136
|
+
documentation: "Executes once, without page or route context, and reuses its markup wherever the file is used. In a component, it runs when the component is registered and every use shares the output; in a dynamic route template, every generated page shares it.",
|
|
135
137
|
},
|
|
136
138
|
{
|
|
137
|
-
name:
|
|
138
|
-
detail:
|
|
139
|
-
documentation:
|
|
139
|
+
name: "data-bascik-routes",
|
|
140
|
+
detail: "Bascik dynamic routes script",
|
|
141
|
+
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.",
|
|
140
142
|
},
|
|
141
143
|
{
|
|
142
|
-
name:
|
|
143
|
-
detail:
|
|
144
|
-
documentation:
|
|
144
|
+
name: "data-bascik-server",
|
|
145
|
+
detail: "Bascik server-side request script",
|
|
146
|
+
documentation: "Executes in Node.js per request on production and dev servers. The default exported handler function replaces this script tag.",
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: "data-bascik-stream",
|
|
150
|
+
detail: "Bascik server-side streaming script",
|
|
151
|
+
documentation: "Executes in Node.js per request on production servers. Streams chunked HTML responses to the client as data becomes available.",
|
|
145
152
|
},
|
|
146
153
|
];
|
|
147
154
|
export const SCRIPT_BLOCK_RE = /(<script(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)<\/script(?:[\t\n\f\r /][^>]*)?>/gi;
|
|
148
155
|
export function maskHtmlRawTextContents(html) {
|
|
149
|
-
return html.replace(/(<(script|style|textarea)(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/\2(?:[\t\n\f\r /][^>]*)?>)/gi, (_match, openTag, _tagName, content, closeTag) => `${openTag}${
|
|
156
|
+
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
157
|
}
|
|
151
158
|
export function escapeRegExp(value) {
|
|
152
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g,
|
|
159
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
153
160
|
}
|
|
154
161
|
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}>`,
|
|
162
|
+
const tn = tagName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
163
|
+
const openRe = new RegExp(`<${tn}[\\s>]`, "gi");
|
|
164
|
+
const closeRe = new RegExp(`<\\/${tn}>`, "gi");
|
|
158
165
|
let depth = 1;
|
|
159
166
|
let pos = contentStart;
|
|
160
167
|
while (pos < html.length) {
|
|
@@ -182,7 +189,7 @@ export function findMatchingClose(html, tagName, contentStart) {
|
|
|
182
189
|
else if (char === "'" && !inDoubleQuote) {
|
|
183
190
|
inSingleQuote = !inSingleQuote;
|
|
184
191
|
}
|
|
185
|
-
else if (char ===
|
|
192
|
+
else if (char === ">" && !inDoubleQuote && !inSingleQuote) {
|
|
186
193
|
tagEnd = i + 1;
|
|
187
194
|
break;
|
|
188
195
|
}
|
|
@@ -205,7 +212,10 @@ export function findMatchingClose(html, tagName, contentStart) {
|
|
|
205
212
|
return -1;
|
|
206
213
|
}
|
|
207
214
|
const isTagNameStart = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
208
|
-
const isTagNameChar = (code) => isTagNameStart(code) ||
|
|
215
|
+
const isTagNameChar = (code) => isTagNameStart(code) ||
|
|
216
|
+
(code >= 48 && code <= 57) ||
|
|
217
|
+
code === 95 ||
|
|
218
|
+
code === 45;
|
|
209
219
|
/**
|
|
210
220
|
* Tag tokens in source order, matching what
|
|
211
221
|
* `/<\/?([A-Za-z][\w-]*)(?:[^>"']|"[^"]*"|'[^']*')*>/g` finds, in linear time.
|
|
@@ -250,7 +260,7 @@ function* scanTags(source) {
|
|
|
250
260
|
};
|
|
251
261
|
let cursor = 0;
|
|
252
262
|
while (cursor < length) {
|
|
253
|
-
const open = source.indexOf(
|
|
263
|
+
const open = source.indexOf("<", cursor);
|
|
254
264
|
if (open < 0)
|
|
255
265
|
return;
|
|
256
266
|
const closing = source.charCodeAt(open + 1) === 47; /* / */
|
|
@@ -267,7 +277,11 @@ function* scanTags(source) {
|
|
|
267
277
|
cursor = open + 1;
|
|
268
278
|
continue;
|
|
269
279
|
}
|
|
270
|
-
yield {
|
|
280
|
+
yield {
|
|
281
|
+
name: source.slice(nameStart, nameEnd),
|
|
282
|
+
closing,
|
|
283
|
+
text: source.slice(open, end),
|
|
284
|
+
};
|
|
271
285
|
cursor = end;
|
|
272
286
|
}
|
|
273
287
|
}
|
|
@@ -288,7 +302,7 @@ export function findNearestParentComponent(source, componentMap) {
|
|
|
288
302
|
}
|
|
289
303
|
export function parseScriptOpenTagAttributes(openTag) {
|
|
290
304
|
const attrs = new Map();
|
|
291
|
-
const insideTag = openTag.replace(/^<script\b/i,
|
|
305
|
+
const insideTag = openTag.replace(/^<script\b/i, "").replace(/>$/, "");
|
|
292
306
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
293
307
|
let match;
|
|
294
308
|
while ((match = attrRe.exec(insideTag)) !== null) {
|
|
@@ -300,14 +314,32 @@ export function parseScriptOpenTagAttributes(openTag) {
|
|
|
300
314
|
}
|
|
301
315
|
return attrs;
|
|
302
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Attribute values of any open tag, keyed by lowercased name. A bare attribute
|
|
319
|
+
* maps to `""`, as the DOM reports it. The first occurrence of a name wins, as
|
|
320
|
+
* in the HTML tokenizer, and text inside a quoted value is never read as an
|
|
321
|
+
* attribute. An unquoted value keeps its slashes; a trailing `/>` ends it.
|
|
322
|
+
*/
|
|
323
|
+
function readOpenTagAttributes(openTag) {
|
|
324
|
+
const attrs = new Map();
|
|
325
|
+
const insideTag = openTag.replace(/^<[^\s/>]+/, "").replace(/\/?>$/, "");
|
|
326
|
+
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
327
|
+
let match;
|
|
328
|
+
while ((match = attrRe.exec(insideTag)) !== null) {
|
|
329
|
+
const name = match[1].toLowerCase();
|
|
330
|
+
if (!attrs.has(name))
|
|
331
|
+
attrs.set(name, match[2] ?? match[3] ?? match[4] ?? "");
|
|
332
|
+
}
|
|
333
|
+
return attrs;
|
|
334
|
+
}
|
|
303
335
|
export function resolveScriptTarget(value, documentDir, importRootAbs, kind) {
|
|
304
|
-
if (value.startsWith(
|
|
336
|
+
if (value.startsWith("./") || value.startsWith("../"))
|
|
305
337
|
return path.resolve(documentDir, value);
|
|
306
|
-
if (value.startsWith(
|
|
338
|
+
if (value.startsWith("@/"))
|
|
307
339
|
return path.resolve(importRootAbs, value.slice(2));
|
|
308
|
-
if (value.startsWith(
|
|
340
|
+
if (value.startsWith("/"))
|
|
309
341
|
return undefined;
|
|
310
|
-
if (kind ===
|
|
342
|
+
if (kind === "src") {
|
|
311
343
|
if (/^[a-z][a-z\d+.-]*:/i.test(value))
|
|
312
344
|
return undefined;
|
|
313
345
|
return path.resolve(documentDir, value);
|
|
@@ -315,17 +347,18 @@ export function resolveScriptTarget(value, documentDir, importRootAbs, kind) {
|
|
|
315
347
|
return undefined;
|
|
316
348
|
}
|
|
317
349
|
export function leadingSlashSpecifierMessage(specifier) {
|
|
318
|
-
const rest = specifier.replace(/^\/+/,
|
|
350
|
+
const rest = specifier.replace(/^\/+/, "");
|
|
319
351
|
return (`Leading-slash specifier '${specifier}' is not supported in Bascik scripts. ` +
|
|
320
352
|
`A bare '/' is ambiguous (filesystem root vs. site root). ` +
|
|
321
353
|
`Use '@/${rest}' to resolve against scripts.importRoot, ` +
|
|
322
354
|
`or './${rest}' to resolve relative to this file.`);
|
|
323
355
|
}
|
|
324
356
|
export function collectLeadingSlashDiagnostics(document, openTag, scriptBody, blockStart, attrs) {
|
|
325
|
-
if (!attrs.has(
|
|
326
|
-
!attrs.has(
|
|
327
|
-
!attrs.has(
|
|
328
|
-
!attrs.has(
|
|
357
|
+
if (!attrs.has("data-bascik-build") &&
|
|
358
|
+
!attrs.has("data-bascik-build-once") &&
|
|
359
|
+
!attrs.has("data-bascik-server") &&
|
|
360
|
+
!attrs.has("data-bascik-routes") &&
|
|
361
|
+
!attrs.has("data-bascik-stream")) {
|
|
329
362
|
return [];
|
|
330
363
|
}
|
|
331
364
|
const out = [];
|
|
@@ -337,40 +370,53 @@ export function collectLeadingSlashDiagnostics(document, openTag, scriptBody, bl
|
|
|
337
370
|
},
|
|
338
371
|
message: leadingSlashSpecifierMessage(specifier),
|
|
339
372
|
severity: DiagnosticSeverity.Error,
|
|
340
|
-
source:
|
|
341
|
-
code:
|
|
373
|
+
source: "bascik",
|
|
374
|
+
code: "leading-slash-specifier",
|
|
342
375
|
});
|
|
343
376
|
};
|
|
344
377
|
const srcMatch = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(openTag);
|
|
345
378
|
if (srcMatch) {
|
|
346
|
-
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ??
|
|
347
|
-
if (srcValue.startsWith(
|
|
379
|
+
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? "";
|
|
380
|
+
if (srcValue.startsWith("/")) {
|
|
348
381
|
const valueStart = blockStart + (srcMatch.index ?? 0) + srcMatch[0].indexOf(srcValue);
|
|
349
382
|
push(valueStart, valueStart + srcValue.length, srcValue);
|
|
350
383
|
}
|
|
351
384
|
}
|
|
352
385
|
const bodyStart = blockStart + openTag.length;
|
|
353
386
|
for (const { start, end, value } of findModuleSpecifiers(scriptBody)) {
|
|
354
|
-
if (value.startsWith(
|
|
387
|
+
if (value.startsWith("/"))
|
|
355
388
|
push(bodyStart + start, bodyStart + end, value);
|
|
356
389
|
}
|
|
357
390
|
return out;
|
|
358
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Escapes plain text so it renders as typed inside markdown: syntax characters are backslash-escaped, runs of
|
|
394
|
+
* spaces become ` `, `>` is escaped, and a newline becomes a hard break. Descriptions come from a
|
|
395
|
+
* component's `<!-- @bascik -->` header and are prose, not markdown, so `<unsafe>` or `*not bold*` stay literal.
|
|
396
|
+
* Matches VS Code's `MarkdownString.appendText`, so the text looks the same in every editor.
|
|
397
|
+
*/
|
|
398
|
+
export function escapeMarkdownText(text) {
|
|
399
|
+
return text
|
|
400
|
+
.replace(/[\\`*_{}[\]()#+\-!~]/g, "\\$&")
|
|
401
|
+
.replace(/([ \t]+)/g, (_match, spaces) => " ".repeat(spaces.length))
|
|
402
|
+
.replace(/>/g, "\\>")
|
|
403
|
+
.replace(/\n/g, "\\\n");
|
|
404
|
+
}
|
|
359
405
|
export function createComponentDocumentationMarkdown(relativePath, metadata) {
|
|
360
406
|
const parts = [];
|
|
361
407
|
if (metadata?.description) {
|
|
362
|
-
parts.push(metadata.description,
|
|
408
|
+
parts.push(escapeMarkdownText(metadata.description), "\n\n");
|
|
363
409
|
}
|
|
364
410
|
parts.push(`Bascik component from \`${relativePath}\`.`);
|
|
365
411
|
if (metadata) {
|
|
366
|
-
parts.push(
|
|
367
|
-
appendMetadataMembersMarkdown(parts,
|
|
368
|
-
appendMetadataMembersMarkdown(parts,
|
|
412
|
+
parts.push("\n\n");
|
|
413
|
+
appendMetadataMembersMarkdown(parts, "Props", metadata.props);
|
|
414
|
+
appendMetadataMembersMarkdown(parts, "Slots", [
|
|
369
415
|
...metadata.slots,
|
|
370
416
|
...(metadata.defaultSlot ? [metadata.defaultSlot] : []),
|
|
371
417
|
]);
|
|
372
418
|
}
|
|
373
|
-
return parts.join(
|
|
419
|
+
return parts.join("");
|
|
374
420
|
}
|
|
375
421
|
function appendMetadataMembersMarkdown(parts, label, members) {
|
|
376
422
|
if (members.length === 0)
|
|
@@ -379,15 +425,15 @@ function appendMetadataMembersMarkdown(parts, label, members) {
|
|
|
379
425
|
for (const member of members) {
|
|
380
426
|
parts.push(`- \`${member.name}\``);
|
|
381
427
|
if (member.description) {
|
|
382
|
-
parts.push(`: ${member.description}`);
|
|
428
|
+
parts.push(`: ${escapeMarkdownText(member.description)}`);
|
|
383
429
|
}
|
|
384
|
-
parts.push(
|
|
430
|
+
parts.push("\n");
|
|
385
431
|
}
|
|
386
|
-
parts.push(
|
|
432
|
+
parts.push("\n");
|
|
387
433
|
}
|
|
388
434
|
export function completionReplacementRange(document, position) {
|
|
389
435
|
const offset = document.offsetAt(position);
|
|
390
|
-
const prefix = /[^\s<>"'=]*$/.exec(document.getText().slice(0, offset))?.[0] ??
|
|
436
|
+
const prefix = /[^\s<>"'=]*$/.exec(document.getText().slice(0, offset))?.[0] ?? "";
|
|
391
437
|
return {
|
|
392
438
|
start: document.positionAt(offset - prefix.length),
|
|
393
439
|
end: position,
|
|
@@ -395,13 +441,13 @@ export function completionReplacementRange(document, position) {
|
|
|
395
441
|
}
|
|
396
442
|
export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts = []) {
|
|
397
443
|
const { languageId } = document;
|
|
398
|
-
if (languageId !==
|
|
399
|
-
languageId !==
|
|
400
|
-
languageId !==
|
|
401
|
-
languageId !==
|
|
444
|
+
if (languageId !== "css" &&
|
|
445
|
+
languageId !== "javascript" &&
|
|
446
|
+
languageId !== "typescript" &&
|
|
447
|
+
languageId !== "html") {
|
|
402
448
|
return [];
|
|
403
449
|
}
|
|
404
|
-
const normalizedDocumentPath = fsPath.replace(/\\/g,
|
|
450
|
+
const normalizedDocumentPath = fsPath.replace(/\\/g, "/");
|
|
405
451
|
if (/\.(test|spec)\.[a-z0-9]+$/i.test(normalizedDocumentPath)) {
|
|
406
452
|
return [];
|
|
407
453
|
}
|
|
@@ -409,27 +455,33 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
409
455
|
const diagnostics = [];
|
|
410
456
|
const isComponentDocument = snapshot !== undefined &&
|
|
411
457
|
isInsideComponentRoots(normalizedDocumentPath, snapshot.componentRoots);
|
|
412
|
-
|
|
413
|
-
|
|
458
|
+
// The API folder is `directory.api`, so it is read from the project when there is one. Without a project
|
|
459
|
+
// (a file opened on its own), the default folder name is the best guess.
|
|
460
|
+
const isInsideApiRoot = snapshot?.apiRoot
|
|
461
|
+
? isInsideComponentRoots(normalizedDocumentPath, [snapshot.apiRoot])
|
|
462
|
+
: normalizedDocumentPath.includes("/src/api/");
|
|
463
|
+
const isApiRouteDocument = isInsideApiRoot &&
|
|
464
|
+
(languageId === "typescript" || languageId === "javascript");
|
|
414
465
|
if (isApiRouteDocument) {
|
|
415
|
-
if (path.basename(normalizedDocumentPath).startsWith(
|
|
466
|
+
if (path.basename(normalizedDocumentPath).startsWith("_middleware.")) {
|
|
416
467
|
diagnostics.push({
|
|
417
468
|
range: {
|
|
418
469
|
start: { line: 0, character: 0 },
|
|
419
470
|
end: { line: 0, character: Math.min(text.length, 10) },
|
|
420
471
|
},
|
|
421
|
-
message:
|
|
472
|
+
message: "Middleware chains (_middleware.ts) are not supported by design in Bascik API routes. Compose plain functions directly in handler files.",
|
|
422
473
|
severity: DiagnosticSeverity.Error,
|
|
423
|
-
source:
|
|
474
|
+
source: "bascik",
|
|
475
|
+
code: "middleware-unsupported",
|
|
424
476
|
});
|
|
425
477
|
}
|
|
426
478
|
const apiDiags = analyzeApiRouteSource(text);
|
|
427
479
|
for (const diag of apiDiags) {
|
|
428
480
|
let severity = DiagnosticSeverity.Warning;
|
|
429
|
-
if (diag.severity ===
|
|
481
|
+
if (diag.severity === "error") {
|
|
430
482
|
severity = DiagnosticSeverity.Error;
|
|
431
483
|
}
|
|
432
|
-
else if (diag.severity ===
|
|
484
|
+
else if (diag.severity === "info") {
|
|
433
485
|
severity = DiagnosticSeverity.Information;
|
|
434
486
|
}
|
|
435
487
|
diagnostics.push({
|
|
@@ -439,69 +491,70 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
439
491
|
},
|
|
440
492
|
message: diag.message,
|
|
441
493
|
severity,
|
|
442
|
-
source:
|
|
494
|
+
source: "bascik",
|
|
495
|
+
code: diag.code,
|
|
443
496
|
});
|
|
444
497
|
}
|
|
445
498
|
}
|
|
446
|
-
if (languageId ===
|
|
447
|
-
if (snapshot &&
|
|
499
|
+
if (languageId === "html" && fsPath.startsWith("/")) {
|
|
500
|
+
if (snapshot &&
|
|
501
|
+
isInsideComponentRoots(normalizedDocumentPath, snapshot.componentRoots)) {
|
|
448
502
|
const fileName = path.basename(normalizedDocumentPath);
|
|
449
|
-
const nameWithoutExt = fileName.replace(/\.html$/i,
|
|
450
|
-
if (!nameWithoutExt.includes(
|
|
503
|
+
const nameWithoutExt = fileName.replace(/\.html$/i, "").toLowerCase();
|
|
504
|
+
if (!nameWithoutExt.includes("-") &&
|
|
451
505
|
!BUILT_IN_HTML_ELEMENTS.has(nameWithoutExt)) {
|
|
452
506
|
diagnostics.push({
|
|
453
507
|
range: {
|
|
454
508
|
start: { line: 0, character: 0 },
|
|
455
509
|
end: { line: 0, character: Math.min(text.length, 10) },
|
|
456
510
|
},
|
|
511
|
+
code: "component-name-not-hyphenated",
|
|
457
512
|
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.`,
|
|
458
513
|
severity: DiagnosticSeverity.Warning,
|
|
459
|
-
source:
|
|
514
|
+
source: "bascik",
|
|
460
515
|
});
|
|
461
516
|
}
|
|
462
517
|
}
|
|
463
518
|
}
|
|
519
|
+
// Scoping rules describe what Bascik cannot rewrite, so they apply to component files, the only code it scopes.
|
|
520
|
+
// Pages and API routes are not scoped. Without a project there is no way to tell, so every file is checked.
|
|
521
|
+
const appliesScopingRules = snapshot === undefined || isComponentDocument;
|
|
464
522
|
const addCompatibilityDiagnostics = (sourceText, kind, offset) => {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
const
|
|
471
|
-
if (!match || typeof match.index !== 'number')
|
|
472
|
-
continue;
|
|
473
|
-
const start = document.positionAt(offset + match.index);
|
|
474
|
-
const end = document.positionAt(offset + match.index + Math.max(match[0].length, 1));
|
|
475
|
-
const severity = rule.severity === 'warning'
|
|
523
|
+
if (!appliesScopingRules)
|
|
524
|
+
return;
|
|
525
|
+
for (const { rule, index, length } of findCompatibilityMatches(sourceText, kind)) {
|
|
526
|
+
const start = document.positionAt(offset + index);
|
|
527
|
+
const end = document.positionAt(offset + index + length);
|
|
528
|
+
const severity = rule.severity === "warning"
|
|
476
529
|
? DiagnosticSeverity.Warning
|
|
477
530
|
: DiagnosticSeverity.Error;
|
|
478
531
|
diagnostics.push({
|
|
479
532
|
range: { start, end },
|
|
480
533
|
message: `${rule.message} ${rule.suggestion}`,
|
|
481
534
|
severity,
|
|
482
|
-
source:
|
|
535
|
+
source: "bascik",
|
|
483
536
|
code: rule.id,
|
|
484
537
|
});
|
|
485
538
|
}
|
|
486
539
|
};
|
|
487
540
|
const isJavaScriptScriptTag = (openTag) => {
|
|
488
541
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
489
|
-
const typeValue = attrs.get(
|
|
542
|
+
const typeValue = attrs.get("type");
|
|
490
543
|
if (!typeValue || typeValue === true)
|
|
491
544
|
return true;
|
|
492
545
|
const normalized = String(typeValue).trim().toLowerCase();
|
|
493
|
-
return (normalized ===
|
|
494
|
-
normalized ===
|
|
495
|
-
normalized ===
|
|
496
|
-
normalized ===
|
|
497
|
-
normalized ===
|
|
546
|
+
return (normalized === "module" ||
|
|
547
|
+
normalized === "text/javascript" ||
|
|
548
|
+
normalized === "application/javascript" ||
|
|
549
|
+
normalized === "text/ecmascript" ||
|
|
550
|
+
normalized === "application/ecmascript");
|
|
498
551
|
};
|
|
499
552
|
const scriptBlockRe = new RegExp(SCRIPT_BLOCK_RE.source, SCRIPT_BLOCK_RE.flags);
|
|
500
553
|
const styleBlockRe = /(<style(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)<\/style(?:[\t\n\f\r /][^>]*)?>/gi;
|
|
501
|
-
if (languageId ===
|
|
554
|
+
if (languageId === "html") {
|
|
502
555
|
if (isComponentDocument) {
|
|
503
556
|
const metadata = analyzeComponentSource(text, {
|
|
504
|
-
hasCompanionStyles: fs.existsSync(fsPath.replace(/\.html$/i,
|
|
557
|
+
hasCompanionStyles: fs.existsSync(fsPath.replace(/\.html$/i, ".css")),
|
|
505
558
|
});
|
|
506
559
|
for (const metadataDiagnostic of metadata.diagnostics) {
|
|
507
560
|
diagnostics.push({
|
|
@@ -511,19 +564,22 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
511
564
|
},
|
|
512
565
|
message: metadataDiagnostic.message,
|
|
513
566
|
severity: DiagnosticSeverity.Warning,
|
|
514
|
-
source:
|
|
567
|
+
source: "bascik",
|
|
515
568
|
code: metadataDiagnostic.code,
|
|
516
569
|
});
|
|
517
570
|
}
|
|
518
|
-
const referenceScanText = maskHtmlRawTextContents(text).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) =>
|
|
519
|
-
const declaredIds = new Set(Array.from(referenceScanText.matchAll(/\sid\s*=\s*(?:"([^"]+)"|'([^']+)')/gi))
|
|
520
|
-
.map((match) => match[1] ?? match[2])
|
|
571
|
+
const referenceScanText = maskHtmlRawTextContents(text).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) => " ".repeat(comment.length));
|
|
572
|
+
const declaredIds = new Set(Array.from(referenceScanText.matchAll(/\sid\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s"'=<>`/>]+))/gi))
|
|
573
|
+
.map((match) => match[1] ?? match[2] ?? match[3])
|
|
521
574
|
.filter((id) => Boolean(id)));
|
|
522
|
-
const idReferenceAttributeRegex = /\s(for|itemref|aria-activedescendant|aria-details|aria-errormessage|aria-labelledby|aria-describedby|aria-controls|aria-owns|aria-flowto)\s*=\s*(?:"([^"]*)"|'([^']*)')/gi;
|
|
575
|
+
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;
|
|
523
576
|
let idReferenceMatch;
|
|
524
577
|
while ((idReferenceMatch =
|
|
525
578
|
idReferenceAttributeRegex.exec(referenceScanText)) !== null) {
|
|
526
|
-
const value = idReferenceMatch[2] ??
|
|
579
|
+
const value = idReferenceMatch[2] ??
|
|
580
|
+
idReferenceMatch[3] ??
|
|
581
|
+
idReferenceMatch[4] ??
|
|
582
|
+
"";
|
|
527
583
|
for (const id of value.trim().split(/\s+/).filter(Boolean)) {
|
|
528
584
|
if (declaredIds.has(id))
|
|
529
585
|
continue;
|
|
@@ -532,17 +588,20 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
532
588
|
const end = document.positionAt(idReferenceMatch.index + Math.max(valueOffset, 0) + id.length);
|
|
533
589
|
diagnostics.push({
|
|
534
590
|
range: { start, end },
|
|
591
|
+
code: "id-reference-undeclared",
|
|
535
592
|
message: `ID reference "${id}" is not declared in this component and will be left unscoped.`,
|
|
536
593
|
severity: DiagnosticSeverity.Information,
|
|
537
|
-
source:
|
|
594
|
+
source: "bascik",
|
|
538
595
|
});
|
|
539
596
|
}
|
|
540
597
|
}
|
|
541
|
-
const fragmentReferenceRegex = /\shref\s*=\s*(?:"#([^"]+)"|'#([^']+)')/gi;
|
|
598
|
+
const fragmentReferenceRegex = /\shref\s*=\s*(?:"#([^"]+)"|'#([^']+)'|#([^\s"'=<>`/>]+))/gi;
|
|
542
599
|
let fragmentReferenceMatch;
|
|
543
600
|
while ((fragmentReferenceMatch =
|
|
544
601
|
fragmentReferenceRegex.exec(referenceScanText)) !== null) {
|
|
545
|
-
const id = fragmentReferenceMatch[1] ??
|
|
602
|
+
const id = fragmentReferenceMatch[1] ??
|
|
603
|
+
fragmentReferenceMatch[2] ??
|
|
604
|
+
fragmentReferenceMatch[3];
|
|
546
605
|
if (!id || declaredIds.has(id))
|
|
547
606
|
continue;
|
|
548
607
|
const idOffset = fragmentReferenceMatch[0].indexOf(id);
|
|
@@ -550,31 +609,33 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
550
609
|
const end = document.positionAt(fragmentReferenceMatch.index + Math.max(idOffset, 0) + id.length);
|
|
551
610
|
diagnostics.push({
|
|
552
611
|
range: { start, end },
|
|
612
|
+
code: "id-reference-undeclared",
|
|
553
613
|
message: `ID reference "${id}" is not declared in this component and will be left unscoped.`,
|
|
554
614
|
severity: DiagnosticSeverity.Information,
|
|
555
|
-
source:
|
|
615
|
+
source: "bascik",
|
|
556
616
|
});
|
|
557
617
|
}
|
|
558
618
|
}
|
|
559
|
-
const preserveDirectiveRegex = /data-bascik-preserve(?:\s*=\s*("([^"]*)"|'([^']*)'))?/gi;
|
|
619
|
+
const preserveDirectiveRegex = /data-bascik-preserve(?:\s*=\s*("([^"]*)"|'([^']*)'|([^\s"'=<>`/>]+)))?/gi;
|
|
560
620
|
let preserveMatch;
|
|
561
621
|
while ((preserveMatch = preserveDirectiveRegex.exec(text)) !== null) {
|
|
562
622
|
if (preserveMatch[1] === undefined)
|
|
563
623
|
continue;
|
|
564
|
-
const value = preserveMatch[2] ?? preserveMatch[3] ??
|
|
624
|
+
const value = preserveMatch[2] ?? preserveMatch[3] ?? preserveMatch[4] ?? "";
|
|
565
625
|
for (const preserveToken of value.trim().split(/\s+/).filter(Boolean)) {
|
|
566
|
-
if (preserveToken ===
|
|
567
|
-
preserveToken ===
|
|
568
|
-
preserveToken ===
|
|
626
|
+
if (preserveToken === "id" ||
|
|
627
|
+
preserveToken === "name" ||
|
|
628
|
+
preserveToken === "class")
|
|
569
629
|
continue;
|
|
570
630
|
const tokenOffset = preserveMatch[0].indexOf(preserveToken);
|
|
571
631
|
const start = document.positionAt(preserveMatch.index + Math.max(tokenOffset, 0));
|
|
572
632
|
const end = document.positionAt(preserveMatch.index + Math.max(tokenOffset, 0) + preserveToken.length);
|
|
573
633
|
diagnostics.push({
|
|
574
634
|
range: { start, end },
|
|
635
|
+
code: "unknown-preserve-token",
|
|
575
636
|
message: `Unknown data-bascik-preserve token "${preserveToken}". Valid tokens are id, name, and class.`,
|
|
576
637
|
severity: DiagnosticSeverity.Warning,
|
|
577
|
-
source:
|
|
638
|
+
source: "bascik",
|
|
578
639
|
});
|
|
579
640
|
}
|
|
580
641
|
}
|
|
@@ -582,35 +643,38 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
582
643
|
let formMatch;
|
|
583
644
|
while (isComponentDocument &&
|
|
584
645
|
(formMatch = formOpenTagRegex.exec(text)) !== null) {
|
|
585
|
-
const
|
|
586
|
-
const action =
|
|
646
|
+
const formAttributes = readOpenTagAttributes(formMatch[0]);
|
|
647
|
+
const action = formAttributes.get("action");
|
|
587
648
|
if (!action || !/^(?:https?:)?\/\//i.test(action))
|
|
588
649
|
continue;
|
|
589
|
-
const
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
650
|
+
const preserveValue = formAttributes.get("data-bascik-preserve");
|
|
651
|
+
// A bare directive, or an empty value (the bare form as serializers
|
|
652
|
+
// write it), preserves every attribute, `name` included.
|
|
653
|
+
const preserveTokens = (preserveValue ?? "")
|
|
654
|
+
.trim()
|
|
655
|
+
.split(/\s+/)
|
|
656
|
+
.filter(Boolean);
|
|
657
|
+
const preservesName = preserveValue !== undefined &&
|
|
658
|
+
(preserveTokens.length === 0 || preserveTokens.includes("name"));
|
|
596
659
|
if (preservesName)
|
|
597
660
|
continue;
|
|
598
661
|
const start = document.positionAt(formMatch.index);
|
|
599
662
|
const end = document.positionAt(formMatch.index + formMatch[0].length);
|
|
600
663
|
diagnostics.push({
|
|
601
664
|
range: { start, end },
|
|
665
|
+
code: "external-form-missing-preserve-name",
|
|
602
666
|
message: 'External form actions require data-bascik-preserve="name" so submitted field names remain literal.',
|
|
603
667
|
severity: DiagnosticSeverity.Warning,
|
|
604
|
-
source:
|
|
668
|
+
source: "bascik",
|
|
605
669
|
});
|
|
606
670
|
}
|
|
607
671
|
if (snapshot && isComponentDocument) {
|
|
608
672
|
const componentName = normalizeComponentName(fsPath);
|
|
609
|
-
const directiveRegex = /data-bascik-attr-([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([\w-]+)"|'([\w-]+)')/gi;
|
|
673
|
+
const directiveRegex = /data-bascik-attr-([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([\w-]+)"|'([\w-]+)'|([\w-]+))/gi;
|
|
610
674
|
let directiveMatch;
|
|
611
675
|
while ((directiveMatch = directiveRegex.exec(text)) !== null) {
|
|
612
676
|
const targetName = directiveMatch[1];
|
|
613
|
-
const propName = directiveMatch[2] ?? directiveMatch[3];
|
|
677
|
+
const propName = directiveMatch[2] ?? directiveMatch[3] ?? directiveMatch[4];
|
|
614
678
|
const suppliedInOpen = openDocumentTexts.some(({ text }) => htmlSuppliesComponentProp(text, componentName, propName));
|
|
615
679
|
const suppliedInDisk = Array.from(snapshot.htmlUsageByFile.values()).some((usageHtml) => htmlSuppliesComponentProp(usageHtml, componentName, propName));
|
|
616
680
|
if (suppliedInOpen || suppliedInDisk)
|
|
@@ -619,23 +683,27 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
619
683
|
const end = document.positionAt(directiveMatch.index + directiveMatch[0].length);
|
|
620
684
|
diagnostics.push({
|
|
621
685
|
range: { start, end },
|
|
686
|
+
// A hint, not a warning. Whether anything supplies the prop is a question about every page, and the
|
|
687
|
+
// snapshot only reads src/**/*.html, so usage from Markdown or a build script is invisible to it.
|
|
688
|
+
code: "attr-prop-unsupplied",
|
|
622
689
|
message: `data-bascik-attr-${targetName} references prop "${propName}", but no <${componentName}> usage supplies data-bascik-prop-${propName}.`,
|
|
623
|
-
severity: DiagnosticSeverity.
|
|
624
|
-
source:
|
|
690
|
+
severity: DiagnosticSeverity.Information,
|
|
691
|
+
source: "bascik",
|
|
625
692
|
});
|
|
626
693
|
}
|
|
627
694
|
}
|
|
628
695
|
let scriptMatch;
|
|
629
696
|
while ((scriptMatch = scriptBlockRe.exec(text)) !== null) {
|
|
630
697
|
const openTag = scriptMatch[1];
|
|
631
|
-
const scriptBody = scriptMatch[2] ??
|
|
698
|
+
const scriptBody = scriptMatch[2] ?? "";
|
|
632
699
|
const scriptBodyOffset = (scriptMatch.index ?? 0) + openTag.length;
|
|
633
700
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
634
701
|
const directiveAttrs = [
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
702
|
+
"data-bascik-build",
|
|
703
|
+
"data-bascik-build-once",
|
|
704
|
+
"data-bascik-server",
|
|
705
|
+
"data-bascik-routes",
|
|
706
|
+
"data-bascik-stream",
|
|
639
707
|
];
|
|
640
708
|
const presentDirectives = directiveAttrs.filter((d) => attrs.has(d));
|
|
641
709
|
for (let i = 0; i < presentDirectives.length; i++) {
|
|
@@ -643,20 +711,25 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
643
711
|
const d1 = presentDirectives[i];
|
|
644
712
|
const d2 = presentDirectives[j];
|
|
645
713
|
let message;
|
|
646
|
-
if ((d1 ===
|
|
647
|
-
(d1 ===
|
|
714
|
+
if ((d1 === "data-bascik-build" && d2 === "data-bascik-build-once") ||
|
|
715
|
+
(d1 === "data-bascik-build-once" && d2 === "data-bascik-build")) {
|
|
648
716
|
message =
|
|
649
|
-
|
|
717
|
+
"data-bascik-build and data-bascik-build-once cannot both appear on the same <script> tag. Choose one build-script mode.";
|
|
650
718
|
}
|
|
651
|
-
else if ((d1 ===
|
|
652
|
-
(d1 ===
|
|
719
|
+
else if ((d1 === "data-bascik-build" && d2 === "data-bascik-server") ||
|
|
720
|
+
(d1 === "data-bascik-server" && d2 === "data-bascik-build")) {
|
|
653
721
|
message =
|
|
654
|
-
|
|
722
|
+
"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.";
|
|
655
723
|
}
|
|
656
|
-
else if ((d1 ===
|
|
657
|
-
(d1 ===
|
|
724
|
+
else if ((d1 === "data-bascik-routes" && d2 === "data-bascik-server") ||
|
|
725
|
+
(d1 === "data-bascik-server" && d2 === "data-bascik-routes")) {
|
|
658
726
|
message =
|
|
659
|
-
|
|
727
|
+
"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.";
|
|
728
|
+
}
|
|
729
|
+
else if ((d1 === "data-bascik-routes" && d2 === "data-bascik-build") ||
|
|
730
|
+
(d1 === "data-bascik-build" && d2 === "data-bascik-routes")) {
|
|
731
|
+
message =
|
|
732
|
+
"data-bascik-routes and data-bascik-build cannot both appear on the same <script> tag. Remove one.";
|
|
660
733
|
}
|
|
661
734
|
else {
|
|
662
735
|
message = `${d1} and ${d2} cannot both appear on the same <script> tag. Remove one.`;
|
|
@@ -667,22 +740,52 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
667
740
|
range: { start, end },
|
|
668
741
|
message,
|
|
669
742
|
severity: DiagnosticSeverity.Error,
|
|
670
|
-
source:
|
|
743
|
+
source: "bascik",
|
|
744
|
+
code: "script-mode-conflict",
|
|
671
745
|
});
|
|
672
746
|
}
|
|
673
747
|
}
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
748
|
+
const buildValue = attrs.get("data-bascik-build");
|
|
749
|
+
if (typeof buildValue === "string" && !isKnownBuildValue(buildValue)) {
|
|
750
|
+
diagnostics.push({
|
|
751
|
+
range: {
|
|
752
|
+
start: document.positionAt(scriptMatch.index ?? 0),
|
|
753
|
+
end: document.positionAt((scriptMatch.index ?? 0) + openTag.length),
|
|
754
|
+
},
|
|
755
|
+
message: unknownDirectiveValueMessage(),
|
|
756
|
+
severity: DiagnosticSeverity.Warning,
|
|
757
|
+
source: "bascik",
|
|
758
|
+
code: "unknown-directive-value",
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
const buildOnceValue = attrs.get("data-bascik-build-once");
|
|
762
|
+
if (typeof buildOnceValue === "string" &&
|
|
763
|
+
!isKnownBuildValue(buildOnceValue)) {
|
|
764
|
+
diagnostics.push({
|
|
765
|
+
range: {
|
|
766
|
+
start: document.positionAt(scriptMatch.index ?? 0),
|
|
767
|
+
end: document.positionAt((scriptMatch.index ?? 0) + openTag.length),
|
|
768
|
+
},
|
|
769
|
+
message: unknownDirectiveValueMessage("data-bascik-build-once"),
|
|
770
|
+
severity: DiagnosticSeverity.Warning,
|
|
771
|
+
source: "bascik",
|
|
772
|
+
code: "unknown-directive-value",
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
const hasServerDirective = attrs.has("data-bascik-server") ||
|
|
776
|
+
attrs.has("data-bascik-stream") ||
|
|
777
|
+
attrs.has("data-bascik-build") ||
|
|
778
|
+
attrs.has("data-bascik-build-once") ||
|
|
779
|
+
attrs.has("data-bascik-routes");
|
|
678
780
|
if (hasServerDirective) {
|
|
679
|
-
const directive = attrs.has(
|
|
680
|
-
?
|
|
681
|
-
: attrs.has(
|
|
682
|
-
?
|
|
683
|
-
: attrs.has(
|
|
684
|
-
|
|
685
|
-
|
|
781
|
+
const directive = attrs.has("data-bascik-stream")
|
|
782
|
+
? "stream"
|
|
783
|
+
: attrs.has("data-bascik-server")
|
|
784
|
+
? "server"
|
|
785
|
+
: attrs.has("data-bascik-build") ||
|
|
786
|
+
attrs.has("data-bascik-build-once")
|
|
787
|
+
? "build"
|
|
788
|
+
: "routes";
|
|
686
789
|
const hasSrcAttribute = /\ssrc\s*=/i.test(openTag);
|
|
687
790
|
const serverDiags = analyzeServerScriptSource(scriptBody, {
|
|
688
791
|
hasSrcAttribute,
|
|
@@ -692,53 +795,102 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
692
795
|
const start = document.positionAt(scriptBodyOffset + sd.start);
|
|
693
796
|
const end = document.positionAt(scriptBodyOffset + sd.end);
|
|
694
797
|
let severity = DiagnosticSeverity.Error;
|
|
695
|
-
if (sd.severity ===
|
|
798
|
+
if (sd.severity === "warning") {
|
|
696
799
|
severity = DiagnosticSeverity.Warning;
|
|
697
800
|
}
|
|
698
|
-
else if (sd.severity ===
|
|
801
|
+
else if (sd.severity === "info") {
|
|
699
802
|
severity = DiagnosticSeverity.Information;
|
|
700
803
|
}
|
|
701
804
|
diagnostics.push({
|
|
702
805
|
range: { start, end },
|
|
703
806
|
message: sd.message,
|
|
704
807
|
severity,
|
|
705
|
-
source:
|
|
808
|
+
source: "bascik",
|
|
706
809
|
code: sd.code,
|
|
707
810
|
});
|
|
708
811
|
}
|
|
709
812
|
}
|
|
710
813
|
diagnostics.push(...collectLeadingSlashDiagnostics(document, openTag, scriptBody, scriptMatch.index ?? 0, attrs));
|
|
711
|
-
|
|
712
|
-
|
|
814
|
+
// A directive script runs in Node, at build time or per request, and never in the browser Bascik scopes.
|
|
815
|
+
if (isJavaScriptScriptTag(openTag) && !hasServerDirective) {
|
|
816
|
+
addCompatibilityDiagnostics(scriptBody, "js", scriptBodyOffset);
|
|
713
817
|
}
|
|
714
818
|
}
|
|
715
819
|
let styleMatch;
|
|
716
|
-
const hasCompanionCss = fsPath.toLowerCase().endsWith(
|
|
717
|
-
fs.existsSync(fsPath.replace(/\.html$/i,
|
|
820
|
+
const hasCompanionCss = fsPath.toLowerCase().endsWith(".html") &&
|
|
821
|
+
fs.existsSync(fsPath.replace(/\.html$/i, ".css"));
|
|
718
822
|
const maskedText = text
|
|
719
|
-
.replace(/(<(style|textarea|script)(?=[\t\n\f\r />])(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/\2(?:[\t\n\f\r /][^>]*)?>)/gi, (_m, open, _tag, content, close) => open +
|
|
720
|
-
.replace(/<!--([\s\S]*?)-->/g, (_m, content) =>
|
|
823
|
+
.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)
|
|
824
|
+
.replace(/<!--([\s\S]*?)-->/g, (_m, content) => "<!--" + " ".repeat(content.length) + "-->");
|
|
721
825
|
const componentMap = snapshot?.componentMap ?? new Map();
|
|
722
826
|
const componentNames = Array.from(componentMap.keys());
|
|
827
|
+
// Typos in Bascik's own attributes. Bascik ignores an attribute it does not know, so nothing else reports
|
|
828
|
+
// them. `<code>` contents are shown as text by default (`scoping.preserve`), so they are not scanned.
|
|
829
|
+
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);
|
|
830
|
+
const anyOpenTagRe = /<([A-Za-z][\w-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
|
|
831
|
+
let anyOpenTag;
|
|
832
|
+
while ((anyOpenTag = anyOpenTagRe.exec(attributeScanText)) !== null) {
|
|
833
|
+
const attributesOffset = anyOpenTag.index + 1 + anyOpenTag[1].length;
|
|
834
|
+
const attributeNameRe = /(?:^|\s)([^\s"'=<>`/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/g;
|
|
835
|
+
let attributeMatch;
|
|
836
|
+
while ((attributeMatch = attributeNameRe.exec(anyOpenTag[2])) !== null) {
|
|
837
|
+
const attributeName = attributeMatch[1].toLowerCase();
|
|
838
|
+
if (!attributeName.startsWith("data-bascik-") ||
|
|
839
|
+
isKnownBascikAttribute(attributeName))
|
|
840
|
+
continue;
|
|
841
|
+
const nameStart = attributesOffset +
|
|
842
|
+
attributeMatch.index +
|
|
843
|
+
attributeMatch[0].indexOf(attributeMatch[1]);
|
|
844
|
+
diagnostics.push({
|
|
845
|
+
range: {
|
|
846
|
+
start: document.positionAt(nameStart),
|
|
847
|
+
end: document.positionAt(nameStart + attributeMatch[1].length),
|
|
848
|
+
},
|
|
849
|
+
message: unknownBascikAttributeMessage(attributeName),
|
|
850
|
+
severity: DiagnosticSeverity.Warning,
|
|
851
|
+
source: "bascik",
|
|
852
|
+
code: "unknown-bascik-attribute",
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
// Component layout convention: styles above the markup, scripts below it.
|
|
857
|
+
if (isComponentDocument) {
|
|
858
|
+
for (const problem of findComponentStructureProblems(maskedText)) {
|
|
859
|
+
const tagLength = problem.kind === "script-before-markup"
|
|
860
|
+
? "<script".length
|
|
861
|
+
: "<style".length;
|
|
862
|
+
diagnostics.push({
|
|
863
|
+
range: {
|
|
864
|
+
start: document.positionAt(problem.index),
|
|
865
|
+
end: document.positionAt(problem.index + tagLength),
|
|
866
|
+
},
|
|
867
|
+
message: problem.message,
|
|
868
|
+
severity: DiagnosticSeverity.Warning,
|
|
869
|
+
source: "bascik",
|
|
870
|
+
code: "component-structure-order",
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
}
|
|
723
874
|
// 1. Diagnose script directives on non-script tags
|
|
724
875
|
const elementOpenTagRe = /<([A-Za-z][\w-]*)(?:\s+(?:[^>"']|"[^"]*"|'[^']*')*?)?\/?>/gi;
|
|
725
876
|
let elementMatch;
|
|
726
877
|
while ((elementMatch = elementOpenTagRe.exec(maskedText)) !== null) {
|
|
727
878
|
const tagName = elementMatch[1].toLowerCase();
|
|
728
|
-
if (tagName ===
|
|
879
|
+
if (tagName === "script")
|
|
729
880
|
continue;
|
|
730
881
|
const openTag = elementMatch[0];
|
|
731
882
|
const matchStart = elementMatch.index;
|
|
732
883
|
const scriptDirectives = new Set([
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
884
|
+
"data-bascik-build",
|
|
885
|
+
"data-bascik-build-once",
|
|
886
|
+
"data-bascik-server",
|
|
887
|
+
"data-bascik-routes",
|
|
888
|
+
"data-bascik-stream",
|
|
737
889
|
]);
|
|
738
890
|
const insideTag = openTag
|
|
739
|
-
.replace(/^<[A-Za-z][\w-]*\s*/i,
|
|
740
|
-
.replace(/\/?>$/,
|
|
741
|
-
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith(
|
|
891
|
+
.replace(/^<[A-Za-z][\w-]*\s*/i, "")
|
|
892
|
+
.replace(/\/?>$/, "");
|
|
893
|
+
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith("/>") ? 2 : 1);
|
|
742
894
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
743
895
|
let attrMatch;
|
|
744
896
|
while ((attrMatch = attrRe.exec(insideTag)) !== null) {
|
|
@@ -748,9 +900,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
748
900
|
const end = document.positionAt(matchStart + baseOffset + attrMatch.index + attrMatch[1].length);
|
|
749
901
|
diagnostics.push({
|
|
750
902
|
range: { start, end },
|
|
903
|
+
code: "directive-on-non-script",
|
|
751
904
|
message: `\`${attrMatch[1]}\` is only valid on <script> tags. It has no effect on <${tagName}>.`,
|
|
752
905
|
severity: DiagnosticSeverity.Error,
|
|
753
|
-
source:
|
|
906
|
+
source: "bascik",
|
|
754
907
|
});
|
|
755
908
|
}
|
|
756
909
|
}
|
|
@@ -762,14 +915,14 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
762
915
|
const openTag = slotMatch[0];
|
|
763
916
|
const matchStart = slotMatch.index;
|
|
764
917
|
const insideTag = openTag
|
|
765
|
-
.replace(/^<[A-Za-z][\w-]*\s*/i,
|
|
766
|
-
.replace(/\/?>$/,
|
|
767
|
-
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith(
|
|
918
|
+
.replace(/^<[A-Za-z][\w-]*\s*/i, "")
|
|
919
|
+
.replace(/\/?>$/, "");
|
|
920
|
+
const baseOffset = openTag.length - insideTag.length - (openTag.endsWith("/>") ? 2 : 1);
|
|
768
921
|
const attrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gi;
|
|
769
922
|
let attrMatch;
|
|
770
923
|
let slotAttr = null;
|
|
771
924
|
while ((attrMatch = attrRe.exec(insideTag)) !== null) {
|
|
772
|
-
if (attrMatch[1].toLowerCase() ===
|
|
925
|
+
if (attrMatch[1].toLowerCase() === "data-bascik-slot") {
|
|
773
926
|
slotAttr = {
|
|
774
927
|
name: attrMatch[1],
|
|
775
928
|
value: attrMatch[2] ?? attrMatch[3] ?? attrMatch[4],
|
|
@@ -789,9 +942,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
789
942
|
const end = document.positionAt(matchStart + baseOffset + slotAttr.index + slotAttr.name.length);
|
|
790
943
|
diagnostics.push({
|
|
791
944
|
range: { start, end },
|
|
792
|
-
|
|
945
|
+
code: "slot-outside-component",
|
|
946
|
+
message: "`data-bascik-slot` is only valid inside a Bascik component body.",
|
|
793
947
|
severity: DiagnosticSeverity.Error,
|
|
794
|
-
source:
|
|
948
|
+
source: "bascik",
|
|
795
949
|
});
|
|
796
950
|
}
|
|
797
951
|
else if (parentName) {
|
|
@@ -803,9 +957,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
803
957
|
const end = document.positionAt(matchStart + baseOffset + slotAttr.index + slotAttr.name.length);
|
|
804
958
|
diagnostics.push({
|
|
805
959
|
range: { start, end },
|
|
960
|
+
code: "undeclared-slot",
|
|
806
961
|
message: `Component <${parentName}> does not declare slot "${slotName}".`,
|
|
807
962
|
severity: DiagnosticSeverity.Error,
|
|
808
|
-
source:
|
|
963
|
+
source: "bascik",
|
|
809
964
|
});
|
|
810
965
|
}
|
|
811
966
|
}
|
|
@@ -813,8 +968,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
813
968
|
}
|
|
814
969
|
if (componentNames.length > 0) {
|
|
815
970
|
componentNames.sort((a, b) => b.length - a.length);
|
|
816
|
-
const escapedNames = componentNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g,
|
|
817
|
-
const componentTagRe = new RegExp(`<(${escapedNames.join(
|
|
971
|
+
const escapedNames = componentNames.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
972
|
+
const componentTagRe = new RegExp(`<(${escapedNames.join("|")})\\b`, "gi");
|
|
818
973
|
let compMatch;
|
|
819
974
|
while ((compMatch = componentTagRe.exec(maskedText)) !== null) {
|
|
820
975
|
const tagStartIndex = compMatch.index;
|
|
@@ -830,7 +985,7 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
830
985
|
else if (char === "'" && !inDoubleQuote) {
|
|
831
986
|
inSingleQuote = !inSingleQuote;
|
|
832
987
|
}
|
|
833
|
-
else if (char ===
|
|
988
|
+
else if (char === ">" && !inDoubleQuote && !inSingleQuote) {
|
|
834
989
|
openTagEndIndex = i + 1;
|
|
835
990
|
break;
|
|
836
991
|
}
|
|
@@ -852,7 +1007,38 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
852
1007
|
range: { start, end },
|
|
853
1008
|
message: `Component <${tagName}> does not declare prop "${propName}".`,
|
|
854
1009
|
severity: DiagnosticSeverity.Warning,
|
|
855
|
-
source:
|
|
1010
|
+
source: "bascik",
|
|
1011
|
+
code: "undeclared-prop",
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
if (compMeta.props.length > 0) {
|
|
1016
|
+
const insideOpenTag = openTagText
|
|
1017
|
+
.replace(new RegExp(`^<${escapeRegExp(tagName)}\\b`, "i"), "")
|
|
1018
|
+
.replace(/\/?>$/, "");
|
|
1019
|
+
const baseAttrOffset = openTagText.indexOf(insideOpenTag);
|
|
1020
|
+
const bareAttrRe = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?/gi;
|
|
1021
|
+
let bareMatch;
|
|
1022
|
+
while ((bareMatch = bareAttrRe.exec(insideOpenTag)) !== null) {
|
|
1023
|
+
const attrName = bareMatch[1];
|
|
1024
|
+
const lowerAttr = attrName.toLowerCase();
|
|
1025
|
+
if (lowerAttr.startsWith("data-bascik-"))
|
|
1026
|
+
continue;
|
|
1027
|
+
const declaredProp = compMeta.props.find((p) => p.name.toLowerCase() === lowerAttr);
|
|
1028
|
+
if (!declaredProp)
|
|
1029
|
+
continue;
|
|
1030
|
+
const alreadySupplied = new RegExp(`\\bdata-bascik-prop-${escapeRegExp(declaredProp.name)}\\b`, "i").test(openTagText);
|
|
1031
|
+
if (alreadySupplied)
|
|
1032
|
+
continue;
|
|
1033
|
+
const attrOffset = tagStartIndex + baseAttrOffset + bareMatch.index;
|
|
1034
|
+
const start = document.positionAt(attrOffset);
|
|
1035
|
+
const end = document.positionAt(attrOffset + attrName.length);
|
|
1036
|
+
diagnostics.push({
|
|
1037
|
+
range: { start, end },
|
|
1038
|
+
message: `Component <${tagName}> declares prop "${declaredProp.name}". Pass it with the data-bascik-prop-${declaredProp.name} prefix instead of "${attrName}".`,
|
|
1039
|
+
severity: DiagnosticSeverity.Warning,
|
|
1040
|
+
source: "bascik",
|
|
1041
|
+
code: "missing-prop-prefix",
|
|
856
1042
|
});
|
|
857
1043
|
}
|
|
858
1044
|
}
|
|
@@ -867,8 +1053,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
867
1053
|
range: { start, end },
|
|
868
1054
|
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.`,
|
|
869
1055
|
severity: DiagnosticSeverity.Warning,
|
|
870
|
-
source:
|
|
871
|
-
code:
|
|
1056
|
+
source: "bascik",
|
|
1057
|
+
code: "unclosed-component-tag",
|
|
872
1058
|
});
|
|
873
1059
|
}
|
|
874
1060
|
else if (compMeta) {
|
|
@@ -880,8 +1066,8 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
880
1066
|
range: { start, end },
|
|
881
1067
|
message: `Component <${tagName}> defines no slots and accepts no children. Use self-closing tag <${tagName} /> instead of paired <${tagName}></${tagName}>.`,
|
|
882
1068
|
severity: DiagnosticSeverity.Warning,
|
|
883
|
-
source:
|
|
884
|
-
code:
|
|
1069
|
+
source: "bascik",
|
|
1070
|
+
code: "prefer-self-closing-component",
|
|
885
1071
|
});
|
|
886
1072
|
}
|
|
887
1073
|
}
|
|
@@ -897,25 +1083,26 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
897
1083
|
const openTag = match[1];
|
|
898
1084
|
const styleBodyOffset = (match.index ?? 0) + openTag.length;
|
|
899
1085
|
// maskedText blanks style bodies (same length), so read the real body from text.
|
|
900
|
-
const styleBody = text.slice(styleBodyOffset, styleBodyOffset + (match[2] ??
|
|
1086
|
+
const styleBody = text.slice(styleBodyOffset, styleBodyOffset + (match[2] ?? "").length);
|
|
901
1087
|
if (hasCompanionCss) {
|
|
902
1088
|
const start = document.positionAt(match.index ?? 0);
|
|
903
1089
|
const end = document.positionAt((match.index ?? 0) + openTag.length);
|
|
904
1090
|
diagnostics.push({
|
|
905
1091
|
range: { start, end },
|
|
906
|
-
|
|
1092
|
+
code: "inline-and-companion-styles",
|
|
1093
|
+
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.",
|
|
907
1094
|
severity: DiagnosticSeverity.Warning,
|
|
908
|
-
source:
|
|
1095
|
+
source: "bascik",
|
|
909
1096
|
});
|
|
910
1097
|
}
|
|
911
|
-
addCompatibilityDiagnostics(styleBody,
|
|
1098
|
+
addCompatibilityDiagnostics(styleBody, "css", styleBodyOffset);
|
|
912
1099
|
}
|
|
913
1100
|
}
|
|
914
|
-
else if (languageId ===
|
|
915
|
-
addCompatibilityDiagnostics(text,
|
|
1101
|
+
else if (languageId === "css") {
|
|
1102
|
+
addCompatibilityDiagnostics(text, "css", 0);
|
|
916
1103
|
}
|
|
917
1104
|
else {
|
|
918
|
-
addCompatibilityDiagnostics(text,
|
|
1105
|
+
addCompatibilityDiagnostics(text, "js", 0);
|
|
919
1106
|
}
|
|
920
1107
|
const ignoreRanges = parseIgnoreDirectives(text, languageId);
|
|
921
1108
|
const extConfig = snapshot ? loadExtensionConfig(snapshot.projectRoot) : {};
|
|
@@ -925,15 +1112,15 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
925
1112
|
}
|
|
926
1113
|
return diagnostics.filter((diag) => {
|
|
927
1114
|
const line = diag.range.start.line;
|
|
928
|
-
const code = typeof diag.code ===
|
|
1115
|
+
const code = typeof diag.code === "string" ? diag.code : undefined;
|
|
929
1116
|
if (code && disabledRules.has(code.toLowerCase())) {
|
|
930
1117
|
return false;
|
|
931
1118
|
}
|
|
932
|
-
if (code ===
|
|
1119
|
+
if (code === "prefer-self-closing-component" &&
|
|
933
1120
|
extConfig.diagnostics?.selfClosingComponents === false) {
|
|
934
1121
|
return false;
|
|
935
1122
|
}
|
|
936
|
-
if (code ===
|
|
1123
|
+
if (code === "unclosed-component-tag" &&
|
|
937
1124
|
extConfig.diagnostics?.unclosedComponents === false) {
|
|
938
1125
|
return false;
|
|
939
1126
|
}
|
|
@@ -941,10 +1128,10 @@ export function createDiagnostics(document, fsPath, snapshot, openDocumentTexts
|
|
|
941
1128
|
});
|
|
942
1129
|
}
|
|
943
1130
|
export function createCompletions(document, position, snapshot) {
|
|
944
|
-
if (document.languageId !==
|
|
1131
|
+
if (document.languageId !== "html")
|
|
945
1132
|
return undefined;
|
|
946
1133
|
const offset = document.offsetAt(position);
|
|
947
|
-
const maskedSource = maskHtmlRawTextContents(document.getText()).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) =>
|
|
1134
|
+
const maskedSource = maskHtmlRawTextContents(document.getText()).replace(/<!--[\s\S]*?(?:-->|$)/g, (comment) => " ".repeat(comment.length));
|
|
948
1135
|
const sourceBeforeCursor = maskedSource.slice(0, offset);
|
|
949
1136
|
const tagMatch = /<([A-Za-z][\w-]*)?$/.exec(sourceBeforeCursor);
|
|
950
1137
|
if (!tagMatch) {
|
|
@@ -954,8 +1141,8 @@ export function createCompletions(document, position, snapshot) {
|
|
|
954
1141
|
const currentTagName = openTagMatch[1].toLowerCase();
|
|
955
1142
|
const openTagText = openTagMatch[0];
|
|
956
1143
|
const currentTagStart = offset - openTagText.length;
|
|
957
|
-
if (currentTagName ===
|
|
958
|
-
const hasAnyDirective = SCRIPT_DIRECTIVES.some((dir) => new RegExp(`\\b${dir.name}\\b`,
|
|
1144
|
+
if (currentTagName === "script") {
|
|
1145
|
+
const hasAnyDirective = SCRIPT_DIRECTIVES.some((dir) => new RegExp(`\\b${dir.name}\\b`, "i").test(openTagText));
|
|
959
1146
|
if (hasAnyDirective)
|
|
960
1147
|
return [];
|
|
961
1148
|
const range = completionReplacementRange(document, position);
|
|
@@ -978,7 +1165,7 @@ export function createCompletions(document, position, snapshot) {
|
|
|
978
1165
|
if (currentComponentMetadata) {
|
|
979
1166
|
const range = completionReplacementRange(document, position);
|
|
980
1167
|
return currentComponentMetadata.props
|
|
981
|
-
.filter(({ name }) => !new RegExp(`\\bdata-bascik-prop-${escapeRegExp(name)}\\s*=`,
|
|
1168
|
+
.filter(({ name }) => !new RegExp(`\\bdata-bascik-prop-${escapeRegExp(name)}\\s*=`, "i").test(openTagText))
|
|
982
1169
|
.map((prop) => {
|
|
983
1170
|
const attribute = `data-bascik-prop-${prop.name}`;
|
|
984
1171
|
const item = {
|
|
@@ -990,12 +1177,12 @@ export function createCompletions(document, position, snapshot) {
|
|
|
990
1177
|
},
|
|
991
1178
|
insertTextFormat: 2, // Snippet
|
|
992
1179
|
sortText: `0_${attribute}`,
|
|
993
|
-
detail:
|
|
1180
|
+
detail: "Bascik component prop",
|
|
994
1181
|
};
|
|
995
1182
|
if (prop.description) {
|
|
996
1183
|
item.documentation = {
|
|
997
1184
|
kind: MarkupKind.Markdown,
|
|
998
|
-
value: prop.description,
|
|
1185
|
+
value: escapeMarkdownText(prop.description),
|
|
999
1186
|
};
|
|
1000
1187
|
}
|
|
1001
1188
|
return item;
|
|
@@ -1020,12 +1207,12 @@ export function createCompletions(document, position, snapshot) {
|
|
|
1020
1207
|
},
|
|
1021
1208
|
filterText: `data-bascik-slot ${slot.name}`,
|
|
1022
1209
|
sortText: `0_data-bascik-slot_${slot.name}`,
|
|
1023
|
-
detail:
|
|
1210
|
+
detail: "Bascik named slot",
|
|
1024
1211
|
};
|
|
1025
1212
|
if (slot.description) {
|
|
1026
1213
|
item.documentation = {
|
|
1027
1214
|
kind: MarkupKind.Markdown,
|
|
1028
|
-
value: slot.description,
|
|
1215
|
+
value: escapeMarkdownText(slot.description),
|
|
1029
1216
|
};
|
|
1030
1217
|
}
|
|
1031
1218
|
return item;
|
|
@@ -1033,52 +1220,54 @@ export function createCompletions(document, position, snapshot) {
|
|
|
1033
1220
|
}
|
|
1034
1221
|
const tagStart = offset - tagMatch[0].length;
|
|
1035
1222
|
const sourceBeforeTag = sourceBeforeCursor.slice(0, tagStart);
|
|
1036
|
-
const previousTagStart = sourceBeforeTag.lastIndexOf(
|
|
1037
|
-
const previousTagEnd = sourceBeforeTag.lastIndexOf(
|
|
1223
|
+
const previousTagStart = sourceBeforeTag.lastIndexOf("<");
|
|
1224
|
+
const previousTagEnd = sourceBeforeTag.lastIndexOf(">");
|
|
1038
1225
|
if (previousTagStart > previousTagEnd)
|
|
1039
1226
|
return undefined;
|
|
1040
|
-
const prefix = (tagMatch[1] ??
|
|
1227
|
+
const prefix = (tagMatch[1] ?? "").toLowerCase();
|
|
1041
1228
|
const start = document.positionAt(offset - prefix.length);
|
|
1042
1229
|
const replacementRange = { start, end: position };
|
|
1043
1230
|
const scriptShorthands = [
|
|
1044
1231
|
{
|
|
1045
|
-
label:
|
|
1046
|
-
detail:
|
|
1047
|
-
documentation:
|
|
1232
|
+
label: "bascik-build",
|
|
1233
|
+
detail: "Bascik build-time script block",
|
|
1234
|
+
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.",
|
|
1048
1235
|
insertText: `<script data-bascik-build>\n export default async function () {\n return \`$0\`;\n }\n</script>`,
|
|
1049
1236
|
},
|
|
1050
1237
|
{
|
|
1051
|
-
label:
|
|
1052
|
-
detail:
|
|
1053
|
-
documentation:
|
|
1238
|
+
label: "bascik-routes",
|
|
1239
|
+
detail: "Bascik dynamic routes script block",
|
|
1240
|
+
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.",
|
|
1054
1241
|
insertText: `<script data-bascik-routes>\n export default async function () {\n return [\n $0\n ];\n }\n</script>`,
|
|
1055
1242
|
},
|
|
1056
1243
|
{
|
|
1057
|
-
label:
|
|
1058
|
-
detail:
|
|
1059
|
-
documentation:
|
|
1244
|
+
label: "bascik-server",
|
|
1245
|
+
detail: "Bascik server-side request script block",
|
|
1246
|
+
documentation: "Executes in Node.js per request on production and dev servers. The default exported handler function replaces this script tag.",
|
|
1060
1247
|
insertText: `<script data-bascik-server>\n export default async function (request, context, { signal }) {\n return \`$0\`;\n }\n</script>`,
|
|
1061
1248
|
},
|
|
1062
1249
|
{
|
|
1063
|
-
label:
|
|
1064
|
-
detail:
|
|
1065
|
-
documentation:
|
|
1250
|
+
label: "bascik-stream",
|
|
1251
|
+
detail: "Bascik server-side streaming script block",
|
|
1252
|
+
documentation: "Executes in Node.js per request on production servers. Streams chunked HTML responses to the client as data becomes available.",
|
|
1066
1253
|
insertText: `<script data-bascik-stream>\n export default async function (request, context, { signal }) {\n return \`$0\`;\n }\n</script>`,
|
|
1067
1254
|
},
|
|
1068
1255
|
];
|
|
1256
|
+
// The replacement starts at the "<" already typed, so the filter text must start there too, or the editor
|
|
1257
|
+
// compares what was typed ("<my-b") against a label without it and hides every item.
|
|
1258
|
+
const tagRange = {
|
|
1259
|
+
start: document.positionAt(tagStart),
|
|
1260
|
+
end: replacementRange.end,
|
|
1261
|
+
};
|
|
1069
1262
|
const shorthandItems = scriptShorthands
|
|
1070
1263
|
.filter((shorthand) => shorthand.label.startsWith(prefix))
|
|
1071
1264
|
.map((shorthand) => ({
|
|
1072
1265
|
label: shorthand.label,
|
|
1073
1266
|
kind: CompletionItemKind.Snippet,
|
|
1074
1267
|
sortText: `0_${shorthand.label}`,
|
|
1075
|
-
range:
|
|
1076
|
-
start: document.positionAt(tagStart),
|
|
1077
|
-
end: replacementRange.end,
|
|
1078
|
-
},
|
|
1079
|
-
insertText: shorthand.insertText,
|
|
1268
|
+
textEdit: { range: tagRange, newText: shorthand.insertText },
|
|
1080
1269
|
insertTextFormat: 2, // Snippet
|
|
1081
|
-
filterText: shorthand.label
|
|
1270
|
+
filterText: `<${shorthand.label}`,
|
|
1082
1271
|
detail: shorthand.detail,
|
|
1083
1272
|
documentation: {
|
|
1084
1273
|
kind: MarkupKind.Markdown,
|
|
@@ -1092,21 +1281,20 @@ export function createCompletions(document, position, snapshot) {
|
|
|
1092
1281
|
const metadata = snapshot.componentMetadata.get(componentName);
|
|
1093
1282
|
const relativePath = path
|
|
1094
1283
|
.relative(snapshot.projectRoot, componentPath)
|
|
1095
|
-
.replace(/\\/g,
|
|
1284
|
+
.replace(/\\/g, "/");
|
|
1096
1285
|
return {
|
|
1097
1286
|
label: componentName,
|
|
1098
1287
|
kind: CompletionItemKind.Class,
|
|
1099
1288
|
sortText: `0_${componentName}`,
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1289
|
+
textEdit: {
|
|
1290
|
+
range: tagRange,
|
|
1291
|
+
newText: metadata?.defaultSlot
|
|
1292
|
+
? `<${componentName}>$0</${componentName}>`
|
|
1293
|
+
: `<${componentName} />$0`,
|
|
1103
1294
|
},
|
|
1104
|
-
insertText: metadata?.defaultSlot
|
|
1105
|
-
? `<${componentName}>$0</${componentName}>`
|
|
1106
|
-
: `<${componentName} />$0`,
|
|
1107
1295
|
insertTextFormat: 2, // Snippet
|
|
1108
|
-
filterText: componentName
|
|
1109
|
-
detail:
|
|
1296
|
+
filterText: `<${componentName}`,
|
|
1297
|
+
detail: "Bascik component",
|
|
1110
1298
|
documentation: {
|
|
1111
1299
|
kind: MarkupKind.Markdown,
|
|
1112
1300
|
value: createComponentDocumentationMarkdown(relativePath, metadata),
|
|
@@ -1149,34 +1337,34 @@ export function createHover(document, position, snapshot) {
|
|
|
1149
1337
|
return undefined;
|
|
1150
1338
|
const relativePath = path
|
|
1151
1339
|
.relative(snapshot.projectRoot, componentPath)
|
|
1152
|
-
.replace(/\\/g,
|
|
1340
|
+
.replace(/\\/g, "/");
|
|
1153
1341
|
const details = [`### \`<${componentName}>\`\n\n`];
|
|
1154
1342
|
if (metadata.description) {
|
|
1155
|
-
details.push(metadata.description,
|
|
1343
|
+
details.push(escapeMarkdownText(metadata.description), "\n\n");
|
|
1156
1344
|
}
|
|
1157
|
-
details.push(`**Source:** [\`${relativePath}\`](
|
|
1158
|
-
appendMetadataMembersMarkdown(details,
|
|
1159
|
-
appendMetadataMembersMarkdown(details,
|
|
1345
|
+
details.push(`**Source:** [\`${relativePath}\`](${pathToFileURL(componentPath).toString()})\n\n`);
|
|
1346
|
+
appendMetadataMembersMarkdown(details, "Props", metadata.props);
|
|
1347
|
+
appendMetadataMembersMarkdown(details, "Slots", [
|
|
1160
1348
|
...metadata.slots,
|
|
1161
1349
|
...(metadata.defaultSlot ? [metadata.defaultSlot] : []),
|
|
1162
1350
|
]);
|
|
1163
1351
|
const features = [
|
|
1164
|
-
metadata.hasStyles ?
|
|
1165
|
-
metadata.hasScripts ?
|
|
1352
|
+
metadata.hasStyles ? "styles" : "",
|
|
1353
|
+
metadata.hasScripts ? "scripts" : "",
|
|
1166
1354
|
].filter(Boolean);
|
|
1167
1355
|
if (features.length > 0) {
|
|
1168
|
-
details.push(`**Includes:** ${features.join(
|
|
1356
|
+
details.push(`**Includes:** ${features.join(", ")}`);
|
|
1169
1357
|
}
|
|
1170
1358
|
return {
|
|
1171
1359
|
contents: {
|
|
1172
1360
|
kind: MarkupKind.Markdown,
|
|
1173
|
-
value: details.join(
|
|
1361
|
+
value: details.join(""),
|
|
1174
1362
|
},
|
|
1175
1363
|
range: wordRange,
|
|
1176
1364
|
};
|
|
1177
1365
|
}
|
|
1178
1366
|
export function createDefinition(document, position, snapshot, fsPath) {
|
|
1179
|
-
if (document.languageId !==
|
|
1367
|
+
if (document.languageId !== "html")
|
|
1180
1368
|
return undefined;
|
|
1181
1369
|
// 1. Check for script import definition
|
|
1182
1370
|
const text = document.getText();
|
|
@@ -1185,16 +1373,17 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1185
1373
|
let scriptMatch;
|
|
1186
1374
|
while ((scriptMatch = SCRIPT_BLOCK_RE.exec(text)) !== null) {
|
|
1187
1375
|
const openTag = scriptMatch[1];
|
|
1188
|
-
const scriptBody = scriptMatch[2] ??
|
|
1376
|
+
const scriptBody = scriptMatch[2] ?? "";
|
|
1189
1377
|
const blockStart = scriptMatch.index ?? 0;
|
|
1190
1378
|
const openTagEnd = blockStart + openTag.length;
|
|
1191
1379
|
const blockEnd = blockStart + scriptMatch[0].length;
|
|
1192
1380
|
if (offset < blockStart || offset > blockEnd)
|
|
1193
1381
|
continue;
|
|
1194
1382
|
const attrs = parseScriptOpenTagAttributes(openTag);
|
|
1195
|
-
if (!attrs.has(
|
|
1196
|
-
!attrs.has(
|
|
1197
|
-
!attrs.has(
|
|
1383
|
+
if (!attrs.has("data-bascik-build") &&
|
|
1384
|
+
!attrs.has("data-bascik-build-once") &&
|
|
1385
|
+
!attrs.has("data-bascik-server") &&
|
|
1386
|
+
!attrs.has("data-bascik-routes")) {
|
|
1198
1387
|
return undefined;
|
|
1199
1388
|
}
|
|
1200
1389
|
const baseDir = path.dirname(fsPath);
|
|
@@ -1203,14 +1392,14 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1203
1392
|
const srcMatch = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(openTag);
|
|
1204
1393
|
if (!srcMatch)
|
|
1205
1394
|
return undefined;
|
|
1206
|
-
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ??
|
|
1395
|
+
const srcValue = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? "";
|
|
1207
1396
|
if (!srcValue)
|
|
1208
1397
|
return undefined;
|
|
1209
1398
|
const valueStart = blockStart + (srcMatch.index ?? 0) + srcMatch[0].indexOf(srcValue);
|
|
1210
1399
|
const valueEnd = valueStart + srcValue.length;
|
|
1211
1400
|
if (offset < valueStart || offset > valueEnd)
|
|
1212
1401
|
return undefined;
|
|
1213
|
-
const resolved = resolveScriptTarget(srcValue, baseDir, importRootAbs,
|
|
1402
|
+
const resolved = resolveScriptTarget(srcValue, baseDir, importRootAbs, "src");
|
|
1214
1403
|
if (!resolved || !fs.existsSync(resolved))
|
|
1215
1404
|
return undefined;
|
|
1216
1405
|
return [
|
|
@@ -1235,7 +1424,7 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1235
1424
|
for (const { start, end, value: specifier } of findModuleSpecifiers(scriptBody)) {
|
|
1236
1425
|
if (bodyOffset < start || bodyOffset > end)
|
|
1237
1426
|
continue;
|
|
1238
|
-
const resolved = resolveScriptTarget(specifier, baseDir, importRootAbs,
|
|
1427
|
+
const resolved = resolveScriptTarget(specifier, baseDir, importRootAbs, "specifier");
|
|
1239
1428
|
if (!resolved || !fs.existsSync(resolved))
|
|
1240
1429
|
return undefined;
|
|
1241
1430
|
return [
|
|
@@ -1295,4 +1484,104 @@ export function createDefinition(document, position, snapshot, fsPath) {
|
|
|
1295
1484
|
}
|
|
1296
1485
|
return undefined;
|
|
1297
1486
|
}
|
|
1487
|
+
function rangesOverlap(a, b) {
|
|
1488
|
+
if (a.end.line < b.start.line || b.end.line < a.start.line)
|
|
1489
|
+
return false;
|
|
1490
|
+
if (a.end.line === b.start.line && a.end.character < b.start.character)
|
|
1491
|
+
return false;
|
|
1492
|
+
if (b.end.line === a.start.line && b.end.character < a.start.character)
|
|
1493
|
+
return false;
|
|
1494
|
+
return true;
|
|
1495
|
+
}
|
|
1496
|
+
export function buildAutoFixEdit(document, diag) {
|
|
1497
|
+
const code = typeof diag.code === "string" ? diag.code : undefined;
|
|
1498
|
+
if (code === "missing-prop-prefix") {
|
|
1499
|
+
const rawAttr = document.getText(diag.range);
|
|
1500
|
+
if (!rawAttr || rawAttr.toLowerCase().startsWith("data-bascik-prop-")) {
|
|
1501
|
+
return undefined;
|
|
1502
|
+
}
|
|
1503
|
+
const newText = `data-bascik-prop-${rawAttr}`;
|
|
1504
|
+
return {
|
|
1505
|
+
title: `Prefix prop with data-bascik-prop- (${newText})`,
|
|
1506
|
+
edit: {
|
|
1507
|
+
range: diag.range,
|
|
1508
|
+
newText,
|
|
1509
|
+
},
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
if (code === "prefer-self-closing-component") {
|
|
1513
|
+
const fullText = document.getText(diag.range);
|
|
1514
|
+
const match = /^<([A-Za-z][\w-]*)(\s+(?:[^>"']|"[^"]*"|'[^']*')*?)?>\s*<\/\1\s*>$/is.exec(fullText);
|
|
1515
|
+
if (!match)
|
|
1516
|
+
return undefined;
|
|
1517
|
+
const tagName = match[1];
|
|
1518
|
+
const attrs = (match[2] ?? "").trimEnd();
|
|
1519
|
+
const newText = `<${tagName}${attrs} />`;
|
|
1520
|
+
return {
|
|
1521
|
+
title: `Convert <${tagName}> to self-closing tag`,
|
|
1522
|
+
edit: {
|
|
1523
|
+
range: diag.range,
|
|
1524
|
+
newText,
|
|
1525
|
+
},
|
|
1526
|
+
};
|
|
1527
|
+
}
|
|
1528
|
+
return undefined;
|
|
1529
|
+
}
|
|
1530
|
+
export function createCodeActions(document, range, diagnostics, snapshot, fsPath) {
|
|
1531
|
+
const activeDiagnostics = diagnostics.length > 0
|
|
1532
|
+
? diagnostics
|
|
1533
|
+
: fsPath
|
|
1534
|
+
? createDiagnostics(document, fsPath, snapshot)
|
|
1535
|
+
: [];
|
|
1536
|
+
const actions = [];
|
|
1537
|
+
for (const diag of activeDiagnostics) {
|
|
1538
|
+
if (!rangesOverlap(diag.range, range))
|
|
1539
|
+
continue;
|
|
1540
|
+
const fix = buildAutoFixEdit(document, diag);
|
|
1541
|
+
if (!fix)
|
|
1542
|
+
continue;
|
|
1543
|
+
actions.push({
|
|
1544
|
+
title: fix.title,
|
|
1545
|
+
kind: CodeActionKind.QuickFix,
|
|
1546
|
+
diagnostics: [diag],
|
|
1547
|
+
isPreferred: true,
|
|
1548
|
+
edit: {
|
|
1549
|
+
changes: {
|
|
1550
|
+
[document.uri]: [fix.edit],
|
|
1551
|
+
},
|
|
1552
|
+
},
|
|
1553
|
+
});
|
|
1554
|
+
}
|
|
1555
|
+
return actions;
|
|
1556
|
+
}
|
|
1557
|
+
export function applyAutoFixes(document, snapshot, fsPath = "/virtual.html") {
|
|
1558
|
+
const diags = createDiagnostics(document, fsPath, snapshot);
|
|
1559
|
+
const edits = [];
|
|
1560
|
+
for (const diag of diags) {
|
|
1561
|
+
const fix = buildAutoFixEdit(document, diag);
|
|
1562
|
+
if (!fix)
|
|
1563
|
+
continue;
|
|
1564
|
+
const startOffset = document.offsetAt(fix.edit.range.start);
|
|
1565
|
+
const endOffset = document.offsetAt(fix.edit.range.end);
|
|
1566
|
+
edits.push({ startOffset, endOffset, newText: fix.edit.newText });
|
|
1567
|
+
}
|
|
1568
|
+
if (edits.length === 0) {
|
|
1569
|
+
return { text: document.getText(), fixedCount: 0 };
|
|
1570
|
+
}
|
|
1571
|
+
edits.sort((a, b) => b.startOffset - a.startOffset);
|
|
1572
|
+
let text = document.getText();
|
|
1573
|
+
let fixedCount = 0;
|
|
1574
|
+
let lastStart = Number.POSITIVE_INFINITY;
|
|
1575
|
+
for (const edit of edits) {
|
|
1576
|
+
if (edit.endOffset > lastStart)
|
|
1577
|
+
continue;
|
|
1578
|
+
text =
|
|
1579
|
+
text.slice(0, edit.startOffset) +
|
|
1580
|
+
edit.newText +
|
|
1581
|
+
text.slice(edit.endOffset);
|
|
1582
|
+
lastStart = edit.startOffset;
|
|
1583
|
+
fixedCount++;
|
|
1584
|
+
}
|
|
1585
|
+
return { text, fixedCount };
|
|
1586
|
+
}
|
|
1298
1587
|
//# sourceMappingURL=analyzer.js.map
|