@nifrajs/cli 5.17.0 → 5.18.0
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/CHANGELOG.md +50 -0
- package/dist/check-diagnostics.d.ts +7 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +3 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan-sql.d.ts +95 -0
- package/dist/check-scan-sql.d.ts.map +1 -0
- package/dist/check-scan-sql.js +1190 -0
- package/dist/check-scan-sql.js.map +1 -0
- package/dist/check-scan.d.ts +5 -93
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +20 -1207
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +9 -0
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts +2 -6
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +249 -80
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts +3 -0
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +18 -4
- package/dist/command-catalog.js.map +1 -1
- package/dist/frontend-guidance.js +1 -1
- package/dist/frontend-guidance.js.map +1 -1
- package/dist/internal/a11y-element.d.ts +33 -0
- package/dist/internal/a11y-element.d.ts.map +1 -0
- package/dist/internal/a11y-element.js +124 -0
- package/dist/internal/a11y-element.js.map +1 -0
- package/dist/internal/a11y-jsx.d.ts +12 -0
- package/dist/internal/a11y-jsx.d.ts.map +1 -0
- package/dist/internal/a11y-jsx.js +42 -0
- package/dist/internal/a11y-jsx.js.map +1 -0
- package/dist/internal/a11y-template.d.ts +11 -0
- package/dist/internal/a11y-template.d.ts.map +1 -0
- package/dist/internal/a11y-template.js +56 -0
- package/dist/internal/a11y-template.js.map +1 -0
- package/dist/internal/aria-data.d.ts +23 -0
- package/dist/internal/aria-data.d.ts.map +1 -0
- package/dist/internal/aria-data.js +220 -0
- package/dist/internal/aria-data.js.map +1 -0
- package/dist/internal/edit-distance.d.ts +3 -0
- package/dist/internal/edit-distance.d.ts.map +1 -0
- package/dist/internal/edit-distance.js +12 -0
- package/dist/internal/edit-distance.js.map +1 -0
- package/dist/internal/imported-as.d.ts +10 -0
- package/dist/internal/imported-as.d.ts.map +1 -0
- package/dist/internal/imported-as.js +23 -0
- package/dist/internal/imported-as.js.map +1 -0
- package/dist/internal/source-facts.d.ts +3 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +2 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/template-markup.d.ts +58 -0
- package/dist/internal/template-markup.d.ts.map +1 -0
- package/dist/internal/template-markup.js +221 -0
- package/dist/internal/template-markup.js.map +1 -0
- package/dist/internal/typescript-import.d.ts.map +1 -1
- package/dist/internal/typescript-import.js +11 -0
- package/dist/internal/typescript-import.js.map +1 -1
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +37 -23
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +5 -3
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-root.js +1 -1
- package/dist/mcp-root.js.map +1 -1
- package/dist/mcp-ws.d.ts.map +1 -1
- package/dist/mcp-ws.js +3 -2
- package/dist/mcp-ws.js.map +1 -1
- package/dist/migrate-layout-config.d.ts +63 -0
- package/dist/migrate-layout-config.d.ts.map +1 -0
- package/dist/migrate-layout-config.js +507 -0
- package/dist/migrate-layout-config.js.map +1 -0
- package/dist/migrate-layout-split.d.ts +46 -0
- package/dist/migrate-layout-split.d.ts.map +1 -0
- package/dist/migrate-layout-split.js +687 -0
- package/dist/migrate-layout-split.js.map +1 -0
- package/dist/migrate-layout.d.ts +4 -24
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +8 -1183
- package/dist/migrate-layout.js.map +1 -1
- package/dist/output-schemas.d.ts.map +1 -1
- package/dist/output-schemas.js +78 -10
- package/dist/output-schemas.js.map +1 -1
- package/dist/recipes/5.18.0.d.ts +9 -0
- package/dist/recipes/5.18.0.d.ts.map +1 -0
- package/dist/recipes/5.18.0.js +56 -0
- package/dist/recipes/5.18.0.js.map +1 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +2 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +6 -0
- package/dist/review.js.map +1 -1
- package/dist/rules/a11y.d.ts +5 -0
- package/dist/rules/a11y.d.ts.map +1 -0
- package/dist/rules/a11y.js +78 -0
- package/dist/rules/a11y.js.map +1 -0
- package/dist/rules/codes.d.ts +6 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +6 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/comment-markers.js +1 -1
- package/dist/rules/comment-markers.js.map +1 -1
- package/dist/rules/explanations.d.ts.map +1 -1
- package/dist/rules/explanations.js +30 -0
- package/dist/rules/explanations.js.map +1 -1
- package/dist/rules/host-exposure.d.ts +23 -0
- package/dist/rules/host-exposure.d.ts.map +1 -0
- package/dist/rules/host-exposure.js +380 -0
- package/dist/rules/host-exposure.js.map +1 -0
- package/dist/rules/internal-links.d.ts.map +1 -1
- package/dist/rules/internal-links.js +16 -141
- package/dist/rules/internal-links.js.map +1 -1
- package/dist/rules/islands.d.ts +54 -0
- package/dist/rules/islands.d.ts.map +1 -1
- package/dist/rules/islands.js +577 -80
- package/dist/rules/islands.js.map +1 -1
- package/dist/rules/security.d.ts +1 -0
- package/dist/rules/security.d.ts.map +1 -1
- package/dist/rules/security.js +34 -7
- package/dist/rules/security.js.map +1 -1
- package/dist/runtime-differences.js +3 -3
- package/dist/runtime-differences.js.map +1 -1
- package/dist/scaffold.d.ts +1 -1
- package/dist/scaffold.d.ts.map +1 -1
- package/dist/scaffold.js +2 -2
- package/dist/scaffold.js.map +1 -1
- package/dist/source-rewrites.d.ts.map +1 -1
- package/dist/source-rewrites.js +2 -19
- package/dist/source-rewrites.js.map +1 -1
- package/docs/examples.json +4 -4
- package/docs/llms-full.txt +422 -42
- package/docs/types.json +466 -56
- package/package.json +12 -12
- package/src/check-diagnostics.ts +11 -0
- package/src/check-scan-sql.ts +1746 -0
- package/src/check-scan.ts +34 -1754
- package/src/check.ts +10 -1
- package/src/cli.ts +258 -98
- package/src/command-catalog.ts +21 -4
- package/src/frontend-guidance.ts +1 -1
- package/src/internal/a11y-element.ts +227 -0
- package/src/internal/a11y-jsx.ts +54 -0
- package/src/internal/a11y-template.ts +74 -0
- package/src/internal/aria-data.ts +242 -0
- package/src/internal/edit-distance.ts +16 -0
- package/src/internal/imported-as.ts +30 -0
- package/src/internal/source-facts.ts +4 -0
- package/src/internal/template-markup.ts +270 -0
- package/src/internal/typescript-import.ts +15 -0
- package/src/mcp-exec.ts +43 -22
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +5 -3
- package/src/mcp-root.ts +1 -1
- package/src/mcp-ws.ts +3 -2
- package/src/migrate-layout-config.ts +615 -0
- package/src/migrate-layout-split.ts +870 -0
- package/src/migrate-layout.ts +37 -1468
- package/src/output-schemas.ts +85 -17
- package/src/recipes/5.18.0.ts +63 -0
- package/src/recipes/index.ts +2 -0
- package/src/review.ts +6 -0
- package/src/rules/a11y.ts +119 -0
- package/src/rules/codes.ts +6 -0
- package/src/rules/comment-markers.ts +2 -2
- package/src/rules/explanations.ts +30 -0
- package/src/rules/host-exposure.ts +531 -0
- package/src/rules/internal-links.ts +21 -154
- package/src/rules/islands.ts +698 -86
- package/src/rules/security.ts +48 -7
- package/src/runtime-differences.ts +3 -3
- package/src/scaffold.ts +3 -3
- package/src/source-rewrites.ts +2 -15
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
import { BACKEND_ROUTE_EXPORTS } from "@nifrajs/web/route-manifest";
|
|
2
|
+
import { codeUnitOrder } from "./internal/code-unit-order.js";
|
|
3
|
+
import { escapeRegExp } from "./internal/escape-regexp.js";
|
|
4
|
+
const BACKEND_EXPORTS = BACKEND_ROUTE_EXPORTS;
|
|
5
|
+
function bindingNames(ts, name, into) {
|
|
6
|
+
if (ts.isIdentifier(name))
|
|
7
|
+
into.add(name.text);
|
|
8
|
+
else
|
|
9
|
+
for (const element of name.elements)
|
|
10
|
+
if (!ts.isOmittedExpression(element))
|
|
11
|
+
bindingNames(ts, element.name, into);
|
|
12
|
+
}
|
|
13
|
+
export const hasModifier = (ts, node, kind) => (ts.canHaveModifiers(node) ? (ts.getModifiers(node) ?? []) : []).some((m) => m.kind === kind);
|
|
14
|
+
function jsxBindings(ts, source) {
|
|
15
|
+
const comments = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
16
|
+
.map(({ pos, end }) => source.slice(pos, end))
|
|
17
|
+
.join("\n");
|
|
18
|
+
if (/@jsxImportSource\s/.test(comments))
|
|
19
|
+
return { element: [], fragment: [] };
|
|
20
|
+
const root = (pragma) => new RegExp(`@${pragma}\\s+([A-Za-z_$][\\w$]*)`).exec(comments)?.[1];
|
|
21
|
+
const element = root("jsx") ?? "React";
|
|
22
|
+
return { element: [element], fragment: [root("jsxFrag") ?? element] };
|
|
23
|
+
}
|
|
24
|
+
function references(ts, node, jsx) {
|
|
25
|
+
const value = new Set();
|
|
26
|
+
const type = new Set();
|
|
27
|
+
const visit = (current, inType) => {
|
|
28
|
+
if (ts.isJsxOpeningElement(current) || ts.isJsxSelfClosingElement(current))
|
|
29
|
+
for (const name of jsx.element)
|
|
30
|
+
value.add(name);
|
|
31
|
+
else if (ts.isJsxFragment(current))
|
|
32
|
+
for (const name of jsx.fragment)
|
|
33
|
+
value.add(name);
|
|
34
|
+
const typed = inType ||
|
|
35
|
+
ts.isTypeNode(current) ||
|
|
36
|
+
(ts.isHeritageClause(current) && current.token === ts.SyntaxKind.ImplementsKeyword);
|
|
37
|
+
if (ts.isIdentifier(current)) {
|
|
38
|
+
const parent = current.parent;
|
|
39
|
+
const isName = parent !== undefined &&
|
|
40
|
+
((ts.isPropertyAccessExpression(parent) && parent.name === current) ||
|
|
41
|
+
(ts.isPropertyAssignment(parent) && parent.name === current) ||
|
|
42
|
+
(ts.isPropertySignature(parent) && parent.name === current) ||
|
|
43
|
+
(ts.isPropertyDeclaration(parent) && parent.name === current) ||
|
|
44
|
+
(ts.isMethodDeclaration(parent) && parent.name === current) ||
|
|
45
|
+
(ts.isMethodSignature(parent) && parent.name === current) ||
|
|
46
|
+
(ts.isQualifiedName(parent) && parent.right === current) ||
|
|
47
|
+
(ts.isJsxAttribute(parent) && parent.name === current) ||
|
|
48
|
+
(ts.isBindingElement(parent) && parent.propertyName === current) ||
|
|
49
|
+
ts.isImportSpecifier(parent) ||
|
|
50
|
+
ts.isExportSpecifier(parent) ||
|
|
51
|
+
ts.isImportClause(parent) ||
|
|
52
|
+
ts.isNamespaceImport(parent));
|
|
53
|
+
if (!isName)
|
|
54
|
+
(typed ? type : value).add(current.text);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (ts.isShorthandPropertyAssignment(current))
|
|
58
|
+
value.add(current.name.text);
|
|
59
|
+
ts.forEachChild(current, (child) => visit(child, typed));
|
|
60
|
+
};
|
|
61
|
+
visit(node, false);
|
|
62
|
+
return { value, type };
|
|
63
|
+
}
|
|
64
|
+
function describeStatements(ts, sf) {
|
|
65
|
+
const jsx = jsxBindings(ts, sf.text);
|
|
66
|
+
return sf.statements.map((node) => {
|
|
67
|
+
const declares = new Set();
|
|
68
|
+
const exports = new Map();
|
|
69
|
+
const listed = [];
|
|
70
|
+
const exported = hasModifier(ts, node, ts.SyntaxKind.ExportKeyword);
|
|
71
|
+
const isDefault = hasModifier(ts, node, ts.SyntaxKind.DefaultKeyword);
|
|
72
|
+
const addExport = (local, as) => {
|
|
73
|
+
exports.set(local, [...(exports.get(local) ?? []), as]);
|
|
74
|
+
};
|
|
75
|
+
if (ts.isImportDeclaration(node)) {
|
|
76
|
+
const clause = node.importClause;
|
|
77
|
+
if (clause?.name)
|
|
78
|
+
declares.add(clause.name.text);
|
|
79
|
+
const bindings = clause?.namedBindings;
|
|
80
|
+
if (bindings && ts.isNamespaceImport(bindings))
|
|
81
|
+
declares.add(bindings.name.text);
|
|
82
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
83
|
+
for (const el of bindings.elements)
|
|
84
|
+
declares.add(el.name.text);
|
|
85
|
+
}
|
|
86
|
+
else if (ts.isVariableStatement(node)) {
|
|
87
|
+
for (const decl of node.declarationList.declarations) {
|
|
88
|
+
const names = new Set();
|
|
89
|
+
bindingNames(ts, decl.name, names);
|
|
90
|
+
for (const name of names) {
|
|
91
|
+
declares.add(name);
|
|
92
|
+
if (exported)
|
|
93
|
+
addExport(name, name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (ts.isFunctionDeclaration(node) ||
|
|
98
|
+
ts.isClassDeclaration(node) ||
|
|
99
|
+
ts.isInterfaceDeclaration(node) ||
|
|
100
|
+
ts.isTypeAliasDeclaration(node) ||
|
|
101
|
+
ts.isEnumDeclaration(node)) {
|
|
102
|
+
const name = node.name?.text;
|
|
103
|
+
if (name !== undefined)
|
|
104
|
+
declares.add(name);
|
|
105
|
+
if (exported)
|
|
106
|
+
addExport(name ?? "default", isDefault ? "default" : (name ?? "default"));
|
|
107
|
+
}
|
|
108
|
+
else if (ts.isExportAssignment(node)) {
|
|
109
|
+
addExport("default", "default");
|
|
110
|
+
}
|
|
111
|
+
else if (ts.isExportDeclaration(node) &&
|
|
112
|
+
node.exportClause &&
|
|
113
|
+
ts.isNamedExports(node.exportClause)) {
|
|
114
|
+
for (const el of node.exportClause.elements) {
|
|
115
|
+
const local = (el.propertyName ?? el.name).text;
|
|
116
|
+
addExport(node.moduleSpecifier ? `\0${el.name.text}` : local, el.name.text);
|
|
117
|
+
if (node.moduleSpecifier === undefined)
|
|
118
|
+
listed.push({ exported: el.name.text, local, type: node.isTypeOnly || el.isTypeOnly });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const refs = ts.isImportDeclaration(node)
|
|
122
|
+
? { value: new Set(), type: new Set() }
|
|
123
|
+
: references(ts, node, jsx);
|
|
124
|
+
for (const name of declares) {
|
|
125
|
+
refs.value.delete(name);
|
|
126
|
+
refs.type.delete(name);
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
node,
|
|
130
|
+
text: node.getFullText(sf),
|
|
131
|
+
declares,
|
|
132
|
+
exports,
|
|
133
|
+
valueRefs: refs.value,
|
|
134
|
+
typeRefs: refs.type,
|
|
135
|
+
listed,
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function filterImport(ts, sf, node, uses, fresh) {
|
|
140
|
+
const clause = node.importClause;
|
|
141
|
+
const spec = node.moduleSpecifier.getText(sf);
|
|
142
|
+
const attributes = node.attributes ? ` ${node.attributes.getText(sf)}` : "";
|
|
143
|
+
if (clause === undefined)
|
|
144
|
+
return node.getFullText(sf);
|
|
145
|
+
const heads = [];
|
|
146
|
+
const named = [];
|
|
147
|
+
let total = 0;
|
|
148
|
+
const add = (into, name, text, bare) => {
|
|
149
|
+
total++;
|
|
150
|
+
if (uses.value.has(name) || uses.type.has(name))
|
|
151
|
+
into.push({ text, bare, typeOnly: !uses.value.has(name) });
|
|
152
|
+
};
|
|
153
|
+
if (clause.name)
|
|
154
|
+
add(heads, clause.name.text, clause.name.text, clause.name.text);
|
|
155
|
+
const bindings = clause.namedBindings;
|
|
156
|
+
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
157
|
+
const text = `* as ${bindings.name.text}`;
|
|
158
|
+
add(heads, bindings.name.text, text, text);
|
|
159
|
+
}
|
|
160
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
161
|
+
for (const el of bindings.elements) {
|
|
162
|
+
const renamed = el.propertyName ? `${el.propertyName.getText(sf)} as ` : "";
|
|
163
|
+
add(named, el.name.text, el.getText(sf), `${renamed}${el.name.text}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const kept = [...heads, ...named];
|
|
167
|
+
if (kept.length === 0)
|
|
168
|
+
return undefined;
|
|
169
|
+
const leading = node.getFullText(sf).slice(0, node.getStart(sf) - node.getFullStart());
|
|
170
|
+
const statement = (typeOnly, body) => `import ${typeOnly ? "type " : ""}${body} from ${spec}${attributes}${terminatorOf(sf, node)}`;
|
|
171
|
+
const list = (parts, text) => parts.length === 0 ? [] : [`{ ${parts.map(text).join(", ")} }`];
|
|
172
|
+
if (!clause.isTypeOnly &&
|
|
173
|
+
node.attributes === undefined &&
|
|
174
|
+
(fresh || kept.length < total) &&
|
|
175
|
+
kept.every((part) => part.typeOnly)) {
|
|
176
|
+
const bodies = [...heads.map((part) => part.bare), ...list(named, (part) => part.bare)];
|
|
177
|
+
return `${leading}${bodies.map((body) => statement(true, body)).join("\n")}`;
|
|
178
|
+
}
|
|
179
|
+
const body = [...heads.map((part) => part.text), ...list(named, (part) => part.text)];
|
|
180
|
+
return `${leading}${statement(clause.isTypeOnly, body.join(", "))}`;
|
|
181
|
+
}
|
|
182
|
+
export const terminatorOf = (sf, node) => node.getText(sf).endsWith(";") ? ";" : "";
|
|
183
|
+
function filterExportList(ts, sf, node, keep) {
|
|
184
|
+
const clause = node.exportClause;
|
|
185
|
+
if (!clause || !ts.isNamedExports(clause))
|
|
186
|
+
return node.getFullText(sf);
|
|
187
|
+
const kept = clause.elements.filter((el) => keep(el.name.text)).map((el) => el.getText(sf));
|
|
188
|
+
if (kept.length === 0)
|
|
189
|
+
return undefined;
|
|
190
|
+
const from = node.moduleSpecifier ? ` from ${node.moduleSpecifier.getText(sf)}` : "";
|
|
191
|
+
const leading = node.getFullText(sf).slice(0, node.getStart(sf) - node.getFullStart());
|
|
192
|
+
return `${leading}export ${node.isTypeOnly ? "type " : ""}{ ${kept.join(", ")} }${from}${terminatorOf(sf, node)}`;
|
|
193
|
+
}
|
|
194
|
+
const joinStatements = (texts) => {
|
|
195
|
+
const body = texts.join("").replace(/^\s*\n/, "");
|
|
196
|
+
return body.endsWith("\n") ? body : `${body}\n`;
|
|
197
|
+
};
|
|
198
|
+
const PURE_OBJECT_CALLS = new Set(["keys", "values", "entries", "freeze", "fromEntries"]);
|
|
199
|
+
function dataBlocker(ts, node, isLocal, reads) {
|
|
200
|
+
const data = (n) => {
|
|
201
|
+
if (ts.isStringLiteral(n) ||
|
|
202
|
+
ts.isNumericLiteral(n) ||
|
|
203
|
+
ts.isBigIntLiteral(n) ||
|
|
204
|
+
ts.isNoSubstitutionTemplateLiteral(n) ||
|
|
205
|
+
n.kind === ts.SyntaxKind.TrueKeyword ||
|
|
206
|
+
n.kind === ts.SyntaxKind.FalseKeyword ||
|
|
207
|
+
n.kind === ts.SyntaxKind.NullKeyword)
|
|
208
|
+
return undefined;
|
|
209
|
+
if (ts.isIdentifier(n)) {
|
|
210
|
+
if (isLocal(n.text)) {
|
|
211
|
+
reads.add(n.text);
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
return n.text === "undefined" ? undefined : n.text;
|
|
215
|
+
}
|
|
216
|
+
const every = (nodes) => {
|
|
217
|
+
for (const child of nodes) {
|
|
218
|
+
const blocker = data(child);
|
|
219
|
+
if (blocker !== undefined)
|
|
220
|
+
return blocker;
|
|
221
|
+
}
|
|
222
|
+
return undefined;
|
|
223
|
+
};
|
|
224
|
+
if (ts.isTemplateExpression(n))
|
|
225
|
+
return every(n.templateSpans.map((span) => span.expression));
|
|
226
|
+
if (ts.isPrefixUnaryExpression(n))
|
|
227
|
+
return n.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
228
|
+
n.operator === ts.SyntaxKind.MinusMinusToken
|
|
229
|
+
? ""
|
|
230
|
+
: data(n.operand);
|
|
231
|
+
if (ts.isBinaryExpression(n)) {
|
|
232
|
+
const operator = n.operatorToken.kind;
|
|
233
|
+
const assigns = operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment;
|
|
234
|
+
if (assigns ||
|
|
235
|
+
operator === ts.SyntaxKind.CommaToken ||
|
|
236
|
+
operator === ts.SyntaxKind.InKeyword ||
|
|
237
|
+
operator === ts.SyntaxKind.InstanceOfKeyword)
|
|
238
|
+
return "";
|
|
239
|
+
return every([n.left, n.right]);
|
|
240
|
+
}
|
|
241
|
+
if (ts.isConditionalExpression(n))
|
|
242
|
+
return every([n.condition, n.whenTrue, n.whenFalse]);
|
|
243
|
+
if (ts.isArrayLiteralExpression(n))
|
|
244
|
+
return every(n.elements.flatMap((element) => ts.isOmittedExpression(element)
|
|
245
|
+
? []
|
|
246
|
+
: [ts.isSpreadElement(element) ? element.expression : element]));
|
|
247
|
+
if (ts.isObjectLiteralExpression(n)) {
|
|
248
|
+
for (const property of n.properties) {
|
|
249
|
+
const blocker = ts.isPropertyAssignment(property)
|
|
250
|
+
? ((ts.isComputedPropertyName(property.name)
|
|
251
|
+
? data(property.name.expression)
|
|
252
|
+
: undefined) ?? data(property.initializer))
|
|
253
|
+
: ts.isShorthandPropertyAssignment(property)
|
|
254
|
+
? property.objectAssignmentInitializer === undefined
|
|
255
|
+
? data(property.name)
|
|
256
|
+
: ""
|
|
257
|
+
: ts.isSpreadAssignment(property)
|
|
258
|
+
? data(property.expression)
|
|
259
|
+
: "";
|
|
260
|
+
if (blocker !== undefined)
|
|
261
|
+
return blocker;
|
|
262
|
+
}
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
if (ts.isParenthesizedExpression(n) ||
|
|
266
|
+
ts.isAsExpression(n) ||
|
|
267
|
+
ts.isSatisfiesExpression(n) ||
|
|
268
|
+
ts.isNonNullExpression(n) ||
|
|
269
|
+
ts.isTypeAssertionExpression(n))
|
|
270
|
+
return data(n.expression);
|
|
271
|
+
if (ts.isPropertyAccessExpression(n))
|
|
272
|
+
return ts.isPrivateIdentifier(n.name) ? "" : data(n.expression);
|
|
273
|
+
if (ts.isElementAccessExpression(n))
|
|
274
|
+
return every([n.expression, n.argumentExpression]);
|
|
275
|
+
if (ts.isCallExpression(n)) {
|
|
276
|
+
const callee = n.expression;
|
|
277
|
+
const objectRead = ts.isPropertyAccessExpression(callee) &&
|
|
278
|
+
ts.isIdentifier(callee.expression) &&
|
|
279
|
+
callee.expression.text === "Object" &&
|
|
280
|
+
!isLocal("Object") &&
|
|
281
|
+
PURE_OBJECT_CALLS.has(callee.name.text) &&
|
|
282
|
+
n.questionDotToken === undefined &&
|
|
283
|
+
n.typeArguments === undefined;
|
|
284
|
+
return objectRead ? every(n.arguments) : "";
|
|
285
|
+
}
|
|
286
|
+
return "";
|
|
287
|
+
};
|
|
288
|
+
return data(node);
|
|
289
|
+
}
|
|
290
|
+
export function splitRouteSource(ts, source, fileName, backendSpecifier, options = {}) {
|
|
291
|
+
const kind = fileName.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
292
|
+
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, kind);
|
|
293
|
+
const statements = describeStatements(ts, sf);
|
|
294
|
+
const issues = [];
|
|
295
|
+
const exportsOf = (info) => [...info.exports.values()].flat();
|
|
296
|
+
const isBackendStatement = (info) => exportsOf(info).some((name) => BACKEND_EXPORTS.has(name));
|
|
297
|
+
const moved = statements.flatMap(exportsOf).filter((name) => BACKEND_EXPORTS.has(name));
|
|
298
|
+
if (moved.length === 0)
|
|
299
|
+
return { frontend: source, backend: undefined, moved: [], issues: [] };
|
|
300
|
+
const declaredBy = new Map();
|
|
301
|
+
const importedBy = new Map();
|
|
302
|
+
for (const info of statements) {
|
|
303
|
+
const into = ts.isImportDeclaration(info.node) ? importedBy : declaredBy;
|
|
304
|
+
for (const name of info.declares)
|
|
305
|
+
into.set(name, info);
|
|
306
|
+
}
|
|
307
|
+
const isTypeDeclaration = (info) => ts.isInterfaceDeclaration(info.node) || ts.isTypeAliasDeclaration(info.node);
|
|
308
|
+
const readsFor = (info, backend) => {
|
|
309
|
+
if (info.listed.length === 0)
|
|
310
|
+
return { value: info.valueRefs, type: info.typeRefs };
|
|
311
|
+
const mine = info.listed.filter((entry) => BACKEND_EXPORTS.has(entry.exported) === backend);
|
|
312
|
+
return {
|
|
313
|
+
value: new Set(mine.filter((entry) => !entry.type).map((entry) => entry.local)),
|
|
314
|
+
type: new Set(mine.filter((entry) => entry.type).map((entry) => entry.local)),
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
const movedLocals = new Set();
|
|
318
|
+
for (const info of statements) {
|
|
319
|
+
for (const [local, names] of info.exports)
|
|
320
|
+
if (names.some((name) => BACKEND_EXPORTS.has(name)))
|
|
321
|
+
movedLocals.add(local);
|
|
322
|
+
}
|
|
323
|
+
const partition = (shared) => {
|
|
324
|
+
const local = (name) => {
|
|
325
|
+
const target = declaredBy.get(name);
|
|
326
|
+
return target === undefined || shared.has(target) ? undefined : target;
|
|
327
|
+
};
|
|
328
|
+
const backendSet = new Set();
|
|
329
|
+
const queue = statements.filter(isBackendStatement);
|
|
330
|
+
for (const info of queue)
|
|
331
|
+
backendSet.add(info);
|
|
332
|
+
while (queue.length > 0) {
|
|
333
|
+
const info = queue.pop();
|
|
334
|
+
if (info === undefined)
|
|
335
|
+
continue;
|
|
336
|
+
const reads = readsFor(info, true);
|
|
337
|
+
for (const name of [...reads.value, ...reads.type]) {
|
|
338
|
+
const target = local(name);
|
|
339
|
+
if (target !== undefined && !backendSet.has(target)) {
|
|
340
|
+
backendSet.add(target);
|
|
341
|
+
queue.push(target);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const frontendKeep = new Set();
|
|
346
|
+
const seed = statements.filter((info) => !ts.isImportDeclaration(info.node) &&
|
|
347
|
+
!shared.has(info) &&
|
|
348
|
+
(!backendSet.has(info) ||
|
|
349
|
+
exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name)) ||
|
|
350
|
+
(info.declares.size === 0 && info.exports.size === 0)));
|
|
351
|
+
const fqueue = [...seed];
|
|
352
|
+
for (const info of fqueue)
|
|
353
|
+
frontendKeep.add(info);
|
|
354
|
+
while (fqueue.length > 0) {
|
|
355
|
+
const info = fqueue.pop();
|
|
356
|
+
if (info === undefined)
|
|
357
|
+
continue;
|
|
358
|
+
const reads = readsFor(info, false);
|
|
359
|
+
for (const name of reads.value) {
|
|
360
|
+
if (movedLocals.has(name) && !info.declares.has(name))
|
|
361
|
+
continue;
|
|
362
|
+
const target = local(name);
|
|
363
|
+
if (target !== undefined && !frontendKeep.has(target) && !isBackendStatement(target)) {
|
|
364
|
+
frontendKeep.add(target);
|
|
365
|
+
fqueue.push(target);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const name of reads.type) {
|
|
369
|
+
if (movedLocals.has(name))
|
|
370
|
+
continue;
|
|
371
|
+
const target = local(name);
|
|
372
|
+
if (target !== undefined && !frontendKeep.has(target) && !isBackendStatement(target)) {
|
|
373
|
+
frontendKeep.add(target);
|
|
374
|
+
fqueue.push(target);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return { backendSet, frontendKeep };
|
|
379
|
+
};
|
|
380
|
+
let { backendSet, frontendKeep } = partition(new Set());
|
|
381
|
+
const copies = () => [...frontendKeep].filter((info) => backendSet.has(info) &&
|
|
382
|
+
!isBackendStatement(info) &&
|
|
383
|
+
info.declares.size > 0 &&
|
|
384
|
+
!isTypeDeclaration(info));
|
|
385
|
+
const shared = new Set();
|
|
386
|
+
const sharedTypes = new Set();
|
|
387
|
+
const sharedImports = new Set();
|
|
388
|
+
const unshared = new Map();
|
|
389
|
+
if (options.sharedFile !== undefined) {
|
|
390
|
+
const gather = (info, closure) => {
|
|
391
|
+
if (closure.values.has(info) || shared.has(info))
|
|
392
|
+
return undefined;
|
|
393
|
+
const node = info.node;
|
|
394
|
+
const reads = new Set();
|
|
395
|
+
let blocker = "";
|
|
396
|
+
if (!isBackendStatement(info) &&
|
|
397
|
+
ts.isVariableStatement(node) &&
|
|
398
|
+
(node.declarationList.flags & ts.NodeFlags.Const) !== 0 &&
|
|
399
|
+
!hasModifier(ts, node, ts.SyntaxKind.DeclareKeyword)) {
|
|
400
|
+
blocker = undefined;
|
|
401
|
+
for (const declaration of node.declarationList.declarations) {
|
|
402
|
+
blocker =
|
|
403
|
+
ts.isIdentifier(declaration.name) && declaration.initializer !== undefined
|
|
404
|
+
? dataBlocker(ts, declaration.initializer, (name) => declaredBy.has(name), reads)
|
|
405
|
+
: "";
|
|
406
|
+
if (blocker !== undefined)
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (blocker !== undefined)
|
|
411
|
+
return blocker !== "" && importedBy.has(blocker)
|
|
412
|
+
? `it reads the import "${blocker}"`
|
|
413
|
+
: "it is not constant data";
|
|
414
|
+
closure.values.add(info);
|
|
415
|
+
for (const name of reads) {
|
|
416
|
+
const target = declaredBy.get(name);
|
|
417
|
+
const why = target === undefined ? undefined : gather(target, closure);
|
|
418
|
+
if (why !== undefined)
|
|
419
|
+
return `it reads "${name}", which ${why.slice(3)}`;
|
|
420
|
+
}
|
|
421
|
+
for (const name of info.typeRefs) {
|
|
422
|
+
const why = gatherType(name, closure);
|
|
423
|
+
if (why !== undefined)
|
|
424
|
+
return why;
|
|
425
|
+
}
|
|
426
|
+
return undefined;
|
|
427
|
+
};
|
|
428
|
+
const gatherType = (name, closure) => {
|
|
429
|
+
const local = declaredBy.get(name);
|
|
430
|
+
if (local === undefined) {
|
|
431
|
+
if (importedBy.has(name))
|
|
432
|
+
closure.imports.add(name);
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
if (!isTypeDeclaration(local)) {
|
|
436
|
+
const why = gather(local, closure);
|
|
437
|
+
return why === undefined ? undefined : `its type reads "${name}", which ${why.slice(3)}`;
|
|
438
|
+
}
|
|
439
|
+
if (closure.types.has(local) || sharedTypes.has(local))
|
|
440
|
+
return undefined;
|
|
441
|
+
closure.types.add(local);
|
|
442
|
+
for (const ref of [...local.typeRefs, ...local.valueRefs]) {
|
|
443
|
+
const why = gatherType(ref, closure);
|
|
444
|
+
if (why !== undefined)
|
|
445
|
+
return why;
|
|
446
|
+
}
|
|
447
|
+
return undefined;
|
|
448
|
+
};
|
|
449
|
+
for (const info of copies()) {
|
|
450
|
+
const closure = { values: new Set(), types: new Set(), imports: new Set() };
|
|
451
|
+
const why = gather(info, closure);
|
|
452
|
+
if (why !== undefined) {
|
|
453
|
+
unshared.set(info, why);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
for (const value of closure.values)
|
|
457
|
+
shared.add(value);
|
|
458
|
+
for (const type of closure.types)
|
|
459
|
+
sharedTypes.add(type);
|
|
460
|
+
for (const name of closure.imports)
|
|
461
|
+
sharedImports.add(name);
|
|
462
|
+
}
|
|
463
|
+
if (shared.size > 0)
|
|
464
|
+
({ backendSet, frontendKeep } = partition(shared));
|
|
465
|
+
}
|
|
466
|
+
const typeImports = new Set();
|
|
467
|
+
for (const info of frontendKeep) {
|
|
468
|
+
if (isBackendStatement(info))
|
|
469
|
+
continue;
|
|
470
|
+
const reads = readsFor(info, false);
|
|
471
|
+
for (const name of movedLocals) {
|
|
472
|
+
if (info.declares.has(name))
|
|
473
|
+
continue;
|
|
474
|
+
if (reads.value.has(name))
|
|
475
|
+
issues.push(`the frontend uses "${name}" at runtime, but it runs on the server only`);
|
|
476
|
+
if (reads.type.has(name) || reads.value.has(name))
|
|
477
|
+
typeImports.add(name);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
for (const info of copies()) {
|
|
481
|
+
const why = unshared.get(info);
|
|
482
|
+
issues.push(`"${[...info.declares].join(", ")}" is needed by both halves and was copied into each${why === undefined ? "" : `, since ${why}`}; consider moving it to shared/`);
|
|
483
|
+
}
|
|
484
|
+
const uses = (infos, backend) => {
|
|
485
|
+
const value = new Set();
|
|
486
|
+
const type = new Set();
|
|
487
|
+
for (const info of infos) {
|
|
488
|
+
const reads = readsFor(info, backend);
|
|
489
|
+
for (const name of reads.value)
|
|
490
|
+
value.add(name);
|
|
491
|
+
for (const name of reads.type)
|
|
492
|
+
type.add(name);
|
|
493
|
+
}
|
|
494
|
+
return { value, type };
|
|
495
|
+
};
|
|
496
|
+
const backendUses = uses(backendSet, true);
|
|
497
|
+
const frontendUses = uses(frontendKeep, false);
|
|
498
|
+
const semi = statements.some((info) => terminatorOf(sf, info.node) !== "") ? ";" : "";
|
|
499
|
+
const firstImport = statements.find((info) => ts.isImportDeclaration(info.node))?.node;
|
|
500
|
+
const quote = firstImport !== undefined && ts.isImportDeclaration(firstImport)
|
|
501
|
+
? (firstImport.moduleSpecifier.getText(sf)[0] ?? '"')
|
|
502
|
+
: '"';
|
|
503
|
+
const sharedNames = [...shared].flatMap((info) => [...info.declares]);
|
|
504
|
+
const sharedSpecifier = (() => {
|
|
505
|
+
const file = options.sharedFile ?? "";
|
|
506
|
+
const relative = statements.flatMap((info) => ts.isImportDeclaration(info.node) &&
|
|
507
|
+
ts.isStringLiteral(info.node.moduleSpecifier) &&
|
|
508
|
+
info.node.moduleSpecifier.text.startsWith(".")
|
|
509
|
+
? [info.node.moduleSpecifier.text]
|
|
510
|
+
: []);
|
|
511
|
+
const extension = relative
|
|
512
|
+
.map((specifier) => /\.(ts|tsx|js|jsx)$/.exec(specifier)?.[1])
|
|
513
|
+
.find((found) => found !== undefined);
|
|
514
|
+
const suffix = extension === undefined ? "" : extension.startsWith("t") ? ".ts" : ".js";
|
|
515
|
+
return `./${file.replace(/\.ts$/, "")}${suffix}`;
|
|
516
|
+
})();
|
|
517
|
+
const sharedImport = (half) => {
|
|
518
|
+
const names = sharedNames
|
|
519
|
+
.filter((name) => half.value.has(name) || half.type.has(name))
|
|
520
|
+
.sort(codeUnitOrder);
|
|
521
|
+
if (names.length === 0)
|
|
522
|
+
return [];
|
|
523
|
+
const typeOnly = names.every((name) => !half.value.has(name));
|
|
524
|
+
return [
|
|
525
|
+
`\nimport ${typeOnly ? "type " : ""}{ ${names.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`,
|
|
526
|
+
];
|
|
527
|
+
};
|
|
528
|
+
const reexported = [...shared].flatMap((info) => [...info.exports].flatMap(([local, names]) => names.map((name) => (name === local ? local : `${local} as ${name}`))));
|
|
529
|
+
const backendTexts = [];
|
|
530
|
+
const frontendTexts = [];
|
|
531
|
+
let afterImports = 0;
|
|
532
|
+
let backendAfterImports = 0;
|
|
533
|
+
for (const info of statements) {
|
|
534
|
+
const node = info.node;
|
|
535
|
+
if (ts.isImportDeclaration(node)) {
|
|
536
|
+
const back = filterImport(ts, sf, node, backendUses, true);
|
|
537
|
+
if (back !== undefined)
|
|
538
|
+
backendTexts.push(node.importClause ? back : "");
|
|
539
|
+
const front = filterImport(ts, sf, node, frontendUses, false);
|
|
540
|
+
if (front !== undefined)
|
|
541
|
+
frontendTexts.push(front);
|
|
542
|
+
afterImports = frontendTexts.length;
|
|
543
|
+
backendAfterImports = backendTexts.length;
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
if (isBackendStatement(info)) {
|
|
547
|
+
if (ts.isExportDeclaration(node) &&
|
|
548
|
+
exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name))) {
|
|
549
|
+
backendTexts.push(filterExportList(ts, sf, node, (name) => BACKEND_EXPORTS.has(name)) ?? "");
|
|
550
|
+
frontendTexts.push(filterExportList(ts, sf, node, (name) => !BACKEND_EXPORTS.has(name)) ?? "");
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (exportsOf(info).some((name) => !BACKEND_EXPORTS.has(name))) {
|
|
554
|
+
issues.push(`one statement exports both "${exportsOf(info).join('", "')}"; split it by hand`);
|
|
555
|
+
}
|
|
556
|
+
backendTexts.push(info.text);
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (backendSet.has(info))
|
|
560
|
+
backendTexts.push(stripExport(ts, sf, info));
|
|
561
|
+
if (frontendKeep.has(info))
|
|
562
|
+
frontendTexts.push(info.text);
|
|
563
|
+
}
|
|
564
|
+
const frontendImports = sharedImport(frontendUses);
|
|
565
|
+
if (reexported.length > 0)
|
|
566
|
+
frontendImports.push(`\nexport { ${reexported.join(", ")} } from ${quote}${sharedSpecifier}${quote}${semi}`);
|
|
567
|
+
if (typeImports.size > 0)
|
|
568
|
+
frontendImports.push(`\nimport type { ${[...typeImports].sort().join(", ")} } from ${quote}${backendSpecifier}${quote}${semi}`);
|
|
569
|
+
frontendTexts.splice(afterImports, 0, ...frontendImports);
|
|
570
|
+
backendTexts.splice(backendAfterImports, 0, ...sharedImport(backendUses));
|
|
571
|
+
const pragmas = (ts.getLeadingCommentRanges(source, 0) ?? [])
|
|
572
|
+
.map((range) => source.slice(range.pos, range.end))
|
|
573
|
+
.filter((comment) => /@jsx/.test(comment));
|
|
574
|
+
const lift = (text) => pragmas.reduce((out, pragma) => out.replace(new RegExp(`${escapeRegExp(pragma)}[ \\t]*\\n?`), ""), text);
|
|
575
|
+
const frontend = lift(joinStatements(frontendTexts));
|
|
576
|
+
let sharedText;
|
|
577
|
+
if (shared.size > 0) {
|
|
578
|
+
const texts = [];
|
|
579
|
+
const typeUses = { value: new Set(), type: sharedImports };
|
|
580
|
+
for (const info of statements) {
|
|
581
|
+
if (ts.isImportDeclaration(info.node)) {
|
|
582
|
+
const kept = filterImport(ts, sf, info.node, typeUses, true);
|
|
583
|
+
if (kept !== undefined && info.node.importClause !== undefined)
|
|
584
|
+
texts.push(kept);
|
|
585
|
+
}
|
|
586
|
+
else if (shared.has(info) || sharedTypes.has(info)) {
|
|
587
|
+
texts.push(withExport(ts, sf, info));
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
sharedText = lift(joinStatements(texts));
|
|
591
|
+
}
|
|
592
|
+
const result = {
|
|
593
|
+
frontend: pragmas.length > 0 ? `${pragmas.join("\n")}\n${frontend}` : frontend,
|
|
594
|
+
backend: lift(joinStatements(backendTexts)),
|
|
595
|
+
moved: [...new Set(moved)].sort(),
|
|
596
|
+
issues,
|
|
597
|
+
};
|
|
598
|
+
return sharedText === undefined ? result : { ...result, shared: sharedText };
|
|
599
|
+
}
|
|
600
|
+
function withExport(ts, sf, info) {
|
|
601
|
+
if (hasModifier(ts, info.node, ts.SyntaxKind.ExportKeyword))
|
|
602
|
+
return info.text;
|
|
603
|
+
const at = info.node.getStart(sf) - info.node.getFullStart();
|
|
604
|
+
return `${info.text.slice(0, at)}export ${info.text.slice(at)}`;
|
|
605
|
+
}
|
|
606
|
+
function stripExport(ts, sf, info) {
|
|
607
|
+
if (info.exports.size === 0)
|
|
608
|
+
return info.text;
|
|
609
|
+
const node = info.node;
|
|
610
|
+
const exportKeyword = (ts.canHaveModifiers(node) ? (ts.getModifiers(node) ?? []) : []).find((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
611
|
+
if (exportKeyword === undefined)
|
|
612
|
+
return info.text;
|
|
613
|
+
const start = exportKeyword.getStart(sf) - node.getFullStart();
|
|
614
|
+
const end = exportKeyword.getEnd() - node.getFullStart();
|
|
615
|
+
return info.text.slice(0, start) + info.text.slice(end).replace(/^\s+/, "");
|
|
616
|
+
}
|
|
617
|
+
export function middlewareSource(ts, source, fileName) {
|
|
618
|
+
const sf = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
619
|
+
let out = source;
|
|
620
|
+
for (const node of [...sf.statements].reverse()) {
|
|
621
|
+
if (ts.isExportAssignment(node) && !node.isExportEquals) {
|
|
622
|
+
out = `${out.slice(0, node.getStart(sf))}export const middleware = ${node.expression.getText(sf)}${terminatorOf(sf, node)}${out.slice(node.getEnd())}`;
|
|
623
|
+
}
|
|
624
|
+
else if (ts.isFunctionDeclaration(node) &&
|
|
625
|
+
hasModifier(ts, node, ts.SyntaxKind.DefaultKeyword)) {
|
|
626
|
+
const modifiers = (ts.getModifiers(node) ?? []).filter((m) => m.kind === ts.SyntaxKind.ExportKeyword || m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
627
|
+
const first = modifiers[0];
|
|
628
|
+
const last = modifiers[modifiers.length - 1];
|
|
629
|
+
if (first === undefined || last === undefined)
|
|
630
|
+
continue;
|
|
631
|
+
if (node.name === undefined) {
|
|
632
|
+
const text = out
|
|
633
|
+
.slice(last.getEnd(), node.getEnd())
|
|
634
|
+
.replace(/^(\s*(?:async\s+)?function\s*\*?)\s*/, "$1 middleware");
|
|
635
|
+
out = `${out.slice(0, first.getStart(sf))}export${text}${out.slice(node.getEnd())}`;
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
out = `${out.slice(0, first.getStart(sf))}${out.slice(last.getEnd()).replace(/^\s+/, "")}`;
|
|
639
|
+
const semi = sf.statements.some((s) => terminatorOf(sf, s) !== "") ? ";" : "";
|
|
640
|
+
out = `${out.trimEnd()}\nexport { ${node.name.text} as middleware }${semi}\n`;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return out;
|
|
645
|
+
}
|
|
646
|
+
export function mergeModules(ts, first, second) {
|
|
647
|
+
if (first.trim() === "")
|
|
648
|
+
return second;
|
|
649
|
+
const split = (text) => {
|
|
650
|
+
const sf = ts.createSourceFile("merge.ts", text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
651
|
+
const imports = [];
|
|
652
|
+
const body = [];
|
|
653
|
+
for (const node of sf.statements) {
|
|
654
|
+
;
|
|
655
|
+
(ts.isImportDeclaration(node) ? imports : body).push(node.getFullText(sf).replace(/^\s*\n/, ""));
|
|
656
|
+
}
|
|
657
|
+
return { imports, body };
|
|
658
|
+
};
|
|
659
|
+
const a = split(first);
|
|
660
|
+
const b = split(second);
|
|
661
|
+
const imports = [...new Set([...a.imports, ...b.imports].map((text) => text.trim()))];
|
|
662
|
+
const body = [...a.body, ...b.body].map((text) => text.trim()).join("\n\n");
|
|
663
|
+
return `${imports.join("\n")}${imports.length > 0 ? "\n\n" : ""}${body}\n`;
|
|
664
|
+
}
|
|
665
|
+
export function scriptBlocks(source) {
|
|
666
|
+
const blocks = [];
|
|
667
|
+
const pattern = /<script\b([^>]*)>([\s\S]*?)<\/script(?=[\s/>])[^>]*>/gi;
|
|
668
|
+
const visible = source.replace(/<!--[\s\S]*?-->/g, (comment) => comment.replace(/[^\n]/g, " "));
|
|
669
|
+
for (const match of visible.matchAll(pattern)) {
|
|
670
|
+
const attrs = match[1] ?? "";
|
|
671
|
+
const start = (match.index ?? 0) + match[0].indexOf(">") + 1;
|
|
672
|
+
blocks.push({
|
|
673
|
+
start,
|
|
674
|
+
end: start + (match[2] ?? "").length,
|
|
675
|
+
module: /\bmodule\b|context=["']module["']/.test(attrs),
|
|
676
|
+
setup: /\bsetup\b/.test(attrs),
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
return blocks;
|
|
680
|
+
}
|
|
681
|
+
export function applyEdits(text, edits) {
|
|
682
|
+
let out = text;
|
|
683
|
+
for (const edit of edits.sort((a, b) => b.start - a.start))
|
|
684
|
+
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
685
|
+
return out;
|
|
686
|
+
}
|
|
687
|
+
//# sourceMappingURL=migrate-layout-split.js.map
|