@kayjs/kay 1.106.0 → 1.108.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/README.md +2 -2
- package/dist/eslint.js +477 -204
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.md +1 -1
- package/llms/actions-refused-fields.md +22 -0
- package/llms/actions.md +2 -22
- package/llms/browser-only.md +1 -1
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -1
- package/llms/components.md +1 -1
- package/llms/deprecated-lints.md +2 -4
- package/llms/island-and-form-lints.md +1 -0
- package/llms/islands.md +11 -2
- package/llms/project-new-and-add.md +1 -1
- package/llms/project.md +2 -2
- package/llms/schemas.md +6 -4
- package/llms/upgrading.md +25 -1
- package/llms/whats-new-archive.md +36 -1
- package/llms/whats-new.md +18 -35
- package/llms.txt +1 -1
- package/package.json +15 -6
- package/src/build/client.ts +23 -26
- package/src/build/entry-files.ts +19 -0
- package/src/build/icon-slots.ts +140 -0
- package/src/build/lazy-components.ts +130 -0
- package/src/build/lazy-manifest.ts +56 -0
- package/src/build/lazy-stubs.ts +31 -0
- package/src/build/libraries.ts +24 -2
- package/src/build/plugin.ts +13 -1
- package/src/build/runtime-chunk.ts +3 -3
- package/src/build/source-files.ts +8 -3
- package/src/build/transform.ts +17 -7
- package/src/build/variants.ts +97 -5
- package/src/check/action-options.ts +56 -0
- package/src/check/project.ts +4 -4
- package/src/check/renamed-packages.ts +3 -63
- package/src/check/schema-json.ts +226 -0
- package/src/check/shadow-types.ts +15 -27
- package/src/compiler/codegen.ts +7 -17
- package/src/compiler/dead-code.ts +22 -0
- package/src/compiler/deprecated-lints-1-105.ts +41 -0
- package/src/compiler/deprecated-lints-1-106.ts +1 -1
- package/src/compiler/deprecated-lints-1-107.ts +14 -0
- package/src/compiler/deprecated-lints.ts +8 -65
- package/src/compiler/dom-property.ts +23 -0
- package/src/compiler/dom.ts +15 -15
- package/src/compiler/import-edits.ts +15 -0
- package/src/compiler/index.ts +8 -3
- package/src/compiler/island-declaration.ts +15 -0
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lazy-imports.ts +357 -0
- package/src/compiler/parser.ts +2 -2
- package/src/compiler/route-lints.ts +2 -0
- package/src/compiler/schema-lints.ts +13 -0
- package/src/compiler/script-server.ts +6 -3
- package/src/content/validate.ts +1 -1
- package/src/new.ts +6 -2
- package/src/package-names.ts +1 -1
- package/src/runtime/action-call.ts +39 -327
- package/src/runtime/action-run.ts +357 -0
- package/src/runtime/action.ts +47 -4
- package/src/runtime/client.ts +10 -0
- package/src/runtime/dom-lazy.ts +276 -0
- package/src/runtime/field-errors.ts +58 -0
- package/src/runtime/field-refusal.ts +179 -0
- package/src/runtime/form.ts +3 -2
- package/src/runtime/html.ts +6 -1
- package/src/runtime/index.ts +2 -0
- package/src/runtime/island.server.ts +36 -2
- package/src/runtime/schema-record.ts +2 -1
- package/src/runtime/schema.client.ts +104 -14
- package/src/runtime/schema.ts +9 -2
- package/src/runtime/standard-schema.ts +32 -0
- package/src/runtime/validate-run.client.ts +179 -0
- package/src/runtime/validate.client.ts +11 -0
- package/src/runtime/validate.ts +4 -137
- package/src/server/actions.ts +48 -43
- package/src/server/answer.ts +102 -0
- package/src/server/api-input.ts +166 -0
- package/src/server/api.ts +12 -44
- package/src/server/app-stages.ts +15 -8
- package/src/server/app.ts +5 -2
- package/src/server/client.ts +2 -0
- package/src/server/dev.ts +7 -9
- package/src/server/file-limit.ts +7 -1
- package/src/server/forms.ts +1 -1
- package/src/server/host-allowlist.ts +7 -7
- package/src/server/index.ts +2 -0
- package/src/server/kay-routes.ts +221 -0
- package/src/server/kay-server.ts +228 -0
- package/src/server/kept-exports.ts +1 -0
- package/src/server/library.ts +0 -2
- package/src/server/mcp.ts +1 -1
- package/src/server/openapi.ts +12 -3
- package/src/server/phases.ts +249 -0
- package/src/server/protection.ts +6 -6
- package/src/server/proxy-key.ts +8 -8
- package/src/server/query.ts +4 -25
- package/src/server/receive.ts +43 -0
- package/src/server/render-document.ts +13 -3
- package/src/server/request-context.ts +137 -0
- package/src/server/schema-check.ts +1 -1
- package/src/server/socket.ts +19 -47
- package/src/server/stream.ts +1 -1
- package/src/server/validate.ts +346 -0
- package/src/test/mount.ts +6 -2
- package/types/build/client.d.ts +3 -2
- package/types/build/client.d.ts.map +1 -1
- package/types/build/entry-files.d.ts +4 -0
- package/types/build/entry-files.d.ts.map +1 -0
- package/types/build/icon-slots.d.ts +7 -0
- package/types/build/icon-slots.d.ts.map +1 -0
- package/types/build/lazy-components.d.ts +6 -0
- package/types/build/lazy-components.d.ts.map +1 -0
- package/types/build/lazy-manifest.d.ts +19 -0
- package/types/build/lazy-manifest.d.ts.map +1 -0
- package/types/build/lazy-stubs.d.ts +5 -0
- package/types/build/lazy-stubs.d.ts.map +1 -0
- package/types/build/libraries.d.ts +2 -0
- package/types/build/libraries.d.ts.map +1 -1
- package/types/build/plugin.d.ts +2 -0
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/source-files.d.ts.map +1 -1
- package/types/build/transform.d.ts +3 -1
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/check/action-options.d.ts +3 -0
- package/types/check/action-options.d.ts.map +1 -0
- package/types/check/renamed-packages.d.ts +0 -3
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-json.d.ts +3 -0
- package/types/check/schema-json.d.ts.map +1 -0
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +2 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/dead-code.d.ts +9 -0
- package/types/compiler/dead-code.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
- package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
- package/types/compiler/deprecated-lints-1-106.d.ts +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +36 -53
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/dom-property.d.ts +9 -0
- package/types/compiler/dom-property.d.ts.map +1 -0
- package/types/compiler/dom.d.ts +1 -0
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/import-edits.d.ts +1 -0
- package/types/compiler/import-edits.d.ts.map +1 -1
- package/types/compiler/index.d.ts +2 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/island-declaration.d.ts +5 -0
- package/types/compiler/island-declaration.d.ts.map +1 -1
- package/types/compiler/kay-exports.d.ts.map +1 -1
- package/types/compiler/lazy-imports.d.ts +33 -0
- package/types/compiler/lazy-imports.d.ts.map +1 -0
- package/types/compiler/lints.d.ts +44 -53
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/route-lints.d.ts +8 -0
- package/types/compiler/route-lints.d.ts.map +1 -1
- package/types/compiler/schema-lints.d.ts +11 -0
- package/types/compiler/schema-lints.d.ts.map +1 -0
- package/types/compiler/script-server.d.ts.map +1 -1
- package/types/new.d.ts +1 -0
- package/types/new.d.ts.map +1 -1
- package/types/runtime/action-call.d.ts.map +1 -1
- package/types/runtime/action-run.d.ts +23 -0
- package/types/runtime/action-run.d.ts.map +1 -0
- package/types/runtime/action.d.ts +26 -1
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/client.d.ts.map +1 -1
- package/types/runtime/dom-lazy.d.ts +6 -0
- package/types/runtime/dom-lazy.d.ts.map +1 -0
- package/types/runtime/field-errors.d.ts +4 -0
- package/types/runtime/field-errors.d.ts.map +1 -0
- package/types/runtime/field-refusal.d.ts +37 -0
- package/types/runtime/field-refusal.d.ts.map +1 -0
- package/types/runtime/form.d.ts +1 -1
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/index.d.ts +2 -0
- package/types/runtime/index.d.ts.map +1 -1
- package/types/runtime/island.server.d.ts +2 -0
- package/types/runtime/island.server.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts.map +1 -1
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +6 -1
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/standard-schema.d.ts +37 -0
- package/types/runtime/standard-schema.d.ts.map +1 -0
- package/types/runtime/validate-run.client.d.ts +4 -0
- package/types/runtime/validate-run.client.d.ts.map +1 -0
- package/types/runtime/validate.client.d.ts +4 -0
- package/types/runtime/validate.client.d.ts.map +1 -0
- package/types/runtime/validate.d.ts +1 -21
- package/types/runtime/validate.d.ts.map +1 -1
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/answer.d.ts +6 -0
- package/types/server/answer.d.ts.map +1 -0
- package/types/server/api-input.d.ts +19 -0
- package/types/server/api-input.d.ts.map +1 -0
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +4 -1
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/app.d.ts +2 -0
- package/types/server/app.d.ts.map +1 -1
- package/types/server/client.d.ts +1 -0
- package/types/server/client.d.ts.map +1 -1
- package/types/server/dev.d.ts +1 -1
- package/types/server/dev.d.ts.map +1 -1
- package/types/server/file-limit.d.ts.map +1 -1
- package/types/server/host-allowlist.d.ts +3 -3
- package/types/server/host-allowlist.d.ts.map +1 -1
- package/types/server/index.d.ts +2 -0
- package/types/server/index.d.ts.map +1 -1
- package/types/server/kay-routes.d.ts +47 -0
- package/types/server/kay-routes.d.ts.map +1 -0
- package/types/server/kay-server.d.ts +25 -0
- package/types/server/kay-server.d.ts.map +1 -0
- package/types/server/kept-exports.d.ts.map +1 -1
- package/types/server/library.d.ts +0 -1
- package/types/server/library.d.ts.map +1 -1
- package/types/server/openapi.d.ts.map +1 -1
- package/types/server/phases.d.ts +36 -0
- package/types/server/phases.d.ts.map +1 -0
- package/types/server/protection.d.ts +3 -2
- package/types/server/protection.d.ts.map +1 -1
- package/types/server/proxy-key.d.ts +3 -3
- package/types/server/proxy-key.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/receive.d.ts +9 -0
- package/types/server/receive.d.ts.map +1 -0
- package/types/server/render-document.d.ts.map +1 -1
- package/types/server/request-context.d.ts +29 -0
- package/types/server/request-context.d.ts.map +1 -0
- package/types/server/schema-check.d.ts +1 -0
- package/types/server/schema-check.d.ts.map +1 -1
- package/types/server/socket.d.ts +3 -5
- package/types/server/socket.d.ts.map +1 -1
- package/types/server/validate.d.ts +26 -0
- package/types/server/validate.d.ts.map +1 -0
- package/types/test/mount.d.ts.map +1 -1
- package/src/check/schema-type.ts +0 -9
- package/src/compiler/deprecated-script-fence.ts +0 -26
- package/src/server/field-errors.ts +0 -136
- package/types/check/schema-type.d.ts +0 -3
- package/types/check/schema-type.d.ts.map +0 -1
- package/types/compiler/deprecated-script-fence.d.ts +0 -3
- package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
- package/types/server/field-errors.d.ts +0 -6
- package/types/server/field-errors.d.ts.map +0 -1
package/src/build/transform.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Diagnostic, SourceMap } from '../compiler'
|
|
2
2
|
import type { ErrorName } from '../compiler/errors'
|
|
3
3
|
import type { IslandCause } from '../compiler/island-cause'
|
|
4
|
+
import type { ImportedComponents } from '../compiler/lazy-imports'
|
|
4
5
|
import type { LintLevels } from '../compiler/lints'
|
|
5
6
|
import type { ImportedSignals } from '../compiler/module-signals'
|
|
6
7
|
import type { ClassTable } from '../runtime/class-table'
|
|
@@ -17,6 +18,7 @@ import { projectPaths } from '../project'
|
|
|
17
18
|
import { awaitAlertOf } from './await-alert'
|
|
18
19
|
import { countCache, pruneCache, readCacheEntry, writeCacheEntry } from './cache'
|
|
19
20
|
import { compilerFingerprint } from './fingerprint'
|
|
21
|
+
import { importedComponents } from './lazy-components'
|
|
20
22
|
import { throughLinks } from './links'
|
|
21
23
|
import { namedRefresh } from './named-refresh'
|
|
22
24
|
import { isPageFile, realpathOrSelf } from './page-dirs'
|
|
@@ -65,6 +67,8 @@ export interface TransformOptions {
|
|
|
65
67
|
writtenProps?: ReadonlySet<string> | undefined
|
|
66
68
|
// The server's build of an app with Tailwind, which merges a shorthand beside a static `class` while rendering; set by `transformKayCached` from the app's stylesheets.
|
|
67
69
|
classMerge?: boolean | undefined
|
|
70
|
+
// False keeps every component's import eager.
|
|
71
|
+
lazy?: boolean | undefined
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
interface Compiled {
|
|
@@ -102,8 +106,8 @@ export class KayCompileError extends Error {
|
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
// .kay source to TypeScript building HTML strings on the server and DOM nodes in the browser; Bun strips the types when it loads the result.
|
|
105
|
-
export function transformKay(source: string, filename: string, options: TransformOptions, signals?: ImportedSignals): string {
|
|
106
|
-
const { code, map } = compile(source, filename, options, signals)
|
|
109
|
+
export function transformKay(source: string, filename: string, options: TransformOptions, signals?: ImportedSignals, components?: ImportedComponents): string {
|
|
110
|
+
const { code, map } = compile(source, filename, options, signals, components)
|
|
107
111
|
|
|
108
112
|
registerSourceMap(filename, { source, tsx: code, map }, options.target === 'dom')
|
|
109
113
|
|
|
@@ -123,9 +127,10 @@ export async function transformKayCached(original: string, filename: string, giv
|
|
|
123
127
|
|
|
124
128
|
// Read once: it is part of the cache key and, on a miss, what the compile reads too.
|
|
125
129
|
const signals = importedSignals(withoutTwin(filename), source)
|
|
130
|
+
const components = componentsOf(source, filename, options)
|
|
126
131
|
const hydratable = options.hydratable ?? false
|
|
127
132
|
const root = options.root ?? process.cwd()
|
|
128
|
-
const entry = cacheEntry(options.cacheDir, filename, `${options.target}${hydratable ? '-hydratable' : ''}${options.inspect === true ? '-inspect' : ''}${options.variant === undefined ? '' : `-uses-${Bun.hash(options.variant).toString(36)}`}`, `${options.target}\0${hydratable}\0${options.pages?.join(',')}\0${options.clientOnly === true}\0${options.inspect === true}\0${tableFingerprint(options.classTable)}\0${classMerge === true}\0${root}\0${throughLinks(root, filename)}\0${uiKeyOf(root, withoutTwin(filename))}\0${JSON.stringify(awaitAlertOf(root))}\0${JSON.stringify(signals)}\0${options.target === 'ssr' && namedRefresh(withoutTwin(filename), root)}\0${source}`)
|
|
133
|
+
const entry = cacheEntry(options.cacheDir, filename, `${options.target}${hydratable ? '-hydratable' : ''}${options.inspect === true ? '-inspect' : ''}${options.variant === undefined ? '' : `-uses-${Bun.hash(options.variant).toString(36)}`}`, `${options.target}\0${hydratable}\0${options.pages?.join(',')}\0${options.clientOnly === true}\0${options.inspect === true}\0${tableFingerprint(options.classTable)}\0${classMerge === true}\0${root}\0${throughLinks(root, filename)}\0${uiKeyOf(root, withoutTwin(filename))}\0${JSON.stringify(awaitAlertOf(root))}\0${JSON.stringify(signals)}\0${JSON.stringify(components)}\0${options.target === 'ssr' && namedRefresh(withoutTwin(filename), root)}\0${source}`)
|
|
129
134
|
const cached = await readCacheEntry<string>(entry)
|
|
130
135
|
|
|
131
136
|
countCache('components', cached !== undefined)
|
|
@@ -134,7 +139,7 @@ export async function transformKayCached(original: string, filename: string, giv
|
|
|
134
139
|
if (cached !== undefined) {
|
|
135
140
|
if (!hasSourceMapOf(filename, source, cached, options.target === 'dom')) {
|
|
136
141
|
registerSourceMap(filename, () => {
|
|
137
|
-
const { code, map } = compile(source, filename, options, signals)
|
|
142
|
+
const { code, map } = compile(source, filename, options, signals, components)
|
|
138
143
|
|
|
139
144
|
return { source, tsx: code, map }
|
|
140
145
|
}, options.target === 'dom')
|
|
@@ -143,7 +148,7 @@ export async function transformKayCached(original: string, filename: string, giv
|
|
|
143
148
|
return cached
|
|
144
149
|
}
|
|
145
150
|
|
|
146
|
-
const code = transformKay(source, filename, options, signals)
|
|
151
|
+
const code = transformKay(source, filename, options, signals, components)
|
|
147
152
|
|
|
148
153
|
await writeCacheEntry(entry, code)
|
|
149
154
|
|
|
@@ -247,8 +252,13 @@ export function cacheEntry(cacheDir: string, filename: string, kind: string, inp
|
|
|
247
252
|
}
|
|
248
253
|
}
|
|
249
254
|
|
|
255
|
+
// The imported components a file may draw lazily: for the browser's build and the server's hydratable one, the only two that split them.
|
|
256
|
+
function componentsOf(source: string, filename: string, options: TransformOptions) {
|
|
257
|
+
return options.lazy !== false && (options.target === 'dom' || options.hydratable === true) ? importedComponents(withoutTwin(filename), source, options.root ?? process.cwd()) : undefined
|
|
258
|
+
}
|
|
259
|
+
|
|
250
260
|
// `filename` may name a static component's `?island` twin, the build of it that renders inside islands.
|
|
251
|
-
function compile(source: string, filename: string, options: TransformOptions, signals = importedSignals(withoutTwin(filename), source)): Compiled {
|
|
261
|
+
function compile(source: string, filename: string, options: TransformOptions, signals = importedSignals(withoutTwin(filename), source), components = componentsOf(source, filename, options)): Compiled {
|
|
252
262
|
const server = options.target === 'ssr'
|
|
253
263
|
const file = withoutTwin(filename)
|
|
254
264
|
const isTwin = file !== filename
|
|
@@ -256,7 +266,7 @@ function compile(source: string, filename: string, options: TransformOptions, si
|
|
|
256
266
|
const root = options.root ?? process.cwd()
|
|
257
267
|
// A page of the routes directory, whose root `@failed` fills a layout outlet rather than being a component's own block.
|
|
258
268
|
const page = isPageFile(file, options.pages ?? [projectPaths(root).routes]) ? true : undefined
|
|
259
|
-
const { tsx, map, diagnostics } = compileKay(source, throughLinks(root, file), { root: options.root, page, includeServer: server, output: server ? 'html' : 'dom', hydratable, twinBuild: isTwin, clientOnly: options.clientOnly, inspect: options.inspect, classTable: options.target === 'dom' ? options.classTable : undefined, classMerge: server ? options.classMerge : undefined, awaitAlert: awaitAlertOf(root, file), importedSignals: signals, namedRefresh: server && namedRefresh(file, root), ui: uiEntryOf(options.root ?? process.cwd(), file), writtenProps: options.writtenProps })
|
|
269
|
+
const { tsx, map, diagnostics } = compileKay(source, throughLinks(root, file), { root: options.root, page, includeServer: server, output: server ? 'html' : 'dom', hydratable, twinBuild: isTwin, clientOnly: options.clientOnly, inspect: options.inspect, classTable: options.target === 'dom' ? options.classTable : undefined, classMerge: server ? options.classMerge : undefined, awaitAlert: awaitAlertOf(root, file), importedSignals: signals, components, namedRefresh: server && namedRefresh(file, root), ui: uiEntryOf(options.root ?? process.cwd(), file), writtenProps: options.writtenProps })
|
|
260
270
|
|
|
261
271
|
if (diagnostics.length > 0)
|
|
262
272
|
throw new KayCompileError(diagnostics.map(diagnostic => ({ ...diagnostic, file, source })))
|
package/src/build/variants.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { parseExpanded } from '../compiler/markdown-block'
|
|
|
9
9
|
// A component names the features an app may leave out with `uses('selectable')`, the `uses` it imports from kayjs-ui's marker module: a call that is true while the prop is there to be written, and that a browser build replaces with `true` or `false` once it has read the props the files of the project write at their call sites. See `specializedSource`.
|
|
10
10
|
const USES_CALL = /\buses\(\s*'(\w+)'(\s*,\s*true)?\s*\)/y
|
|
11
11
|
const KAY_IMPORT = /^import\s+(\w+)\s+from\s+(["'])([^"'\n]+\.kay)\2/gm
|
|
12
|
+
// A component the compiler imports lazily, `KayLazy(key, () => import("./it.kay"))`.
|
|
13
|
+
const LAZY_IMPORT = /\bimport\(\s*(["'])([^"'\n]+\.kay)\1\s*\)/g
|
|
12
14
|
const USES_IMPORT = /^import\s*\{[^}]*\buses\b[^}]*\}\s*from\s*(["'])([^"'\n]+)\1/m
|
|
13
15
|
// The marker module, by where the library keeps it: an app's own `uses` is another function.
|
|
14
16
|
const MARKER_FILE = /(?:packages\/ui|@kayjs\/ui)\/src\/uses\.ts$/
|
|
@@ -297,6 +299,95 @@ function splitTop(text: string, operator: '&&' | '||'): string[] | undefined {
|
|
|
297
299
|
return parts
|
|
298
300
|
}
|
|
299
301
|
|
|
302
|
+
// The depth-0 positions of `char` in `text`, outside brackets and strings; a `?.` and a `??` are no ternary.
|
|
303
|
+
function topLevel(text: string, char: '?' | ':'): number[] {
|
|
304
|
+
const found: number[] = []
|
|
305
|
+
let depth = 0
|
|
306
|
+
let quote = ''
|
|
307
|
+
|
|
308
|
+
for (let at = 0; at < text.length; at++) {
|
|
309
|
+
const here = text[at]!
|
|
310
|
+
|
|
311
|
+
if (quote !== '') {
|
|
312
|
+
if (here === '\\')
|
|
313
|
+
at++
|
|
314
|
+
else if (here === quote)
|
|
315
|
+
quote = ''
|
|
316
|
+
}
|
|
317
|
+
else if (here === '\'' || here === '"' || here === '`') {
|
|
318
|
+
quote = here
|
|
319
|
+
}
|
|
320
|
+
else if (here === '(' || here === '[' || here === '{') {
|
|
321
|
+
depth++
|
|
322
|
+
}
|
|
323
|
+
else if (here === ')' || here === ']' || here === '}') {
|
|
324
|
+
depth--
|
|
325
|
+
}
|
|
326
|
+
else if (here === char && depth === 0 && (char === ':' || (text[at + 1] !== '.' && text[at + 1] !== '?'))) {
|
|
327
|
+
found.push(at)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return found
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Where the brace opened just before `from` closes, past the strings in between; -1 when it never does, or a template literal holds a `${`, whose nesting is not tracked.
|
|
335
|
+
function closingBrace(text: string, from: number): number {
|
|
336
|
+
let depth = 1
|
|
337
|
+
let quote = ''
|
|
338
|
+
|
|
339
|
+
for (let at = from; at < text.length; at++) {
|
|
340
|
+
const char = text[at]!
|
|
341
|
+
|
|
342
|
+
if (quote !== '') {
|
|
343
|
+
if (char === '\\')
|
|
344
|
+
at++
|
|
345
|
+
else if (quote === '`' && char === '$' && text[at + 1] === '{')
|
|
346
|
+
return -1
|
|
347
|
+
else if (char === quote)
|
|
348
|
+
quote = ''
|
|
349
|
+
}
|
|
350
|
+
else if (char === '\'' || char === '"' || char === '`') {
|
|
351
|
+
quote = char
|
|
352
|
+
}
|
|
353
|
+
else if (char === '{') {
|
|
354
|
+
depth++
|
|
355
|
+
}
|
|
356
|
+
else if (char === '}' && --depth === 0) {
|
|
357
|
+
return at
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return -1
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// An arrow, an assignment or a comma in a condition binds looser than the ternary, so what comes before the `?` is not the whole test.
|
|
365
|
+
const LOWER_THAN_TERNARY = /=>|,|(?<![=!<>])=(?!=)/
|
|
366
|
+
|
|
367
|
+
// An attribute `name={cond ? value : undefined}` whose condition is false is `{undefined}`, which the compiler leaves unbound. Only a ternary with no other at its top level is read.
|
|
368
|
+
function foldAbsentAttributes(line: string): string {
|
|
369
|
+
let result = line
|
|
370
|
+
let from = 0
|
|
371
|
+
|
|
372
|
+
for (;;) {
|
|
373
|
+
const open = result.indexOf('={', from)
|
|
374
|
+
|
|
375
|
+
if (open === -1)
|
|
376
|
+
return result
|
|
377
|
+
|
|
378
|
+
const start = open + 2
|
|
379
|
+
const end = closingBrace(result, start)
|
|
380
|
+
const inner = result.slice(start, end)
|
|
381
|
+
const question = topLevel(inner, '?')[0]
|
|
382
|
+
const colons = topLevel(inner, ':')
|
|
383
|
+
|
|
384
|
+
from = start
|
|
385
|
+
|
|
386
|
+
if (end !== -1 && question !== undefined && colons.length === 1 && inner.slice(colons[0]! + 1).trim() === 'undefined' && !LOWER_THAN_TERNARY.test(inner.slice(0, question)) && alwaysFalse(inner.slice(0, question)))
|
|
387
|
+
result = `${result.slice(0, start)}undefined${result.slice(end)}`
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
300
391
|
// A destructured default that only an absent feature reads, `label = uses('x') ? wording('a', 'A') : ''` once the call is false: the closure it would make is left out.
|
|
301
392
|
const ABSENT_DEFAULT = / = false(?: \|\| false)* \? wording\([^()]*\) : ''/g
|
|
302
393
|
const DEAD_SPREAD = /^(\s*)\.\.\.\(false(?: \|\| false)*\)\s*$/
|
|
@@ -341,7 +432,7 @@ export function specializedSource(source: string, present: ReadonlySet<string>,
|
|
|
341
432
|
if (hit === null) {
|
|
342
433
|
// A call in an attribute or a text expression is folded as one in the script.
|
|
343
434
|
if (lines[at]!.includes('uses('))
|
|
344
|
-
lines[at] = replaced(lines[at]!)
|
|
435
|
+
lines[at] = foldAbsentAttributes(replaced(lines[at]!))
|
|
345
436
|
|
|
346
437
|
continue
|
|
347
438
|
}
|
|
@@ -452,11 +543,11 @@ interface Candidate {
|
|
|
452
543
|
async function candidatesOf(code: string, file: string): Promise<Candidate[]> {
|
|
453
544
|
const found: Candidate[] = []
|
|
454
545
|
|
|
455
|
-
for (const [
|
|
546
|
+
for (const [local, specifier] of [...code.matchAll(KAY_IMPORT).map(([, name, , from]) => [name!, from!] as const), ...code.matchAll(LAZY_IMPORT).map(([, , from]) => ['', from!] as const)]) {
|
|
456
547
|
let path: string
|
|
457
548
|
|
|
458
549
|
try {
|
|
459
|
-
path = Bun.resolveSync(specifier
|
|
550
|
+
path = Bun.resolveSync(specifier, dirname(file))
|
|
460
551
|
}
|
|
461
552
|
catch {
|
|
462
553
|
continue
|
|
@@ -465,7 +556,7 @@ async function candidatesOf(code: string, file: string): Promise<Candidate[]> {
|
|
|
465
556
|
const { names: features, defaults } = await declaredFeatures(path)
|
|
466
557
|
|
|
467
558
|
if (features.length > 0)
|
|
468
|
-
found.push({ local
|
|
559
|
+
found.push({ local, specifier, path, features, defaults })
|
|
469
560
|
}
|
|
470
561
|
|
|
471
562
|
return found
|
|
@@ -503,7 +594,7 @@ export class VariantIndex {
|
|
|
503
594
|
}
|
|
504
595
|
|
|
505
596
|
private async read(file: string, source: string): Promise<void> {
|
|
506
|
-
const candidates = await candidatesOf(source, file)
|
|
597
|
+
const candidates = (await candidatesOf(source, file)).filter(candidate => candidate.local !== '')
|
|
507
598
|
|
|
508
599
|
if (candidates.length === 0)
|
|
509
600
|
return
|
|
@@ -563,6 +654,7 @@ export class VariantIndex {
|
|
|
563
654
|
const used = this.usedOf(path, features, defaults)
|
|
564
655
|
|
|
565
656
|
result = result.replace(new RegExp(`^(import\\s+\\w+\\s+from\\s+)(["'])${RegExp.escape(specifier)}\\2`, 'm'), (_import, head: string, quote: string) => `${head}${quote}${specifier}?uses=${used}${quote}`)
|
|
657
|
+
result = result.replace(new RegExp(`(\\bimport\\(\\s*)(["'])${RegExp.escape(specifier)}\\2`, 'g'), (_import, head: string, quote: string) => `${head}${quote}${specifier}?uses=${used}${quote}`)
|
|
566
658
|
}
|
|
567
659
|
|
|
568
660
|
return result
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { CheckDiagnostic, CheckOptions } from '.'
|
|
2
|
+
import type { Node } from '../compiler/estree'
|
|
3
|
+
import { lintLevel } from '../compiler/lints'
|
|
4
|
+
import { importsOf, walk } from './ast'
|
|
5
|
+
import { scan } from './renamed-exports'
|
|
6
|
+
|
|
7
|
+
const KAY = /^kay(?:\/|$)/
|
|
8
|
+
const NEEDLE = /\baction\s*\(/
|
|
9
|
+
const TRAILING_COMMA = /,$/
|
|
10
|
+
const FUNCTIONS = new Set(['ArrowFunctionExpression', 'FunctionExpression'])
|
|
11
|
+
|
|
12
|
+
// `action(schema, handler, options)` and `action(handler, options)` as kay 1.107 replaces them: the options come first, with the input in them, as an API route's do: `action({ input: schema, ...options }, handler)`.
|
|
13
|
+
// Found on the `action` a file imports from `kay`, in the TypeScript files and the fences of `.kay` files; the fix moves the arguments and keeps their text.
|
|
14
|
+
export async function actionOptions(root: string, options: CheckOptions): Promise<CheckDiagnostic[]> {
|
|
15
|
+
if (lintLevel('deprecated-action-options', options.lints) === 'allow')
|
|
16
|
+
return []
|
|
17
|
+
|
|
18
|
+
return scan(root, options, 'deprecated-action-options', NEEDLE, ({ parsed, report }) => {
|
|
19
|
+
for (const { ast, base, text } of parsed) {
|
|
20
|
+
const locals = importsOf(ast, source => KAY.test(source), 'action').locals
|
|
21
|
+
|
|
22
|
+
if (locals.size === 0)
|
|
23
|
+
continue
|
|
24
|
+
|
|
25
|
+
const slice = (node: Node): string => text.slice(node.start, node.end)
|
|
26
|
+
|
|
27
|
+
walk(ast, (node) => {
|
|
28
|
+
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier' || !locals.has(node.callee.name))
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
const [first, second, third] = node.arguments
|
|
32
|
+
|
|
33
|
+
if (first === undefined || second === undefined || node.arguments.length > 3 || node.arguments.some(argument => argument.type === 'SpreadElement'))
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
// `action(handler, options)`
|
|
37
|
+
if (FUNCTIONS.has(first.type)) {
|
|
38
|
+
if (third !== undefined || FUNCTIONS.has(second.type))
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
report(base + node.start, base + node.end, 'an action takes its options before the handler since 1.107, as an API route does, `action(options, handler)`; the old order works until 2.0', 'write the options first', [{ start: base + first.start, end: base + second.end, text: `${slice(second)}, ${slice(first)}` }])
|
|
42
|
+
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// `action(schema, handler, options)`
|
|
47
|
+
if (third === undefined)
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
const inner = third.type === 'ObjectExpression' ? text.slice(third.start + 1, third.end - 1).trim().replace(TRAILING_COMMA, '') : `...${slice(third)}`
|
|
51
|
+
|
|
52
|
+
report(base + node.start, base + node.end, 'an action takes its input and its options in one object before the handler since 1.107, `action({ input, ...options }, handler)`; the old form works until 2.0', 'write the input and the options first', [{ start: base + first.start, end: base + third.end, text: `{ input: ${slice(first)}${inner === '' ? '' : `, ${inner}`} }, ${slice(second)}` }])
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
package/src/check/project.ts
CHANGED
|
@@ -34,7 +34,7 @@ import { browserModules, islandEdges } from './island-modules'
|
|
|
34
34
|
import { componentImporters, islandOnlyComponents, renderedInIslands } from './island-only'
|
|
35
35
|
import { linkFlagTests } from './link-flag-tests'
|
|
36
36
|
import { FILES_MARKER, stampOf } from './loaded'
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
import { configSeoLints } from './seo'
|
|
39
39
|
import { serverOnlyInIsland } from './server-only-reads'
|
|
40
40
|
import { fromCompile } from './shadow-types'
|
|
@@ -106,13 +106,13 @@ export function configProblems(root: string, options: CheckOptions, types: Shado
|
|
|
106
106
|
// The collections may still be read, which only the feeds and their own problems wait for.
|
|
107
107
|
export async function projectProblems(root: string, options: CheckOptions, collections: ContentChecks | Promise<ContentChecks>, memory?: ProbeMemory, scanned?: ServedProject): Promise<CheckDiagnostic[]> {
|
|
108
108
|
return inWalkScope(async () => {
|
|
109
|
-
const [content, messages, boot, modules,
|
|
109
|
+
const [content, messages, boot, modules, linkTests] = await Promise.all([collections, checkMessages(root, options, memory), (async () => {
|
|
110
110
|
const read = await collections
|
|
111
111
|
|
|
112
112
|
return bootProblems(resolve(root), options, read.feeds, scanned, read.routes)
|
|
113
|
-
})(), moduleLints(root, options),
|
|
113
|
+
})(), moduleLints(root, options), linkFlagTests(root, options.lints)])
|
|
114
114
|
|
|
115
|
-
return [...content.problems, ...messages, ...boot, ...modules, ...
|
|
115
|
+
return [...content.problems, ...messages, ...boot, ...modules, ...linkTests, ...uiProblems(root), ...contextModuleProblems(root)]
|
|
116
116
|
})
|
|
117
117
|
}
|
|
118
118
|
|
|
@@ -1,34 +1,12 @@
|
|
|
1
|
-
import type { CheckDiagnostic } from '.'
|
|
2
|
-
import type { LintLevels } from '../compiler/lints'
|
|
3
1
|
import { writeFile } from 'node:fs/promises'
|
|
4
2
|
import { basename } from 'node:path'
|
|
5
3
|
import { findProjectFiles } from '../build/files'
|
|
6
|
-
|
|
7
|
-
import { OLD_PACKAGE_NAME,
|
|
4
|
+
|
|
5
|
+
import { OLD_PACKAGE_NAME, RENAMED_PACKAGES } from '../package-names'
|
|
8
6
|
|
|
9
7
|
// The files that name a package: the scripts, components and stylesheets of the project, and its manifest and tsconfigs.
|
|
10
8
|
const NAMING_FILE = /^(?:package\.json|tsconfig[\w.-]*\.json)$|\.(?:[cm]?[jt]sx?|kay|css)$/
|
|
11
9
|
|
|
12
|
-
// The version a manifest entry asks of the package: after a key, `"@kaynooo/kayjs-ui": "~1.103.0"`, or after an alias, `npm:@kaynooo/kayjs@~1.103.0`.
|
|
13
|
-
const RANGE_AFTER_KEY = /"\s*:\s*"[~^]?(\d+\.\d+\.\d+)(?=")/y
|
|
14
|
-
const RANGE_AFTER_ALIAS = /@[~^]?(\d+\.\d+\.\d+)(?=")/y
|
|
15
|
-
|
|
16
|
-
// The range's version, when it is below the first one published under the new names, which no version of the new package satisfies: written as that first version, keeping its `~` or `^`.
|
|
17
|
-
function raisedRange(source: string, from: number): { start: number, end: number, text: string } | undefined {
|
|
18
|
-
const pattern = source[from] === '"' ? RANGE_AFTER_KEY : RANGE_AFTER_ALIAS
|
|
19
|
-
|
|
20
|
-
pattern.lastIndex = from
|
|
21
|
-
|
|
22
|
-
const found = pattern.exec(source)
|
|
23
|
-
|
|
24
|
-
if (found === null || Bun.semver.order(found[1]!, RENAMED_IN) !== -1)
|
|
25
|
-
return undefined
|
|
26
|
-
|
|
27
|
-
const start = from + found[0].length - found[1]!.length
|
|
28
|
-
|
|
29
|
-
return { start, end: start + found[1]!.length, text: RENAMED_IN }
|
|
30
|
-
}
|
|
31
|
-
|
|
32
10
|
const BLOCK_COMMENT = /\/\*[\s\S]*?(?:\*\/|$)/g
|
|
33
11
|
|
|
34
12
|
// What a name may follow inside its string: nothing, an alias, or a path through node_modules, as a stylesheet's `@source`.
|
|
@@ -86,7 +64,7 @@ function kindOf(file: string): Kind {
|
|
|
86
64
|
return file.endsWith('.css') ? 'stylesheet' : 'script'
|
|
87
65
|
}
|
|
88
66
|
|
|
89
|
-
// The position of each old package name a file depends on, as
|
|
67
|
+
// The position of each old package name a file depends on, as `kay upgrade` rewrites them.
|
|
90
68
|
export function oldNamesIn(file: string, source: string): RegExpExecArray[] {
|
|
91
69
|
const kind = kindOf(file)
|
|
92
70
|
const comments = kind === 'manifest' || kind === 'tsconfig' ? [] : [...source.matchAll(BLOCK_COMMENT)].map((c): [number, number] => [c.index, c.index + c[0].length])
|
|
@@ -117,41 +95,3 @@ export async function renameProjectFiles(root: string): Promise<void> {
|
|
|
117
95
|
await writeFile(file, renamed + source.slice(from))
|
|
118
96
|
}
|
|
119
97
|
}
|
|
120
|
-
|
|
121
|
-
// kay and its libraries under the `@kaynooo` scope, named as an import, `require` or `import()` specifier, a config key, a stylesheet's `@import` or `@plugin` path or a package.json entry, which are `@kayjs/*` since 1.104: each name found gets a safe fix writing the new one.
|
|
122
|
-
// A package.json that lists the new name already would get the entry twice, so its names are reported with a fix that is not safe; one that does not has the range raised to the first version under the new name.
|
|
123
|
-
export async function renamedPackages(root: string, levels: LintLevels | undefined): Promise<CheckDiagnostic[]> {
|
|
124
|
-
const level = lintLevel('deprecated-package-names', levels)
|
|
125
|
-
|
|
126
|
-
if (level === 'allow')
|
|
127
|
-
return []
|
|
128
|
-
|
|
129
|
-
const found: CheckDiagnostic[] = []
|
|
130
|
-
|
|
131
|
-
for (const file of (await findProjectFiles(root, NAMING_FILE, { project: true, ownKayOnly: true })).sort()) {
|
|
132
|
-
const source = await Bun.file(file).text()
|
|
133
|
-
const manifest = basename(file) === 'package.json'
|
|
134
|
-
|
|
135
|
-
for (const match of oldNamesIn(file, source)) {
|
|
136
|
-
const name = match[0]
|
|
137
|
-
|
|
138
|
-
const next = RENAMED_PACKAGES[name]!
|
|
139
|
-
const before = source.slice(0, match.index).split('\n')
|
|
140
|
-
const raised = manifest ? raisedRange(source, match.index + name.length) : undefined
|
|
141
|
-
|
|
142
|
-
found.push({
|
|
143
|
-
file,
|
|
144
|
-
line: before.length,
|
|
145
|
-
column: before.at(-1)!.length + 1,
|
|
146
|
-
end: match.index + name.length,
|
|
147
|
-
severity: level === 'warn' ? 'warning' : 'error',
|
|
148
|
-
code: 'deprecated-package-names',
|
|
149
|
-
message: `\`${name}\` is \`${next}\` since 1.104: kay and its libraries are published under the \`@kayjs\` scope`,
|
|
150
|
-
help: manifest ? `write \`${next}\` and run \`bun install\`, or run \`kay upgrade\`` : `import from \`${next}\``,
|
|
151
|
-
fixes: [{ title: `write ${next}`, safe: !manifest || !source.includes(`"${next}"`), edits: [{ start: match.index, end: match.index + name.length, text: next }, ...raised === undefined ? [] : [raised]] }],
|
|
152
|
-
})
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return found
|
|
157
|
-
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { CheckDiagnostic, CheckOptions } from '.'
|
|
2
|
+
import type { Node, Program } from '../compiler/estree'
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
4
|
+
import { dirname, resolve } from 'node:path'
|
|
5
|
+
import { parseProgram } from '../compiler/estree'
|
|
6
|
+
import { apiScope, fromApi, isPlain, keyName } from './api-scope'
|
|
7
|
+
import { importedName, importsOf, walk } from './ast'
|
|
8
|
+
import { scan } from './renamed-exports'
|
|
9
|
+
|
|
10
|
+
const KAY = /^kay(?:\/|$)/
|
|
11
|
+
const NEEDLE = /\baction\b|\bapi\b/
|
|
12
|
+
const VALIBOT = /^valibot(?:\/|$)/
|
|
13
|
+
const SOURCE = /\.tsx?$/
|
|
14
|
+
const ROUTE_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete', 'all'])
|
|
15
|
+
const ROUTE_SCHEMAS = new Set(['body', 'query', 'params', 'headers', 'cookie'])
|
|
16
|
+
const ACTION_SCHEMAS = new Set(['input'])
|
|
17
|
+
const RELATIVE = /^\.\.?\//
|
|
18
|
+
const EXTENSIONS = ['', '.ts', '.tsx', '/index.ts']
|
|
19
|
+
const WRAPPERS = new Set(['TSAsExpression', 'TSSatisfiesExpression', 'TSNonNullExpression', 'ParenthesizedExpression'])
|
|
20
|
+
const MAX_HOPS = 4
|
|
21
|
+
|
|
22
|
+
// A module of the project: its program and where it is, so a name it imports from a neighbour is read there.
|
|
23
|
+
interface Module {
|
|
24
|
+
ast: Program
|
|
25
|
+
file: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function readModule(from: string, specifier: string): Module | undefined {
|
|
29
|
+
for (const extension of EXTENSIONS) {
|
|
30
|
+
const file = resolve(dirname(from), specifier + extension)
|
|
31
|
+
|
|
32
|
+
if (!SOURCE.test(file) || !existsSync(file))
|
|
33
|
+
continue
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
return { ast: parseProgram(readFileSync(file, 'utf8'), { lang: file.endsWith('.tsx') ? 'tsx' : 'ts', recover: true }).program, file }
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return undefined
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The initializer of the top-level `const name`, exported or not.
|
|
47
|
+
function declared(ast: Program, name: string): Node | undefined {
|
|
48
|
+
for (const statement of ast.body) {
|
|
49
|
+
const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement
|
|
50
|
+
|
|
51
|
+
if (declaration?.type !== 'VariableDeclaration')
|
|
52
|
+
continue
|
|
53
|
+
|
|
54
|
+
for (const declarator of declaration.declarations) {
|
|
55
|
+
if (declarator.id.type === 'Identifier' && declarator.id.name === name && declarator.init)
|
|
56
|
+
return declarator.init
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The module and the name a relative import binds `local` to; `default` for a default import.
|
|
64
|
+
function importedFrom({ ast, file }: Module, local: string): { module: Module, name: string } | undefined {
|
|
65
|
+
for (const statement of ast.body) {
|
|
66
|
+
if (statement.type !== 'ImportDeclaration' || statement.importKind === 'type' || !RELATIVE.test(statement.source.value))
|
|
67
|
+
continue
|
|
68
|
+
|
|
69
|
+
for (const specifier of statement.specifiers) {
|
|
70
|
+
if (specifier.type === 'ImportNamespaceSpecifier' || specifier.local.name !== local)
|
|
71
|
+
continue
|
|
72
|
+
|
|
73
|
+
const module = readModule(file, statement.source.value)
|
|
74
|
+
const name = specifier.type === 'ImportDefaultSpecifier' ? 'default' : importedName(specifier)
|
|
75
|
+
|
|
76
|
+
return module && { module, name }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// What a module exports under `name`: a `const`, `export default <expression>`, or one `export { name } from './other'` hop.
|
|
84
|
+
function exported(module: Module, name: string, hops = 0): { init: Node, module: Module } | undefined {
|
|
85
|
+
for (const statement of module.ast.body) {
|
|
86
|
+
if (name === 'default' && statement.type === 'ExportDefaultDeclaration' && !statement.declaration.type.endsWith('Declaration'))
|
|
87
|
+
return { init: statement.declaration as unknown as Node, module }
|
|
88
|
+
|
|
89
|
+
if (statement.type !== 'ExportNamedDeclaration' || statement.source === null || hops > 0)
|
|
90
|
+
continue
|
|
91
|
+
|
|
92
|
+
for (const specifier of statement.specifiers) {
|
|
93
|
+
if ((specifier.exported.type === 'Identifier' ? specifier.exported.name : specifier.exported.value) !== name)
|
|
94
|
+
continue
|
|
95
|
+
|
|
96
|
+
const next = readModule(module.file, statement.source.value)
|
|
97
|
+
|
|
98
|
+
return next && exported(next, specifier.local.type === 'Identifier' ? specifier.local.name : specifier.local.value, hops + 1)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const init = declared(module.ast, name)
|
|
103
|
+
|
|
104
|
+
return init && { init, module }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function paramName(param: { type: string, left?: unknown, name?: string }): string | undefined {
|
|
108
|
+
if (param.type === 'AssignmentPattern')
|
|
109
|
+
return paramName(param.left as { type: string })
|
|
110
|
+
|
|
111
|
+
return param.type === 'Identifier' ? param.name : undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Whether the file binds `name` anywhere but its import, as a parameter or a declaration, so a use of it may be that binding.
|
|
115
|
+
function rebound(ast: Program, name: string): boolean {
|
|
116
|
+
let found = false
|
|
117
|
+
|
|
118
|
+
walk(ast, (node) => {
|
|
119
|
+
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.id.name === name)
|
|
120
|
+
found = true
|
|
121
|
+
else if ((node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && node.params.some(param => paramName(param) === name))
|
|
122
|
+
found = true
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return found
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function fromValibot({ ast }: Module, name: string): boolean {
|
|
129
|
+
return ast.body.some(statement => statement.type === 'ImportDeclaration' && statement.importKind !== 'type' && VALIBOT.test(statement.source.value) && statement.specifiers.some(specifier => specifier.local.name === name)) && !rebound(ast, name)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Whether the expression names `toStandardJsonSchema` anywhere, which gives a valibot schema what it lacks.
|
|
133
|
+
function wrapsJsonSchema(node: Node): boolean {
|
|
134
|
+
let found = false
|
|
135
|
+
|
|
136
|
+
walk(node, (inner) => {
|
|
137
|
+
if (inner.type === 'Identifier' && inner.name === 'toStandardJsonSchema')
|
|
138
|
+
found = true
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
return found
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Whether an object literal holds `~standard` with no `jsonSchema` in it; one with a spread, or a `~standard` that is no literal, is taken to have it.
|
|
145
|
+
function handWritten(object: Extract<Node, { type: 'ObjectExpression' }>): boolean {
|
|
146
|
+
const standard = object.properties.find(property => isPlain(property) && keyName(property) === '~standard')
|
|
147
|
+
|
|
148
|
+
if (standard === undefined || !isPlain(standard) || standard.value.type !== 'ObjectExpression')
|
|
149
|
+
return false
|
|
150
|
+
|
|
151
|
+
return standard.value.properties.every(property => isPlain(property) && keyName(property) !== 'jsonSchema')
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Whether the expression is a Standard Schema validator known to lack `~standard.jsonSchema`: an object literal written by hand, or what valibot builds, which carries none until `toStandardJsonSchema` wraps it.
|
|
155
|
+
// A name is followed to its `const`, here or in a module imported by a relative path; anything else, a call of the app's own or a library other than valibot, is taken to have it.
|
|
156
|
+
function lacksJsonSchema(node: Node, module: Module, hops = 0): boolean {
|
|
157
|
+
if (wrapsJsonSchema(node))
|
|
158
|
+
return false
|
|
159
|
+
|
|
160
|
+
let current = node
|
|
161
|
+
|
|
162
|
+
for (;;) {
|
|
163
|
+
if (WRAPPERS.has(current.type))
|
|
164
|
+
current = (current as unknown as { expression: Node }).expression
|
|
165
|
+
else if (current.type === 'CallExpression')
|
|
166
|
+
current = current.callee
|
|
167
|
+
else if (current.type === 'MemberExpression')
|
|
168
|
+
current = current.object
|
|
169
|
+
else
|
|
170
|
+
break
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (current.type === 'ObjectExpression')
|
|
174
|
+
return handWritten(current)
|
|
175
|
+
|
|
176
|
+
if (current.type !== 'Identifier' || hops > MAX_HOPS)
|
|
177
|
+
return false
|
|
178
|
+
|
|
179
|
+
if (fromValibot(module, current.name))
|
|
180
|
+
return true
|
|
181
|
+
|
|
182
|
+
const init = declared(module.ast, current.name)
|
|
183
|
+
|
|
184
|
+
if (init)
|
|
185
|
+
return lacksJsonSchema(init, module, hops + 1)
|
|
186
|
+
|
|
187
|
+
const imported = importedFrom(module, current.name)
|
|
188
|
+
const remote = imported && exported(imported.module, imported.name)
|
|
189
|
+
|
|
190
|
+
return remote !== undefined && lacksJsonSchema(remote.init, remote.module, hops + 1)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// The values of the options object's members named `names`.
|
|
194
|
+
function positions(options: Node | undefined, names: Set<string>): Node[] {
|
|
195
|
+
if (options?.type !== 'ObjectExpression')
|
|
196
|
+
return []
|
|
197
|
+
|
|
198
|
+
return options.properties.flatMap(property => isPlain(property) && names.has(keyName(property) ?? '') ? [property.value] : [])
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// A Standard Schema validator with no `~standard.jsonSchema` where an action's `input` or an API route's `body`, `query`, `params`, `headers` or `cookie` takes a schema: it still validates, but a form shows no constraints, a form or a query is not read as numbers and booleans, and OpenAPI and MCP show an open schema.
|
|
202
|
+
// Read in the TypeScript files and the fences of `.kay` files alike, from what the source shows: an object literal with `~standard`, and valibot's builders, followed through a `const` and a relative import.
|
|
203
|
+
export async function schemaJson(root: string, options: CheckOptions): Promise<CheckDiagnostic[]> {
|
|
204
|
+
return scan(root, options, 'schema-without-json-schema', NEEDLE, ({ file, parsed, report }) => {
|
|
205
|
+
for (const { ast, base } of parsed) {
|
|
206
|
+
const { locals: actions, namespaces } = importsOf(ast, source => KAY.test(source), 'action')
|
|
207
|
+
const scope = apiScope(ast)
|
|
208
|
+
const module: Module = { ast, file }
|
|
209
|
+
|
|
210
|
+
walk(ast, (node) => {
|
|
211
|
+
if (node.type !== 'CallExpression')
|
|
212
|
+
return
|
|
213
|
+
|
|
214
|
+
let schemas: Node[] = []
|
|
215
|
+
|
|
216
|
+
if ((node.callee.type === 'Identifier' && actions.has(node.callee.name)) || (node.callee.type === 'MemberExpression' && !node.callee.computed && node.callee.object.type === 'Identifier' && namespaces.has(node.callee.object.name) && node.callee.property.type === 'Identifier' && node.callee.property.name === 'action'))
|
|
217
|
+
schemas = positions(node.arguments[0], ACTION_SCHEMAS)
|
|
218
|
+
else if (node.callee.type === 'MemberExpression' && !node.callee.computed && node.callee.property.type === 'Identifier' && ROUTE_METHODS.has(node.callee.property.name) && fromApi(node.callee.object, scope))
|
|
219
|
+
schemas = positions(node.arguments[2], ROUTE_SCHEMAS)
|
|
220
|
+
|
|
221
|
+
for (const schema of schemas.filter(candidate => lacksJsonSchema(candidate, module)))
|
|
222
|
+
report(base + schema.start, base + schema.end, 'this Standard Schema validator has no `~standard.jsonSchema`: it still validates, but a form shows no constraints, a form or a query is not read as numbers and booleans, and OpenAPI and MCP show an open schema', 'give it `~standard.jsonSchema` (Zod 4.2+ and ArkType 2.1.28+ carry it, valibot through `toStandardJsonSchema` from `@valibot/to-json-schema`) or build the schema with `s`', null)
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
}
|