@kerfjs/ui 5.0.0-beta.129 → 5.0.0-beta.130
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/ai/component-catalog.json +14 -9
- package/ai/component-composition-rules.json +1 -1
- package/ai/component-composition.d.ts +17 -0
- package/ai/component-composition.json +4 -3
- package/ai/component-composition.schema.json +326 -80
- package/ai/component-props-overlay.json +8568 -628
- package/ai/component-props-overlay.schema.json +15 -1
- package/ai/component-props.json +6221 -2069
- package/ai/component-props.schema.json +7 -0
- package/ai/components/radio-group.md +5 -1
- package/ai/components/state-banner.md +1 -1
- package/ai/components/surface-scaffold.md +3 -2
- package/ai/components/tab-bar.md +1 -1
- package/ai/components/toolbar-control-group.md +1 -1
- package/ai/public-api-signatures-v1.md +205 -42
- package/ai/skill.md +21 -8
- package/ai/view-document.schema.json +140 -86
- package/ai/webawesome-jsx-signatures-v1.md +1 -1
- package/dist/browser/column-explorer.js +5 -1
- package/dist/browser/radio-group.js +1 -0
- package/dist/catalog-resources.d.ts +2 -2
- package/dist/catalog.d.ts +2 -2
- package/dist/catalog.js +5 -5
- package/dist/{chunk-E2MTHHLS.js → chunk-2FA3SX57.js} +2 -2
- package/dist/{chunk-E2MTHHLS.js.map → chunk-2FA3SX57.js.map} +1 -1
- package/dist/{chunk-ZC4KSRAF.js → chunk-4CYCJ3NY.js} +6 -4
- package/dist/chunk-4CYCJ3NY.js.map +1 -0
- package/dist/{chunk-IMXLOMTF.js → chunk-76BLSFEA.js} +30 -12
- package/dist/chunk-76BLSFEA.js.map +1 -0
- package/dist/{chunk-IR32NI2O.js → chunk-ALBFHXMZ.js} +3 -3
- package/dist/chunk-ALBFHXMZ.js.map +1 -0
- package/dist/{chunk-3ZWYGGEC.js → chunk-ASU4ODOH.js} +38 -7
- package/dist/chunk-ASU4ODOH.js.map +1 -0
- package/dist/chunk-BMUYZKXW.js +155 -0
- package/dist/chunk-BMUYZKXW.js.map +1 -0
- package/dist/{chunk-GQUDCWOD.js → chunk-D2KHQNJ6.js} +49 -23
- package/dist/chunk-D2KHQNJ6.js.map +1 -0
- package/dist/{chunk-34FWYJX6.js → chunk-ECJHCPBN.js} +8 -2
- package/dist/chunk-ECJHCPBN.js.map +1 -0
- package/dist/{chunk-DBCDYBZP.js → chunk-HHEBNP6Z.js} +2 -2
- package/dist/{chunk-DBCDYBZP.js.map → chunk-HHEBNP6Z.js.map} +1 -1
- package/dist/{chunk-3ZKUXWAV.js → chunk-IPYQYLSX.js} +16 -9
- package/dist/chunk-IPYQYLSX.js.map +1 -0
- package/dist/{chunk-SJEPM5ZU.js → chunk-LETAE6SX.js} +2 -2
- package/dist/{chunk-SJEPM5ZU.js.map → chunk-LETAE6SX.js.map} +1 -1
- package/dist/chunk-MVWR5WRX.js +99 -0
- package/dist/chunk-MVWR5WRX.js.map +1 -0
- package/dist/{chunk-6T7AX365.js → chunk-OSL4LQ7T.js} +52 -23
- package/dist/chunk-OSL4LQ7T.js.map +1 -0
- package/dist/{chunk-OHT2AXER.js → chunk-QSJAIXLM.js} +19 -3
- package/dist/chunk-QSJAIXLM.js.map +1 -0
- package/dist/{chunk-UQ4FRQ6M.js → chunk-R4JK2C6D.js} +13 -3
- package/dist/chunk-R4JK2C6D.js.map +1 -0
- package/dist/{chunk-5A2DN3LR.js → chunk-SGYBIKQA.js} +3 -1
- package/dist/chunk-SGYBIKQA.js.map +1 -0
- package/dist/{chunk-K3C35KGR.js → chunk-XO5IOPM5.js} +2 -2
- package/dist/{chunk-K3C35KGR.js.map → chunk-XO5IOPM5.js.map} +1 -1
- package/dist/collapsible-panel.d.ts +2 -2
- package/dist/collapsible-panel.js +2 -2
- package/dist/column-explorer.d.ts +5 -1
- package/dist/column-explorer.js +5 -1
- package/dist/column-track.d.ts +3 -1
- package/dist/column-track.js +1 -1
- package/dist/editor.d.ts +87 -2
- package/dist/editor.js +2249 -1320
- package/dist/editor.js.map +1 -1
- package/dist/index.js +12 -12
- package/dist/list-detail-dialog.d.ts +2 -2
- package/dist/list-detail-dialog.js +4 -4
- package/dist/list-header.d.ts +47 -5
- package/dist/list-header.js +1 -1
- package/dist/message-composer.js +1 -1
- package/dist/{nav-stack-vQ3ieerq.d.ts → nav-stack-DKrvqFjA.d.ts} +1 -1
- package/dist/nav-stack.d.ts +2 -2
- package/dist/nav-stack.js +1 -1
- package/dist/{panel-toolbar-vg6Ir5Dy.d.ts → panel-toolbar-CEcHHDkn.d.ts} +8 -0
- package/dist/radio-group.d.ts +19 -2
- package/dist/radio-group.js +3 -1
- package/dist/split-view.d.ts +2 -2
- package/dist/split-view.js +1 -1
- package/dist/styles/column-track.css +7 -2
- package/dist/styles/components/actions/toolbar-control-group/toolbar-control-group.css +11 -4
- package/dist/styles/components/collections/list-header/list-header.css +50 -0
- package/dist/styles/components/collections/list-item/list-item.css +6 -1
- package/dist/styles/components/collections/tree-list/tree-list.css +3 -2
- package/dist/styles/components/forms/radio-group/radio-group.css +107 -0
- package/dist/styles/components/layout/column-track/column-track.css +7 -2
- package/dist/styles/components/surfaces/surface-scaffold/surface-scaffold.css +30 -1
- package/dist/styles/editor/editor.css +0 -23
- package/dist/styles/list-header.css +50 -0
- package/dist/styles/list-item.css +6 -1
- package/dist/styles/radio-group.css +107 -0
- package/dist/styles/surface-scaffold.css +30 -1
- package/dist/styles/toolbar-control-group.css +11 -4
- package/dist/styles/tree-list.css +3 -2
- package/dist/surface-scaffold-register.js +1 -1
- package/dist/surface-scaffold.d.ts +46 -11
- package/dist/surface-scaffold.js +2 -2
- package/dist/toolbar-control-group.d.ts +10 -1
- package/dist/toolbar-control-group.js +1 -1
- package/dist/tree-list.d.ts +7 -1
- package/dist/tree-list.js +1 -1
- package/dist/view-runtime.d.ts +9 -0
- package/dist/view-runtime.js +8 -0
- package/dist/view-runtime.js.map +1 -0
- package/dist/wire-catalog.d.ts +2 -2
- package/dist/wire-catalog.js +1 -1
- package/dist/wire-search-fields.js +1 -1
- package/dist/wire-tab-bars.d.ts +10 -0
- package/dist/wire-tab-bars.js +1 -1
- package/dist/wire-tag-fields.js +1 -1
- package/dist/wire-workbench.d.ts +17 -1
- package/dist/wire-workbench.js +1 -1
- package/dist/workbench-dialog.d.ts +2 -2
- package/dist/workbench-dialog.js +4 -4
- package/dist/workbench.d.ts +10 -2
- package/dist/workbench.js +1 -1
- package/docs/accessibility.md +1 -1
- package/docs/column-explorer.md +4 -0
- package/docs/component-contract.md +10 -4
- package/docs/component-integration.md +7 -3
- package/docs/component-selection.md +2 -1
- package/docs/design/sketch/Kerf-UI.sketch +0 -0
- package/docs/design/sketch/coverage.md +7 -6
- package/docs/design/sketch/forms/README.md +5 -4
- package/docs/design/sketch/unified/pages/forms.png +0 -0
- package/docs/design/templates/radio-group/tiles-dark.svg +1 -0
- package/docs/design/templates/radio-group/tiles.svg +1 -0
- package/docs/design/templates/radio-group-dark.svg +1 -1
- package/docs/design/templates/radio-group.svg +1 -1
- package/docs/editor-host.md +90 -5
- package/docs/interaction-conventions.md +2 -0
- package/docs/layout.md +1 -1
- package/docs/surface-scaffold.md +49 -2
- package/docs/tree-list.md +10 -0
- package/docs/workbench.md +16 -0
- package/evaluator/firefox-app-data.d.mts +4 -0
- package/evaluator/firefox-app-data.mjs +35 -0
- package/evaluator/index.mjs +19 -5
- package/llms.txt +20 -4
- package/package.json +8 -1
- package/props/component-props.d.mts +3 -0
- package/view/cli.mjs +36 -5
- package/view/contract.mjs +3 -0
- package/view/generate.mjs +8 -7
- package/view/index.d.mts +14 -0
- package/view/project-types-worker.mjs +55 -0
- package/view/project-types.d.mts +43 -0
- package/view/project-types.mjs +618 -0
- package/view/type-expression.d.mts +15 -0
- package/view/type-expression.mjs +162 -0
- package/view/types.mjs +4 -0
- package/view/validate.mjs +20 -1
- package/vite-editor.d.mts +31 -3
- package/vite-editor.mjs +168 -4
- package/dist/chunk-34FWYJX6.js.map +0 -1
- package/dist/chunk-3ZKUXWAV.js.map +0 -1
- package/dist/chunk-3ZWYGGEC.js.map +0 -1
- package/dist/chunk-5A2DN3LR.js.map +0 -1
- package/dist/chunk-6T7AX365.js.map +0 -1
- package/dist/chunk-BMOZSNDU.js +0 -133
- package/dist/chunk-BMOZSNDU.js.map +0 -1
- package/dist/chunk-GQUDCWOD.js.map +0 -1
- package/dist/chunk-IMXLOMTF.js.map +0 -1
- package/dist/chunk-IR32NI2O.js.map +0 -1
- package/dist/chunk-MLDO7WLA.js +0 -82
- package/dist/chunk-MLDO7WLA.js.map +0 -1
- package/dist/chunk-OHT2AXER.js.map +0 -1
- package/dist/chunk-UQ4FRQ6M.js.map +0 -1
- package/dist/chunk-ZC4KSRAF.js.map +0 -1
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
// Node-only TypeScript language service for parameter expressions. This module
|
|
2
|
+
// is loaded by the dev-server/CLI, never by the browser view runtime.
|
|
3
|
+
import { readFileSync, realpathSync, statSync } from 'node:fs';
|
|
4
|
+
import {
|
|
5
|
+
basename,
|
|
6
|
+
dirname,
|
|
7
|
+
isAbsolute,
|
|
8
|
+
relative,
|
|
9
|
+
resolve,
|
|
10
|
+
sep,
|
|
11
|
+
} from 'node:path';
|
|
12
|
+
|
|
13
|
+
import ts from 'typescript';
|
|
14
|
+
|
|
15
|
+
import { createPropsContext, describeType } from '../props/component-props.mjs';
|
|
16
|
+
import { generateView } from './generate.mjs';
|
|
17
|
+
import { propertyName, typeText } from './contract.mjs';
|
|
18
|
+
import { validateView } from './validate.mjs';
|
|
19
|
+
import {
|
|
20
|
+
containedTypeExpression,
|
|
21
|
+
hasTypeExpressions,
|
|
22
|
+
typeExpressionText,
|
|
23
|
+
typeSourcePath,
|
|
24
|
+
} from './type-expression.mjs';
|
|
25
|
+
|
|
26
|
+
const PREFIX = 'type __KerfParameter = ';
|
|
27
|
+
const SUFFIX = ';\nexport {};\n';
|
|
28
|
+
const message = (item) =>
|
|
29
|
+
ts.flattenDiagnosticMessageText(item.messageText, '\n');
|
|
30
|
+
const posix = (path) => path.split(sep).join('/');
|
|
31
|
+
const sourceModule = (from, file) => {
|
|
32
|
+
const path = posix(relative(dirname(from), file)).replace(
|
|
33
|
+
/\.(?:d\.)?[cm]?tsx?$/,
|
|
34
|
+
'.js',
|
|
35
|
+
);
|
|
36
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
37
|
+
};
|
|
38
|
+
const inside = (root, file) => {
|
|
39
|
+
const path = relative(root, file);
|
|
40
|
+
return path !== '..' && !path.startsWith(`..${sep}`) && !isAbsolute(path);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const printType = (node, source) => {
|
|
44
|
+
const normalized = ts.transform(node, [
|
|
45
|
+
(context) => {
|
|
46
|
+
const visit = (item) => {
|
|
47
|
+
if (
|
|
48
|
+
ts.isImportTypeNode(item) &&
|
|
49
|
+
ts.isLiteralTypeNode(item.argument) &&
|
|
50
|
+
ts.isStringLiteral(item.argument.literal)
|
|
51
|
+
) {
|
|
52
|
+
return ts.factory.updateImportTypeNode(
|
|
53
|
+
item,
|
|
54
|
+
ts.factory.createLiteralTypeNode(
|
|
55
|
+
ts.factory.createStringLiteral(item.argument.literal.text),
|
|
56
|
+
),
|
|
57
|
+
item.attributes,
|
|
58
|
+
item.qualifier,
|
|
59
|
+
item.typeArguments,
|
|
60
|
+
item.isTypeOf,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return ts.visitEachChild(item, visit, context);
|
|
64
|
+
};
|
|
65
|
+
return (item) => ts.visitNode(item, visit);
|
|
66
|
+
},
|
|
67
|
+
]);
|
|
68
|
+
const result = ts
|
|
69
|
+
.createPrinter()
|
|
70
|
+
.printNode(ts.EmitHint.Unspecified, normalized.transformed[0], source);
|
|
71
|
+
normalized.dispose();
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** The nearest config owns a file's project context for CLI checks. */
|
|
76
|
+
export function projectTypeRoot(path) {
|
|
77
|
+
const config = ts.findConfigFile(
|
|
78
|
+
dirname(resolve(path)),
|
|
79
|
+
ts.sys.fileExists,
|
|
80
|
+
'tsconfig.json',
|
|
81
|
+
);
|
|
82
|
+
return config ? dirname(config) : dirname(resolve(path));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function inlineShape(type, definitions, seen = new Set(), depth = 0) {
|
|
86
|
+
if (type.kind === 'ref') {
|
|
87
|
+
if (seen.has(type.ref) || depth > 8)
|
|
88
|
+
return { kind: 'unknown', text: type.text };
|
|
89
|
+
return inlineShape(
|
|
90
|
+
definitions.get(type.ref) ?? { kind: 'unknown' },
|
|
91
|
+
definitions,
|
|
92
|
+
new Set([...seen, type.ref]),
|
|
93
|
+
depth + 1,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return Object.fromEntries(
|
|
97
|
+
Object.entries(type).map(([key, value]) => [
|
|
98
|
+
key,
|
|
99
|
+
Array.isArray(value)
|
|
100
|
+
? value.map((item) =>
|
|
101
|
+
item?.kind
|
|
102
|
+
? inlineShape(item, definitions, seen, depth + 1)
|
|
103
|
+
: item?.type
|
|
104
|
+
? {
|
|
105
|
+
...item,
|
|
106
|
+
type: inlineShape(item.type, definitions, seen, depth + 1),
|
|
107
|
+
}
|
|
108
|
+
: item,
|
|
109
|
+
)
|
|
110
|
+
: value?.kind
|
|
111
|
+
? inlineShape(value, definitions, seen, depth + 1)
|
|
112
|
+
: value,
|
|
113
|
+
]),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** One reusable language service rooted at the project's own tsconfig and files. */
|
|
118
|
+
export function createProjectTypeService(root) {
|
|
119
|
+
root = resolve(root);
|
|
120
|
+
const readConfig = () => {
|
|
121
|
+
const path = ts.findConfigFile(root, ts.sys.fileExists, 'tsconfig.json');
|
|
122
|
+
return path && inside(root, path)
|
|
123
|
+
? ts.getParsedCommandLineOfConfigFile(
|
|
124
|
+
path,
|
|
125
|
+
{},
|
|
126
|
+
{ ...ts.sys, onUnRecoverableConfigFileDiagnostic() {} },
|
|
127
|
+
)
|
|
128
|
+
: undefined;
|
|
129
|
+
};
|
|
130
|
+
let config = readConfig();
|
|
131
|
+
const compilerOptions = () => ({
|
|
132
|
+
target: ts.ScriptTarget.ES2022,
|
|
133
|
+
module: ts.ModuleKind.ESNext,
|
|
134
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
135
|
+
strict: true,
|
|
136
|
+
lib: ['lib.es2022.d.ts', 'lib.dom.d.ts'],
|
|
137
|
+
...config?.options,
|
|
138
|
+
noUnusedLocals: false,
|
|
139
|
+
noUnusedParameters: false,
|
|
140
|
+
skipLibCheck: true,
|
|
141
|
+
noEmit: true,
|
|
142
|
+
ignoreDeprecations: '6.0',
|
|
143
|
+
});
|
|
144
|
+
let options = compilerOptions();
|
|
145
|
+
let file = resolve(root, '.__kerf_parameter__.ts');
|
|
146
|
+
let content = '';
|
|
147
|
+
let version = 0;
|
|
148
|
+
// Auto-import completions compute a module specifier per project export,
|
|
149
|
+
// and each reads the nearest package.json (KF-SNM61J: about 1,300 reads of
|
|
150
|
+
// one file per request). Package manifests are read once per request.
|
|
151
|
+
let manifests = new Map();
|
|
152
|
+
const snapshots = new Map();
|
|
153
|
+
const host = {
|
|
154
|
+
...ts.sys,
|
|
155
|
+
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
|
|
156
|
+
getCurrentDirectory: () => root,
|
|
157
|
+
getCompilationSettings: () => options,
|
|
158
|
+
getDefaultLibFileName: (value) => ts.getDefaultLibFilePath(value),
|
|
159
|
+
getProjectVersion: () => String(version),
|
|
160
|
+
getScriptFileNames: () => [...(config?.fileNames ?? []), file],
|
|
161
|
+
getScriptVersion: (name) => {
|
|
162
|
+
if (name === file) return String(version);
|
|
163
|
+
try {
|
|
164
|
+
return String(statSync(name).mtimeMs);
|
|
165
|
+
} catch {
|
|
166
|
+
return 'missing';
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
// The language service asks for every program file's snapshot on each
|
|
170
|
+
// new project version, even when its script version is unchanged; reading
|
|
171
|
+
// all of them again cost about half a second per request (KF-SNM61J).
|
|
172
|
+
getScriptSnapshot: (name) => {
|
|
173
|
+
if (name === file) return ts.ScriptSnapshot.fromString(content);
|
|
174
|
+
try {
|
|
175
|
+
const mtime = statSync(name).mtimeMs;
|
|
176
|
+
const cached = snapshots.get(name);
|
|
177
|
+
if (cached?.mtime === mtime) return cached.snapshot;
|
|
178
|
+
const snapshot = ts.ScriptSnapshot.fromString(
|
|
179
|
+
readFileSync(name, 'utf8'),
|
|
180
|
+
);
|
|
181
|
+
snapshots.set(name, { mtime, snapshot });
|
|
182
|
+
return snapshot;
|
|
183
|
+
} catch {
|
|
184
|
+
snapshots.delete(name);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
fileExists: (name) => name === file || ts.sys.fileExists(name),
|
|
189
|
+
readFile: (name) => {
|
|
190
|
+
if (name === file) return content;
|
|
191
|
+
if (basename(name) !== 'package.json') return ts.sys.readFile(name);
|
|
192
|
+
if (!manifests.has(name)) manifests.set(name, ts.sys.readFile(name));
|
|
193
|
+
return manifests.get(name);
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
const service = ts.createLanguageService(host);
|
|
197
|
+
const set = (source, text) => {
|
|
198
|
+
if (!typeSourcePath(source) || !source.endsWith('.kui.json'))
|
|
199
|
+
throw new Error('A type needs a project-relative .kui.json source.');
|
|
200
|
+
file = resolve(root, dirname(source), '.__kerf_parameter__.ts');
|
|
201
|
+
config = readConfig();
|
|
202
|
+
options = compilerOptions();
|
|
203
|
+
content = text;
|
|
204
|
+
manifests = new Map();
|
|
205
|
+
version += 1;
|
|
206
|
+
};
|
|
207
|
+
const diagnostics = () => [
|
|
208
|
+
...(config?.errors ?? []).filter(
|
|
209
|
+
(item) => item.code !== 18002 && item.code !== 18003,
|
|
210
|
+
),
|
|
211
|
+
...service.getCompilerOptionsDiagnostics(),
|
|
212
|
+
...service.getSyntacticDiagnostics(file),
|
|
213
|
+
...service.getSemanticDiagnostics(file),
|
|
214
|
+
];
|
|
215
|
+
const expression = () => {
|
|
216
|
+
const source = service.getProgram().getSourceFile(file);
|
|
217
|
+
const [alias, marker] = source.statements;
|
|
218
|
+
if (
|
|
219
|
+
source.parseDiagnostics.length ||
|
|
220
|
+
source.statements.length !== 2 ||
|
|
221
|
+
!ts.isTypeAliasDeclaration(alias) ||
|
|
222
|
+
alias.name.text !== '__KerfParameter' ||
|
|
223
|
+
!ts.isExportDeclaration(marker) ||
|
|
224
|
+
marker.exportClause?.elements?.length !== 0
|
|
225
|
+
)
|
|
226
|
+
return undefined;
|
|
227
|
+
return { source, alias };
|
|
228
|
+
};
|
|
229
|
+
const exportsByName = () => {
|
|
230
|
+
const program = service.getProgram();
|
|
231
|
+
const checker = program.getTypeChecker();
|
|
232
|
+
const found = new Map();
|
|
233
|
+
for (const source of program.getSourceFiles()) {
|
|
234
|
+
if (
|
|
235
|
+
source.fileName === file ||
|
|
236
|
+
!inside(root, source.fileName) ||
|
|
237
|
+
source.fileName.includes(`${sep}node_modules${sep}`)
|
|
238
|
+
)
|
|
239
|
+
continue;
|
|
240
|
+
const symbol = checker.getSymbolAtLocation(source);
|
|
241
|
+
if (!symbol) continue;
|
|
242
|
+
for (const item of checker.getExportsOfModule(symbol)) {
|
|
243
|
+
const actual =
|
|
244
|
+
item.flags & ts.SymbolFlags.Alias
|
|
245
|
+
? checker.getAliasedSymbol(item)
|
|
246
|
+
: item;
|
|
247
|
+
if (
|
|
248
|
+
!(
|
|
249
|
+
actual.flags &
|
|
250
|
+
(ts.SymbolFlags.Type |
|
|
251
|
+
ts.SymbolFlags.Namespace |
|
|
252
|
+
ts.SymbolFlags.Value)
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
continue;
|
|
256
|
+
const entries = found.get(item.name) ?? [];
|
|
257
|
+
if (!entries.some((entry) => entry.symbol === actual))
|
|
258
|
+
entries.push({ source: source.fileName, symbol: actual });
|
|
259
|
+
found.set(item.name, entries);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return found;
|
|
263
|
+
};
|
|
264
|
+
const referenceTokens = (node, source, start) => {
|
|
265
|
+
const references = [];
|
|
266
|
+
const walk = (child) => {
|
|
267
|
+
if (
|
|
268
|
+
ts.isImportTypeNode(child) &&
|
|
269
|
+
ts.isLiteralTypeNode(child.argument) &&
|
|
270
|
+
ts.isStringLiteral(child.argument.literal)
|
|
271
|
+
) {
|
|
272
|
+
const literal = child.argument.literal;
|
|
273
|
+
const module = literal.text;
|
|
274
|
+
if (
|
|
275
|
+
isAbsolute(module) ||
|
|
276
|
+
(module.startsWith('.') &&
|
|
277
|
+
!inside(root, resolve(dirname(file), module)))
|
|
278
|
+
)
|
|
279
|
+
throw new Error(
|
|
280
|
+
'A parameter type import must stay inside the project.',
|
|
281
|
+
);
|
|
282
|
+
if (module.startsWith('.')) {
|
|
283
|
+
const resolved = ts.resolveModuleName(
|
|
284
|
+
module,
|
|
285
|
+
file,
|
|
286
|
+
options,
|
|
287
|
+
host,
|
|
288
|
+
).resolvedModule;
|
|
289
|
+
if (
|
|
290
|
+
resolved &&
|
|
291
|
+
!inside(realpathSync(root), realpathSync(resolved.resolvedFileName))
|
|
292
|
+
)
|
|
293
|
+
throw new Error(
|
|
294
|
+
'A parameter type import must stay inside the project.',
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
references.push({
|
|
298
|
+
start: literal.getStart(source) - start,
|
|
299
|
+
length: literal.end - literal.getStart(source),
|
|
300
|
+
module,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
ts.forEachChild(child, walk);
|
|
304
|
+
};
|
|
305
|
+
walk(node);
|
|
306
|
+
return references.sort((a, b) => a.start - b.start);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/** Parse, resolve and format an expression; unresolved names are never accepted. */
|
|
310
|
+
const declaredExpression = (type, catalog, source) => {
|
|
311
|
+
if (type.kind === 'content') return 'import("kerfjs").SafeHtml';
|
|
312
|
+
if (type.kind === 'prop') {
|
|
313
|
+
const owner = catalog?.components.get(type.component)?.component;
|
|
314
|
+
if (!owner?.import || !owner.export)
|
|
315
|
+
throw new Error('The parameter names an unavailable component prop.');
|
|
316
|
+
let module = owner.import;
|
|
317
|
+
if (isAbsolute(module)) {
|
|
318
|
+
if (!inside(root, module))
|
|
319
|
+
throw new Error('The component prop must belong to this project.');
|
|
320
|
+
module = sourceModule(resolve(root, source), module);
|
|
321
|
+
}
|
|
322
|
+
return `Parameters<typeof import(${JSON.stringify(module)}).${owner.export}>[0][${JSON.stringify(type.prop)}]`;
|
|
323
|
+
}
|
|
324
|
+
if (type.kind === 'array')
|
|
325
|
+
return `ReadonlyArray<${declaredExpression(type.items, catalog, source)}>`;
|
|
326
|
+
if (type.kind === 'object')
|
|
327
|
+
return `{ ${(type.props ?? []).map((prop) => `readonly ${propertyName(prop.name)}${prop.required ? '' : '?'}: ${declaredExpression(prop.type, catalog, source)};`).join(' ')} }`;
|
|
328
|
+
return typeText(type);
|
|
329
|
+
};
|
|
330
|
+
function analyze({
|
|
331
|
+
source,
|
|
332
|
+
text,
|
|
333
|
+
cursor = text.length,
|
|
334
|
+
autoImport = true,
|
|
335
|
+
suggest = true,
|
|
336
|
+
declared,
|
|
337
|
+
catalog,
|
|
338
|
+
}) {
|
|
339
|
+
if (typeof text !== 'string' || text.length > 16384)
|
|
340
|
+
throw new Error('Type expressions are at most 16384 characters.');
|
|
341
|
+
if (declared && !declared.typescript && text === typeText(declared))
|
|
342
|
+
return analyze({
|
|
343
|
+
source,
|
|
344
|
+
text: declaredExpression(declared, catalog, source),
|
|
345
|
+
suggest,
|
|
346
|
+
});
|
|
347
|
+
cursor = Number.isInteger(cursor)
|
|
348
|
+
? Math.max(0, Math.min(cursor, text.length))
|
|
349
|
+
: text.length;
|
|
350
|
+
set(source, PREFIX + text + SUFFIX);
|
|
351
|
+
const syntax = ts.createSourceFile(
|
|
352
|
+
file,
|
|
353
|
+
content,
|
|
354
|
+
ts.ScriptTarget.Latest,
|
|
355
|
+
true,
|
|
356
|
+
);
|
|
357
|
+
if (ts.isTypeAliasDeclaration(syntax.statements[0]))
|
|
358
|
+
referenceTokens(syntax.statements[0].type, syntax, PREFIX.length);
|
|
359
|
+
const parsed = expression();
|
|
360
|
+
// Auto-import completions are most of an analysis's cost, so a caller
|
|
361
|
+
// that will not show suggestions (formatting a finished draft) skips them.
|
|
362
|
+
const completions = suggest
|
|
363
|
+
? service.getCompletionsAtPosition(
|
|
364
|
+
file,
|
|
365
|
+
PREFIX.length + Math.max(0, Math.min(cursor, text.length)),
|
|
366
|
+
{
|
|
367
|
+
includeCompletionsForModuleExports: true,
|
|
368
|
+
includeCompletionsWithInsertText: true,
|
|
369
|
+
},
|
|
370
|
+
)
|
|
371
|
+
: undefined;
|
|
372
|
+
const fragment = /[A-Za-z_$][\w$]*$/.exec(text.slice(0, cursor));
|
|
373
|
+
const start = fragment ? cursor - fragment[0].length : cursor;
|
|
374
|
+
const names = exportsByName();
|
|
375
|
+
const suggestions = (completions?.entries ?? [])
|
|
376
|
+
.filter(
|
|
377
|
+
(entry) =>
|
|
378
|
+
[
|
|
379
|
+
'keyword',
|
|
380
|
+
'type',
|
|
381
|
+
'interface',
|
|
382
|
+
'class',
|
|
383
|
+
'enum',
|
|
384
|
+
'type parameter',
|
|
385
|
+
'module',
|
|
386
|
+
'const',
|
|
387
|
+
'function',
|
|
388
|
+
'var',
|
|
389
|
+
'alias',
|
|
390
|
+
].includes(entry.kind) &&
|
|
391
|
+
(!fragment ||
|
|
392
|
+
entry.name.toLowerCase().startsWith(fragment[0].toLowerCase())),
|
|
393
|
+
)
|
|
394
|
+
.slice(0, 30)
|
|
395
|
+
.map((entry) => {
|
|
396
|
+
let inserted = entry.name;
|
|
397
|
+
let owner = entry.source;
|
|
398
|
+
if (owner && isAbsolute(owner)) owner = sourceModule(file, owner);
|
|
399
|
+
if (owner) inserted = `import(${JSON.stringify(owner)}).${entry.name}`;
|
|
400
|
+
return {
|
|
401
|
+
name: entry.name,
|
|
402
|
+
source: owner,
|
|
403
|
+
text: text.slice(0, start) + inserted + text.slice(cursor),
|
|
404
|
+
cursor: start + inserted.length,
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
if (!parsed || !containedTypeExpression(text))
|
|
408
|
+
return {
|
|
409
|
+
diagnostics: [
|
|
410
|
+
{ message: 'Enter one complete TypeScript type expression.' },
|
|
411
|
+
],
|
|
412
|
+
suggestions,
|
|
413
|
+
};
|
|
414
|
+
// A unique project export can be referenced by its short name. Keep its
|
|
415
|
+
// portable module identity explicit in the canonical expression we persist.
|
|
416
|
+
if (autoImport && diagnostics().some((item) => item.code === 2304)) {
|
|
417
|
+
const missing = new Set(
|
|
418
|
+
diagnostics()
|
|
419
|
+
.filter((item) => item.code === 2304)
|
|
420
|
+
.map((item) => item.start),
|
|
421
|
+
);
|
|
422
|
+
let changed = false;
|
|
423
|
+
const result = ts.transform(parsed.alias.type, [
|
|
424
|
+
(context) => {
|
|
425
|
+
const visit = (node) => {
|
|
426
|
+
if (ts.isTypeReferenceNode(node) || ts.isTypeQueryNode(node)) {
|
|
427
|
+
const name = ts.isTypeReferenceNode(node)
|
|
428
|
+
? node.typeName
|
|
429
|
+
: node.exprName;
|
|
430
|
+
let head = name;
|
|
431
|
+
while (ts.isQualifiedName(head)) head = head.left;
|
|
432
|
+
const matches = missing.has(head.getStart(parsed.source))
|
|
433
|
+
? (names.get(head.text) ?? []).filter(
|
|
434
|
+
(item) =>
|
|
435
|
+
item.symbol.flags &
|
|
436
|
+
(ts.isTypeQueryNode(node)
|
|
437
|
+
? ts.SymbolFlags.Value | ts.SymbolFlags.Namespace
|
|
438
|
+
: ts.SymbolFlags.Type | ts.SymbolFlags.Namespace),
|
|
439
|
+
)
|
|
440
|
+
: [];
|
|
441
|
+
if (
|
|
442
|
+
matches.length &&
|
|
443
|
+
matches.every((item) => item.symbol === matches[0].symbol)
|
|
444
|
+
) {
|
|
445
|
+
const owner = matches.sort(
|
|
446
|
+
(a, b) => a.source.length - b.source.length,
|
|
447
|
+
)[0].source;
|
|
448
|
+
changed = true;
|
|
449
|
+
return ts.factory.createImportTypeNode(
|
|
450
|
+
ts.factory.createLiteralTypeNode(
|
|
451
|
+
ts.factory.createStringLiteral(sourceModule(file, owner)),
|
|
452
|
+
),
|
|
453
|
+
undefined,
|
|
454
|
+
name,
|
|
455
|
+
node.typeArguments,
|
|
456
|
+
ts.isTypeQueryNode(node),
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return ts.visitEachChild(node, visit, context);
|
|
461
|
+
};
|
|
462
|
+
return (node) => ts.visitNode(node, visit);
|
|
463
|
+
},
|
|
464
|
+
]);
|
|
465
|
+
const canonical = printType(result.transformed[0], parsed.source);
|
|
466
|
+
result.dispose();
|
|
467
|
+
if (changed) {
|
|
468
|
+
const analyzed = analyze({
|
|
469
|
+
source,
|
|
470
|
+
text: canonical,
|
|
471
|
+
cursor: canonical.length,
|
|
472
|
+
autoImport: false,
|
|
473
|
+
suggest: false,
|
|
474
|
+
});
|
|
475
|
+
return { ...analyzed, suggestions };
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const errors = diagnostics().filter(
|
|
479
|
+
(item) => item.category === ts.DiagnosticCategory.Error,
|
|
480
|
+
);
|
|
481
|
+
if (errors.length)
|
|
482
|
+
return {
|
|
483
|
+
diagnostics: errors.map((item) => ({
|
|
484
|
+
message: message(item),
|
|
485
|
+
start: Math.max(0, (item.start ?? PREFIX.length) - PREFIX.length),
|
|
486
|
+
length: item.length ?? 0,
|
|
487
|
+
})),
|
|
488
|
+
suggestions,
|
|
489
|
+
};
|
|
490
|
+
const printed = printType(parsed.alias.type, parsed.source);
|
|
491
|
+
set(source, PREFIX + printed + SUFFIX);
|
|
492
|
+
const formatted = expression();
|
|
493
|
+
const program = service.getProgram();
|
|
494
|
+
const checker = program.getTypeChecker();
|
|
495
|
+
const context = createPropsContext(checker, root);
|
|
496
|
+
const shape = inlineShape(
|
|
497
|
+
describeType(context, checker.getTypeFromTypeNode(formatted.alias.type)),
|
|
498
|
+
context.definitions,
|
|
499
|
+
);
|
|
500
|
+
const references = referenceTokens(
|
|
501
|
+
formatted.alias.type,
|
|
502
|
+
formatted.source,
|
|
503
|
+
PREFIX.length,
|
|
504
|
+
);
|
|
505
|
+
return {
|
|
506
|
+
diagnostics: [],
|
|
507
|
+
suggestions,
|
|
508
|
+
type: { ...shape, typescript: { text: printed, source, references } },
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** Refresh cached projections and compile actual generated bindings in context. */
|
|
513
|
+
function validateDocument(document, catalog, source, options = {}) {
|
|
514
|
+
if (!hasTypeExpressions(document)) return { diagnostics: [], document };
|
|
515
|
+
const params = { ...document.params };
|
|
516
|
+
const errors = [];
|
|
517
|
+
for (const [name, param] of Object.entries(params)) {
|
|
518
|
+
if (!param.type.typescript) continue;
|
|
519
|
+
let result;
|
|
520
|
+
try {
|
|
521
|
+
const metadata = param.type.typescript;
|
|
522
|
+
set(metadata.source, PREFIX + metadata.text + SUFFIX);
|
|
523
|
+
const syntax = ts.createSourceFile(
|
|
524
|
+
file,
|
|
525
|
+
content,
|
|
526
|
+
ts.ScriptTarget.Latest,
|
|
527
|
+
true,
|
|
528
|
+
);
|
|
529
|
+
const alias = syntax.statements[0];
|
|
530
|
+
if (
|
|
531
|
+
!containedTypeExpression(metadata.text) ||
|
|
532
|
+
syntax.parseDiagnostics.length ||
|
|
533
|
+
syntax.statements.length !== 2 ||
|
|
534
|
+
!ts.isTypeAliasDeclaration(alias)
|
|
535
|
+
)
|
|
536
|
+
throw new Error('Enter one TypeScript type expression.');
|
|
537
|
+
const text = printType(alias.type, syntax);
|
|
538
|
+
set(metadata.source, PREFIX + text + SUFFIX);
|
|
539
|
+
const printed = ts.createSourceFile(
|
|
540
|
+
file,
|
|
541
|
+
content,
|
|
542
|
+
ts.ScriptTarget.Latest,
|
|
543
|
+
true,
|
|
544
|
+
);
|
|
545
|
+
const carrier = {
|
|
546
|
+
typescript: {
|
|
547
|
+
...metadata,
|
|
548
|
+
text,
|
|
549
|
+
references: referenceTokens(
|
|
550
|
+
printed.statements[0].type,
|
|
551
|
+
printed,
|
|
552
|
+
PREFIX.length,
|
|
553
|
+
),
|
|
554
|
+
},
|
|
555
|
+
};
|
|
556
|
+
result = analyze({
|
|
557
|
+
source,
|
|
558
|
+
text: typeExpressionText(carrier, source),
|
|
559
|
+
suggest: false,
|
|
560
|
+
});
|
|
561
|
+
} catch (error) {
|
|
562
|
+
result = { diagnostics: [{ message: error.message }] };
|
|
563
|
+
}
|
|
564
|
+
errors.push(
|
|
565
|
+
...result.diagnostics.map((item) => ({
|
|
566
|
+
code: 'KUI-V018',
|
|
567
|
+
path: `$.params.${name}.type`,
|
|
568
|
+
message: item.message,
|
|
569
|
+
})),
|
|
570
|
+
);
|
|
571
|
+
if (result.type) params[name] = { ...param, type: result.type };
|
|
572
|
+
}
|
|
573
|
+
const next = { ...document, params };
|
|
574
|
+
if (errors.length) return { diagnostics: errors, document: next };
|
|
575
|
+
const resolveView =
|
|
576
|
+
options.resolveView ??
|
|
577
|
+
((_src, path) => {
|
|
578
|
+
const target = resolve(root, dirname(source), path);
|
|
579
|
+
if (!inside(root, target) || !target.endsWith('.kui.json'))
|
|
580
|
+
return undefined;
|
|
581
|
+
try {
|
|
582
|
+
return JSON.parse(readFileSync(target, 'utf8'));
|
|
583
|
+
} catch {
|
|
584
|
+
return undefined;
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
const structural = validateView(next, catalog, { resolveView });
|
|
588
|
+
if (!structural.valid)
|
|
589
|
+
return { diagnostics: structural.diagnostics, document: next };
|
|
590
|
+
const generated = generateView(next, catalog, {
|
|
591
|
+
source,
|
|
592
|
+
typeSource: source,
|
|
593
|
+
importFrom: resolve(root, dirname(source)),
|
|
594
|
+
resolveView,
|
|
595
|
+
});
|
|
596
|
+
const defaults = Object.entries(params)
|
|
597
|
+
.flatMap(([name, param], index) =>
|
|
598
|
+
param.type.typescript && Object.hasOwn(param, 'default')
|
|
599
|
+
? [
|
|
600
|
+
`const __kerfDefault${index}: ${typeExpressionText(param.type, source)} = ${JSON.stringify(param.default)}; // ${name}`,
|
|
601
|
+
]
|
|
602
|
+
: [],
|
|
603
|
+
)
|
|
604
|
+
.join('\n');
|
|
605
|
+
set(source, generated.code + '\n' + defaults);
|
|
606
|
+
return {
|
|
607
|
+
document: next,
|
|
608
|
+
diagnostics: diagnostics()
|
|
609
|
+
.filter((item) => item.category === ts.DiagnosticCategory.Error)
|
|
610
|
+
.map((item) => ({
|
|
611
|
+
code: 'KUI-V018',
|
|
612
|
+
path: '$.params',
|
|
613
|
+
message: message(item),
|
|
614
|
+
})),
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
return { analyze, validateDocument, dispose: () => service.dispose() };
|
|
618
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ViewDocument, ViewType } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
/** Lexical containment only; the project compiler owns syntax and semantics. */
|
|
4
|
+
export function containedTypeExpression(text: unknown): boolean;
|
|
5
|
+
export function typeSourcePath(value: unknown): value is string;
|
|
6
|
+
export function validTypeExpression(
|
|
7
|
+
type: unknown,
|
|
8
|
+
): type is ViewType & { typescript: NonNullable<ViewType['typescript']> };
|
|
9
|
+
export function typeExpressionText(
|
|
10
|
+
type: Pick<ViewType, 'typescript'>,
|
|
11
|
+
source?: string,
|
|
12
|
+
): string;
|
|
13
|
+
export function hasTypeExpressions(
|
|
14
|
+
document: Pick<ViewDocument, 'params'>,
|
|
15
|
+
): boolean;
|