@kayjs/kay 1.105.0 → 1.106.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/dist/eslint.js +435 -120
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.md +2 -2
- package/llms/actions.md +22 -19
- package/llms/analytics.md +1 -1
- package/llms/api-from-islands.md +1 -1
- package/llms/api-routes.md +6 -6
- package/llms/check-and-tools.md +1 -1
- package/llms/code-lints.md +2 -2
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -0
- package/llms/content-cms.md +1 -1
- package/llms/content-markdown-to-caching.md +1 -1
- package/llms/content-preview.md +2 -2
- package/llms/content.md +21 -21
- package/llms/deprecated-lints-1-105-libraries.md +2 -0
- package/llms/deprecated-lints-1-105.md +3 -3
- package/llms/deprecated-lints.md +3 -2
- package/llms/dev-tool.md +2 -2
- package/llms/environment.md +22 -22
- package/llms/files.md +5 -5
- package/llms/islands-state-to-ids.md +2 -1
- package/llms/local-collections.md +2 -2
- package/llms/mcp.md +2 -1
- package/llms/navigation-from-code.md +1 -1
- package/llms/notifications.md +4 -2
- package/llms/project.md +1 -1
- package/llms/rate-limits.md +1 -1
- package/llms/recipes.md +16 -16
- package/llms/rooms.md +4 -4
- package/llms/routing-endpoints-and-base.md +1 -1
- package/llms/routing.md +5 -5
- package/llms/schemas.md +10 -5
- package/llms/server.md +7 -7
- package/llms/site.md +9 -9
- package/llms/sitemap-search-fonts.md +2 -0
- package/llms/styles-and-assets.md +2 -1
- package/llms/templates-elements-slots-snippets.md +2 -2
- package/llms/testing-browser.md +20 -0
- package/llms/testing.md +1 -1
- package/llms/upgrading-1-105-config.md +2 -2
- package/llms/upgrading.md +18 -3
- package/llms/whats-new-archive.md +35 -66
- package/llms/whats-new.md +34 -28
- package/llms.txt +1 -1
- package/package.json +15 -11
- package/src/add.ts +1 -1
- package/src/build/class-table.ts +2 -2
- package/src/build/client.ts +7 -6
- package/src/build/plugin.ts +14 -11
- package/src/build/styles.ts +3 -3
- package/src/build/transform.ts +3 -1
- package/src/build/ui-layers.ts +30 -0
- package/src/build/ui.ts +108 -11
- package/src/build/variants.ts +119 -43
- package/src/build/worker-bundle.ts +6 -0
- package/src/check/boot.ts +22 -1
- package/src/check/call-result.ts +242 -0
- package/src/check/config-objects.ts +17 -2
- package/src/check/diagnose.ts +22 -0
- package/src/check/handler-events.ts +149 -0
- package/src/check/index.ts +6 -2
- package/src/check/link-flag-tests.ts +130 -0
- package/src/check/project.ts +4 -3
- package/src/check/refusal-calls.ts +2 -2
- package/src/check/regroup.ts +5 -3
- package/src/check/renamed-packages.ts +58 -14
- package/src/check/schema-builders.ts +256 -0
- package/src/check/secret-keys.ts +15 -2
- package/src/check/shadow-types.ts +2 -0
- package/src/check/template-hints.ts +1 -1
- package/src/check/tool-labels.ts +11 -9
- package/src/check/ui-types.ts +4 -3
- package/src/check/ui.ts +14 -11
- package/src/commands/env-declare.ts +18 -6
- package/src/commands/routes.ts +1 -1
- package/src/compiler/a11y.ts +8 -1
- package/src/compiler/ast.ts +1 -1
- package/src/compiler/bound-props.ts +1 -1
- package/src/compiler/codegen.ts +6 -6
- package/src/compiler/deprecated-lints-1-106.ts +14 -0
- package/src/compiler/deprecated-lints.ts +10 -5
- package/src/compiler/deprecated-typed-lints.ts +23 -0
- package/src/compiler/dom-here.ts +72 -0
- package/src/compiler/dom.ts +23 -23
- package/src/compiler/fence-checks.ts +12 -11
- package/src/compiler/field-described.ts +2 -2
- package/src/compiler/helper-imports.ts +1 -1
- package/src/compiler/html-fields.ts +92 -0
- package/src/compiler/html-initial.ts +5 -3
- package/src/compiler/html.ts +14 -31
- package/src/compiler/index.ts +3 -1
- package/src/compiler/items.ts +5 -5
- package/src/compiler/link-flags.ts +6 -5
- package/src/compiler/lints.ts +12 -12
- package/src/compiler/rest-omits.ts +15 -0
- package/src/config-loader.ts +10 -2
- package/src/config-resolve.ts +3 -1
- package/src/config-shape.ts +2 -2
- package/src/config.ts +20 -4
- package/src/content/markdown.ts +2 -3
- package/src/generate-resource.ts +9 -9
- package/src/generate.ts +1 -1
- package/src/mcp/guards.ts +1 -1
- package/src/mcp/tools.ts +1 -1
- package/src/migrate/content.ts +61 -55
- package/src/runtime/action-shared.ts +2 -2
- package/src/runtime/action.ts +6 -2
- package/src/runtime/form-types.ts +99 -0
- package/src/runtime/form.ts +22 -7
- package/src/runtime/html.ts +36 -5
- package/src/runtime/markdown-autolink.ts +69 -0
- package/src/runtime/markdown-render.client.ts +39 -3
- package/src/runtime/markdown.client.ts +6 -3
- package/src/runtime/markdown.server.ts +6 -3
- package/src/runtime/markdown.ts +125 -1
- package/src/runtime/navigate-api.ts +10 -5
- package/src/runtime/persist.ts +7 -7
- package/src/runtime/query-param.ts +1 -1
- package/src/runtime/reactive.ts +14 -0
- package/src/runtime/sanitize.ts +3 -1
- package/src/runtime/schema-record.ts +31 -0
- package/src/runtime/schema.client.ts +1 -4
- package/src/runtime/schema.ts +321 -26
- package/src/runtime/swap-focus.ts +59 -11
- package/src/server/action-module.ts +16 -0
- package/src/server/actions.ts +6 -14
- package/src/server/api-modules.ts +1 -1
- package/src/server/api.ts +13 -1
- package/src/server/app-stages.ts +23 -18
- package/src/server/dev-ui.ts +2 -2
- package/src/server/env-state.ts +1 -1
- package/src/server/env.ts +11 -3
- package/src/server/favicon.ts +47 -1
- package/src/server/image-metadata-bmff.ts +1 -1
- package/src/server/kept-pages.ts +3 -3
- package/src/server/library.ts +2 -0
- package/src/server/mcp.ts +13 -12
- package/src/server/notifications.ts +4 -2
- package/src/server/offline.ts +24 -7
- package/src/server/push-settings.ts +16 -2
- package/src/server/push.ts +12 -4
- package/src/server/query.ts +13 -6
- package/src/server/render.ts +1 -1
- package/src/server/scope.ts +20 -13
- package/src/server/signals.ts +4 -4
- package/src/template-ws.ts +1 -1
- package/src/test/a11y-dom.ts +70 -0
- package/src/test/html-snapshot.ts +103 -0
- package/src/test/index.ts +2 -1
- package/src/test/mount.ts +8 -1
- package/src/test/page-checks.ts +12 -0
- package/src/test/screenshot-match.ts +2 -2
- package/src/test/shared-modules.ts +27 -5
- package/src/test/site-types.ts +5 -1
- package/types/build/class-table.d.ts +1 -1
- package/types/build/class-table.d.ts.map +1 -1
- package/types/build/client.d.ts.map +1 -1
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/transform.d.ts +1 -0
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/ui-layers.d.ts +5 -0
- package/types/build/ui-layers.d.ts.map +1 -0
- package/types/build/ui.d.ts +7 -0
- package/types/build/ui.d.ts.map +1 -1
- package/types/build/variants.d.ts +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/build/worker-bundle.d.ts +1 -0
- package/types/build/worker-bundle.d.ts.map +1 -1
- package/types/check/boot.d.ts.map +1 -1
- package/types/check/call-result.d.ts +7 -0
- package/types/check/call-result.d.ts.map +1 -0
- package/types/check/config-objects.d.ts.map +1 -1
- package/types/check/diagnose.d.ts.map +1 -1
- package/types/check/handler-events.d.ts +9 -0
- package/types/check/handler-events.d.ts.map +1 -0
- package/types/check/index.d.ts +1 -0
- package/types/check/index.d.ts.map +1 -1
- package/types/check/link-flag-tests.d.ts +4 -0
- package/types/check/link-flag-tests.d.ts.map +1 -0
- package/types/check/project.d.ts.map +1 -1
- package/types/check/regroup.d.ts +1 -1
- package/types/check/regroup.d.ts.map +1 -1
- package/types/check/renamed-packages.d.ts +1 -0
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-builders.d.ts +3 -0
- package/types/check/schema-builders.d.ts.map +1 -0
- package/types/check/secret-keys.d.ts.map +1 -1
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/check/template-hints.d.ts +1 -0
- package/types/check/template-hints.d.ts.map +1 -1
- package/types/check/tool-labels.d.ts.map +1 -1
- package/types/check/ui-types.d.ts.map +1 -1
- package/types/check/ui.d.ts.map +1 -1
- package/types/commands/env-declare.d.ts.map +1 -1
- package/types/compiler/a11y.d.ts.map +1 -1
- package/types/compiler/bound-props.d.ts +1 -0
- package/types/compiler/bound-props.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +1 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +32 -5
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/deprecated-typed-lints.d.ts +21 -0
- package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
- package/types/compiler/dom-here.d.ts +14 -0
- package/types/compiler/dom-here.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/fence-checks.d.ts.map +1 -1
- package/types/compiler/field-described.d.ts +1 -1
- package/types/compiler/field-described.d.ts.map +1 -1
- package/types/compiler/html-fields.d.ts +19 -0
- package/types/compiler/html-fields.d.ts.map +1 -0
- package/types/compiler/html-initial.d.ts +3 -2
- package/types/compiler/html-initial.d.ts.map +1 -1
- package/types/compiler/html.d.ts.map +1 -1
- package/types/compiler/index.d.ts +1 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/items.d.ts +4 -1
- package/types/compiler/items.d.ts.map +1 -1
- package/types/compiler/link-flags.d.ts +6 -0
- package/types/compiler/link-flags.d.ts.map +1 -1
- package/types/compiler/lints.d.ts +44 -17
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/rest-omits.d.ts +4 -0
- package/types/compiler/rest-omits.d.ts.map +1 -0
- package/types/config-loader.d.ts.map +1 -1
- package/types/config-resolve.d.ts.map +1 -1
- package/types/config.d.ts +15 -2
- package/types/config.d.ts.map +1 -1
- package/types/content/markdown.d.ts.map +1 -1
- package/types/migrate/content.d.ts.map +1 -1
- package/types/runtime/action-shared.d.ts +1 -1
- package/types/runtime/action-shared.d.ts.map +1 -1
- package/types/runtime/action.d.ts +3 -0
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/form-types.d.ts +6 -0
- package/types/runtime/form-types.d.ts.map +1 -0
- package/types/runtime/form.d.ts +3 -1
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts +3 -0
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/markdown-autolink.d.ts +11 -0
- package/types/runtime/markdown-autolink.d.ts.map +1 -0
- package/types/runtime/markdown-render.client.d.ts +2 -1
- package/types/runtime/markdown-render.client.d.ts.map +1 -1
- package/types/runtime/markdown.client.d.ts +3 -1
- package/types/runtime/markdown.client.d.ts.map +1 -1
- package/types/runtime/markdown.d.ts +11 -0
- package/types/runtime/markdown.d.ts.map +1 -1
- package/types/runtime/markdown.server.d.ts +3 -1
- package/types/runtime/markdown.server.d.ts.map +1 -1
- package/types/runtime/navigate-api.d.ts +1 -1
- package/types/runtime/navigate-api.d.ts.map +1 -1
- package/types/runtime/persist.d.ts +2 -2
- package/types/runtime/persist.d.ts.map +1 -1
- package/types/runtime/reactive.d.ts +3 -0
- package/types/runtime/reactive.d.ts.map +1 -1
- package/types/runtime/sanitize.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts +9 -0
- package/types/runtime/schema-record.d.ts.map +1 -0
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +94 -10
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/swap-focus.d.ts.map +1 -1
- package/types/server/action-module.d.ts +2 -0
- package/types/server/action-module.d.ts.map +1 -0
- package/types/server/actions.d.ts +1 -0
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +3 -2
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/env-state.d.ts.map +1 -1
- package/types/server/env.d.ts +5 -1
- package/types/server/env.d.ts.map +1 -1
- package/types/server/favicon.d.ts +3 -1
- package/types/server/favicon.d.ts.map +1 -1
- package/types/server/kept-pages.d.ts +1 -1
- package/types/server/kept-pages.d.ts.map +1 -1
- package/types/server/library.d.ts +2 -0
- package/types/server/library.d.ts.map +1 -1
- package/types/server/mcp.d.ts +1 -1
- package/types/server/mcp.d.ts.map +1 -1
- package/types/server/notifications.d.ts +2 -1
- package/types/server/notifications.d.ts.map +1 -1
- package/types/server/offline.d.ts.map +1 -1
- package/types/server/push-settings.d.ts +1 -1
- package/types/server/push-settings.d.ts.map +1 -1
- package/types/server/push.d.ts +1 -0
- package/types/server/push.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/scope.d.ts +1 -1
- package/types/server/scope.d.ts.map +1 -1
- package/types/test/a11y-dom.d.ts +20 -0
- package/types/test/a11y-dom.d.ts.map +1 -0
- package/types/test/html-snapshot.d.ts +4 -0
- package/types/test/html-snapshot.d.ts.map +1 -0
- package/types/test/index.d.ts.map +1 -1
- package/types/test/mount.d.ts +3 -0
- package/types/test/mount.d.ts.map +1 -1
- package/types/test/page-checks.d.ts +6 -0
- package/types/test/page-checks.d.ts.map +1 -0
- package/types/test/screenshot-match.d.ts +1 -1
- package/types/test/screenshot-match.d.ts.map +1 -1
- package/types/test/shared-modules.d.ts.map +1 -1
- package/types/test/site-types.d.ts +3 -1
- package/types/test/site-types.d.ts.map +1 -1
package/src/check/diagnose.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { Fix, Span } from '../compiler/ast'
|
|
|
4
4
|
import type { LintLevel, LintName } from '../compiler/lints'
|
|
5
5
|
import type { I18nConfig } from '../config'
|
|
6
6
|
import type { RouteMount } from '../server/routes'
|
|
7
|
+
import type { OldFormLint } from './handler-events'
|
|
7
8
|
import type { ShadowFile } from './shadow'
|
|
8
9
|
import { existsSync, realpathSync } from 'node:fs'
|
|
9
10
|
import { basename, dirname, relative, resolve, sep } from 'node:path'
|
|
@@ -17,7 +18,9 @@ import { WRITABLE } from '../compiler/names'
|
|
|
17
18
|
import { parseKay } from '../compiler/parser'
|
|
18
19
|
import { UNSERVED_MESSAGE } from '../runtime/checks'
|
|
19
20
|
import { localePrefixes, localeSettings } from '../server/i18n'
|
|
21
|
+
import { callResultLint } from './call-result'
|
|
20
22
|
import { unknownNameHelp } from './export-homes'
|
|
23
|
+
import { handlerEventLint } from './handler-events'
|
|
21
24
|
import { loopIndexFix, positionalKeyFix } from './loop-index-fix'
|
|
22
25
|
import { sourceSpan } from './shadow'
|
|
23
26
|
import { lastOverload, richCallHint, templateHint } from './template-hints'
|
|
@@ -128,6 +131,18 @@ function blockWithoutParameters(diagnostic: TsgoDiagnostic, source: string, span
|
|
|
128
131
|
return `the component calls \`@${name}\` with (${parameters}): write it with them, as \`@${name}(${[...parameters.matchAll(PARAMETER_NAME)].map(match => `${match[1] ?? ''}${match[2]}`).join(', ')})\``
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
// What the types say of a callback given a function written for its arguments before 1.105, or of an island's call read as data: the deprecated lint that names it, with its fix.
|
|
135
|
+
function oldForm(message: string, source: string, span: Span | undefined): (OldFormLint & { code: LintName }) | undefined {
|
|
136
|
+
const handler = handlerEventLint(message, source, span)
|
|
137
|
+
|
|
138
|
+
if (handler !== undefined)
|
|
139
|
+
return { code: 'deprecated-callback-handler', ...handler }
|
|
140
|
+
|
|
141
|
+
const result = callResultLint(message, source, span, true)
|
|
142
|
+
|
|
143
|
+
return result && { code: 'deprecated-call-result', ...result }
|
|
144
|
+
}
|
|
145
|
+
|
|
131
146
|
// A diagnostic in a .kay file's shadow TSX, read at the levels its source gives the lints.
|
|
132
147
|
export function diagnoseShadow(shadow: ShadowFile, reported: TsgoDiagnostic, levelAt: (lint: LintName, offset: number) => LintLevel, messageKeys: readonly string[] = []): Diagnosed | undefined {
|
|
133
148
|
const span = sourceSpan(shadow, reported.start, reported.end, 'loose')
|
|
@@ -149,6 +164,13 @@ export function diagnoseShadow(shadow: ShadowFile, reported: TsgoDiagnostic, lev
|
|
|
149
164
|
return level === 'allow' ? undefined : { ...lint, severity: severityOf(level), span, ...offered }
|
|
150
165
|
}
|
|
151
166
|
|
|
167
|
+
// A type error that is an old form of before 1.105, with the fix that writes the new one.
|
|
168
|
+
const old = oldForm(diagnostic.message, shadow.source, span)
|
|
169
|
+
const oldLevel = old === undefined ? 'allow' : levelAt(old.code, offset)
|
|
170
|
+
|
|
171
|
+
if (old !== undefined && oldLevel !== 'allow')
|
|
172
|
+
return { ...old, severity: severityOf(oldLevel) }
|
|
173
|
+
|
|
152
174
|
// Unread code is the `unread-name` lint; a name kay generated, which the template reads in a way it does not spell out, is no code of the user's.
|
|
153
175
|
if (diagnostic.code !== undefined && UNUSED_CODES.has(diagnostic.code)) {
|
|
154
176
|
const level = shadow.map.isExact(diagnostic.start) ? levelAt('unread-name', offset) : 'allow'
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Fix, Span } from '../compiler/ast'
|
|
2
|
+
import { attributeNamed } from './template-hints'
|
|
3
|
+
|
|
4
|
+
// `Type '(next: boolean) => void' is not assignable to type '(event: { checked: boolean; }) => unknown'.` and the incompatible parameter that follows.
|
|
5
|
+
const EVENT_CALLBACK = /^Type '([\s\S]+?)' is not assignable to type '\(event: \{ ([\s\S]*?) \}\) => unknown'\.\n\s+Types of parameters '[^']*' and 'event' are incompatible\.\n([\s\S]*)$/
|
|
6
|
+
const PARAMETER_TYPE = /(?:to type|from type) '([\s\S]*?)'(?:\.|:)/
|
|
7
|
+
const FIELD = /^(\w+)\??:/
|
|
8
|
+
const CALLBACK_NAME = /^on[A-Z]\w*$/
|
|
9
|
+
const HANDLER_NAME = /^[A-Z_$][\w$]*(?:\??\.[A-Z_$][\w$]*)*$/i
|
|
10
|
+
// An object literal type or a named type, which no old argument was apart from a `File`: the handler may be written for the event already.
|
|
11
|
+
const OBJECT_TYPE = /^(?:\{|object\b|(?!File\b)[A-Z])/
|
|
12
|
+
const COMPONENT = /^([A-Z][\w$]*)(?:[.#][^\s(]*)?\(/
|
|
13
|
+
const NAME_AT = /^[\w$]+/
|
|
14
|
+
const EVENT_WORD = /\bevent\b/
|
|
15
|
+
const ELEMENT = /^[a-z.#][^\s(]*\(/
|
|
16
|
+
|
|
17
|
+
// What an old-form lint of the type check reports.
|
|
18
|
+
export interface OldFormLint {
|
|
19
|
+
message: string
|
|
20
|
+
help: string
|
|
21
|
+
span: Span
|
|
22
|
+
fixes?: Fix[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The segments of a type's text between its top-level `separator`s.
|
|
26
|
+
function splitTop(text: string, separator: string): string[] {
|
|
27
|
+
const parts: string[] = []
|
|
28
|
+
let depth = 0
|
|
29
|
+
let from = 0
|
|
30
|
+
|
|
31
|
+
for (const [index, char] of [...text].entries()) {
|
|
32
|
+
if ('{([<'.includes(char)) {
|
|
33
|
+
depth++
|
|
34
|
+
}
|
|
35
|
+
else if ('})]'.includes(char) || (char === '>' && text[index - 1] !== '=')) {
|
|
36
|
+
depth--
|
|
37
|
+
}
|
|
38
|
+
else if (char === separator && depth === 0) {
|
|
39
|
+
parts.push(text.slice(from, index))
|
|
40
|
+
from = index + 1
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
parts.push(text.slice(from))
|
|
45
|
+
|
|
46
|
+
return parts.map(part => part.trim()).filter(part => part !== '')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// How many parameters a function type written as `(a: A, b: B) => R` takes, 1 for any other type, as a signal's `Setter<T>` takes one.
|
|
50
|
+
function parameterCount(type: string): number {
|
|
51
|
+
if (!type.startsWith('('))
|
|
52
|
+
return 1
|
|
53
|
+
|
|
54
|
+
let depth = 0
|
|
55
|
+
|
|
56
|
+
for (const [index, char] of [...type].entries()) {
|
|
57
|
+
if (char === '(')
|
|
58
|
+
depth++
|
|
59
|
+
else if (char === ')' && --depth === 0)
|
|
60
|
+
return splitTop(type.slice(1, index), ',').length
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return 1
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Whether the first parameter of a function type written as `(a: A, b: B) => R` is typed as an object, a literal or a named type.
|
|
67
|
+
function takesObject(type: string): boolean {
|
|
68
|
+
if (!type.startsWith('('))
|
|
69
|
+
return false
|
|
70
|
+
|
|
71
|
+
const first = splitTop(type.slice(1), ',')[0] ?? ''
|
|
72
|
+
|
|
73
|
+
// A destructured parameter takes an object whatever its type is called.
|
|
74
|
+
if (first.startsWith('{'))
|
|
75
|
+
return true
|
|
76
|
+
|
|
77
|
+
const colon = first.indexOf(':')
|
|
78
|
+
|
|
79
|
+
return colon !== -1 && OBJECT_TYPE.test(first.slice(colon + 1).trim())
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// The component an attribute is written on, read off its line or, for an attribute on a line of its own, the line above it that opens the call.
|
|
83
|
+
function componentAt(source: string, at: number): string | undefined {
|
|
84
|
+
const lineEnd = source.indexOf('\n', at)
|
|
85
|
+
const [own, ...above] = source.slice(0, lineEnd === -1 ? source.length : lineEnd).split('\n').toReversed()
|
|
86
|
+
const opened = COMPONENT.exec(own!.trimStart())
|
|
87
|
+
|
|
88
|
+
if (opened !== null)
|
|
89
|
+
return opened[1]
|
|
90
|
+
|
|
91
|
+
if (ELEMENT.test(own!.trimStart()))
|
|
92
|
+
return undefined
|
|
93
|
+
|
|
94
|
+
const indent = own!.length - own!.trimStart().length
|
|
95
|
+
|
|
96
|
+
for (const line of above) {
|
|
97
|
+
const trimmed = line.trimStart()
|
|
98
|
+
|
|
99
|
+
if (trimmed !== '' && line.length - trimmed.length < indent)
|
|
100
|
+
return COMPONENT.exec(trimmed)?.[1]
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return undefined
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A callback a component of a kayjs library takes as one event object since 1.105, given a function written for its positional arguments: the handler of `onValueChange={query.set}`, `onCheckedChange={toggle}` or `onFiles={upload}`.
|
|
107
|
+
// The fix wraps a handler written as a name, `event => query.set(event.value)`, passing the fields the handler took, first ones first. It is applied where the handler's parameter is no object, which the old arguments were; a handler taking an object may be written for the event already, so its fix is offered.
|
|
108
|
+
export function handlerEventLint(message: string, source: string, span: Span | undefined): OldFormLint | undefined {
|
|
109
|
+
const found = EVENT_CALLBACK.exec(message)
|
|
110
|
+
|
|
111
|
+
if (found === null || span === undefined)
|
|
112
|
+
return undefined
|
|
113
|
+
|
|
114
|
+
const name = NAME_AT.exec(source.slice(span.start))?.[0]
|
|
115
|
+
|
|
116
|
+
if (name === undefined || !CALLBACK_NAME.test(name))
|
|
117
|
+
return undefined
|
|
118
|
+
|
|
119
|
+
const component = componentAt(source, span.start)
|
|
120
|
+
|
|
121
|
+
if (component === undefined || !new RegExp(`^import\\b[^\\n]*\\b${component}\\b[^\\n]*from\\s+['"]@kayjs/`, 'm').test(source))
|
|
122
|
+
return undefined
|
|
123
|
+
|
|
124
|
+
const [, handlerType, eventFields, rest] = found
|
|
125
|
+
const fields = splitTop(eventFields!, ';').flatMap(segment => FIELD.exec(segment)?.[1] ?? [])
|
|
126
|
+
|
|
127
|
+
if (fields.length === 0)
|
|
128
|
+
return undefined
|
|
129
|
+
|
|
130
|
+
const object = takesObject(handlerType!) || PARAMETER_TYPE.exec(rest!)?.[1]?.startsWith('{') === true
|
|
131
|
+
const taken = fields.slice(0, Math.max(1, Math.min(parameterCount(handlerType!), fields.length)))
|
|
132
|
+
const attribute = attributeNamed(source, { start: span.start, end: source.length }, name)
|
|
133
|
+
const value = attribute === undefined ? undefined : source.slice(attribute.start + name.length + 1, attribute.end)
|
|
134
|
+
const inner = value?.startsWith('{') && value.endsWith('}') ? value.slice(1, -1).trim() : undefined
|
|
135
|
+
const handler = inner !== undefined && HANDLER_NAME.test(inner) && !EVENT_WORD.test(inner) ? inner : undefined
|
|
136
|
+
const written = `event => ${handler}(${taken.map(field => `event.${field}`).join(', ')})`
|
|
137
|
+
const report = {
|
|
138
|
+
message: `\`${name}\` on \`${component}\` takes one event object since 1.105, \`{ ${fields.join(', ')} }\`, where it took ${fields.map(field => `\`${field}\``).join(', ')} as ${fields.length === 1 ? 'its argument' : 'arguments'}`,
|
|
139
|
+
help: handler === undefined ? 'take the event object, `({ value }) => ...`, and read the fields from it' : `write \`${name}={${written}}\``,
|
|
140
|
+
span,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (handler === undefined || attribute === undefined)
|
|
144
|
+
return report
|
|
145
|
+
|
|
146
|
+
const start = attribute.start + name.length + 1 + value!.indexOf(handler)
|
|
147
|
+
|
|
148
|
+
return { ...report, fixes: [{ title: `Write \`${name}={${written}}\``, safe: !object, edits: [{ start, end: start + handler.length, text: written }] }] }
|
|
149
|
+
}
|
package/src/check/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { lintLevels } from '../compiler/lints'
|
|
|
14
14
|
import { offsetAt, positionAt } from '../compiler/mapping'
|
|
15
15
|
import { nativeExe } from '../compiler/native-typescript'
|
|
16
16
|
import { pickConfig } from '../config'
|
|
17
|
+
import { moduleCallResult } from './call-result'
|
|
17
18
|
import { diagnoseShadow, reportsTypeErrors, servedByTheApp, shownOutsideKay, unusedCodesOf } from './diagnose'
|
|
18
19
|
import { unknownNameHelp } from './export-homes'
|
|
19
20
|
import { generatedProblem } from './generated'
|
|
@@ -88,6 +89,8 @@ export interface CheckOptions extends Omit<Pick<KayConfig, (typeof CHECK_CONFIG)
|
|
|
88
89
|
launch?: boolean | undefined
|
|
89
90
|
// Set by `kay build --static`: a static host reads no `SITE_URL` once built, so a missing site is reported.
|
|
90
91
|
exported?: boolean | undefined
|
|
92
|
+
// The header `proxy.key` names, which the fix of `testSite({ proxyKey })` writes beside the secret.
|
|
93
|
+
proxyHeader?: string | undefined
|
|
91
94
|
}
|
|
92
95
|
|
|
93
96
|
export interface CheckDiagnostic extends Pick<Diagnostic, 'label' | 'related' | 'note' | 'help' | 'severity' | 'fixes'> {
|
|
@@ -113,8 +116,9 @@ function helped({ message, help }: CheckDiagnostic): Pick<CheckDiagnostic, 'help
|
|
|
113
116
|
export function configCheckOptions(config: LoadedConfig): CheckOptions {
|
|
114
117
|
const { ops, ...picked } = pickConfig(config, CHECK_CONFIG)
|
|
115
118
|
const { observe } = ops ?? {}
|
|
119
|
+
const proxyHeader = typeof config.proxy === 'object' ? config.proxy.key?.header : undefined
|
|
116
120
|
|
|
117
|
-
return { ...picked, ...config.store?.database !== undefined && { storeDatabase: config.store.database }, ...ops !== undefined && { ops: { health: ops.health, ...observe !== undefined && { observe: { requestIdHeader: observe.requestIdHeader, loadBudget: observe.loadBudget } } } }, ...config.publicEnv !== undefined && { publicEnv: config.publicEnv } }
|
|
121
|
+
return { ...picked, ...proxyHeader !== undefined && { proxyHeader }, ...config.store?.database !== undefined && { storeDatabase: config.store.database }, ...ops !== undefined && { ops: { health: ops.health, ...observe !== undefined && { observe: { requestIdHeader: observe.requestIdHeader, loadBudget: observe.loadBudget } } } }, ...config.publicEnv !== undefined && { publicEnv: config.publicEnv } }
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
// Type-checks a project, .kay components included, and reports every error at the line and column the user wrote.
|
|
@@ -207,7 +211,7 @@ async function runCheck(root: string, given: CheckOptions, probes?: ProbeMemory)
|
|
|
207
211
|
|
|
208
212
|
const mapped: CheckDiagnostic = shadow && diagnosed
|
|
209
213
|
? { ...diagnostic, file: shadow.file, ...positionAt(shadow.source, diagnosed.span?.start ?? 0), code: diagnosed.code ?? diagnostic.code, severity: diagnosed.severity, message: diagnosed.message, note: diagnosed.note, help: diagnosed.help ?? (shadow.route === true ? helped(diagnostic).help : undefined), ...diagnosed.fixes && { fixes: diagnosed.fixes } }
|
|
210
|
-
: { ...diagnostic, ...helped(diagnostic) }
|
|
214
|
+
: { ...diagnostic, ...helped(diagnostic), ...await moduleCallResult(diagnostic, options.lints) }
|
|
211
215
|
const generated = generatedProblem(root, mapped, types, content)
|
|
212
216
|
|
|
213
217
|
// A guard file imported for two lines of the exports check is reported once.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { CheckDiagnostic } from '.'
|
|
2
|
+
import type { Fix } from '../compiler/ast'
|
|
3
|
+
import type { Node } from '../compiler/estree'
|
|
4
|
+
import type { LintLevels } from '../compiler/lints'
|
|
5
|
+
import { findProjectFiles } from '../build/files'
|
|
6
|
+
import { parseProgram } from '../compiler/estree'
|
|
7
|
+
import { OLD_LINK_FLAGS } from '../compiler/link-flags'
|
|
8
|
+
import { lintLevel } from '../compiler/lints'
|
|
9
|
+
import { childrenOf } from './ast'
|
|
10
|
+
|
|
11
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/
|
|
12
|
+
const OLD_FLAG = new RegExp(`(?<![\\w-])(${Object.keys(OLD_LINK_FLAGS).join('|')})(?![\\w-])`, 'g')
|
|
13
|
+
const DATASET_END = /dataset$/
|
|
14
|
+
const ATTRIBUTE_CALL = /\.(?:get|has|set|remove|toggle)Attribute\(\s*$/
|
|
15
|
+
|
|
16
|
+
// The `dataset` property of each old flag: `data-keep-scroll` is `keepScroll`.
|
|
17
|
+
const DATASET_NAMES = new Map(Object.keys(OLD_LINK_FLAGS).map(name => [name.slice(5).replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase()), name]))
|
|
18
|
+
|
|
19
|
+
interface Scanned {
|
|
20
|
+
// The content of each string literal and template chunk, as `[start, end]` offsets.
|
|
21
|
+
strings: [number, number][]
|
|
22
|
+
// The `x.dataset.<flag>` member expressions, and whether each is assigned, updated or deleted.
|
|
23
|
+
datasets: { node: Node, written: boolean, direct: boolean }[]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const WRAPPERS = new Set(['ChainExpression', 'TSNonNullExpression', 'ParenthesizedExpression'])
|
|
27
|
+
|
|
28
|
+
// The expression under any wrappers.
|
|
29
|
+
function unwrapped(node: Node): Node {
|
|
30
|
+
let inner = node
|
|
31
|
+
|
|
32
|
+
while (inner.type === 'ChainExpression' || inner.type === 'TSNonNullExpression' || inner.type === 'ParenthesizedExpression')
|
|
33
|
+
inner = inner.expression
|
|
34
|
+
|
|
35
|
+
return inner
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isDataset(node: Node): boolean {
|
|
39
|
+
const inner = unwrapped(node)
|
|
40
|
+
|
|
41
|
+
return inner.type === 'MemberExpression' && !inner.computed && inner.property.type === 'Identifier' && inner.property.name === 'dataset'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// What the parsed program holds that names a flag, so a regex literal or a comment cannot shift what is a string.
|
|
45
|
+
function scan(source: string): Scanned {
|
|
46
|
+
const found: Scanned = { strings: [], datasets: [] }
|
|
47
|
+
const visit = (node: Node, ancestors: Node[]) => {
|
|
48
|
+
if (node.type === 'Literal' && typeof node.value === 'string') {
|
|
49
|
+
found.strings.push([node.start + 1, node.end - 1])
|
|
50
|
+
}
|
|
51
|
+
else if (node.type === 'TemplateElement') {
|
|
52
|
+
// The span holds its delimiters: a backtick or `}` before, a backtick or `${` after.
|
|
53
|
+
found.strings.push([node.start + 1, node.end - (source[node.end - 1] === '`' ? 1 : 2)])
|
|
54
|
+
}
|
|
55
|
+
else if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier' && DATASET_NAMES.has(node.property.name) && isDataset(node.object)) {
|
|
56
|
+
// Through `?.`, `!` and parentheses: `delete el?.dataset.keepScroll` and `el.dataset!.keepScroll = 1` write it.
|
|
57
|
+
let at = ancestors.length - 1
|
|
58
|
+
let target: Node = node
|
|
59
|
+
|
|
60
|
+
while (at >= 0 && WRAPPERS.has(ancestors[at]!.type))
|
|
61
|
+
target = ancestors[at--]!
|
|
62
|
+
|
|
63
|
+
const parent = ancestors[at]
|
|
64
|
+
const assigned = parent?.type === 'AssignmentExpression' && parent.left === target
|
|
65
|
+
|
|
66
|
+
found.datasets.push({ node, written: assigned || parent?.type === 'UpdateExpression' || (parent?.type === 'UnaryExpression' && parent.operator === 'delete'), direct: unwrapped(node.object) === node.object })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const child of childrenOf(node))
|
|
70
|
+
visit(child, [...ancestors, node])
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
visit(parseProgram(source, { lang: 'ts', recover: true }).program, [])
|
|
74
|
+
|
|
75
|
+
return found
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The selectors, attribute reads and `dataset` reads of an app's test files that name a link flag by its name before 1.105, which `deprecated-link-flags` rewrites in the templates: `[data-keep-scroll]` is written `[data-scroll=keep]`, as the template's attribute was.
|
|
79
|
+
// An attribute read by name (`getAttribute('data-keep-scroll')`) and a read of `dataset.keepScroll` get a fix that is not safe, as the new name holds a word where the old one was there or not; any other string naming the flag (a selector with a value, one built in a template) and a write of `dataset` are reported with no fix.
|
|
80
|
+
export async function linkFlagTests(root: string, levels: LintLevels | undefined): Promise<CheckDiagnostic[]> {
|
|
81
|
+
const level = lintLevel('deprecated-link-flags', levels)
|
|
82
|
+
|
|
83
|
+
if (level === 'allow')
|
|
84
|
+
return []
|
|
85
|
+
|
|
86
|
+
const found: CheckDiagnostic[] = []
|
|
87
|
+
|
|
88
|
+
for (const file of (await findProjectFiles(root, TEST_FILE, { project: true, ownKayOnly: true })).sort()) {
|
|
89
|
+
const source = await Bun.file(file).text()
|
|
90
|
+
const { strings, datasets } = scan(source)
|
|
91
|
+
const report = (start: number, end: number, message: string, help: string, fixes: Fix[] | undefined) => {
|
|
92
|
+
const before = source.slice(0, start).split('\n')
|
|
93
|
+
|
|
94
|
+
found.push({ file, line: before.length, column: before.at(-1)!.length + 1, end, severity: level === 'warn' ? 'warning' : 'error', code: 'deprecated-link-flags', message, help, ...fixes === undefined ? {} : { fixes } })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const match of source.matchAll(OLD_FLAG)) {
|
|
98
|
+
const name = match[1]!
|
|
99
|
+
const end = match.index + name.length
|
|
100
|
+
const string = strings.find(([from, to]) => match.index >= from && end <= to)
|
|
101
|
+
|
|
102
|
+
if (string === undefined)
|
|
103
|
+
continue
|
|
104
|
+
|
|
105
|
+
const next = OLD_LINK_FLAGS[name]!
|
|
106
|
+
const written = `${next.name}=${next.value}`
|
|
107
|
+
const selector = source[match.index - 1] === '[' && source[end] === ']'
|
|
108
|
+
const read = !selector && string[1] - string[0] === name.length && ATTRIBUTE_CALL.test(source.slice(Math.max(0, string[0] - 201), string[0] - 1))
|
|
109
|
+
let fixes: Fix[] | undefined
|
|
110
|
+
|
|
111
|
+
if (selector)
|
|
112
|
+
fixes = [{ title: `write [${written}]`, safe: true, edits: [{ start: match.index - 1, end: end + 1, text: `[${written}]` }] }]
|
|
113
|
+
else if (read)
|
|
114
|
+
fixes = [{ title: `write ${next.name}`, safe: false, edits: [{ start: match.index, end, text: next.name }] }]
|
|
115
|
+
|
|
116
|
+
report(match.index, end, `\`${name}\` is \`${written}\`, deprecated since 1.105: a template written with the new attribute no longer matches this`, `select \`[${written}]\`; an attribute read follows the new name, whose value is \`${next.value}\` where the old one was present`, fixes)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const { node, written, direct } of datasets) {
|
|
120
|
+
const member = node as Node & { object: Node, property: Node & { name: string } }
|
|
121
|
+
const old = OLD_LINK_FLAGS[DATASET_NAMES.get(member.property.name)!]!
|
|
122
|
+
const to = `dataset.${old.name.slice(5)} === '${old.value}'`
|
|
123
|
+
const fixes: Fix[] | undefined = written || !direct ? undefined : [{ title: `write ${to}`, safe: false, edits: [{ start: node.start, end: node.end, text: `(${source.slice(member.object.start, member.object.end).replace(DATASET_END, '')}${to})` }] }]
|
|
124
|
+
|
|
125
|
+
report(node.start, node.end, `\`dataset.${member.property.name}\` is \`${to}\`, deprecated since 1.105: the template's attribute is \`${old.name}\``, `read \`${to}\`; a write is set on \`${old.name}\` by hand`, fixes)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return found.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.column - b.column)
|
|
130
|
+
}
|
package/src/check/project.ts
CHANGED
|
@@ -32,6 +32,7 @@ import { dockerfileProblems } from './dockerfile'
|
|
|
32
32
|
import { CONTENT_CHECK, contentPropsCheck } from './generated'
|
|
33
33
|
import { browserModules, islandEdges } from './island-modules'
|
|
34
34
|
import { componentImporters, islandOnlyComponents, renderedInIslands } from './island-only'
|
|
35
|
+
import { linkFlagTests } from './link-flag-tests'
|
|
35
36
|
import { FILES_MARKER, stampOf } from './loaded'
|
|
36
37
|
import { renamedPackages } from './renamed-packages'
|
|
37
38
|
import { configSeoLints } from './seo'
|
|
@@ -105,13 +106,13 @@ export function configProblems(root: string, options: CheckOptions, types: Shado
|
|
|
105
106
|
// The collections may still be read, which only the feeds and their own problems wait for.
|
|
106
107
|
export async function projectProblems(root: string, options: CheckOptions, collections: ContentChecks | Promise<ContentChecks>, memory?: ProbeMemory, scanned?: ServedProject): Promise<CheckDiagnostic[]> {
|
|
107
108
|
return inWalkScope(async () => {
|
|
108
|
-
const [content, messages, boot, modules, packages] = await Promise.all([collections, checkMessages(root, options, memory), (async () => {
|
|
109
|
+
const [content, messages, boot, modules, packages, linkTests] = await Promise.all([collections, checkMessages(root, options, memory), (async () => {
|
|
109
110
|
const read = await collections
|
|
110
111
|
|
|
111
112
|
return bootProblems(resolve(root), options, read.feeds, scanned, read.routes)
|
|
112
|
-
})(), moduleLints(root, options), renamedPackages(root, options.lints)])
|
|
113
|
+
})(), moduleLints(root, options), renamedPackages(root, options.lints), linkFlagTests(root, options.lints)])
|
|
113
114
|
|
|
114
|
-
return [...content.problems, ...messages, ...boot, ...modules, ...packages, ...uiProblems(root), ...contextModuleProblems(root)]
|
|
115
|
+
return [...content.problems, ...messages, ...boot, ...modules, ...packages, ...linkTests, ...uiProblems(root), ...contextModuleProblems(root)]
|
|
115
116
|
})
|
|
116
117
|
}
|
|
117
118
|
|
|
@@ -71,7 +71,7 @@ function isErrorStatus(node: Node | undefined): boolean {
|
|
|
71
71
|
return node?.type === 'Literal' && typeof node.value === 'number' && node.value >= 400 && node.value <= 599
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
// `return ctx.fail(status, data, errors)`, `return ctx.deny(status, { data, errors })` since 1.105: an action refuses as a guard and a page do. A text `data` is the form's message
|
|
74
|
+
// `return ctx.fail(status, data, errors)`, `return ctx.deny(status, { data, errors })` since 1.105: an action refuses as a guard and a page do. A text `data` is the form's message and is written as the `message`, and a call with neither data nor errors is left, as `deny(status)` stops with plain text.
|
|
75
75
|
// Found on `ctx.fail` of a handler's `ctx`; the fix is made where the call is returned, as `deny` throws and `fail` did not.
|
|
76
76
|
function failCalls({ parsed, report }: Found): void {
|
|
77
77
|
for (const { ast, base, text } of parsed) {
|
|
@@ -83,7 +83,7 @@ function failCalls({ parsed, report }: Found): void {
|
|
|
83
83
|
const parts: Part[] = []
|
|
84
84
|
|
|
85
85
|
if (data !== undefined && !isUndefined(data))
|
|
86
|
-
parts.push(['data', text.slice(data.start, data.end)])
|
|
86
|
+
parts.push([isText(data) ? 'message' : 'data', text.slice(data.start, data.end)])
|
|
87
87
|
|
|
88
88
|
if (errors !== undefined && !isUndefined(errors))
|
|
89
89
|
parts.push(['errors', text.slice(errors.start, errors.end)])
|
package/src/check/regroup.ts
CHANGED
|
@@ -12,8 +12,8 @@ export function nameOf(property: Node): string {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
// The edits that group the old keys, leaving every other property, its comments and the object's layout as written: the first old property of a feature becomes `feature: { part: value }` holding all of its parts, the others are removed. Undefined where that cannot be done without reading what a value is: a spread, a computed or shorthand key, a part named twice, a feature written both ways, or a comment that a removal would take.
|
|
15
|
-
// With `whole`, a part is the property as written (a shorthand or a method included) rather than `part: value`, for a group that takes the keys under their own names.
|
|
16
|
-
export function regrouped(object: NodeOfType<'ObjectExpression'>, text: string, movedTo: (name: string, property: Property) => [string, string] | undefined, whole = false): Edit[] | undefined {
|
|
15
|
+
// With `whole`, a part is the property as written (a shorthand or a method included) rather than `part: value`, for a group that takes the keys under their own names. `shaped` writes the value a part takes from the old key's name and value text, for a part that wraps it.
|
|
16
|
+
export function regrouped(object: NodeOfType<'ObjectExpression'>, text: string, movedTo: (name: string, property: Property) => [string, string] | undefined, whole = false, shaped?: (name: string, value: string) => string): Edit[] | undefined {
|
|
17
17
|
const properties: Property[] = []
|
|
18
18
|
|
|
19
19
|
for (const property of object.properties) {
|
|
@@ -43,7 +43,9 @@ export function regrouped(object: NodeOfType<'ObjectExpression'>, text: string,
|
|
|
43
43
|
if ((property.shorthand && !whole) || held.parts.has(part))
|
|
44
44
|
return undefined
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
const value = text.slice(property.value.start, property.value.end)
|
|
47
|
+
|
|
48
|
+
held.parts.set(part, whole ? text.slice(property.start, property.end) : shaped?.(name, value) ?? value)
|
|
47
49
|
features.set(feature, held)
|
|
48
50
|
moved.add(index)
|
|
49
51
|
}
|
|
@@ -4,7 +4,7 @@ import { writeFile } from 'node:fs/promises'
|
|
|
4
4
|
import { basename } from 'node:path'
|
|
5
5
|
import { findProjectFiles } from '../build/files'
|
|
6
6
|
import { lintLevel } from '../compiler/lints'
|
|
7
|
-
import { OLD_PACKAGE_NAME, RENAMED_IN, RENAMED_PACKAGES
|
|
7
|
+
import { OLD_PACKAGE_NAME, RENAMED_IN, RENAMED_PACKAGES } from '../package-names'
|
|
8
8
|
|
|
9
9
|
// The files that name a package: the scripts, components and stylesheets of the project, and its manifest and tsconfigs.
|
|
10
10
|
const NAMING_FILE = /^(?:package\.json|tsconfig[\w.-]*\.json)$|\.(?:[cm]?[jt]sx?|kay|css)$/
|
|
@@ -37,8 +37,18 @@ const BEFORE_NAME_IN_STRING = /^(?:npm:|[\w./-]*node_modules\/)?$/
|
|
|
37
37
|
const LINE_COMMENT = /(?:^|[^:])\/\//
|
|
38
38
|
const KEY_END = /^"\s*:/
|
|
39
39
|
|
|
40
|
-
//
|
|
41
|
-
|
|
40
|
+
// A tsconfig names a package anywhere a string does: `extends`, `types`, `paths`.
|
|
41
|
+
type Kind = 'manifest' | 'tsconfig' | 'stylesheet' | 'script'
|
|
42
|
+
|
|
43
|
+
// What opens the string of a module specifier in a script or a component: `from`, `import 'x'`, `import(`, `require(`, `require.resolve(`, `import.meta.resolve(`, `declare module` and a test's `mock.module(`.
|
|
44
|
+
const SPECIFIER_BEFORE = /(?:\b(?:from|import|module)|\b(?:require|require\.resolve|import\.meta\.resolve|import|mock\.module)\s*\()\s*$/
|
|
45
|
+
// A config key: a quoted name keyed to anything but a string that names a package or a version, which is data (a table of renames, a fixture of versions): `librarySettings: { '@kayjs/ui': './ui.ts' }` or a component path keyed to its options.
|
|
46
|
+
const CONFIG_KEY_END = /^[\w./*-]*["'`]\s*:\s*(?!\s|["'`](?:@kay|[~^]?\d))/
|
|
47
|
+
// What opens the path of a stylesheet that names a package.
|
|
48
|
+
const STYLESHEET_BEFORE = /@(?:import|plugin|source|config|reference)\b[^;'"`]*$/
|
|
49
|
+
|
|
50
|
+
// Whether the name at `index` is what the project depends on, not a word of a comment, a sentence or a string of data: in a stylesheet the path of an `@import`, `@plugin` or `@source`, in a script the specifier of an import, a `require` or `import()` or a config key, in a manifest a key or an alias, in a tsconfig any string.
|
|
51
|
+
function namesAPackage(source: string, index: number, end: number, kind: Kind, comments: [number, number][]): boolean {
|
|
42
52
|
if (comments.some(([from, to]) => index >= from && index < to))
|
|
43
53
|
return false
|
|
44
54
|
|
|
@@ -52,7 +62,36 @@ function namesAPackage(source: string, index: number, end: number, manifest: boo
|
|
|
52
62
|
if (LINE_COMMENT.test(before.slice(0, quote)))
|
|
53
63
|
return false
|
|
54
64
|
|
|
55
|
-
|
|
65
|
+
if (kind === 'manifest')
|
|
66
|
+
return before.endsWith('npm:') || KEY_END.test(source.slice(end))
|
|
67
|
+
|
|
68
|
+
if (kind === 'tsconfig')
|
|
69
|
+
return true
|
|
70
|
+
|
|
71
|
+
const opening = source.slice(Math.max(0, lineStart + quote - 200), lineStart + quote)
|
|
72
|
+
|
|
73
|
+
if (kind === 'stylesheet')
|
|
74
|
+
return STYLESHEET_BEFORE.test(opening)
|
|
75
|
+
|
|
76
|
+
return SPECIFIER_BEFORE.test(opening) || (before.slice(quote + 1) === '' && CONFIG_KEY_END.test(source.slice(end)))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function kindOf(file: string): Kind {
|
|
80
|
+
if (basename(file) === 'package.json')
|
|
81
|
+
return 'manifest'
|
|
82
|
+
|
|
83
|
+
if (file.endsWith('.json'))
|
|
84
|
+
return 'tsconfig'
|
|
85
|
+
|
|
86
|
+
return file.endsWith('.css') ? 'stylesheet' : 'script'
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The position of each old package name a file depends on, as the lint and `kay upgrade` rewrite them.
|
|
90
|
+
export function oldNamesIn(file: string, source: string): RegExpExecArray[] {
|
|
91
|
+
const kind = kindOf(file)
|
|
92
|
+
const comments = kind === 'manifest' || kind === 'tsconfig' ? [] : [...source.matchAll(BLOCK_COMMENT)].map((c): [number, number] => [c.index, c.index + c[0].length])
|
|
93
|
+
|
|
94
|
+
return [...source.matchAll(OLD_PACKAGE_NAME)].filter(match => namesAPackage(source, match.index, match.index + match[0].length, kind, comments))
|
|
56
95
|
}
|
|
57
96
|
|
|
58
97
|
// Every file of the project that names a package, but its manifest, written with the new names: what `kay upgrade` does before it runs the kay it installed, which loads the project's config and so cannot meet an old name.
|
|
@@ -62,14 +101,24 @@ export async function renameProjectFiles(root: string): Promise<void> {
|
|
|
62
101
|
continue
|
|
63
102
|
|
|
64
103
|
const source = await Bun.file(file).text()
|
|
65
|
-
const
|
|
104
|
+
const names = oldNamesIn(file, source)
|
|
105
|
+
|
|
106
|
+
if (names.length === 0)
|
|
107
|
+
continue
|
|
66
108
|
|
|
67
|
-
|
|
68
|
-
|
|
109
|
+
let renamed = ''
|
|
110
|
+
let from = 0
|
|
111
|
+
|
|
112
|
+
for (const match of names) {
|
|
113
|
+
renamed += source.slice(from, match.index) + RENAMED_PACKAGES[match[0]]!
|
|
114
|
+
from = match.index + match[0].length
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
await writeFile(file, renamed + source.slice(from))
|
|
69
118
|
}
|
|
70
119
|
}
|
|
71
120
|
|
|
72
|
-
// kay and its libraries under the `@kaynooo` scope, named
|
|
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.
|
|
73
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.
|
|
74
123
|
export async function renamedPackages(root: string, levels: LintLevels | undefined): Promise<CheckDiagnostic[]> {
|
|
75
124
|
const level = lintLevel('deprecated-package-names', levels)
|
|
@@ -83,14 +132,9 @@ export async function renamedPackages(root: string, levels: LintLevels | undefin
|
|
|
83
132
|
const source = await Bun.file(file).text()
|
|
84
133
|
const manifest = basename(file) === 'package.json'
|
|
85
134
|
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
for (const match of source.matchAll(OLD_PACKAGE_NAME)) {
|
|
135
|
+
for (const match of oldNamesIn(file, source)) {
|
|
89
136
|
const name = match[0]
|
|
90
137
|
|
|
91
|
-
if (!namesAPackage(source, match.index, match.index + name.length, manifest, comments))
|
|
92
|
-
continue
|
|
93
|
-
|
|
94
138
|
const next = RENAMED_PACKAGES[name]!
|
|
95
139
|
const before = source.slice(0, match.index).split('\n')
|
|
96
140
|
const raised = manifest ? raisedRange(source, match.index + name.length) : undefined
|