@ogygia/compiler 0.0.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -1
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/app-spec.d.ts +18 -0
- package/dist/app-spec.js +22 -0
- package/dist/blank-styles.d.ts +1 -0
- package/dist/blank-styles.js +24 -0
- package/dist/bom.d.ts +2 -0
- package/dist/bom.js +7 -0
- package/dist/build-cache.d.ts +13 -0
- package/dist/build-cache.js +65 -0
- package/dist/ctx.d.ts +238 -0
- package/dist/ctx.js +221 -0
- package/dist/dev/css-scope.d.ts +16 -0
- package/dist/dev/css-scope.js +32 -0
- package/dist/dev/dev-hmr.d.ts +24 -0
- package/dist/dev/dev-hmr.js +51 -0
- package/dist/dev/hmr.d.ts +34 -0
- package/dist/dev/hmr.js +52 -0
- package/dist/driver.d.ts +351 -0
- package/dist/driver.js +1402 -0
- package/dist/exports/index.d.ts +77 -0
- package/dist/exports/index.js +349 -0
- package/dist/exports/parse.d.ts +65 -0
- package/dist/exports/parse.js +251 -0
- package/dist/fouc-css.d.ts +112 -0
- package/dist/fouc-css.js +314 -0
- package/dist/free-vars.d.ts +32 -0
- package/dist/free-vars.js +454 -0
- package/dist/fs.d.ts +31 -0
- package/dist/fs.js +82 -0
- package/dist/ids.d.ts +49 -0
- package/dist/ids.js +52 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +23 -0
- package/dist/kit.d.ts +204 -0
- package/dist/kit.js +554 -0
- package/dist/leg.d.ts +34 -0
- package/dist/leg.js +26 -0
- package/dist/link/analyses.d.ts +26 -0
- package/dist/link/analyses.js +237 -0
- package/dist/link/build-output.d.ts +47 -0
- package/dist/link/build-output.js +168 -0
- package/dist/link/caps.d.ts +32 -0
- package/dist/link/caps.js +39 -0
- package/dist/link/context-detect.d.ts +28 -0
- package/dist/link/context-detect.js +143 -0
- package/dist/link/emit-gate.d.ts +43 -0
- package/dist/link/emit-gate.js +55 -0
- package/dist/link/file-token.d.ts +11 -0
- package/dist/link/file-token.js +15 -0
- package/dist/link/island-deps.d.ts +183 -0
- package/dist/link/island-deps.js +802 -0
- package/dist/link/location-map.d.ts +24 -0
- package/dist/link/location-map.js +51 -0
- package/dist/link/own-reach.d.ts +15 -0
- package/dist/link/own-reach.js +188 -0
- package/dist/link/registry-stub.d.ts +50 -0
- package/dist/link/registry-stub.js +163 -0
- package/dist/link/relocations.d.ts +79 -0
- package/dist/link/relocations.js +204 -0
- package/dist/link/router-css.d.ts +38 -0
- package/dist/link/router-css.js +124 -0
- package/dist/link/runtime-entry.d.ts +103 -0
- package/dist/link/runtime-entry.js +183 -0
- package/dist/link/server-manifest.d.ts +25 -0
- package/dist/link/server-manifest.js +102 -0
- package/dist/link/transport.d.ts +42 -0
- package/dist/link/transport.js +91 -0
- package/dist/macros/dollar.d.ts +24 -0
- package/dist/macros/dollar.js +397 -0
- package/dist/macros/pipeline.d.ts +50 -0
- package/dist/macros/pipeline.js +133 -0
- package/dist/macros/source.d.ts +9 -0
- package/dist/macros/source.js +115 -0
- package/dist/macros/store.d.ts +22 -0
- package/dist/macros/store.js +217 -0
- package/dist/macros/wire.d.ts +17 -0
- package/dist/macros/wire.js +116 -0
- package/dist/map-chain.d.ts +18 -0
- package/dist/map-chain.js +42 -0
- package/dist/messages.d.ts +673 -0
- package/dist/messages.js +938 -0
- package/dist/ownership-stamps.d.ts +11 -0
- package/dist/ownership-stamps.js +112 -0
- package/dist/parse/oxc.d.ts +36 -0
- package/dist/parse/oxc.js +35 -0
- package/dist/parse/roots.d.ts +4 -0
- package/dist/parse/roots.js +27 -0
- package/dist/parse/scan.d.ts +65 -0
- package/dist/parse/scan.js +297 -0
- package/dist/parse/svelte-ast.d.ts +7 -0
- package/dist/parse/svelte-ast.js +99 -0
- package/dist/program.d.ts +140 -0
- package/dist/program.js +304 -0
- package/dist/queries.d.ts +105 -0
- package/dist/queries.js +326 -0
- package/dist/region/emit.d.ts +168 -0
- package/dist/region/emit.js +292 -0
- package/dist/region/identity.d.ts +32 -0
- package/dist/region/identity.js +46 -0
- package/dist/region/ir.d.ts +56 -0
- package/dist/region/ir.js +0 -0
- package/dist/region/marked-imports.d.ts +27 -0
- package/dist/region/marked-imports.js +265 -0
- package/dist/region/transform.d.ts +189 -0
- package/dist/region/transform.js +1565 -0
- package/dist/scan-facts.d.ts +36 -0
- package/dist/scan-facts.js +54 -0
- package/dist/script-request.d.ts +1 -0
- package/dist/script-request.js +33 -0
- package/dist/splice.d.ts +11 -0
- package/dist/splice.js +33 -0
- package/dist/transportables.d.ts +23 -0
- package/dist/transportables.js +144 -0
- package/dist/virtual/core.d.ts +158 -0
- package/dist/virtual/core.js +588 -0
- package/dist/virtual/resolver.d.ts +62 -0
- package/dist/virtual/resolver.js +265 -0
- package/dist/virtual/specifier.d.ts +10 -0
- package/dist/virtual/specifier.js +6 -0
- package/dist/virtual/table.d.ts +120 -0
- package/dist/virtual/table.js +54 -0
- package/dist/virtual/types.d.ts +17 -0
- package/dist/virtual/types.js +100 -0
- package/dist/virtual/value-names.d.ts +2 -0
- package/dist/virtual/value-names.js +18 -0
- package/dist/vite/definers.d.ts +16 -0
- package/dist/vite/definers.js +69 -0
- package/dist/vite/dev-maps.d.ts +34 -0
- package/dist/vite/dev-maps.js +91 -0
- package/dist/vite/index.d.ts +21 -0
- package/dist/vite/index.js +890 -0
- package/dist/vite/kit-dirs.d.ts +6 -0
- package/dist/vite/kit-dirs.js +53 -0
- package/dist/vite/late-island.d.ts +8 -0
- package/dist/vite/late-island.js +18 -0
- package/dist/vite/lazy-files.d.ts +2 -0
- package/dist/vite/lazy-files.js +72 -0
- package/dist/vite/options.d.ts +195 -0
- package/dist/vite/options.js +72 -0
- package/dist/vite/package-files.d.ts +3 -0
- package/dist/vite/package-files.js +88 -0
- package/dist/vite/paths.d.ts +16 -0
- package/dist/vite/paths.js +39 -0
- package/dist/vite/plugin-api.d.ts +316 -0
- package/dist/vite/plugin-api.js +328 -0
- package/dist/vite/route-watch.d.ts +15 -0
- package/dist/vite/route-watch.js +32 -0
- package/dist/vite/session.d.ts +28 -0
- package/dist/vite/session.js +43 -0
- package/dist/vite/sourcemaps.d.ts +19 -0
- package/dist/vite/sourcemaps.js +75 -0
- package/dist/vite/style-preprocess.d.ts +2 -0
- package/dist/vite/style-preprocess.js +84 -0
- package/dist/vite/svelte-entries.d.ts +4 -0
- package/dist/vite/svelte-entries.js +10 -0
- package/dist/vite/svelte-options.d.ts +20 -0
- package/dist/vite/svelte-options.js +22 -0
- package/package.json +87 -12
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Edit } from "./splice.js";
|
|
2
|
+
/**
|
|
3
|
+
* `source` with every opaque element stamped, and every element holding an `{@html}` beside other
|
|
4
|
+
* children stamped `data-og-html` with each `{@html}` range marked; or `null` when there is nothing to
|
|
5
|
+
* stamp (or the file does not parse — the real compile reports that). Outermost wins: nothing is
|
|
6
|
+
* stamped inside a stamped element (the repair never enters it anyway).
|
|
7
|
+
*/
|
|
8
|
+
export declare function stamp_opaque(raw: string, filename: string): string | null;
|
|
9
|
+
/** The inserts `stamp_opaque` makes to `source` (no byte-order mark): none when there is nothing to
|
|
10
|
+
* stamp. Server leg only: the client never sees a stamp or a mark. */
|
|
11
|
+
export declare function opaque_stamp_edits(source: string, filename: string): Edit[];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { splice_text } from "./splice.js";
|
|
2
|
+
import { blank_styles } from "./blank-styles.js";
|
|
3
|
+
import { strip_bom } from "./bom.js";
|
|
4
|
+
import { HTML_RANGE_MARK, OG_HTML, OG_OPAQUE } from "@ogygia/core/contract";
|
|
5
|
+
import { parse } from "svelte/compiler";
|
|
6
|
+
// src/ownership-stamps.ts
|
|
7
|
+
const MAY_STAMP_RE = /\{@html|bind:(?:innerHTML|textContent|innerText)|<[a-z][\w.]*-[\w-]*[\s/>]|\sis=/;
|
|
8
|
+
const WHOLESALE_BINDS = /* @__PURE__ */ new Set([
|
|
9
|
+
"innerHTML",
|
|
10
|
+
"textContent",
|
|
11
|
+
"innerText"
|
|
12
|
+
]);
|
|
13
|
+
const is_blank = (n) => n.type === "Text" && !String(n.data ?? "").trim();
|
|
14
|
+
function literal_attributes(el) {
|
|
15
|
+
for (const a of el.attributes ?? []) {
|
|
16
|
+
if (a.type !== "Attribute") return false;
|
|
17
|
+
const v = a.value;
|
|
18
|
+
if (v === true) continue;
|
|
19
|
+
if ((Array.isArray(v) ? v : [v]).some((p) => p.type !== "Text")) return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
function all_static(nodes) {
|
|
24
|
+
for (const n of nodes) {
|
|
25
|
+
if (n.type === "Text" || n.type === "Comment") continue;
|
|
26
|
+
if (n.type !== "RegularElement") return false;
|
|
27
|
+
if (!literal_attributes(n)) return false;
|
|
28
|
+
if (!all_static(n.fragment?.nodes ?? [])) return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
function is_custom_element(el) {
|
|
33
|
+
return String(el.name).includes("-") || (el.attributes ?? []).some((a) => a.type === "Attribute" && a.name === "is");
|
|
34
|
+
}
|
|
35
|
+
function opaque(el) {
|
|
36
|
+
if (has_attribute(el, OG_OPAQUE)) return false;
|
|
37
|
+
const kids = (el.fragment?.nodes ?? []).filter((n) => !is_blank(n));
|
|
38
|
+
if (kids.length === 1 && kids[0].type === "HtmlTag") return true;
|
|
39
|
+
if ((el.attributes ?? []).some((a) => a.type === "BindDirective" && WHOLESALE_BINDS.has(a.name))) return true;
|
|
40
|
+
return is_custom_element(el) && all_static(el.fragment?.nodes ?? []);
|
|
41
|
+
}
|
|
42
|
+
function has_attribute(el, name) {
|
|
43
|
+
return (el.attributes ?? []).some((a) => a.type === "Attribute" && a.name === name);
|
|
44
|
+
}
|
|
45
|
+
const insert = (pos, text) => ({
|
|
46
|
+
start: pos,
|
|
47
|
+
end: pos,
|
|
48
|
+
text
|
|
49
|
+
});
|
|
50
|
+
const RANGE_OPEN = `((s) => s && '<!--${HTML_RANGE_MARK}-->' + s)(String((`;
|
|
51
|
+
const RANGE_CLOSE = `) ?? ''))`;
|
|
52
|
+
const CHILD_KEYS = [
|
|
53
|
+
"fragment",
|
|
54
|
+
"consequent",
|
|
55
|
+
"alternate",
|
|
56
|
+
"body",
|
|
57
|
+
"pending",
|
|
58
|
+
"then",
|
|
59
|
+
"catch",
|
|
60
|
+
"fallback"
|
|
61
|
+
];
|
|
62
|
+
/**
|
|
63
|
+
* `source` with every opaque element stamped, and every element holding an `{@html}` beside other
|
|
64
|
+
* children stamped `data-og-html` with each `{@html}` range marked; or `null` when there is nothing to
|
|
65
|
+
* stamp (or the file does not parse — the real compile reports that). Outermost wins: nothing is
|
|
66
|
+
* stamped inside a stamped element (the repair never enters it anyway).
|
|
67
|
+
*/
|
|
68
|
+
function stamp_opaque(raw, filename) {
|
|
69
|
+
const source = strip_bom(raw);
|
|
70
|
+
const edits = opaque_stamp_edits(source, filename);
|
|
71
|
+
return edits.length ? splice_text(source, edits) : null;
|
|
72
|
+
}
|
|
73
|
+
/** The inserts `stamp_opaque` makes to `source` (no byte-order mark): none when there is nothing to
|
|
74
|
+
* stamp. Server leg only: the client never sees a stamp or a mark. */
|
|
75
|
+
function opaque_stamp_edits(source, filename) {
|
|
76
|
+
if (!MAY_STAMP_RE.test(source)) return [];
|
|
77
|
+
let ast;
|
|
78
|
+
try {
|
|
79
|
+
ast = parse(blank_styles(source), {
|
|
80
|
+
modern: true,
|
|
81
|
+
filename
|
|
82
|
+
});
|
|
83
|
+
} catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
const edits = [];
|
|
87
|
+
const visit = (nodes) => {
|
|
88
|
+
for (const n of nodes ?? []) {
|
|
89
|
+
if (n.type === "RegularElement" && opaque(n)) {
|
|
90
|
+
edits.push(insert(n.start + 1 + String(n.name).length, ` ${OG_OPAQUE}`));
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (n.type === "RegularElement" && !has_attribute(n, OG_OPAQUE)) {
|
|
94
|
+
const html_tags = (n.fragment?.nodes ?? []).filter((c) => c.type === "HtmlTag");
|
|
95
|
+
if (html_tags.length) {
|
|
96
|
+
if (!has_attribute(n, OG_HTML)) edits.push(insert(n.start + 1 + String(n.name).length, ` ${OG_HTML}`));
|
|
97
|
+
for (const h of html_tags) {
|
|
98
|
+
edits.push(insert(h.expression.start, RANGE_OPEN));
|
|
99
|
+
edits.push(insert(h.expression.end, RANGE_CLOSE));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
for (const k of CHILD_KEYS) {
|
|
104
|
+
const child = n[k];
|
|
105
|
+
if (child && Array.isArray(child.nodes)) visit(child.nodes);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
visit(ast.fragment?.nodes);
|
|
110
|
+
return edits;
|
|
111
|
+
}
|
|
112
|
+
export { opaque_stamp_edits, stamp_opaque };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An AST node as the compiler's passes read it: oxc's ESTree and Svelte's template AST, walked
|
|
3
|
+
* generically and read by field name. Kept loose on purpose: the walkers index arbitrary fields
|
|
4
|
+
* (`node[key]`), and some passes accept node shapes beyond oxc's (store.ts also matches Babel's
|
|
5
|
+
* `StringLiteral` / `ObjectProperty`), which a precise ESTree union would rule out.
|
|
6
|
+
*/
|
|
7
|
+
export type AstNode = Record<string, any>;
|
|
8
|
+
type Node = AstNode;
|
|
9
|
+
export type Comment = {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
};
|
|
13
|
+
/** A parse: `ok` exactly when there is a program. */
|
|
14
|
+
export type ParseResult = {
|
|
15
|
+
program: Node;
|
|
16
|
+
ok: true;
|
|
17
|
+
comments: Comment[];
|
|
18
|
+
} | {
|
|
19
|
+
program: null;
|
|
20
|
+
ok: false;
|
|
21
|
+
comments: Comment[];
|
|
22
|
+
};
|
|
23
|
+
/** The raw oxc parse signature (`(id, code) → { program, errors, comments }`). */
|
|
24
|
+
export type RawParse = (id: string, code: string) => {
|
|
25
|
+
program?: Node;
|
|
26
|
+
errors?: unknown[];
|
|
27
|
+
comments?: Comment[];
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Parse `code` (named by `id`, whose extension picks the dialect) to an oxc/ESTree program. Never
|
|
31
|
+
* throws: a syntax error (or a half-typed file mid-edit) yields `{ program: null, ok: false }`, and
|
|
32
|
+
* callers fall back (scanner) or skip. Offsets on the returned nodes are exact against `code`.
|
|
33
|
+
* `comments` carries trivia (block/line) with byte offsets — not present in the AST body.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parse_module(code: string, id: string): ParseResult;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
// src/parse/oxc.ts
|
|
3
|
+
let node_parse_sync;
|
|
4
|
+
function raw_parse(id, code) {
|
|
5
|
+
if (!node_parse_sync) node_parse_sync = createRequire(import.meta.url)("vite").parseSync;
|
|
6
|
+
return node_parse_sync(id, code);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Parse `code` (named by `id`, whose extension picks the dialect) to an oxc/ESTree program. Never
|
|
10
|
+
* throws: a syntax error (or a half-typed file mid-edit) yields `{ program: null, ok: false }`, and
|
|
11
|
+
* callers fall back (scanner) or skip. Offsets on the returned nodes are exact against `code`.
|
|
12
|
+
* `comments` carries trivia (block/line) with byte offsets — not present in the AST body.
|
|
13
|
+
*/
|
|
14
|
+
function parse_module(code, id) {
|
|
15
|
+
try {
|
|
16
|
+
const result = raw_parse(id, code);
|
|
17
|
+
if (!result.program || result.errors && result.errors.length > 0) return {
|
|
18
|
+
program: null,
|
|
19
|
+
ok: false,
|
|
20
|
+
comments: []
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
program: result.program,
|
|
24
|
+
ok: true,
|
|
25
|
+
comments: result.comments ?? []
|
|
26
|
+
};
|
|
27
|
+
} catch {
|
|
28
|
+
return {
|
|
29
|
+
program: null,
|
|
30
|
+
ok: false,
|
|
31
|
+
comments: []
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export { parse_module };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AstNode } from "./oxc.js";
|
|
2
|
+
/** A module's code as AST roots: oxc's program for a script; for a `.svelte` file, its module
|
|
3
|
+
* script, its instance script and its template. Null when the code does not parse. */
|
|
4
|
+
export declare function parse_roots(code: string, id: string, kind: "svelte" | "script"): AstNode[] | null;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { parse_module } from "./oxc.js";
|
|
2
|
+
import { blank_styles } from "../blank-styles.js";
|
|
3
|
+
import { parse } from "svelte/compiler";
|
|
4
|
+
// src/parse/roots.ts
|
|
5
|
+
/** A module's code as AST roots: oxc's program for a script; for a `.svelte` file, its module
|
|
6
|
+
* script, its instance script and its template. Null when the code does not parse. */
|
|
7
|
+
function parse_roots(code, id, kind) {
|
|
8
|
+
if (kind === "script") {
|
|
9
|
+
const r = parse_module(code, id);
|
|
10
|
+
return r.ok && r.program ? [r.program] : null;
|
|
11
|
+
}
|
|
12
|
+
let ast;
|
|
13
|
+
try {
|
|
14
|
+
ast = parse(blank_styles(code), {
|
|
15
|
+
modern: true,
|
|
16
|
+
filename: id
|
|
17
|
+
});
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const roots = [];
|
|
22
|
+
if (ast.module?.content) roots.push(ast.module.content);
|
|
23
|
+
if (ast.instance?.content) roots.push(ast.instance.content);
|
|
24
|
+
if (ast.fragment) roots.push(ast.fragment);
|
|
25
|
+
return roots;
|
|
26
|
+
}
|
|
27
|
+
export { parse_roots };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { FoundTag } from "@ogygia/core/contract";
|
|
2
|
+
/** Index of the `)` matching the `(` at `open`, skipping strings, comments, regex literals (incl.
|
|
3
|
+
* `[…]` char classes, which may hold an unescaped `/`) and template text. -1 if unbalanced. */
|
|
4
|
+
export declare function match_close(src: string, open: number): number;
|
|
5
|
+
/** One located `import.meta.og.<method>(<args>)` call. `args` is the VERBATIM text between the
|
|
6
|
+
* parens; `method` is the dotted tail after the shared prefix (`loader.git`, `code`, `regions`). */
|
|
7
|
+
export type OgCall = {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
method: string;
|
|
11
|
+
args: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Find every `<prefix><method>(…)` call in `src`. `prefix` is the literal namespace head
|
|
15
|
+
* (`import.meta.og.`, a word's start); `method` is the dotted identifier chain that follows, up to
|
|
16
|
+
* the `(`. Only in code (a template's `${…}` included), so a marker in prose is never a hit. Pure.
|
|
17
|
+
*/
|
|
18
|
+
export declare function find_og_calls(src: string, prefix: string): OgCall[];
|
|
19
|
+
/** Split a call's argument text into the first string-literal (unquoted) and the verbatim rest.
|
|
20
|
+
* Throws (with `who` in the message) when the first argument is not a STATIC string literal — a
|
|
21
|
+
* bare identifier, or a template literal with `${…}` interpolation (not build-determinable). */
|
|
22
|
+
export declare function split_first_string(args: string, who: string): {
|
|
23
|
+
value: string;
|
|
24
|
+
rest: string;
|
|
25
|
+
};
|
|
26
|
+
/** A slice of JS/TS to analyze, plus where it starts in the original source (for offset mapping). */
|
|
27
|
+
export type JsRegion = {
|
|
28
|
+
code: string;
|
|
29
|
+
offset: number;
|
|
30
|
+
};
|
|
31
|
+
/** Does `id` (a module path, maybe with a `?query`) end in a plain JS/TS extension? */
|
|
32
|
+
export declare function is_js_module(id: string): boolean;
|
|
33
|
+
/** Does `id` end in one of the configured MARKUP extensions (`.svelte`, `.svx`, `.md`)? */
|
|
34
|
+
export declare function is_markup_module(id: string, markup_exts: readonly string[]): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Extract the JS regions of a module for construct analysis. Returns `null` when the extension is not
|
|
37
|
+
* a recognized construct host (the caller then leaves the file untouched). A JS/TS module is one
|
|
38
|
+
* region; a markup file is its `<script>` blocks. `.svelte.ts`/`.svelte.js` hit the JS branch first
|
|
39
|
+
* (they end in `.ts`/`.js`), so a `.svelte`-suffixed markup ext never shadows them.
|
|
40
|
+
*/
|
|
41
|
+
export declare function og_js_regions(src: string, id: string, markup_exts: readonly string[]): JsRegion[] | null;
|
|
42
|
+
/** A `<script>` or `<style>` block of a component's source. */
|
|
43
|
+
export interface ComponentBlock {
|
|
44
|
+
readonly kind: "script" | "style";
|
|
45
|
+
/** `<script module>` (or Svelte 4's `context="module"`) */
|
|
46
|
+
readonly module: boolean;
|
|
47
|
+
/** its `lang` (lower case), or null */
|
|
48
|
+
readonly lang: string | null;
|
|
49
|
+
/** the opening tag: its `<` and just past its `>` */
|
|
50
|
+
readonly open: number;
|
|
51
|
+
/** its content: from just past the opening tag to the `</` of its end tag */
|
|
52
|
+
readonly start: number;
|
|
53
|
+
readonly end: number;
|
|
54
|
+
/** just past the end tag's `>` */
|
|
55
|
+
readonly close: number;
|
|
56
|
+
/** the opening tag as the walk read it (core's `attr_spans` cuts an attribute out of it) */
|
|
57
|
+
readonly tag: FoundTag;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Every `<script>` and `<style>` block of a component's source, in order, read with core's tag walk
|
|
61
|
+
* (#1370): a value may hold `>` or the other quote, any quoting and case, a comment holds no block,
|
|
62
|
+
* and a block's text holds no tag (`<style>` inside a script's string is text). A block whose end
|
|
63
|
+
* tag never comes is not one (the source ends inside it). test/component-blocks.test.ts.
|
|
64
|
+
*/
|
|
65
|
+
export declare function component_blocks(src: string): ComponentBlock[];
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { OgygiaError } from "../messages.js";
|
|
2
|
+
import { next_any_tag, tag_name } from "@ogygia/core/contract";
|
|
3
|
+
// src/parse/scan.ts
|
|
4
|
+
const WS_RE = /\s/;
|
|
5
|
+
const TEMPLATE_INTERPOLATION_RE = /\$\{/;
|
|
6
|
+
const WORD_START_RE = /[A-Za-z_$]|[^\s\p{ASCII}]/u;
|
|
7
|
+
const WORD_RE = /[A-Za-z0-9_$]|[^\s\p{ASCII}]/u;
|
|
8
|
+
const NUMBER_RE = /[0-9A-Za-z_.]/;
|
|
9
|
+
const DIGIT_RE = /[0-9]/;
|
|
10
|
+
function word_leaves(word) {
|
|
11
|
+
switch (word) {
|
|
12
|
+
case "if":
|
|
13
|
+
case "while":
|
|
14
|
+
case "for":
|
|
15
|
+
case "with": return "head";
|
|
16
|
+
case "return":
|
|
17
|
+
case "typeof":
|
|
18
|
+
case "instanceof":
|
|
19
|
+
case "in":
|
|
20
|
+
case "of":
|
|
21
|
+
case "new":
|
|
22
|
+
case "delete":
|
|
23
|
+
case "void":
|
|
24
|
+
case "throw":
|
|
25
|
+
case "case":
|
|
26
|
+
case "do":
|
|
27
|
+
case "else":
|
|
28
|
+
case "yield":
|
|
29
|
+
case "await": return "operand";
|
|
30
|
+
default: return "value";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const STOP = -1;
|
|
34
|
+
function walk(src, from, visit) {
|
|
35
|
+
const stack = [];
|
|
36
|
+
let value_before = false;
|
|
37
|
+
let head = false;
|
|
38
|
+
let dot_before = false;
|
|
39
|
+
const n = src.length;
|
|
40
|
+
const template = (at) => {
|
|
41
|
+
for (let j = at; j < n; j++) {
|
|
42
|
+
const c = src[j];
|
|
43
|
+
if (c === "\\") j++;
|
|
44
|
+
else if (c === "`") {
|
|
45
|
+
value_before = true;
|
|
46
|
+
return j + 1;
|
|
47
|
+
} else if (c === "$" && src[j + 1] === "{") {
|
|
48
|
+
stack.push({
|
|
49
|
+
c: "${",
|
|
50
|
+
regex_after: false
|
|
51
|
+
});
|
|
52
|
+
value_before = false;
|
|
53
|
+
return j + 2;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return n;
|
|
57
|
+
};
|
|
58
|
+
let i = from;
|
|
59
|
+
while (i < n) {
|
|
60
|
+
const c = src[i];
|
|
61
|
+
const c2 = src[i + 1];
|
|
62
|
+
if (c === "'" || c === "\"") {
|
|
63
|
+
let j = i + 1;
|
|
64
|
+
for (; j < n; j++) if (src[j] === "\\") j++;
|
|
65
|
+
else if (src[j] === c || src[j] === "\n") break;
|
|
66
|
+
i = j + 1;
|
|
67
|
+
value_before = true;
|
|
68
|
+
dot_before = head = false;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (c === "`") {
|
|
72
|
+
i = template(i + 1);
|
|
73
|
+
dot_before = head = false;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (c === "/" && c2 === "/") {
|
|
77
|
+
const nl = src.indexOf("\n", i + 2);
|
|
78
|
+
i = nl < 0 ? n : nl + 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (c === "/" && c2 === "*") {
|
|
82
|
+
const end = src.indexOf("*/", i + 2);
|
|
83
|
+
i = end < 0 ? n : end + 2;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (c === "/" && !value_before) {
|
|
87
|
+
let j = i + 1;
|
|
88
|
+
let in_class = false;
|
|
89
|
+
for (; j < n; j++) {
|
|
90
|
+
const r = src[j];
|
|
91
|
+
if (r === "\\") j++;
|
|
92
|
+
else if (r === "\n") break;
|
|
93
|
+
else if (r === "[") in_class = true;
|
|
94
|
+
else if (r === "]") in_class = false;
|
|
95
|
+
else if (r === "/" && !in_class) break;
|
|
96
|
+
}
|
|
97
|
+
j++;
|
|
98
|
+
while (j < n && WORD_RE.test(src[j])) j++;
|
|
99
|
+
i = j;
|
|
100
|
+
value_before = true;
|
|
101
|
+
dot_before = head = false;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (WORD_START_RE.test(c) || DIGIT_RE.test(c)) {
|
|
105
|
+
const number = DIGIT_RE.test(c);
|
|
106
|
+
let j = i + 1;
|
|
107
|
+
while (j < n && (number ? NUMBER_RE : WORD_RE).test(src[j])) j++;
|
|
108
|
+
const word = src.slice(i, j);
|
|
109
|
+
const property = dot_before;
|
|
110
|
+
const resume = number || property ? void 0 : visit({
|
|
111
|
+
at: i,
|
|
112
|
+
kind: "word",
|
|
113
|
+
end: j
|
|
114
|
+
});
|
|
115
|
+
if (resume === STOP) return;
|
|
116
|
+
if (typeof resume === "number") {
|
|
117
|
+
i = resume;
|
|
118
|
+
value_before = true;
|
|
119
|
+
dot_before = head = false;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
i = j;
|
|
123
|
+
const leaves = property || number ? "value" : word_leaves(word);
|
|
124
|
+
if (!(head && word === "await")) head = leaves === "head";
|
|
125
|
+
value_before = leaves !== "operand";
|
|
126
|
+
dot_before = false;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (c === "(" || c === "[" || c === "{") {
|
|
130
|
+
stack.push({
|
|
131
|
+
c,
|
|
132
|
+
regex_after: c === "{" || c === "(" && head
|
|
133
|
+
});
|
|
134
|
+
if (visit({
|
|
135
|
+
at: i,
|
|
136
|
+
kind: "open"
|
|
137
|
+
}) === STOP) return;
|
|
138
|
+
i++;
|
|
139
|
+
value_before = dot_before = head = false;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (c === ")" || c === "]" || c === "}") {
|
|
143
|
+
const top = stack.pop();
|
|
144
|
+
if (c === "}" && top?.c === "${") {
|
|
145
|
+
i = template(i + 1);
|
|
146
|
+
dot_before = head = false;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (visit({
|
|
150
|
+
at: i,
|
|
151
|
+
kind: "close"
|
|
152
|
+
}) === STOP) return;
|
|
153
|
+
i++;
|
|
154
|
+
value_before = !(top?.regex_after ?? false);
|
|
155
|
+
dot_before = head = false;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
i++;
|
|
159
|
+
if (WS_RE.test(c)) continue;
|
|
160
|
+
if ((c === "+" || c === "-") && c2 === c) {
|
|
161
|
+
i++;
|
|
162
|
+
if (value_before) continue;
|
|
163
|
+
}
|
|
164
|
+
value_before = false;
|
|
165
|
+
if (c === "." && c2 === "." && src[i + 1] === ".") {
|
|
166
|
+
i += 2;
|
|
167
|
+
dot_before = false;
|
|
168
|
+
} else dot_before = c === ".";
|
|
169
|
+
head = false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Index of the `)` matching the `(` at `open`, skipping strings, comments, regex literals (incl.
|
|
173
|
+
* `[…]` char classes, which may hold an unescaped `/`) and template text. -1 if unbalanced. */
|
|
174
|
+
function match_close(src, open) {
|
|
175
|
+
let depth = 0;
|
|
176
|
+
let found = -1;
|
|
177
|
+
walk(src, open, (met) => {
|
|
178
|
+
if (met.kind === "open") depth++;
|
|
179
|
+
else if (met.kind === "close" && --depth === 0) {
|
|
180
|
+
found = met.at;
|
|
181
|
+
return STOP;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return found;
|
|
185
|
+
}
|
|
186
|
+
const IDENT = /[A-Za-z0-9_$.]/;
|
|
187
|
+
/**
|
|
188
|
+
* Find every `<prefix><method>(…)` call in `src`. `prefix` is the literal namespace head
|
|
189
|
+
* (`import.meta.og.`, a word's start); `method` is the dotted identifier chain that follows, up to
|
|
190
|
+
* the `(`. Only in code (a template's `${…}` included), so a marker in prose is never a hit. Pure.
|
|
191
|
+
*/
|
|
192
|
+
function find_og_calls(src, prefix) {
|
|
193
|
+
const calls = [];
|
|
194
|
+
walk(src, 0, (met) => {
|
|
195
|
+
if (met.kind !== "word" || !src.startsWith(prefix, met.at)) return;
|
|
196
|
+
let p = met.at + prefix.length;
|
|
197
|
+
const name_start = p;
|
|
198
|
+
while (p < src.length && IDENT.test(src[p])) p++;
|
|
199
|
+
const method = src.slice(name_start, p);
|
|
200
|
+
let q = p;
|
|
201
|
+
while (q < src.length && WS_RE.test(src[q])) q++;
|
|
202
|
+
const close = method && src[q] === "(" ? match_close(src, q) : -1;
|
|
203
|
+
if (close < 0) return p;
|
|
204
|
+
calls.push({
|
|
205
|
+
start: met.at,
|
|
206
|
+
end: close + 1,
|
|
207
|
+
method,
|
|
208
|
+
args: src.slice(q + 1, close)
|
|
209
|
+
});
|
|
210
|
+
return close + 1;
|
|
211
|
+
});
|
|
212
|
+
return calls;
|
|
213
|
+
}
|
|
214
|
+
/** Split a call's argument text into the first string-literal (unquoted) and the verbatim rest.
|
|
215
|
+
* Throws (with `who` in the message) when the first argument is not a STATIC string literal — a
|
|
216
|
+
* bare identifier, or a template literal with `${…}` interpolation (not build-determinable). */
|
|
217
|
+
function split_first_string(args, who) {
|
|
218
|
+
let i = 0;
|
|
219
|
+
while (i < args.length && WS_RE.test(args[i])) i++;
|
|
220
|
+
const q = args[i];
|
|
221
|
+
if (q !== "'" && q !== "\"" && q !== "`") throw new OgygiaError("call_arg_not_literal", {
|
|
222
|
+
call: who,
|
|
223
|
+
got: args.slice(0, 24)
|
|
224
|
+
});
|
|
225
|
+
let j = i + 1;
|
|
226
|
+
for (; j < args.length; j++) if (args[j] === "\\") j++;
|
|
227
|
+
else if (args[j] === q) break;
|
|
228
|
+
const value = args.slice(i + 1, j);
|
|
229
|
+
if (q === "`" && TEMPLATE_INTERPOLATION_RE.test(value)) throw new OgygiaError("call_arg_interpolated", { call: who });
|
|
230
|
+
let k = j + 1;
|
|
231
|
+
while (k < args.length && WS_RE.test(args[k])) k++;
|
|
232
|
+
if (k < args.length && args[k] !== ",") throw new OgygiaError("call_arg_in_expression", {
|
|
233
|
+
call: who,
|
|
234
|
+
near: args.slice(i, k + 8)
|
|
235
|
+
});
|
|
236
|
+
if (args[k] === ",") k++;
|
|
237
|
+
return {
|
|
238
|
+
value,
|
|
239
|
+
rest: args.slice(k).trim()
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const JS_EXT = /\.(ts|js|mjs|cjs|mts|cts)$/;
|
|
243
|
+
/** Does `id` (a module path, maybe with a `?query`) end in a plain JS/TS extension? */
|
|
244
|
+
function is_js_module(id) {
|
|
245
|
+
const clean = id.split("?")[0];
|
|
246
|
+
return JS_EXT.test(clean);
|
|
247
|
+
}
|
|
248
|
+
/** Does `id` end in one of the configured MARKUP extensions (`.svelte`, `.svx`, `.md`)? */
|
|
249
|
+
function is_markup_module(id, markup_exts) {
|
|
250
|
+
const clean = id.split("?")[0];
|
|
251
|
+
return markup_exts.some((ext) => clean.endsWith(ext));
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Extract the JS regions of a module for construct analysis. Returns `null` when the extension is not
|
|
255
|
+
* a recognized construct host (the caller then leaves the file untouched). A JS/TS module is one
|
|
256
|
+
* region; a markup file is its `<script>` blocks. `.svelte.ts`/`.svelte.js` hit the JS branch first
|
|
257
|
+
* (they end in `.ts`/`.js`), so a `.svelte`-suffixed markup ext never shadows them.
|
|
258
|
+
*/
|
|
259
|
+
function og_js_regions(src, id, markup_exts) {
|
|
260
|
+
if (is_js_module(id)) return [{
|
|
261
|
+
code: src,
|
|
262
|
+
offset: 0
|
|
263
|
+
}];
|
|
264
|
+
if (is_markup_module(id, markup_exts)) return component_blocks(src).filter((b) => b.kind === "script").map((b) => ({
|
|
265
|
+
code: src.slice(b.start, b.end),
|
|
266
|
+
offset: b.start
|
|
267
|
+
}));
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Every `<script>` and `<style>` block of a component's source, in order, read with core's tag walk
|
|
272
|
+
* (#1370): a value may hold `>` or the other quote, any quoting and case, a comment holds no block,
|
|
273
|
+
* and a block's text holds no tag (`<style>` inside a script's string is text). A block whose end
|
|
274
|
+
* tag never comes is not one (the source ends inside it). test/component-blocks.test.ts.
|
|
275
|
+
*/
|
|
276
|
+
function component_blocks(src) {
|
|
277
|
+
const blocks = [];
|
|
278
|
+
for (let tag = next_any_tag(src, 0); tag !== null; tag = next_any_tag(src, tag.next)) {
|
|
279
|
+
const kind = tag_name(src, tag);
|
|
280
|
+
if (kind !== "script" && kind !== "style") continue;
|
|
281
|
+
if (tag.next >= src.length) break;
|
|
282
|
+
const gt = src.indexOf(">", tag.next);
|
|
283
|
+
const lang = tag.attr("lang");
|
|
284
|
+
blocks.push({
|
|
285
|
+
kind,
|
|
286
|
+
module: kind === "script" && (tag.attr("module") !== void 0 || tag.attr("context") === "module"),
|
|
287
|
+
lang: lang ? lang.toLowerCase() : null,
|
|
288
|
+
open: tag.start,
|
|
289
|
+
start: tag.end,
|
|
290
|
+
end: tag.next,
|
|
291
|
+
close: gt === -1 ? src.length : gt + 1,
|
|
292
|
+
tag
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return blocks;
|
|
296
|
+
}
|
|
297
|
+
export { component_blocks, find_og_calls, is_js_module, is_markup_module, match_close, og_js_regions, split_first_string };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every node of Svelte's modern template AST (`parse(…, { modern: true })`) ogygia's code reads, with
|
|
3
|
+
* the fields it reads on it. test/canary/svelte-ast.test.ts parses a fixture that holds every one
|
|
4
|
+
* with the installed Svelte and checks each node has its fields; it also fails when a file that walks
|
|
5
|
+
* the AST names a node type this table lacks, so a new read comes with its canary (#1722).
|
|
6
|
+
*/
|
|
7
|
+
export declare const SVELTE_AST_READS: Readonly<Record<string, readonly string[]>>;
|