-
Notifications
You must be signed in to change notification settings - Fork 70
fix: scaffolded apps get real core types and a usable typescript range #1452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ede9213
fix: type the core exports an app resolves as any
vivek7405 043a117
fix: stop the scaffold declaring a typescript it cannot use
vivek7405 20335c2
docs: correct the JSONC claim in the typescript-floor guard
vivek7405 b6fd36e
fix: repair the three consumers the Handle fix exposed
vivek7405 467ead6
chore: drop an eslint directive from a repo with no eslint
vivek7405 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export interface CSSResult { | ||
| _$webjsCss: true; | ||
| text: string; | ||
| } | ||
|
|
||
| export function css(strings: TemplateStringsArray | string[], ...values: unknown[]): CSSResult; | ||
| export function isCSS(x: unknown): x is CSSResult; | ||
| export function adoptStyles(root: ShadowRoot | Document, styles: CSSResult[]): void; | ||
| export function stylesToString(styles: CSSResult[]): string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export function escapeText(s: string): string; | ||
| export function escapeAttr(s: string): string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export interface TemplateResult { | ||
| _$webjs: 'template'; | ||
| strings: TemplateStringsArray | string[]; | ||
| values: unknown[]; | ||
| } | ||
|
|
||
| export function html(strings: TemplateStringsArray | string[], ...values: unknown[]): TemplateResult; | ||
| export function isTemplate(x: unknown): x is TemplateResult; | ||
| export const MARKER: 'wjm-'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // The runtime value also carries a module-private `Symbol.for('webjs.repeat')` | ||
| // key, the marker the renderers check. It is deliberately absent here: it is not | ||
| // exported, so it cannot be named, and no consumer constructs one by hand. | ||
| export interface RepeatDirective<T> { | ||
| items: T[]; | ||
| keyFn: (item: T, i: number) => string | number; | ||
| templateFn: (item: T, i: number) => unknown; | ||
| } | ||
|
|
||
| export function repeat<T>( | ||
| items: Iterable<T>, | ||
| keyFn: (item: T, i: number) => string | number, | ||
| templateFn: (item: T, i: number) => unknown, | ||
| ): RepeatDirective<T>; | ||
| export function isRepeat(x: unknown): x is RepeatDirective<unknown>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // `body` is widened off `RequestInit` on purpose: richFetch also accepts a plain | ||
| // object, which it serializes with the WebJs wire format. `Omit` first, because | ||
| // an intersection would narrow the property back to `BodyInit | null`. | ||
| export function richFetch<T>(url: string | URL, init?: Omit<RequestInit, 'body'> & { body?: unknown }): Promise<T>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export interface SuspenseBoundary { | ||
| _$webjsSuspense: true; | ||
| fallback: unknown; | ||
| children: unknown; | ||
| } | ||
|
|
||
| export function Suspense(props: { fallback: unknown; children: unknown | Promise<unknown> }): SuspenseBoundary; | ||
| export function isSuspense(x: unknown): x is SuspenseBoundary; | ||
| export const SUSPENSE: unique symbol; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export interface ConnectOptions { | ||
| onOpen?: (ev: Event) => void; | ||
| // `any`, matching the JSDoc, and load-bearing rather than lazy: the socket | ||
| // delivers an arbitrary JSON payload, and the contract is that the CALLER | ||
| // names the shape it expects (`(msg: ChatMessage) => ...`). Narrowing this to | ||
| // `unknown` type-checks here and breaks every such handler, which is not a | ||
| // change a PR filling in missing declarations gets to make. | ||
| onMessage?: (data: any, ev: MessageEvent) => void; | ||
| onClose?: (ev: CloseEvent) => void; | ||
| onError?: (ev: Event) => void; | ||
| protocols?: string | string[]; | ||
| reconnect?: boolean; | ||
| } | ||
|
|
||
| export interface WSConnection { | ||
| send(data: string | ArrayBuffer | ArrayBufferView | object): void; | ||
| close(code?: number, reason?: string): void; | ||
| readonly socket: WebSocket | null; | ||
| readonly readyState: 0 | 1 | 2 | 3; | ||
| } | ||
|
|
||
| export function connectWS(url: string, opts?: ConnectOptions): WSConnection; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /** | ||
| * The generated `package.json` must not permit a TypeScript that cannot read | ||
| * the `tsconfig.json` the SAME generator writes. | ||
| * | ||
| * The scaffold shipped `"typescript": "^5.6.0"` alongside a tsconfig setting | ||
| * `erasableSyntaxOnly`, which landed in TypeScript 5.8. Every version in the | ||
| * lower half of that range refuses the config outright with | ||
| * `TS5023: Unknown compiler option 'erasableSyntaxOnly'`, exit 2, nothing else | ||
| * checked. It stayed invisible because `npm install` resolves a caret range to | ||
| * the newest matching version, so a fresh scaffold picked up 5.9 and worked; it | ||
| * bites a pinned install, an older lockfile, or a toolchain whose own compiler | ||
| * is older. Nothing tied the two files together, so they were free to drift. | ||
| * | ||
| * This ties them. `REQUIRES` maps each compiler option the generator emits to | ||
| * the TypeScript version that introduced it, and the test asserts two things: | ||
| * the declared range's LOWEST satisfying version clears the highest floor among | ||
| * the emitted options, and every emitted option is classified. The second half | ||
| * is what keeps this from rotting: adding an option the table does not know | ||
| * fails the test until someone records its floor, the same "classify it or CI | ||
| * stays red" contract as the gallery-coverage manifest. | ||
| * | ||
| * Counterfactual: restore `^5.6.0` (or add an unclassified option) and this | ||
| * fails. | ||
| */ | ||
| import { test } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { mkdtemp, rm, readFile } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
| import { tmpdir } from 'node:os'; | ||
|
|
||
| import { scaffoldApp } from '../../packages/cli/lib/create.js'; | ||
|
|
||
| /** | ||
| * The TypeScript release that introduced each compiler option the generated | ||
| * tsconfig sets. `1.0.0` means "as old as anything we care about", used for the | ||
| * options that predate every version this project could run. | ||
| */ | ||
| const REQUIRES = { | ||
| target: '1.0.0', | ||
| module: '1.0.0', | ||
| moduleResolution: '1.0.0', | ||
| lib: '1.0.0', | ||
| types: '1.0.0', | ||
| strict: '2.3.0', | ||
| noEmit: '1.0.0', | ||
| skipLibCheck: '2.0.0', | ||
| plugins: '2.3.0', | ||
| allowImportingTsExtensions: '5.0.0', | ||
| // The option this guard exists for. | ||
| erasableSyntaxOnly: '5.8.0', | ||
| }; | ||
|
|
||
| const TEMPLATES = ['full-stack', 'api']; | ||
|
|
||
| for (const template of TEMPLATES) { | ||
| test(`${template}: the declared typescript range can read the generated tsconfig`, async () => { | ||
| const cwd = await mkdtemp(join(tmpdir(), `webjs-tsfloor-${template}-`)); | ||
| try { | ||
| await scaffoldApp('demo', cwd, { template, install: false }); | ||
| const pkg = JSON.parse(await readFile(join(cwd, 'demo', 'package.json'), 'utf8')); | ||
| // The generator emits plain JSON today (JSON.stringify, no comments), | ||
| // but tsconfig.json is JSONC by convention, so parse defensively: a | ||
| // comment added to the output later must red an assertion here, never | ||
| // crash the parse. | ||
| const tsconfigRaw = await readFile(join(cwd, 'demo', 'tsconfig.json'), 'utf8'); | ||
| const options = Object.keys(JSON.parse(stripJsonComments(tsconfigRaw)).compilerOptions); | ||
|
|
||
| const unclassified = options.filter((o) => !(o in REQUIRES)); | ||
| assert.deepEqual( | ||
| unclassified, | ||
| [], | ||
| `the generated tsconfig sets compiler option(s) with no recorded TypeScript ` + | ||
| `floor: ${unclassified.join(', ')}. Add each to REQUIRES with the version ` + | ||
| `that introduced it, so the declared range keeps being checked against it.`, | ||
| ); | ||
|
|
||
| const required = options | ||
| .map((o) => REQUIRES[o]) | ||
| .reduce((hi, v) => (compare(v, hi) > 0 ? v : hi), '1.0.0'); | ||
|
|
||
| const range = pkg.devDependencies?.typescript; | ||
| assert.ok(range, `${template}: the generated package.json declares no typescript`); | ||
| // The LOWEST version the range admits is the one that has to work: npm | ||
| // resolves a caret to the newest match today, which is exactly why the | ||
| // drift went unnoticed. | ||
| const lowest = lowestSatisfying(range); | ||
| assert.ok( | ||
| compare(lowest, required) >= 0, | ||
| `${template}: "typescript": "${range}" admits ${lowest}, but the ` + | ||
| `generated tsconfig needs at least ${required} (its highest option floor). ` + | ||
| `That version refuses the config with TS5023 and checks nothing.`, | ||
| ); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * The lowest version a range admits. Deliberately narrow: it understands the | ||
| * range shapes a generated manifest actually uses and THROWS on anything else, | ||
| * because a range this cannot read is one it must not silently pass. Written | ||
| * out rather than pulled from `semver`, which this repo does not declare as a | ||
| * dependency (it is only present transitively, so importing it here would make | ||
| * the test hostage to an unrelated lockfile change). | ||
| */ | ||
| function lowestSatisfying(range) { | ||
| const m = /^\s*(?:\^|~|>=)?\s*(\d+)\.(\d+)\.(\d+)\s*$/.exec(range); | ||
| if (!m) { | ||
| throw new Error( | ||
| `cannot read the version range ${JSON.stringify(range)}. Extend ` + | ||
| `lowestSatisfying() to cover it rather than loosening this guard.`, | ||
| ); | ||
| } | ||
| return `${m[1]}.${m[2]}.${m[3]}`; | ||
| } | ||
|
|
||
| /** Numeric x.y.z comparison. Returns >0 when `a` is newer than `b`. */ | ||
| function compare(a, b) { | ||
| const pa = a.split('.').map(Number); | ||
| const pb = b.split('.').map(Number); | ||
| for (let i = 0; i < 3; i += 1) { | ||
| if (pa[i] !== pb[i]) return pa[i] - pb[i]; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * Strip `//` and block comments from JSONC. The generated tsconfig has none | ||
| * today, so this is a no-op on it; it exists so a comment added to the output | ||
| * later degrades to a failed assertion instead of a parse crash. String-aware, | ||
| * so a `//` inside a value is not eaten. | ||
| */ | ||
| function stripJsonComments(text) { | ||
| let out = ''; | ||
| let inString = false; | ||
| let inLine = false; | ||
| let inBlock = false; | ||
| for (let i = 0; i < text.length; i += 1) { | ||
| const c = text[i]; | ||
| const next = text[i + 1]; | ||
| if (inLine) { | ||
| if (c === '\n') { inLine = false; out += c; } | ||
| continue; | ||
| } | ||
| if (inBlock) { | ||
| if (c === '*' && next === '/') { inBlock = false; i += 1; } | ||
| continue; | ||
| } | ||
| if (inString) { | ||
| out += c; | ||
| if (c === '\\') { out += next; i += 1; continue; } | ||
| if (c === '"') inString = false; | ||
| continue; | ||
| } | ||
| if (c === '"') { inString = true; out += c; continue; } | ||
| if (c === '/' && next === '/') { inLine = true; i += 1; continue; } | ||
| if (c === '/' && next === '*') { inBlock = true; i += 1; continue; } | ||
| out += c; | ||
| } | ||
| return out; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.