soma-kit 0.1.10 → 0.1.12
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 +103 -110
- package/bin/soma-kit.mjs +1 -1
- package/bin/soma.mjs +22 -0
- package/package.json +13 -7
- package/packs/finance/reconcile-board.ts +5 -2
- package/packs/finance/reconcile-desk/compile.ts +3 -2
- package/spec/generated/kit-app.schema.json +776 -38
- package/spec/generated/kit-page.schema.json +1613 -224
- package/spec/kit-grammar.json +770 -529
- package/spec/kit-grammar.schema.json +1962 -392
- package/src/app/app-config.ts +371 -347
- package/src/app/component-gate.ts +12 -15
- package/src/app/compose-apps.ts +79 -8
- package/src/app/define-kit-actions.ts +245 -41
- package/src/app/define-kit-app.ts +174 -31
- package/src/app/define-kit-view.ts +100 -0
- package/src/app/envelope.ts +1 -161
- package/src/app/index.ts +13 -2
- package/src/app/kit-view-context.ts +290 -0
- package/src/app/notify-renderer.ts +1 -115
- package/src/app/paged-list-adapter.ts +1 -240
- package/src/app/server-pages.ts +8 -2
- package/src/app/server.ts +491 -101
- package/src/app/submit.types.ts +1 -1
- package/src/app/theme-endpoint.ts +63 -0
- package/src/app/theme-runtime-shared.ts +1 -0
- package/src/app/theme-runtime.ts +663 -0
- package/src/base/action-busy.ts +2 -2
- package/src/base/action-gate.ts +4 -255
- package/src/base/action-slots.ts +47 -0
- package/src/base/alert.ts +5 -3
- package/src/base/avatar.ts +2 -2
- package/src/base/button.ts +46 -29
- package/src/base/cascader.ts +11 -12
- package/src/base/check-core.ts +1 -1
- package/src/base/choice.ts +28 -26
- package/src/base/class-candidates.ts +17 -0
- package/src/base/color-picker.ts +6 -6
- package/src/base/combobox.ts +5 -3
- package/src/base/command-palette.ts +9 -5
- package/src/base/countdown.ts +1 -1
- package/src/base/date-range.ts +25 -11
- package/src/base/date.ts +85 -17
- package/src/base/divider.ts +2 -2
- package/src/base/fab.ts +11 -4
- package/src/base/image.ts +1 -1
- package/src/base/index.ts +9 -3
- package/src/base/input.ts +42 -13
- package/src/base/kbd.ts +3 -1
- package/src/base/link.ts +4 -4
- package/src/base/listbox-core.ts +50 -30
- package/src/base/loading-gate.ts +133 -0
- package/src/base/mentions.ts +5 -5
- package/src/base/menu-button.ts +6 -1
- package/src/base/menu-core.ts +4 -0
- package/src/base/motion/aura.ts +2 -2
- package/src/base/motion/hover-3d.ts +2 -2
- package/src/base/motion/hover-gallery.ts +2 -2
- package/src/base/motion/marquee.ts +2 -2
- package/src/base/motion/text-rotate.ts +2 -2
- package/src/base/number.ts +93 -80
- package/src/base/otp.ts +4 -1
- package/src/base/permission-gate.ts +6 -0
- package/src/base/popover.ts +40 -8
- package/src/base/popup.ts +79 -14
- package/src/base/price-text.ts +2 -2
- package/src/base/progress.ts +2 -2
- package/src/base/qr-code.ts +4 -4
- package/src/base/qty-stepper.ts +9 -3
- package/src/base/range.ts +2 -2
- package/src/base/rating.ts +1 -1
- package/src/base/segmented.ts +4 -0
- package/src/base/select.ts +4 -3
- package/src/base/shared.ts +221 -29
- package/src/base/swap.ts +4 -1
- package/src/base/switch.ts +1 -1
- package/src/base/tag-input.ts +14 -10
- package/src/base/tag.ts +5 -1
- package/src/base/text.ts +39 -6
- package/src/base/textarea.ts +37 -11
- package/src/base/time-picker.ts +12 -9
- package/src/base/tree-select.ts +15 -8
- package/src/base/upload.ts +34 -5
- package/src/base/virtual-list.ts +107 -1
- package/src/biz/approval-desk/compile.ts +23 -41
- package/src/biz/auth-panel/compile.ts +37 -52
- package/src/biz/auth-panel/index.ts +1 -1
- package/src/biz/category-crud/compile.ts +22 -36
- package/src/biz/checkout-page/compile.ts +21 -5
- package/src/biz/compose.ts +13 -27
- package/src/biz/define.ts +5 -4
- package/src/biz/import-wizard/compile.ts +22 -26
- package/src/biz/import-wizard/hub.ts +126 -61
- package/src/biz/import-wizard/shell.ts +3 -3
- package/src/biz/index.ts +3 -3
- package/src/biz/landing-page/compile.ts +19 -3
- package/src/biz/link/hub.ts +49 -20
- package/src/biz/link/shared.ts +115 -118
- package/src/biz/master-sub/compile.ts +31 -44
- package/src/biz/multi-board/compile.ts +34 -49
- package/src/biz/multi-board/index.ts +18 -1
- package/src/biz/multi-board/shell.ts +24 -15
- package/src/biz/notification-center/compile.ts +21 -33
- package/src/biz/page-templates/domain-templates.ts +80 -13
- package/src/biz/page-templates/result-page/compile.ts +11 -2
- package/src/biz/page-templates/shared.ts +34 -36
- package/src/biz/record-detail/compile.ts +47 -42
- package/src/biz/record-detail/index.ts +11 -1
- package/src/biz/settings-center/compile.ts +6 -5
- package/src/biz/shared/action-host.ts +91 -211
- package/src/biz/shared/compile-helpers.ts +80 -0
- package/src/biz/shared/coordinator.ts +486 -83
- package/src/biz/shared/index.ts +8 -2
- package/src/biz/shared/overlay-shell.ts +80 -0
- package/src/biz/shared/pipes.ts +43 -30
- package/src/biz/shared/remove-key.ts +6 -0
- package/src/biz/shared/remove-wire.ts +29 -0
- package/src/biz/shared/runtime-hub.ts +216 -24
- package/src/biz/single-form/compile.ts +15 -37
- package/src/biz/standard-crud/compile.ts +62 -107
- package/src/biz/standard-crud/shell.ts +16 -66
- package/src/biz/step-form/compile.ts +26 -97
- package/src/biz/step-form/hub.ts +34 -41
- package/src/biz/types.ts +4 -3
- package/src/cli/core-imports.ts +54 -0
- package/src/cli/kit-cli.ts +130 -26
- package/src/cli/kit-init.ts +222 -0
- package/src/cli/service-endpoint-check.ts +425 -0
- package/src/cli/source-path-check.d.mts +17 -0
- package/src/cli/source-path-check.mjs +334 -0
- package/src/cli/view-writes.ts +752 -0
- package/src/compile/action-host-attach.ts +21 -29
- package/src/compile/closed-set.ts +509 -0
- package/src/compile/compile-page.ts +219 -72
- package/src/compile/component-candidates.ts +1 -1
- package/src/compile/hints.ts +30 -1
- package/src/compile/index.ts +1 -0
- package/src/compile/kit-error.ts +1 -0
- package/src/compile/locate.ts +21 -1
- package/src/compile/parse.ts +370 -41
- package/src/compile/redirect.ts +6 -2
- package/src/compile/styles.ts +35 -2
- package/src/compile/types.ts +18 -2
- package/src/compile/view-check.ts +151 -0
- package/src/data/amount-breakdown.ts +2 -2
- package/src/data/anchor-nav.ts +2 -2
- package/src/data/approval-flow.ts +2 -2
- package/src/data/assignment-picker.ts +40 -29
- package/src/data/async-result-panel.ts +62 -6
- package/src/data/breadcrumbs.ts +2 -2
- package/src/data/calendar.ts +22 -25
- package/src/data/carousel.ts +5 -2
- package/src/data/chart.ts +42 -34
- package/src/data/checklist.ts +2 -2
- package/src/data/code-view.ts +4 -2
- package/src/data/comment-thread.ts +5 -2
- package/src/data/confirm-dialog.ts +10 -0
- package/src/data/context-menu.ts +29 -37
- package/src/data/d9-util.ts +2 -18
- package/src/data/diff.ts +4 -2
- package/src/data/dock.ts +2 -2
- package/src/data/embed.ts +2 -1
- package/src/data/field-mapper.ts +31 -26
- package/src/data/flow-editor-contract.ts +1 -1
- package/src/data/flow-editor.ts +89 -100
- package/src/data/flow-node-options.ts +90 -0
- package/src/data/flow-remote-options.ts +0 -103
- package/src/data/footer.ts +2 -2
- package/src/data/form-draft-registry.ts +2 -4
- package/src/data/gantt.ts +14 -17
- package/src/data/hero.ts +2 -2
- package/src/data/image-viewer.ts +20 -26
- package/src/data/index.ts +0 -2
- package/src/data/item-list.ts +31 -22
- package/src/data/kanban.ts +43 -42
- package/src/data/media-grid.ts +14 -19
- package/src/data/menu.ts +2 -2
- package/src/data/mockup.ts +2 -2
- package/src/data/modal-dialog.ts +22 -18
- package/src/data/navbar.ts +7 -2
- package/src/data/notify-renderer.ts +235 -0
- package/src/data/object-form.ts +1036 -648
- package/src/data/pager.ts +65 -29
- package/src/data/permission-matrix.ts +20 -21
- package/src/data/permission-tree.ts +28 -26
- package/src/data/pricing-table.ts +9 -3
- package/src/data/reaction-bar.ts +77 -88
- package/src/data/read-rollback.ts +73 -0
- package/src/data/record-card.ts +3 -3
- package/src/data/record-picker.ts +93 -50
- package/src/data/redirect.ts +32 -32
- package/src/data/remote-options.ts +13 -6
- package/src/data/request-console.ts +5 -3
- package/src/data/resident.ts +2 -1
- package/src/data/result-panel.ts +4 -2
- package/src/data/rich-text.ts +45 -55
- package/src/data/row-editor.ts +285 -52
- package/src/data/rule-builder.ts +18 -12
- package/src/data/shape-util.ts +4 -0
- package/src/data/stat-group.ts +243 -101
- package/src/data/static-registry.ts +22 -11
- package/src/data/steps.ts +2 -2
- package/src/data/survey.ts +24 -29
- package/src/data/system-switcher.ts +2 -2
- package/src/data/timeline.ts +2 -2
- package/src/data/tour-guide.ts +22 -29
- package/src/data/transfer.ts +27 -23
- package/src/data/tree-table.ts +96 -48
- package/src/data/tree.ts +27 -34
- package/src/data/trend-line.ts +6 -24
- package/src/data/virtual-rows.ts +118 -76
- package/src/data/watermark.ts +2 -2
- package/src/data/wrap.ts +63 -38
- package/src/frames/blank-frame.ts +1 -0
- package/src/frames/index.ts +2 -2
- package/src/frames/page-bar.ts +12 -11
- package/src/frames/page-header.ts +14 -5
- package/src/frames/page-status.ts +2 -2
- package/src/frames/shared.ts +23 -17
- package/src/frames/single-frame.ts +1 -0
- package/src/frames/split-frame/shell.ts +5 -5
- package/src/frames/split-frame/wiring.ts +2 -2
- package/src/frames/tabs-frame/shell.ts +2 -13
- package/src/frames/tabs-frame/wiring.ts +4 -8
- package/src/frames/top-main-frame.ts +1 -0
- package/src/frames/types.ts +13 -2
- package/src/frames/workspace-frame/extras.ts +20 -4
- package/src/frames/workspace-frame/index.ts +1 -1
- package/src/frames/workspace-frame/shell.ts +1 -0
- package/src/frames/workspace-frame/state.ts +13 -6
- package/src/frames/workspace-frame/view.ts +119 -25
- package/src/generated/kit-grammar-runtime.ts +430 -366
- package/src/generated/page-origin.ts +0 -11
- package/src/i18n/kit-text.ts +23 -27
- package/src/index.ts +2 -1
- package/src/object-renderer/action-gate.ts +11 -147
- package/src/object-renderer/arrange.ts +71 -77
- package/src/object-renderer/collapse-wiring.ts +2 -2
- package/src/object-renderer/collection-expand.ts +2 -2
- package/src/object-renderer/collection-parts.ts +216 -100
- package/src/object-renderer/collection-table.ts +180 -42
- package/src/object-renderer/collection.ts +95 -79
- package/src/object-renderer/compile-data.ts +85 -100
- package/src/object-renderer/crud-bridge.ts +32 -0
- package/src/object-renderer/defaults.ts +8 -14
- package/src/object-renderer/display-checks.ts +3 -24
- package/src/object-renderer/emit-events.ts +186 -172
- package/src/object-renderer/emit-object.ts +15 -2
- package/src/object-renderer/emit-static.ts +174 -132
- package/src/object-renderer/field-value.ts +593 -396
- package/src/object-renderer/filter-reload.ts +70 -0
- package/src/object-renderer/form-detail.ts +112 -2
- package/src/object-renderer/index.ts +26 -0
- package/src/object-renderer/key-field.ts +12 -0
- package/src/object-renderer/linkage.ts +7 -270
- package/src/object-renderer/merge-class.ts +18 -1
- package/src/object-renderer/path-pick.ts +27 -0
- package/src/object-renderer/render-object.ts +405 -86
- package/src/object-renderer/request-actions.ts +230 -249
- package/src/object-renderer/table-actions.ts +195 -95
- package/src/object-renderer/table-summary.ts +2 -2
- package/src/object-renderer/table-tools.ts +418 -84
- package/src/object-renderer/types.ts +128 -122
- package/src/object-renderer/view-binding.ts +196 -0
- package/src/page-target/ai-surface.ts +37 -0
- package/src/page-target/compile.ts +48 -8
- package/src/page-target/index.ts +8 -1
- package/src/page-target/intent-schema.ts +154 -28
- package/src/page-target/target.ts +257 -50
- package/src/page-target/writer-policy.ts +14 -3
- package/src/recipes/container.ts +165 -0
- package/src/recipes/control.ts +219 -0
- package/src/recipes/index.ts +263 -0
- package/src/recipes/overlay.ts +182 -0
- package/src/recipes/state.ts +70 -0
- package/src/recipes/table-row.ts +51 -0
- package/src/recipes/typography.ts +87 -0
- package/src/shared/action-field-guard.ts +41 -0
- package/src/shared/envelope.ts +207 -0
- package/src/shared/escape.ts +57 -0
- package/src/shared/kit-error.ts +21 -0
- package/src/shared/paged-list-adapter.ts +240 -0
- package/src/shared/path-guard.ts +127 -0
- package/src/shared/theme-runtime-shared.ts +175 -0
- package/src/shared/view-info.ts +10 -0
- package/src/theme/index.ts +12 -0
- package/src/theme/kit-theme-text.ts +1 -1
- package/src/theme/kit-theme.css +38 -1
- package/src/theme/kit-theme.tpl +38 -1
- package/src/theme/nine-dim-structure.ts +77 -0
- package/src/theme/surface-layers.ts +97 -0
- package/src/theme/theme-tokens.ts +85 -79
- package/src/theme/theme-types.ts +71 -0
- package/src/theme/typography.ts +17 -17
- package/src/biz/shared/step-values.ts +0 -25
- package/src/biz/step-form/steps-ref.ts +0 -112
- package/src/data/options-bus.ts +0 -24
- package/src/data/prompt-dialog.ts +0 -235
- package/src/object-renderer/adopt-gate.ts +0 -333
- package/src/object-renderer/field-policy.ts +0 -89
- package/src/object-renderer/outcome-draft.ts +0 -61
- package/src/theme/nine-dim-palettes.ts +0 -1278
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
const FS_METHODS = new Set([
|
|
4
|
+
'readFileSync', 'writeFileSync', 'appendFileSync', 'existsSync', 'readdirSync', 'statSync', 'lstatSync',
|
|
5
|
+
'mkdirSync', 'rmSync', 'unlinkSync', 'copyFileSync', 'renameSync', 'openSync', 'accessSync', 'realpathSync',
|
|
6
|
+
'readFile', 'writeFile', 'appendFile', 'readdir', 'stat', 'lstat', 'mkdir', 'rm', 'unlink', 'copyFile',
|
|
7
|
+
'rename', 'open', 'access', 'realpath', 'createReadStream', 'createWriteStream', 'truncate', 'chmod', 'chown',
|
|
8
|
+
]);
|
|
9
|
+
const HTTP_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete', 'request']);
|
|
10
|
+
const PATH_METHODS = new Set(['join', 'resolve']);
|
|
11
|
+
const DRIVE_PATH = /^[A-Za-z]:[\\/]/;
|
|
12
|
+
|
|
13
|
+
function moduleKind(moduleName) {
|
|
14
|
+
const normalized = moduleName.replace(/^node:/, '');
|
|
15
|
+
if (normalized === 'fs' || normalized === 'fs/promises') return 'fs';
|
|
16
|
+
if (normalized === 'path') return 'path';
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function scopeForVariable(node) {
|
|
21
|
+
const list = node.parent;
|
|
22
|
+
const flags = ts.isVariableDeclarationList(list) ? list.flags : 0;
|
|
23
|
+
const isVar = (flags & ts.NodeFlags.BlockScoped) === 0;
|
|
24
|
+
for (let current = list.parent; current !== undefined; current = current.parent) {
|
|
25
|
+
if (ts.isSourceFile(current) || ts.isBlock(current) || ts.isModuleBlock(current) || ts.isCaseBlock(current)
|
|
26
|
+
|| ts.isForStatement(current) || ts.isForInStatement(current) || ts.isForOfStatement(current) || ts.isCatchClause(current)) {
|
|
27
|
+
if (isVar) {
|
|
28
|
+
for (let parent = current.parent; parent !== undefined; parent = parent.parent) {
|
|
29
|
+
if (ts.isFunctionLike(parent) || ts.isSourceFile(parent)) return parent;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return current;
|
|
33
|
+
}
|
|
34
|
+
if (ts.isFunctionLike(current)) return current;
|
|
35
|
+
}
|
|
36
|
+
return node.getSourceFile();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function scopeBindings() {
|
|
40
|
+
return new Map();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function addScopeBinding(scopes, scope, name, node, kind) {
|
|
44
|
+
let bindings = scopes.get(scope);
|
|
45
|
+
if (bindings === undefined) { bindings = new Map(); scopes.set(scope, bindings); }
|
|
46
|
+
bindings.set(name, { node, ...(kind !== undefined ? { kind } : {}) });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function bindingName(node) {
|
|
50
|
+
return ts.isIdentifier(node) ? node.text : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function bindingIdentifiers(pattern) {
|
|
54
|
+
if (ts.isIdentifier(pattern)) return [{ name: pattern.text, node: pattern }];
|
|
55
|
+
if (ts.isObjectBindingPattern(pattern) || ts.isArrayBindingPattern(pattern)) {
|
|
56
|
+
return pattern.elements.flatMap((element) => ts.isOmittedExpression(element) ? [] : bindingIdentifiers(element.name));
|
|
57
|
+
}
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function bindImport(statement, scopes) {
|
|
62
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) return;
|
|
63
|
+
const kind = moduleKind(statement.moduleSpecifier.text);
|
|
64
|
+
if (kind === undefined || statement.importClause?.isTypeOnly) return;
|
|
65
|
+
const scope = statement.getSourceFile();
|
|
66
|
+
const clause = statement.importClause;
|
|
67
|
+
if (clause?.name !== undefined) addScopeBinding(scopes, scope, clause.name.text, clause.name, kind);
|
|
68
|
+
const named = clause?.namedBindings;
|
|
69
|
+
if (named === undefined) return;
|
|
70
|
+
if (ts.isNamespaceImport(named)) {
|
|
71
|
+
addScopeBinding(scopes, scope, named.name.text, named.name, kind);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
for (const element of named.elements) {
|
|
75
|
+
const original = element.propertyName?.text ?? element.name.text;
|
|
76
|
+
const bindingKind = kind === 'fs'
|
|
77
|
+
? original === 'promises' ? 'fsPromises' : FS_METHODS.has(original) ? `fsFunction:${original}` : undefined
|
|
78
|
+
: PATH_METHODS.has(original) ? `pathFunction:${original}` : undefined;
|
|
79
|
+
if (bindingKind !== undefined) addScopeBinding(scopes, scope, element.name.text, element.name, bindingKind);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getVisibleBinding(identifier, fromNode, scopes) {
|
|
84
|
+
for (let current = fromNode; current !== undefined; current = current.parent) {
|
|
85
|
+
const bindings = scopes.get(current);
|
|
86
|
+
const binding = bindings?.get(identifier);
|
|
87
|
+
if (binding !== undefined) return binding;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function unwrapModuleExpression(expression) {
|
|
93
|
+
let current = expression;
|
|
94
|
+
while (ts.isAwaitExpression(current) || ts.isParenthesizedExpression(current) || ts.isAsExpression(current)) {
|
|
95
|
+
current = current.expression;
|
|
96
|
+
}
|
|
97
|
+
return current;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function initializerModuleKind(initializer) {
|
|
101
|
+
const expression = unwrapModuleExpression(initializer);
|
|
102
|
+
if (!ts.isCallExpression(expression)) return undefined;
|
|
103
|
+
const isRequire = ts.isIdentifier(expression.expression) && expression.expression.text === 'require';
|
|
104
|
+
const isStaticImport = expression.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
105
|
+
if (!isRequire && !isStaticImport) return undefined;
|
|
106
|
+
const module = expression.arguments[0];
|
|
107
|
+
return module !== undefined && (ts.isStringLiteral(module) || ts.isNoSubstitutionTemplateLiteral(module))
|
|
108
|
+
? moduleKind(module.text)
|
|
109
|
+
: undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function bindVariableDeclaration(node, scopes) {
|
|
113
|
+
const scope = scopeForVariable(node);
|
|
114
|
+
const required = node.initializer === undefined ? undefined : initializerModuleKind(node.initializer);
|
|
115
|
+
for (const binding of bindingIdentifiers(node.name)) addScopeBinding(scopes, scope, binding.name, node, required);
|
|
116
|
+
if (required === undefined || !ts.isObjectBindingPattern(node.name)) return;
|
|
117
|
+
for (const element of node.name.elements) {
|
|
118
|
+
const original = element.propertyName && ts.isIdentifier(element.propertyName)
|
|
119
|
+
? element.propertyName.text
|
|
120
|
+
: bindingName(element.name);
|
|
121
|
+
const alias = bindingName(element.name);
|
|
122
|
+
if (original === undefined || alias === undefined) continue;
|
|
123
|
+
const kind = required === 'fs'
|
|
124
|
+
? original === 'promises' ? 'fsPromises' : FS_METHODS.has(original) ? `fsFunction:${original}` : undefined
|
|
125
|
+
: PATH_METHODS.has(original) ? `pathFunction:${original}` : undefined;
|
|
126
|
+
addScopeBinding(scopes, scope, alias, node, kind);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function declarationScope(node) {
|
|
131
|
+
for (let current = node.parent; current !== undefined; current = current.parent) {
|
|
132
|
+
if (ts.isSourceFile(current) || ts.isBlock(current) || ts.isModuleBlock(current) || ts.isCaseBlock(current)) return current;
|
|
133
|
+
}
|
|
134
|
+
return node.getSourceFile();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function collectScopes(sourceFile) {
|
|
138
|
+
const scopes = scopeBindings();
|
|
139
|
+
for (const statement of sourceFile.statements) bindImport(statement, scopes);
|
|
140
|
+
|
|
141
|
+
const visit = (node) => {
|
|
142
|
+
if (ts.isVariableDeclaration(node) && !ts.isCatchClause(node.parent)) bindVariableDeclaration(node, scopes);
|
|
143
|
+
if (ts.isFunctionLike(node)) {
|
|
144
|
+
for (const parameter of node.parameters) {
|
|
145
|
+
for (const binding of bindingIdentifiers(parameter.name)) addScopeBinding(scopes, node, binding.name, parameter);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name !== undefined) {
|
|
149
|
+
addScopeBinding(scopes, declarationScope(node), node.name.text, node);
|
|
150
|
+
}
|
|
151
|
+
if ((ts.isFunctionExpression(node) || ts.isClassExpression(node)) && node.name !== undefined) {
|
|
152
|
+
addScopeBinding(scopes, node, node.name.text, node);
|
|
153
|
+
}
|
|
154
|
+
if (ts.isCatchClause(node) && node.variableDeclaration) {
|
|
155
|
+
for (const binding of bindingIdentifiers(node.variableDeclaration.name)) addScopeBinding(scopes, node, binding.name, node.variableDeclaration);
|
|
156
|
+
}
|
|
157
|
+
ts.forEachChild(node, visit);
|
|
158
|
+
};
|
|
159
|
+
visit(sourceFile);
|
|
160
|
+
return scopes;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function resolveBindingKind(identifier, fromNode, scopes, seen = new Set()) {
|
|
164
|
+
if (seen.has(identifier)) return undefined;
|
|
165
|
+
seen.add(identifier);
|
|
166
|
+
const binding = getVisibleBinding(identifier, fromNode, scopes);
|
|
167
|
+
if (binding === undefined) return undefined;
|
|
168
|
+
if (binding.kind !== undefined) return binding.kind;
|
|
169
|
+
if (!ts.isVariableDeclaration(binding.node) || binding.node.initializer === undefined) return undefined;
|
|
170
|
+
const init = binding.node.initializer;
|
|
171
|
+
const required = initializerModuleKind(init);
|
|
172
|
+
if (required !== undefined) return required;
|
|
173
|
+
if (ts.isIdentifier(init)) return resolveBindingKind(init.text, binding.node, scopes, seen);
|
|
174
|
+
if (ts.isPropertyAccessExpression(init) && ts.isIdentifier(init.expression)) {
|
|
175
|
+
const ownerKind = resolveBindingKind(init.expression.text, binding.node, scopes, seen);
|
|
176
|
+
if (ownerKind === 'fs' && init.name.text === 'promises') return 'fsPromises';
|
|
177
|
+
if (ownerKind === 'fs' && FS_METHODS.has(init.name.text)) return `fsFunction:${init.name.text}`;
|
|
178
|
+
if (ownerKind === 'path' && PATH_METHODS.has(init.name.text)) return `pathFunction:${init.name.text}`;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function staticString(expression, fromNode, scopes, seen = new Set()) {
|
|
184
|
+
if (ts.isStringLiteral(expression) || ts.isNoSubstitutionTemplateLiteral(expression)) return expression.text;
|
|
185
|
+
if (ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression) || ts.isTypeAssertionExpression(expression) || ts.isSatisfiesExpression(expression)) {
|
|
186
|
+
return staticString(expression.expression, fromNode, scopes, seen);
|
|
187
|
+
}
|
|
188
|
+
if (!ts.isIdentifier(expression) || seen.has(expression.text)) return undefined;
|
|
189
|
+
seen.add(expression.text);
|
|
190
|
+
const binding = getVisibleBinding(expression.text, fromNode, scopes);
|
|
191
|
+
if (binding === undefined || !ts.isVariableDeclaration(binding.node) || binding.node.initializer === undefined) return undefined;
|
|
192
|
+
const flags = ts.isVariableDeclarationList(binding.node.parent) ? binding.node.parent.flags : 0;
|
|
193
|
+
if ((flags & ts.NodeFlags.Const) === 0) return undefined;
|
|
194
|
+
return staticString(binding.node.initializer, binding.node, scopes, seen);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function fsPathKind(value) {
|
|
198
|
+
return value.startsWith('/') || value.startsWith('\\') || DRIVE_PATH.test(value) || /^file:/i.test(value) ? 'absolute' : 'relative';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function recognizedPathMethod(call, context, scopes) {
|
|
202
|
+
if (!ts.isCallExpression(call)) return undefined;
|
|
203
|
+
const expression = call.expression;
|
|
204
|
+
if (ts.isIdentifier(expression)) {
|
|
205
|
+
const kind = resolveBindingKind(expression.text, context, scopes);
|
|
206
|
+
return kind?.startsWith('pathFunction:') ? kind.slice('pathFunction:'.length) : undefined;
|
|
207
|
+
}
|
|
208
|
+
if (!ts.isPropertyAccessExpression(expression)) return undefined;
|
|
209
|
+
const method = expression.name.text;
|
|
210
|
+
if (!PATH_METHODS.has(method) || !ts.isIdentifier(expression.expression)) return undefined;
|
|
211
|
+
return resolveBindingKind(expression.expression.text, context, scopes) === 'path' ? method : undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function analyzeFilePath(expression, context, scopes, seen = new Set()) {
|
|
215
|
+
const value = staticString(expression, context, scopes);
|
|
216
|
+
if (value !== undefined) return { pathKind: fsPathKind(value), value };
|
|
217
|
+
|
|
218
|
+
if (ts.isIdentifier(expression) && !seen.has(expression.text)) {
|
|
219
|
+
seen.add(expression.text);
|
|
220
|
+
const binding = getVisibleBinding(expression.text, context, scopes);
|
|
221
|
+
if (binding === undefined || !ts.isVariableDeclaration(binding.node) || binding.node.initializer === undefined) return undefined;
|
|
222
|
+
const flags = ts.isVariableDeclarationList(binding.node.parent) ? binding.node.parent.flags : 0;
|
|
223
|
+
if ((flags & ts.NodeFlags.Const) === 0) return undefined;
|
|
224
|
+
return analyzeFilePath(binding.node.initializer, binding.node, scopes, seen);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const method = recognizedPathMethod(expression, context, scopes);
|
|
228
|
+
if (method === undefined) return undefined;
|
|
229
|
+
const [base, ...parts] = expression.arguments;
|
|
230
|
+
if (base === undefined) return { pathKind: 'relative', value: expression.getText() };
|
|
231
|
+
const baseValue = staticString(base, expression, scopes);
|
|
232
|
+
if (baseValue !== undefined) return { pathKind: fsPathKind(baseValue), value: expression.getText() };
|
|
233
|
+
for (const part of parts) {
|
|
234
|
+
const partValue = staticString(part, expression, scopes);
|
|
235
|
+
if (partValue !== undefined && fsPathKind(partValue) === 'absolute') return { pathKind: 'absolute', value: expression.getText() };
|
|
236
|
+
}
|
|
237
|
+
return { explicitBase: true };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function httpCallKind(expression) {
|
|
241
|
+
const chain = propertyChain(expression);
|
|
242
|
+
const method = chain.at(-1);
|
|
243
|
+
if (chain.length === 1 && method === 'fetch') return { callee: 'fetch', allowExternal: true };
|
|
244
|
+
if (chain.length === 2 && chain[0] === 'ctx' && method === 'navigate') return { callee: 'ctx.navigate', allowExternal: false };
|
|
245
|
+
if (HTTP_METHODS.has(method) && chain.at(-2) === 'http' && (chain.length === 2 || (chain.length === 3 && chain[0] === 'ctx'))) {
|
|
246
|
+
return { callee: `${chain.length === 3 ? 'ctx.' : ''}http.${method}`, allowExternal: true };
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function propertyChain(node) {
|
|
252
|
+
const segments = [];
|
|
253
|
+
let current = node;
|
|
254
|
+
while (ts.isPropertyAccessExpression(current)) {
|
|
255
|
+
segments.unshift(current.name.text);
|
|
256
|
+
current = current.expression;
|
|
257
|
+
}
|
|
258
|
+
if (ts.isIdentifier(current)) segments.unshift(current.text);
|
|
259
|
+
return segments;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function fsCallKind(expression, context, scopes) {
|
|
263
|
+
if (ts.isIdentifier(expression)) {
|
|
264
|
+
const kind = resolveBindingKind(expression.text, context, scopes);
|
|
265
|
+
return kind?.startsWith('fsFunction:') ? kind.slice('fsFunction:'.length) : undefined;
|
|
266
|
+
}
|
|
267
|
+
if (!ts.isPropertyAccessExpression(expression)) return undefined;
|
|
268
|
+
const method = expression.name.text;
|
|
269
|
+
if (!FS_METHODS.has(method)) return undefined;
|
|
270
|
+
if (ts.isIdentifier(expression.expression)) {
|
|
271
|
+
const kind = resolveBindingKind(expression.expression.text, context, scopes);
|
|
272
|
+
return kind === 'fs' || kind === 'fsPromises' ? method : undefined;
|
|
273
|
+
}
|
|
274
|
+
if (ts.isPropertyAccessExpression(expression.expression)
|
|
275
|
+
&& ts.isIdentifier(expression.expression.expression)
|
|
276
|
+
&& expression.expression.name.text === 'promises'
|
|
277
|
+
&& resolveBindingKind(expression.expression.expression.text, context, scopes) === 'fs') return method;
|
|
278
|
+
if (ts.isCallExpression(expression.expression) && initializerModuleKind(expression.expression) === 'fs') return method;
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function pathArgumentIndexes(method) {
|
|
283
|
+
if (method === 'copyFile' || method === 'copyFileSync' || method === 'rename' || method === 'renameSync') return [0, 1];
|
|
284
|
+
return [0];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function scriptKindFor(fileName) {
|
|
288
|
+
const extension = typeof fileName === 'string' ? /\.([^.\\/]+)$/.exec(fileName)?.[1]?.toLowerCase() : undefined;
|
|
289
|
+
if (extension === 'tsx') return ts.ScriptKind.TSX;
|
|
290
|
+
if (extension === 'jsx') return ts.ScriptKind.JSX;
|
|
291
|
+
if (extension === 'js' || extension === 'mjs' || extension === 'cjs') return ts.ScriptKind.JS;
|
|
292
|
+
return ts.ScriptKind.TS;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** TypeScript AST 扫描器;URL 规则经回调注入,G100 可只消费文件系统结果。 */
|
|
296
|
+
export function scanPathSourceUsages(source, options = {}) {
|
|
297
|
+
const fileName = options.fileName ?? 'source-path-check.ts';
|
|
298
|
+
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, scriptKindFor(fileName));
|
|
299
|
+
const scopes = collectScopes(sourceFile);
|
|
300
|
+
const issues = [];
|
|
301
|
+
|
|
302
|
+
const visit = (node) => {
|
|
303
|
+
if (ts.isCallExpression(node)) {
|
|
304
|
+
const urlCall = httpCallKind(node.expression);
|
|
305
|
+
const url = node.arguments[0];
|
|
306
|
+
if (urlCall !== undefined && url !== undefined && options.isInvalidUrl !== undefined) {
|
|
307
|
+
const value = staticString(url, node, scopes);
|
|
308
|
+
if (value !== undefined && options.isInvalidUrl(value, urlCall.allowExternal)) {
|
|
309
|
+
issues.push({ kind: 'url', index: node.getStart(sourceFile), callee: urlCall.callee, value });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const fsMethod = fsCallKind(node.expression, node, scopes);
|
|
314
|
+
if (fsMethod !== undefined) {
|
|
315
|
+
for (const index of pathArgumentIndexes(fsMethod)) {
|
|
316
|
+
const expression = node.arguments[index];
|
|
317
|
+
if (expression === undefined) continue;
|
|
318
|
+
const result = analyzeFilePath(expression, node, scopes);
|
|
319
|
+
if (result === undefined || result.explicitBase === true) continue;
|
|
320
|
+
issues.push({
|
|
321
|
+
kind: 'filesystem',
|
|
322
|
+
pathKind: result.pathKind,
|
|
323
|
+
index: node.getStart(sourceFile),
|
|
324
|
+
callee: node.expression.getText(sourceFile),
|
|
325
|
+
value: result.value,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
ts.forEachChild(node, visit);
|
|
331
|
+
};
|
|
332
|
+
visit(sourceFile);
|
|
333
|
+
return issues;
|
|
334
|
+
}
|