@sdxc/kit 2026.10.16 → 2026.10.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +391 -20
- package/changelog.jsonl +1160 -1035
- package/config.schema.json +47 -1
- package/dist/author.d.ts +36 -0
- package/dist/author.js +107 -0
- package/dist/cli.js +12 -0
- package/dist/commands/create.js +9 -4
- package/dist/commands/deploy.d.ts +23 -0
- package/dist/commands/deploy.js +167 -0
- package/dist/commands/destroy.d.ts +22 -0
- package/dist/commands/destroy.js +92 -0
- package/dist/commands/doctor.d.ts +26 -0
- package/dist/commands/doctor.js +65 -0
- package/dist/commands/initializers.d.ts +16 -0
- package/dist/commands/initializers.js +23 -0
- package/dist/commands/make.d.ts +72 -6
- package/dist/commands/make.js +127 -19
- package/dist/commands/middleware.d.ts +16 -0
- package/dist/commands/middleware.js +21 -0
- package/dist/commands/models.d.ts +18 -0
- package/dist/commands/models.js +29 -0
- package/dist/commands/notes.d.ts +17 -0
- package/dist/commands/notes.js +27 -0
- package/dist/commands/routes.d.ts +25 -0
- package/dist/commands/routes.js +50 -0
- package/dist/commands/run.d.ts +18 -2
- package/dist/commands/run.js +35 -12
- package/dist/commands/scaffold.d.ts +29 -0
- package/dist/commands/scaffold.js +33 -0
- package/dist/commands/schedule.d.ts +16 -0
- package/dist/commands/schedule.js +21 -0
- package/dist/commands/stats.d.ts +16 -0
- package/dist/commands/stats.js +21 -0
- package/dist/commands/ui/update.d.ts +6 -3
- package/dist/commands/ui/update.js +24 -7
- package/dist/commands/upgrade.d.ts +3 -1
- package/dist/commands/upgrade.js +88 -23
- package/dist/config.d.ts +18 -0
- package/dist/config.js +28 -1
- package/dist/contract.d.ts +97 -3
- package/dist/contract.js +360 -26
- package/dist/create/templates.d.ts +8 -1
- package/dist/create/templates.js +26 -2
- package/dist/dependencies.d.ts +4 -1
- package/dist/dependencies.js +13 -1
- package/dist/deploy/plan.d.ts +49 -0
- package/dist/deploy/plan.js +68 -0
- package/dist/destroy.d.ts +40 -0
- package/dist/destroy.js +311 -0
- package/dist/doctor/check.d.ts +71 -0
- package/dist/doctor/check.js +53 -0
- package/dist/doctor/checks.d.ts +23 -0
- package/dist/doctor/checks.js +68 -0
- package/dist/doctor/define.d.ts +59 -0
- package/dist/doctor/define.js +16 -0
- package/dist/doctor/emails.d.ts +18 -0
- package/dist/doctor/emails.js +243 -0
- package/dist/doctor/env.d.ts +26 -0
- package/dist/doctor/env.js +88 -0
- package/dist/doctor/files.d.ts +14 -0
- package/dist/doctor/files.js +34 -0
- package/dist/doctor/fixture.d.ts +23 -0
- package/dist/doctor/fixture.js +46 -0
- package/dist/doctor/install.d.ts +17 -0
- package/dist/doctor/install.js +37 -0
- package/dist/doctor/installed.d.ts +11 -0
- package/dist/doctor/installed.js +79 -0
- package/dist/doctor/layout.d.ts +23 -0
- package/dist/doctor/layout.js +83 -0
- package/dist/doctor/migrations.d.ts +16 -0
- package/dist/doctor/migrations.js +96 -0
- package/dist/doctor/models.d.ts +13 -0
- package/dist/doctor/models.js +69 -0
- package/dist/doctor/pins.d.ts +15 -0
- package/dist/doctor/pins.js +79 -0
- package/dist/doctor/project.d.ts +58 -0
- package/dist/doctor/project.js +261 -0
- package/dist/doctor/remix.d.ts +14 -0
- package/dist/doctor/remix.js +44 -0
- package/dist/doctor/routes.d.ts +11 -0
- package/dist/doctor/routes.js +49 -0
- package/dist/doctor/typegen.d.ts +13 -0
- package/dist/doctor/typegen.js +57 -0
- package/dist/doctor/ui.d.ts +14 -0
- package/dist/doctor/ui.js +51 -0
- package/dist/init.d.ts +5 -3
- package/dist/init.js +15 -7
- package/dist/initializers.d.ts +25 -0
- package/dist/initializers.js +89 -0
- package/dist/lock.d.ts +2 -2
- package/dist/make/actions.d.ts +23 -0
- package/dist/make/actions.js +82 -0
- package/dist/make/declare.d.ts +32 -4
- package/dist/make/declare.js +405 -27
- package/dist/make/email.d.ts +23 -0
- package/dist/make/email.js +111 -0
- package/dist/make/from-fields.d.ts +30 -0
- package/dist/make/from-fields.js +162 -0
- package/dist/make/from-model.d.ts +39 -0
- package/dist/make/from-model.js +161 -0
- package/dist/make/generate.d.ts +16 -5
- package/dist/make/generate.js +43 -14
- package/dist/make/guard.d.ts +35 -0
- package/dist/make/guard.js +73 -0
- package/dist/make/imports.d.ts +6 -0
- package/dist/make/imports.js +19 -0
- package/dist/make/index.d.ts +47 -4
- package/dist/make/index.js +287 -25
- package/dist/make/kinds.d.ts +31 -3
- package/dist/make/kinds.js +82 -9
- package/dist/make/mount.d.ts +25 -0
- package/dist/make/mount.js +135 -0
- package/dist/make/schema-diff.d.ts +58 -0
- package/dist/make/schema-diff.js +609 -0
- package/dist/make/schema.d.ts +76 -0
- package/dist/make/schema.js +206 -0
- package/dist/make/stubs.d.ts +7 -5
- package/dist/make/stubs.js +16 -5
- package/dist/make/test.d.ts +43 -0
- package/dist/make/test.js +149 -0
- package/dist/make/trigger.d.ts +31 -0
- package/dist/make/trigger.js +163 -0
- package/dist/models.d.ts +42 -0
- package/dist/models.js +149 -0
- package/dist/notes.d.ts +28 -0
- package/dist/notes.js +66 -0
- package/dist/reference.d.ts +27 -0
- package/dist/reference.js +103 -0
- package/dist/releases.d.ts +9 -0
- package/dist/releases.js +29 -8
- package/dist/routes/chains.d.ts +34 -0
- package/dist/routes/chains.js +95 -0
- package/dist/routes/filter.d.ts +53 -0
- package/dist/routes/filter.js +147 -0
- package/dist/routes/format.d.ts +23 -0
- package/dist/routes/format.js +123 -0
- package/dist/routes/helpers.d.ts +21 -0
- package/dist/routes/helpers.js +165 -0
- package/dist/routes/index.d.ts +56 -0
- package/dist/routes/index.js +167 -0
- package/dist/routes/modules.d.ts +116 -0
- package/dist/routes/modules.js +235 -0
- package/dist/routes/parse.d.ts +161 -0
- package/dist/routes/parse.js +621 -0
- package/dist/routes/routers.d.ts +69 -0
- package/dist/routes/routers.js +327 -0
- package/dist/scaffold/fields.d.ts +129 -0
- package/dist/scaffold/fields.js +421 -0
- package/dist/scaffold/index.d.ts +37 -0
- package/dist/scaffold/index.js +613 -0
- package/dist/scaffold/render.d.ts +51 -0
- package/dist/scaffold/render.js +332 -0
- package/dist/schedule.d.ts +45 -0
- package/dist/schedule.js +124 -0
- package/dist/stats.d.ts +37 -0
- package/dist/stats.js +126 -0
- package/dist/testing.d.ts +12 -2
- package/dist/testing.js +22 -3
- package/dist/ui/commands.d.ts +10 -3
- package/dist/ui/commands.js +85 -15
- package/dist/ui/registry.d.ts +8 -8
- package/dist/upgrade/agents.d.ts +49 -0
- package/dist/upgrade/agents.js +110 -0
- package/dist/upgrade/changelog.d.ts +2 -2
- package/dist/upgrade/project.d.ts +4 -0
- package/dist/upgrade/project.js +11 -1
- package/dist/upgrade/prompt.d.ts +11 -0
- package/dist/upgrade/prompt.js +50 -7
- package/package.json +6 -4
- package/registry/ui/alert-dialog.json +2 -2
- package/registry/ui/attachment.json +2 -2
- package/registry/ui/calendar.json +2 -2
- package/registry/ui/color-picker.json +2 -2
- package/registry/ui/command.json +2 -2
- package/registry/ui/date-picker.json +2 -2
- package/registry/ui/date-range-picker.json +2 -2
- package/registry/ui/dialog.json +2 -2
- package/registry/ui/file-trigger.json +2 -2
- package/registry/ui/heading-scope.json +2 -2
- package/registry/ui/message-scroller.json +2 -2
- package/registry/ui/number-field.json +2 -2
- package/registry/ui/range-calendar.json +2 -2
- package/registry/ui/resizable.json +2 -2
- package/registry/ui/select.json +2 -2
- package/registry/ui/shared-element.json +2 -2
- package/stubs/command.stub +4 -0
- package/stubs/command.test.stub +3 -0
- package/stubs/controller.api.resource.stub +23 -0
- package/stubs/controller.api.stub +21 -0
- package/stubs/controller.api.test.stub +18 -0
- package/stubs/controller.form.stub +40 -10
- package/stubs/controller.form.test.stub +61 -0
- package/stubs/controller.resource.form.stub +67 -0
- package/stubs/controller.resource.stub +28 -0
- package/stubs/controller.stub +15 -8
- package/stubs/controller.test.stub +3 -0
- package/stubs/email.stub +15 -38
- package/stubs/email.test.stub +12 -7
- package/stubs/factory.stub +4 -1
- package/stubs/initializer.stub +2 -0
- package/stubs/initializer.test.stub +3 -0
- package/stubs/job.cron.stub +4 -0
- package/stubs/job.stub +4 -0
- package/stubs/job.test.stub +3 -0
- package/stubs/mcp.resource.stub +3 -0
- package/stubs/mcp.stub +3 -0
- package/stubs/mcp.test.stub +3 -0
- package/stubs/middleware.stub +6 -1
- package/stubs/middleware.test.stub +3 -0
- package/stubs/model.stub +12 -3
- package/stubs/model.test.stub +3 -0
- package/stubs/policy.test.stub +36 -7
- package/stubs/scaffold.api.create.stub +34 -0
- package/stubs/scaffold.api.create.test.stub +37 -0
- package/stubs/scaffold.api.destroy.stub +34 -0
- package/stubs/scaffold.api.destroy.test.stub +44 -0
- package/stubs/scaffold.api.index.stub +23 -0
- package/stubs/scaffold.api.index.test.stub +31 -0
- package/stubs/scaffold.api.show.stub +27 -0
- package/stubs/scaffold.api.show.test.stub +41 -0
- package/stubs/scaffold.api.update.stub +44 -0
- package/stubs/scaffold.api.update.test.stub +46 -0
- package/stubs/scaffold.command.create.stub +22 -0
- package/stubs/scaffold.command.create.test.stub +30 -0
- package/stubs/scaffold.command.delete.stub +20 -0
- package/stubs/scaffold.command.delete.test.stub +39 -0
- package/stubs/scaffold.command.list.stub +18 -0
- package/stubs/scaffold.command.list.test.stub +31 -0
- package/stubs/scaffold.command.show.stub +19 -0
- package/stubs/scaffold.command.show.test.stub +37 -0
- package/stubs/scaffold.controller.destroy.stub +35 -0
- package/stubs/scaffold.controller.destroy.test.stub +65 -0
- package/stubs/scaffold.controller.edit.stub +97 -0
- package/stubs/scaffold.controller.edit.test.stub +91 -0
- package/stubs/scaffold.controller.index.stub +55 -0
- package/stubs/scaffold.controller.index.test.stub +32 -0
- package/stubs/scaffold.controller.new.stub +70 -0
- package/stubs/scaffold.controller.new.test.stub +81 -0
- package/stubs/scaffold.controller.show.stub +59 -0
- package/stubs/scaffold.controller.show.test.stub +43 -0
- package/stubs/scaffold.factory.stub +16 -0
- package/stubs/scaffold.form.stub +75 -0
- package/stubs/scaffold.mcp.create.stub +24 -0
- package/stubs/scaffold.mcp.create.test.stub +36 -0
- package/stubs/scaffold.mcp.delete.stub +31 -0
- package/stubs/scaffold.mcp.delete.test.stub +43 -0
- package/stubs/scaffold.mcp.get.stub +21 -0
- package/stubs/scaffold.mcp.get.test.stub +31 -0
- package/stubs/scaffold.mcp.list.stub +23 -0
- package/stubs/scaffold.mcp.list.test.stub +25 -0
- package/stubs/scaffold.mcp.update.stub +37 -0
- package/stubs/scaffold.mcp.update.test.stub +40 -0
- package/stubs/scaffold.migration.stub +2 -0
- package/stubs/scaffold.model.stub +35 -0
- package/stubs/scaffold.model.test.stub +25 -0
- package/stubs/scaffold.test-database.stub +33 -0
- package/stubs/scaffold.test-mcp.stub +92 -0
- package/stubs/scaffold.validator.stub +18 -0
- package/stubs/scaffold.validator.test.stub +26 -0
- package/stubs/service.stub +4 -0
- package/stubs/service.test.stub +3 -0
- package/stubs/validator.stub +3 -0
- package/stubs/validator.test.stub +3 -0
- package/templates/app/.env.example +16 -0
- package/templates/app/AGENTS.md +163 -0
- package/templates/app/CLAUDE.md +1 -0
- package/templates/app/README.md +79 -37
- package/templates/app/app/commands/about.test.ts +1 -1
- package/templates/app/app/commands/console.test.ts +26 -0
- package/templates/app/app/commands/console.ts +19 -0
- package/templates/app/app/commands/db/console.ts +6 -0
- package/templates/app/app/commands/db/fresh.test.ts +57 -0
- package/templates/app/app/commands/db/fresh.ts +37 -0
- package/templates/app/app/commands/db/migrate.test.ts +46 -0
- package/templates/app/app/commands/db/migrate.ts +88 -0
- package/templates/app/app/commands/db/prepare.test.ts +39 -0
- package/templates/app/app/commands/db/prepare.ts +35 -0
- package/templates/app/app/commands/db/seed.test.ts +38 -0
- package/templates/app/app/commands/db/seed.ts +26 -0
- package/templates/app/app/commands/deploy.test.ts +34 -0
- package/templates/app/app/commands/deploy.ts +18 -0
- package/templates/app/app/commands/emails/preview.test.tsx +63 -0
- package/templates/app/app/commands/emails/preview.ts +24 -0
- package/templates/app/app/commands/index.ts +72 -1
- package/templates/app/app/commands/typegen.test.ts +17 -0
- package/templates/app/app/commands/typegen.ts +16 -0
- package/templates/app/app/emails/index.ts +10 -0
- package/templates/app/app/http/controllers/api/health.tsx +15 -0
- package/templates/app/app/http/controllers/api/not-found.tsx +16 -0
- package/templates/app/app/http/controllers/api/openapi.tsx +17 -0
- package/templates/app/app/http/controllers/home.tsx +21 -13
- package/templates/app/app/http/controllers/mcp.tsx +16 -0
- package/templates/app/app/http/controllers/not-found.tsx +23 -18
- package/templates/app/app/http/controllers/robots.test.ts +18 -0
- package/templates/app/app/http/controllers/robots.ts +24 -0
- package/templates/app/app/http/controllers/sitemap.test.ts +28 -0
- package/templates/app/app/http/controllers/sitemap.ts +25 -0
- package/templates/app/app/http/middleware/access.test.ts +43 -0
- package/templates/app/app/http/middleware/access.ts +41 -0
- package/templates/app/app/http/middleware/api.ts +19 -0
- package/templates/app/app/http/middleware/page.test.ts +43 -0
- package/templates/app/app/http/middleware/page.ts +51 -0
- package/templates/app/app/http/middleware/server-timing.test.ts +15 -0
- package/templates/app/app/http/middleware/server-timing.ts +42 -0
- package/templates/app/app/initializers/assets.ts +1 -1
- package/templates/app/app/initializers/captcha.ts +11 -0
- package/templates/app/app/initializers/cli.ts +68 -2
- package/templates/app/app/initializers/database.test.ts +18 -0
- package/templates/app/app/initializers/database.ts +18 -0
- package/templates/app/app/initializers/dispatcher.ts +26 -0
- package/templates/app/app/initializers/honeypot.ts +11 -0
- package/templates/app/app/initializers/i18n.test.ts +16 -0
- package/templates/app/app/initializers/i18n.ts +28 -0
- package/templates/app/app/initializers/local-database.test.ts +33 -0
- package/templates/app/app/initializers/local-database.ts +53 -0
- package/templates/app/app/initializers/mail-transport.test.ts +27 -0
- package/templates/app/app/initializers/mail-transport.ts +26 -0
- package/templates/app/app/initializers/mail.test.ts +27 -0
- package/templates/app/app/initializers/mail.ts +19 -0
- package/templates/app/app/initializers/mcp.ts +1 -1
- package/templates/app/app/initializers/openapi.ts +21 -0
- package/templates/app/app/initializers/router.test.ts +68 -0
- package/templates/app/app/initializers/router.tsx +81 -25
- package/templates/app/app/initializers/seo.ts +15 -0
- package/templates/app/app/initializers/session.test.ts +34 -0
- package/templates/app/app/initializers/session.ts +29 -0
- package/templates/app/app/initializers/spam.ts +10 -0
- package/templates/app/app/jobs/email.test.tsx +70 -0
- package/templates/app/app/jobs/email.ts +16 -0
- package/templates/app/app/jobs/index.ts +6 -2
- package/templates/app/app/models/index.ts +10 -0
- package/templates/app/app/policies/abilities.ts +13 -0
- package/templates/app/app/policies/policy.test.ts +15 -0
- package/templates/app/app/policies/policy.ts +30 -0
- package/templates/app/app/services/commands.ts +30 -0
- package/templates/app/app/services/migrations.test.ts +92 -0
- package/templates/app/app/services/migrations.ts +117 -0
- package/templates/app/app/test/cloudflare-workers.ts +25 -0
- package/templates/app/app/test/local-database.ts +67 -0
- package/templates/app/app/test/session.ts +28 -0
- package/templates/app/bootstrap/cli.ts +1 -1
- package/templates/app/bootstrap/console.ts +13 -0
- package/templates/app/bootstrap/worker.ts +4 -4
- package/templates/app/cloudflare.config.ts +30 -2
- package/templates/app/config/constants.ts +19 -0
- package/templates/app/config/problems.ts +24 -0
- package/templates/app/config/security-policy.ts +34 -0
- package/templates/app/config/security-txt.ts +28 -0
- package/templates/app/database/migrations/.gitkeep +0 -0
- package/templates/app/database/seed.ts +12 -0
- package/templates/app/package.json +37 -6
- package/templates/app/resources/layouts/document.tsx +28 -7
- package/templates/app/resources/locales/en.json +10 -0
- package/templates/app/routes/api.ts +29 -0
- package/templates/app/routes/web.ts +6 -4
- package/templates/app/vite.config.ts +4 -0
- package/stubs/policy.stub +0 -13
- package/templates/app/config/router-context.d.ts +0 -20
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the expressions a route contract or a router initializer is written in out of
|
|
3
|
+
* `@sdxc/codemod` tokens: literals, identifiers, member access, calls, objects, arrays, arrow
|
|
4
|
+
* functions and `import()`. Anything else becomes an opaque node, so one unread line costs one entry.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Statement keywords that start a new top-level statement, which bounds the one before it. */
|
|
10
|
+
const STATEMENT_STARTS = new Set([
|
|
11
|
+
"import",
|
|
12
|
+
"export",
|
|
13
|
+
"const",
|
|
14
|
+
"let",
|
|
15
|
+
"var",
|
|
16
|
+
"function",
|
|
17
|
+
"async",
|
|
18
|
+
"class",
|
|
19
|
+
"interface",
|
|
20
|
+
"type",
|
|
21
|
+
"declare",
|
|
22
|
+
"enum",
|
|
23
|
+
]);
|
|
24
|
+
/** Tokens that end an expression inside a list, a block or a statement. */
|
|
25
|
+
const DELIMITERS = new Set([",", ")", "]", "}", ";"]);
|
|
26
|
+
/**
|
|
27
|
+
* Reads expressions from one file's code tokens (comments and JSX markup removed). Every read is
|
|
28
|
+
* bounded by a token index limit, so an expression it cannot read never swallows the next one.
|
|
29
|
+
*/
|
|
30
|
+
export class Reader {
|
|
31
|
+
/** The code tokens. */
|
|
32
|
+
tokens;
|
|
33
|
+
/** The file's source, which node ranges index into. */
|
|
34
|
+
source;
|
|
35
|
+
/** For each bracket or template piece, the index of its partner; -1 for any other token. */
|
|
36
|
+
match;
|
|
37
|
+
/** For each token, how many brackets are open around it. */
|
|
38
|
+
depth;
|
|
39
|
+
/** Pairs the brackets of `tokens`, which are expected to be balanced, as the scanner guarantees. */
|
|
40
|
+
constructor(tokens, source) {
|
|
41
|
+
this.tokens = tokens.filter((token) => token.type !== "comment" && token.type !== "jsx");
|
|
42
|
+
this.source = source;
|
|
43
|
+
this.match = Array.from({ length: this.tokens.length }, () => -1);
|
|
44
|
+
this.depth = Array.from({ length: this.tokens.length }, () => 0);
|
|
45
|
+
let open = [];
|
|
46
|
+
for (let [index, token] of this.tokens.entries()) {
|
|
47
|
+
if (closes(token)) {
|
|
48
|
+
let opener = open.pop() ?? -1;
|
|
49
|
+
if (opener !== -1) {
|
|
50
|
+
this.match[opener] = index;
|
|
51
|
+
this.match[index] = opener;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.depth[index] = open.length;
|
|
55
|
+
if (opens(token))
|
|
56
|
+
open.push(index);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** The source text of a node, whitespace collapsed to single spaces. */
|
|
60
|
+
text(node) {
|
|
61
|
+
return this.source.slice(node.start, node.end).replace(/\s+/g, " ").trim();
|
|
62
|
+
}
|
|
63
|
+
/** Whether the token at `index` is the punctuator `value`. */
|
|
64
|
+
is(index, value) {
|
|
65
|
+
let token = this.tokens[index];
|
|
66
|
+
return token?.type === "punctuator" && token.value === value;
|
|
67
|
+
}
|
|
68
|
+
/** Whether the token at `index` is the identifier `value`. */
|
|
69
|
+
isWord(index, value) {
|
|
70
|
+
let token = this.tokens[index];
|
|
71
|
+
return token?.type === "identifier" && token.value === value;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Reads the expression starting at `index`, stopping before `limit`. An expression with an
|
|
75
|
+
* operator the reader does not evaluate becomes one opaque node up to the next delimiter.
|
|
76
|
+
*/
|
|
77
|
+
expression(index, limit) {
|
|
78
|
+
let read = this.postfix(index, limit);
|
|
79
|
+
let next = this.skipTypeSuffix(read.next, limit);
|
|
80
|
+
if (next >= limit || this.isDelimiter(next))
|
|
81
|
+
return { node: read.node, next };
|
|
82
|
+
let end = this.skipToDelimiter(next, limit);
|
|
83
|
+
return { node: this.opaque(index, end), next: end };
|
|
84
|
+
}
|
|
85
|
+
/** Every top-level declaration, export and the default export of the file. */
|
|
86
|
+
declarations() {
|
|
87
|
+
let result = { locals: new Map(), exports: new Map(), defaultExport: null };
|
|
88
|
+
let starts = this.statementStarts();
|
|
89
|
+
for (let [position, start] of starts.entries()) {
|
|
90
|
+
let limit = starts[position + 1] ?? this.tokens.length;
|
|
91
|
+
this.declaration(start, limit, result);
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
/** The index of every token at depth 0 that starts a statement. */
|
|
96
|
+
statementStarts() {
|
|
97
|
+
let starts = [];
|
|
98
|
+
for (let [index, token] of this.tokens.entries()) {
|
|
99
|
+
if (this.depth[index] !== 0 || token.type !== "identifier")
|
|
100
|
+
continue;
|
|
101
|
+
if (!STATEMENT_STARTS.has(token.value))
|
|
102
|
+
continue;
|
|
103
|
+
let previous = this.tokens[index - 1];
|
|
104
|
+
let boundary = previous === undefined ||
|
|
105
|
+
token.newlineBefore ||
|
|
106
|
+
(previous.type === "punctuator" && (previous.value === ";" || previous.value === "}"));
|
|
107
|
+
if (!boundary || this.isWord(index - 1, "export") || this.isWord(index - 1, "async")) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (this.isWord(index - 1, "default"))
|
|
111
|
+
continue;
|
|
112
|
+
starts.push(index);
|
|
113
|
+
}
|
|
114
|
+
return starts;
|
|
115
|
+
}
|
|
116
|
+
/** Records the declaration in the statement `[start, limit)`, when it is one the reader reads. */
|
|
117
|
+
declaration(start, limit, result) {
|
|
118
|
+
let index = start;
|
|
119
|
+
let exported = false;
|
|
120
|
+
if (this.isWord(index, "export")) {
|
|
121
|
+
exported = true;
|
|
122
|
+
index += 1;
|
|
123
|
+
if (this.isWord(index, "default")) {
|
|
124
|
+
let value = this.functionDeclaration(index + 1, limit);
|
|
125
|
+
result.defaultExport = value?.node ?? this.expression(index + 1, limit).node;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (this.is(index, "{")) {
|
|
129
|
+
this.exportList(index, limit, result);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
let declared = this.functionDeclaration(index, limit);
|
|
134
|
+
if (declared !== null) {
|
|
135
|
+
if (declared.name !== null) {
|
|
136
|
+
result.locals.set(declared.name, declared.node);
|
|
137
|
+
if (exported)
|
|
138
|
+
result.exports.set(declared.name, declared.name);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (!["const", "let", "var"].some((word) => this.isWord(index, word)))
|
|
143
|
+
return;
|
|
144
|
+
index += 1;
|
|
145
|
+
while (index < limit) {
|
|
146
|
+
let name = this.tokens[index];
|
|
147
|
+
if (name?.type !== "identifier")
|
|
148
|
+
return;
|
|
149
|
+
index += 1;
|
|
150
|
+
if (this.is(index, "!"))
|
|
151
|
+
index += 1;
|
|
152
|
+
if (this.is(index, ":"))
|
|
153
|
+
index = this.skipType(index + 1, limit);
|
|
154
|
+
if (!this.is(index, "="))
|
|
155
|
+
return;
|
|
156
|
+
let read = this.expression(index + 1, limit);
|
|
157
|
+
result.locals.set(name.value, read.node);
|
|
158
|
+
if (exported)
|
|
159
|
+
result.exports.set(name.value, name.value);
|
|
160
|
+
if (!this.is(read.next, ","))
|
|
161
|
+
return;
|
|
162
|
+
index = read.next + 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Reads `export { a, b as c }` without a `from` clause. */
|
|
166
|
+
exportList(open, limit, result) {
|
|
167
|
+
let close = this.match[open] ?? -1;
|
|
168
|
+
if (close === -1 || close >= limit)
|
|
169
|
+
return;
|
|
170
|
+
if (this.isWord(close + 1, "from"))
|
|
171
|
+
return;
|
|
172
|
+
let index = open + 1;
|
|
173
|
+
while (index < close) {
|
|
174
|
+
if (this.isWord(index, "type"))
|
|
175
|
+
index += 1;
|
|
176
|
+
let local = this.tokens[index];
|
|
177
|
+
if (local === undefined || local.type !== "identifier")
|
|
178
|
+
return;
|
|
179
|
+
let exported = local.value;
|
|
180
|
+
index += 1;
|
|
181
|
+
if (this.isWord(index, "as")) {
|
|
182
|
+
exported = this.tokens[index + 1]?.value ?? exported;
|
|
183
|
+
index += 2;
|
|
184
|
+
}
|
|
185
|
+
result.exports.set(exported, local.value);
|
|
186
|
+
if (this.is(index, ","))
|
|
187
|
+
index += 1;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/** `[async] function name(…) { … }` at `index`, as a function node, or `null`. */
|
|
191
|
+
functionDeclaration(index, limit) {
|
|
192
|
+
let start = index;
|
|
193
|
+
if (this.isWord(index, "async"))
|
|
194
|
+
index += 1;
|
|
195
|
+
if (!this.isWord(index, "function"))
|
|
196
|
+
return null;
|
|
197
|
+
index += 1;
|
|
198
|
+
if (this.is(index, "*"))
|
|
199
|
+
index += 1;
|
|
200
|
+
let name = null;
|
|
201
|
+
if (this.tokens[index]?.type === "identifier") {
|
|
202
|
+
name = this.tokens[index]?.value ?? null;
|
|
203
|
+
index += 1;
|
|
204
|
+
}
|
|
205
|
+
if (this.is(index, "<"))
|
|
206
|
+
index = this.skipAngles(index, limit);
|
|
207
|
+
if (!this.is(index, "("))
|
|
208
|
+
return null;
|
|
209
|
+
index = (this.match[index] ?? index) + 1;
|
|
210
|
+
if (this.is(index, ":"))
|
|
211
|
+
index = this.skipType(index + 1, limit, "{");
|
|
212
|
+
if (!this.is(index, "{"))
|
|
213
|
+
return null;
|
|
214
|
+
let close = this.match[index] ?? index;
|
|
215
|
+
return {
|
|
216
|
+
name,
|
|
217
|
+
node: {
|
|
218
|
+
kind: "function",
|
|
219
|
+
body: this.returned(index, close),
|
|
220
|
+
...this.range(start, close + 1),
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/** The expression a block's first top-level `return` answers, or `null`. */
|
|
225
|
+
returned(open, close) {
|
|
226
|
+
let index = open + 1;
|
|
227
|
+
while (index < close) {
|
|
228
|
+
let token = this.tokens[index];
|
|
229
|
+
if (token !== undefined && opens(token)) {
|
|
230
|
+
index = (this.match[index] ?? index) + 1;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (this.isWord(index, "return")) {
|
|
234
|
+
if (this.isDelimiter(index + 1))
|
|
235
|
+
return null;
|
|
236
|
+
return this.expression(index + 1, close).node;
|
|
237
|
+
}
|
|
238
|
+
index += 1;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
/** A primary expression followed by member accesses, calls and non-null assertions. */
|
|
243
|
+
postfix(index, limit) {
|
|
244
|
+
let read = this.primary(index, limit);
|
|
245
|
+
let node = read.node;
|
|
246
|
+
let next = read.next;
|
|
247
|
+
while (next < limit) {
|
|
248
|
+
if ((this.is(next, ".") || this.is(next, "?.")) &&
|
|
249
|
+
this.tokens[next + 1]?.type === "identifier") {
|
|
250
|
+
node = {
|
|
251
|
+
kind: "member",
|
|
252
|
+
object: node,
|
|
253
|
+
property: this.tokens[next + 1]?.value ?? "",
|
|
254
|
+
...this.range(index, next + 2),
|
|
255
|
+
};
|
|
256
|
+
next += 2;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (this.is(next, "?.") && (this.is(next + 1, "(") || this.is(next + 1, "["))) {
|
|
260
|
+
next += 1;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (this.is(next, "[")) {
|
|
264
|
+
let close = this.match[next] ?? next;
|
|
265
|
+
let key = this.tokens[next + 1];
|
|
266
|
+
if (close === next + 2 && key?.type === "string") {
|
|
267
|
+
node = {
|
|
268
|
+
kind: "member",
|
|
269
|
+
object: node,
|
|
270
|
+
property: cook(key.value),
|
|
271
|
+
...this.range(index, close + 1),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
node = this.opaque(index, close + 1);
|
|
276
|
+
}
|
|
277
|
+
next = close + 1;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (this.is(next, "<") && node.kind !== "opaque") {
|
|
281
|
+
let after = this.skipAngles(next, limit);
|
|
282
|
+
if (!this.is(after, "("))
|
|
283
|
+
break;
|
|
284
|
+
next = after;
|
|
285
|
+
}
|
|
286
|
+
if (this.is(next, "(")) {
|
|
287
|
+
let close = this.match[next] ?? next;
|
|
288
|
+
node = {
|
|
289
|
+
kind: "call",
|
|
290
|
+
callee: node,
|
|
291
|
+
args: this.list(next, close),
|
|
292
|
+
...this.range(index, close + 1),
|
|
293
|
+
};
|
|
294
|
+
next = close + 1;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (this.is(next, "!")) {
|
|
298
|
+
next += 1;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
return { node, next };
|
|
304
|
+
}
|
|
305
|
+
/** One literal, identifier, bracketed expression, object, array, function or `import()`. */
|
|
306
|
+
primary(index, limit) {
|
|
307
|
+
let token = this.tokens[index];
|
|
308
|
+
if (token === undefined)
|
|
309
|
+
return { node: this.opaque(index, index), next: index };
|
|
310
|
+
if (token.type === "string" || token.type === "template") {
|
|
311
|
+
return {
|
|
312
|
+
node: { kind: "string", value: cook(token.value), ...this.range(index, index + 1) },
|
|
313
|
+
next: index + 1,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (token.type === "template-head") {
|
|
317
|
+
let end = index;
|
|
318
|
+
while (end < limit && this.tokens[end]?.type !== "template-tail") {
|
|
319
|
+
let partner = this.match[end] ?? -1;
|
|
320
|
+
if (partner <= end)
|
|
321
|
+
break;
|
|
322
|
+
end = partner;
|
|
323
|
+
}
|
|
324
|
+
return { node: this.opaque(index, end + 1), next: end + 1 };
|
|
325
|
+
}
|
|
326
|
+
if (token.type === "identifier")
|
|
327
|
+
return this.word(index, limit);
|
|
328
|
+
if (this.is(index, "(")) {
|
|
329
|
+
let close = this.match[index] ?? index;
|
|
330
|
+
let arrow = this.arrowAfterParameters(close, limit);
|
|
331
|
+
if (arrow !== null)
|
|
332
|
+
return this.arrowBody(index, arrow, limit);
|
|
333
|
+
let inner = this.expression(index + 1, close).node;
|
|
334
|
+
return { node: inner, next: close + 1 };
|
|
335
|
+
}
|
|
336
|
+
if (this.is(index, "{"))
|
|
337
|
+
return this.object(index);
|
|
338
|
+
if (this.is(index, "[")) {
|
|
339
|
+
let close = this.match[index] ?? index;
|
|
340
|
+
return {
|
|
341
|
+
node: { kind: "array", elements: this.list(index, close), ...this.range(index, close + 1) },
|
|
342
|
+
next: close + 1,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
return { node: this.opaque(index, index + 1), next: index + 1 };
|
|
346
|
+
}
|
|
347
|
+
/** An identifier, keyword-led expression (`import()`, `function`, `new`, `async`) or arrow. */
|
|
348
|
+
word(index, limit) {
|
|
349
|
+
let value = this.tokens[index]?.value ?? "";
|
|
350
|
+
if (value === "import" && this.is(index + 1, "(")) {
|
|
351
|
+
let close = this.match[index + 1] ?? index + 1;
|
|
352
|
+
let specifier = this.tokens[index + 2];
|
|
353
|
+
let node = specifier?.type === "string"
|
|
354
|
+
? { kind: "import", specifier: cook(specifier.value), ...this.range(index, close + 1) }
|
|
355
|
+
: this.opaque(index, close + 1);
|
|
356
|
+
return { node, next: close + 1 };
|
|
357
|
+
}
|
|
358
|
+
if (value === "async" && !this.tokens[index + 1]?.newlineBefore) {
|
|
359
|
+
if (this.isWord(index + 1, "function")) {
|
|
360
|
+
let declared = this.functionDeclaration(index, limit);
|
|
361
|
+
if (declared !== null)
|
|
362
|
+
return { node: declared.node, next: this.after(declared.node) };
|
|
363
|
+
}
|
|
364
|
+
if (this.is(index + 1, "(")) {
|
|
365
|
+
let close = this.match[index + 1] ?? index + 1;
|
|
366
|
+
let arrow = this.arrowAfterParameters(close, limit);
|
|
367
|
+
if (arrow !== null)
|
|
368
|
+
return this.arrowBody(index, arrow, limit);
|
|
369
|
+
}
|
|
370
|
+
if (this.tokens[index + 1]?.type === "identifier" && this.is(index + 2, "=>")) {
|
|
371
|
+
return this.arrowBody(index, index + 2, limit);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (value === "function") {
|
|
375
|
+
let declared = this.functionDeclaration(index, limit);
|
|
376
|
+
if (declared !== null)
|
|
377
|
+
return { node: declared.node, next: this.after(declared.node) };
|
|
378
|
+
}
|
|
379
|
+
if (value === "new") {
|
|
380
|
+
let read = this.postfix(index + 1, limit);
|
|
381
|
+
return { node: this.opaque(index, read.next), next: read.next };
|
|
382
|
+
}
|
|
383
|
+
if (this.is(index + 1, "=>"))
|
|
384
|
+
return this.arrowBody(index, index + 1, limit);
|
|
385
|
+
return {
|
|
386
|
+
node: { kind: "identifier", name: value, ...this.range(index, index + 1) },
|
|
387
|
+
next: index + 1,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
/** The index of `=>` after a parameter list closing at `close`, past a return type; or `null`. */
|
|
391
|
+
arrowAfterParameters(close, limit) {
|
|
392
|
+
let index = close + 1;
|
|
393
|
+
if (this.is(index, ":"))
|
|
394
|
+
index = this.skipType(index + 1, limit, "=>");
|
|
395
|
+
return this.is(index, "=>") ? index : null;
|
|
396
|
+
}
|
|
397
|
+
/** The arrow function starting at `start` whose `=>` is at `arrow`. */
|
|
398
|
+
arrowBody(start, arrow, limit) {
|
|
399
|
+
if (this.is(arrow + 1, "{")) {
|
|
400
|
+
let close = this.match[arrow + 1] ?? arrow + 1;
|
|
401
|
+
return {
|
|
402
|
+
node: {
|
|
403
|
+
kind: "function",
|
|
404
|
+
body: this.returned(arrow + 1, close),
|
|
405
|
+
...this.range(start, close + 1),
|
|
406
|
+
},
|
|
407
|
+
next: close + 1,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
let body = this.expression(arrow + 1, limit);
|
|
411
|
+
return {
|
|
412
|
+
node: { kind: "function", body: body.node, ...this.range(start, body.next) },
|
|
413
|
+
next: body.next,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/** The object literal opening at `open`. */
|
|
417
|
+
object(open) {
|
|
418
|
+
let close = this.match[open] ?? open;
|
|
419
|
+
let entries = [];
|
|
420
|
+
let index = open + 1;
|
|
421
|
+
while (index < close) {
|
|
422
|
+
if (this.is(index, "...")) {
|
|
423
|
+
let read = this.expression(index + 1, close);
|
|
424
|
+
entries.push({ spread: read.node });
|
|
425
|
+
index = this.afterComma(read.next, close);
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
let key = this.tokens[index];
|
|
429
|
+
let modifier = key?.type === "identifier" &&
|
|
430
|
+
["get", "set", "async"].includes(key.value) &&
|
|
431
|
+
!this.is(index + 1, ":") &&
|
|
432
|
+
!this.is(index + 1, ",") &&
|
|
433
|
+
!this.is(index + 1, "(") &&
|
|
434
|
+
index + 1 < close;
|
|
435
|
+
if (modifier || this.is(index, "*") || this.is(index, "[")) {
|
|
436
|
+
index = this.afterComma(this.skipToDelimiter(index + 1, close), close);
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
let name = key?.type === "identifier" || key?.type === "number"
|
|
440
|
+
? key.value
|
|
441
|
+
: key?.type === "string"
|
|
442
|
+
? cook(key.value)
|
|
443
|
+
: null;
|
|
444
|
+
if (name === null) {
|
|
445
|
+
index = this.afterComma(this.skipToDelimiter(index, close), close);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (this.is(index + 1, "?"))
|
|
449
|
+
index += 1;
|
|
450
|
+
if (this.is(index + 1, ":")) {
|
|
451
|
+
let read = this.expression(index + 2, close);
|
|
452
|
+
entries.push({ key: name, value: read.node });
|
|
453
|
+
index = this.afterComma(read.next, close);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (this.is(index + 1, ",") || index + 1 === close) {
|
|
457
|
+
entries.push({
|
|
458
|
+
key: name,
|
|
459
|
+
value: { kind: "identifier", name, ...this.range(index, index + 1) },
|
|
460
|
+
});
|
|
461
|
+
index = this.afterComma(index + 1, close);
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
index = this.afterComma(this.skipToDelimiter(index + 1, close), close);
|
|
465
|
+
}
|
|
466
|
+
return { node: { kind: "object", entries, ...this.range(open, close + 1) }, next: close + 1 };
|
|
467
|
+
}
|
|
468
|
+
/** The comma-separated elements between the bracket at `open` and `close`. */
|
|
469
|
+
list(open, close) {
|
|
470
|
+
let elements = [];
|
|
471
|
+
let index = open + 1;
|
|
472
|
+
while (index < close) {
|
|
473
|
+
if (this.is(index, ",")) {
|
|
474
|
+
index += 1;
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
let spread = this.is(index, "...");
|
|
478
|
+
let read = this.expression(spread ? index + 1 : index, close);
|
|
479
|
+
elements.push({ spread, node: read.node });
|
|
480
|
+
index = this.afterComma(read.next, close);
|
|
481
|
+
}
|
|
482
|
+
return elements;
|
|
483
|
+
}
|
|
484
|
+
/** The index after the comma that ends an entry at `index`, skipping anything unread. */
|
|
485
|
+
afterComma(index, close) {
|
|
486
|
+
let next = this.is(index, ",") ? index : this.skipToDelimiter(index, close);
|
|
487
|
+
return next < close ? next + 1 : close;
|
|
488
|
+
}
|
|
489
|
+
/** Past `as T`, `satisfies T` and `!` after an expression. */
|
|
490
|
+
skipTypeSuffix(index, limit) {
|
|
491
|
+
let next = index;
|
|
492
|
+
while (next < limit) {
|
|
493
|
+
if (this.isWord(next, "as") || this.isWord(next, "satisfies")) {
|
|
494
|
+
next = this.skipType(next + 1, limit);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (this.is(next, "!")) {
|
|
498
|
+
next += 1;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
return next;
|
|
504
|
+
}
|
|
505
|
+
/** Past a type annotation, stopping at a delimiter, `=`, or `stop`, outside its angle brackets. */
|
|
506
|
+
skipType(index, limit, stop = "=") {
|
|
507
|
+
let angles = 0;
|
|
508
|
+
let next = index;
|
|
509
|
+
while (next < limit) {
|
|
510
|
+
let token = this.tokens[next];
|
|
511
|
+
if (token === undefined)
|
|
512
|
+
break;
|
|
513
|
+
if (opens(token)) {
|
|
514
|
+
next = (this.match[next] ?? next) + 1;
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (angles === 0 && (this.isDelimiter(next) || this.is(next, "=") || this.is(next, stop))) {
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
if (this.is(next, "<"))
|
|
521
|
+
angles += 1;
|
|
522
|
+
if (this.is(next, ">") && angles > 0)
|
|
523
|
+
angles -= 1;
|
|
524
|
+
next += 1;
|
|
525
|
+
}
|
|
526
|
+
return next;
|
|
527
|
+
}
|
|
528
|
+
/** Past the type arguments opening at `index`, or `index` itself when they do not close. */
|
|
529
|
+
skipAngles(index, limit) {
|
|
530
|
+
let angles = 0;
|
|
531
|
+
let next = index;
|
|
532
|
+
while (next < limit) {
|
|
533
|
+
let token = this.tokens[next];
|
|
534
|
+
if (token === undefined)
|
|
535
|
+
break;
|
|
536
|
+
if (opens(token)) {
|
|
537
|
+
next = (this.match[next] ?? next) + 1;
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
if (this.isDelimiter(next) && !this.is(next, ","))
|
|
541
|
+
return index;
|
|
542
|
+
if (this.is(next, "<"))
|
|
543
|
+
angles += 1;
|
|
544
|
+
if (this.is(next, ">")) {
|
|
545
|
+
angles -= 1;
|
|
546
|
+
if (angles === 0)
|
|
547
|
+
return next + 1;
|
|
548
|
+
}
|
|
549
|
+
next += 1;
|
|
550
|
+
}
|
|
551
|
+
return index;
|
|
552
|
+
}
|
|
553
|
+
/** The index of the next delimiter at the depth of `index`, skipping bracketed code. */
|
|
554
|
+
skipToDelimiter(index, limit) {
|
|
555
|
+
let next = index;
|
|
556
|
+
while (next < limit) {
|
|
557
|
+
let token = this.tokens[next];
|
|
558
|
+
if (token === undefined)
|
|
559
|
+
break;
|
|
560
|
+
if (opens(token) && (this.match[next] ?? -1) > next) {
|
|
561
|
+
next = this.match[next] ?? next;
|
|
562
|
+
if (this.tokens[next]?.type === "template-middle")
|
|
563
|
+
continue;
|
|
564
|
+
next += 1;
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
if (this.isDelimiter(next))
|
|
568
|
+
break;
|
|
569
|
+
next += 1;
|
|
570
|
+
}
|
|
571
|
+
return Math.min(next, limit);
|
|
572
|
+
}
|
|
573
|
+
/** Whether the token at `index` ends an expression. */
|
|
574
|
+
isDelimiter(index) {
|
|
575
|
+
let token = this.tokens[index];
|
|
576
|
+
return token?.type === "punctuator" && DELIMITERS.has(token.value);
|
|
577
|
+
}
|
|
578
|
+
/** The token index right after a node's last token. */
|
|
579
|
+
after(node) {
|
|
580
|
+
let index = this.tokens.findIndex((token) => token.start >= node.end);
|
|
581
|
+
return index === -1 ? this.tokens.length : index;
|
|
582
|
+
}
|
|
583
|
+
/** An opaque node over the tokens `[start, end)`. */
|
|
584
|
+
opaque(start, end) {
|
|
585
|
+
return { kind: "opaque", ...this.range(start, end) };
|
|
586
|
+
}
|
|
587
|
+
/** The source range of the tokens `[start, end)`. */
|
|
588
|
+
range(start, end) {
|
|
589
|
+
let first = this.tokens[start];
|
|
590
|
+
let last = this.tokens[Math.max(start, end - 1)];
|
|
591
|
+
let from = first?.start ?? this.source.length;
|
|
592
|
+
return { start: from, end: end > start ? (last?.end ?? from) : from };
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/** Whether a token opens a bracket or a template substitution. */
|
|
596
|
+
function opens(token) {
|
|
597
|
+
if (token.type === "template-head" || token.type === "template-middle")
|
|
598
|
+
return true;
|
|
599
|
+
return (token.type === "punctuator" &&
|
|
600
|
+
(token.value === "(" || token.value === "[" || token.value === "{"));
|
|
601
|
+
}
|
|
602
|
+
/** Whether a token closes a bracket or a template substitution. */
|
|
603
|
+
function closes(token) {
|
|
604
|
+
if (token.type === "template-tail" || token.type === "template-middle")
|
|
605
|
+
return true;
|
|
606
|
+
return (token.type === "punctuator" &&
|
|
607
|
+
(token.value === ")" || token.value === "]" || token.value === "}"));
|
|
608
|
+
}
|
|
609
|
+
/** A string or template literal's value: quotes removed and the common escapes resolved. */
|
|
610
|
+
export function cook(raw) {
|
|
611
|
+
return raw
|
|
612
|
+
.slice(1, -1)
|
|
613
|
+
.replace(/\\(u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.)/g, (_, escape) => {
|
|
614
|
+
if (escape.startsWith("u{"))
|
|
615
|
+
return String.fromCodePoint(Number.parseInt(escape.slice(2, -1), 16));
|
|
616
|
+
if (escape.startsWith("u") || escape.startsWith("x")) {
|
|
617
|
+
return String.fromCharCode(Number.parseInt(escape.slice(1), 16));
|
|
618
|
+
}
|
|
619
|
+
return { n: "\n", t: "\t", r: "\r", "0": "\0" }[escape] ?? escape;
|
|
620
|
+
});
|
|
621
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads a router initializer: the global middleware of each `createRouter()`, and every
|
|
3
|
+
* `router.map(target, handler)` with the controller its handler loads, the guards `lazy()`
|
|
4
|
+
* runs first and the middleware the controller declares for each action.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Module, Project, Value } from "./modules.js";
|
|
10
|
+
import type { Node } from "./parse.js";
|
|
11
|
+
/** One middleware as written, and where it comes from. */
|
|
12
|
+
export interface MiddlewareEntry {
|
|
13
|
+
/** The expression as written, a long call shortened to `callee(…)`. */
|
|
14
|
+
name: string;
|
|
15
|
+
/**
|
|
16
|
+
* The project file (`app/http/middleware/page.ts`) or package (`@sdxc/logger/middleware`) the
|
|
17
|
+
* expression's name is imported from; `null` when the same file declares it.
|
|
18
|
+
*/
|
|
19
|
+
from: string | null;
|
|
20
|
+
}
|
|
21
|
+
/** The module a mapping loads and the export it serves. */
|
|
22
|
+
export interface ControllerRef {
|
|
23
|
+
/** Project-relative; `null` when the mapping could not be followed to a file. */
|
|
24
|
+
file: string | null;
|
|
25
|
+
/** `default`, a named export, or `null` for an inline controller or an unknown export. */
|
|
26
|
+
export: string | null;
|
|
27
|
+
}
|
|
28
|
+
/** What one mapping runs for one action. */
|
|
29
|
+
export interface Handler {
|
|
30
|
+
controller: ControllerRef;
|
|
31
|
+
guards: MiddlewareEntry[];
|
|
32
|
+
/** The controller's own middleware for this action, its chain first. */
|
|
33
|
+
middleware: MiddlewareEntry[];
|
|
34
|
+
}
|
|
35
|
+
/** A router's handler for a mapping, resolved per action key path. */
|
|
36
|
+
export interface MappedHandler {
|
|
37
|
+
/** The handler for the route reached by `path` inside the mapped target. */
|
|
38
|
+
forAction(path: readonly string[]): Promise<Handler>;
|
|
39
|
+
/** The handler expression, for diagnostics. */
|
|
40
|
+
text: string;
|
|
41
|
+
/**
|
|
42
|
+
* Each part that loads its controller with the router, as the action key path reaching it
|
|
43
|
+
* (`[]` for the whole handler). Parts behind `lazy()` or a dynamic `import()` load only when a
|
|
44
|
+
* request reaches them, and a handler that could not be followed reports none.
|
|
45
|
+
*/
|
|
46
|
+
eager: string[][];
|
|
47
|
+
}
|
|
48
|
+
/** One `router.map()` call. */
|
|
49
|
+
export interface Mapping {
|
|
50
|
+
/** The evaluated target: a route, a route map, or an object of routes. */
|
|
51
|
+
target: Value;
|
|
52
|
+
handler: MappedHandler;
|
|
53
|
+
/** The target expression, for diagnostics. */
|
|
54
|
+
text: string;
|
|
55
|
+
}
|
|
56
|
+
/** One router a file creates. */
|
|
57
|
+
export interface Router {
|
|
58
|
+
/** The initializer, project-relative. */
|
|
59
|
+
file: string;
|
|
60
|
+
middleware: MiddlewareEntry[];
|
|
61
|
+
mappings: Mapping[];
|
|
62
|
+
}
|
|
63
|
+
/** The routers `module` creates, in source order. */
|
|
64
|
+
export declare function readRouters(project: Project, module: Module): Promise<Router[]>;
|
|
65
|
+
/**
|
|
66
|
+
* The middleware an expression lists. Arrays are read element by element, and an array held
|
|
67
|
+
* in a top-level variable (`CMS_GUARDS`), directly or spread, is expanded to what it holds.
|
|
68
|
+
*/
|
|
69
|
+
export declare function middlewareList(project: Project, module: Module, node: Node, seen?: ReadonlySet<string>): Promise<MiddlewareEntry[]>;
|