-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typechecking inputs/outputs in action transform tests (#75935)
This PR attempts to add typechecking to the server actions tests fixtures in order to add an extra layer of validation against compiler bugs. (there's a couple!). Unfortunately the tests were pretty loosey-goosey around undefined variables, so there's a lot of noise in adding missing imports for `Button` etc. i tried to call out the interesting changes via PR comments. Note that this is a bit limited, because we often have to rely on typescript inferring some sensible types for the outputs -- see e.g. the issue with `24/output.js` mentioned below. In case typescript can't infer sensible types for something, we can just exclude that file from checking. this seems to be pretty rare, so i think it's okay. We should also definitely add typechecking to the other fixture folders, but this PR is already big, so i want to do that piecemeal. ### TODO - [x] invoke the typechecker in CI - added a `check-compiler-fixtures` script that runs as part of `types-and-precompiled` (open to feedback, maybe this isn't the right place?) - [x] figure out how to make `tsc` error on `crates/next-custom-transforms/tests/fixture/server-actions/server-graph/51/output.js`, where we're referencing a non-existent variable `$$RSC_SERVER_ACTION_0`. right now, it emits no errors - this is a compiler bug, and i want to catch bugs like this in the future - solution: #75944 - [x] figure out why `tsc` is complaining about a type mismatch in `crates/next-custom-transforms/tests/fixture/server-actions/server-graph/24/output.js` - we can work around this by loosening the types for `registerServerReference`, but i'd rather we didn't have to do that - this seems to be a bizarre TS behavior where `fn.bind(...)` makes all the params optional in JS files. ignoring this for now
- Loading branch information
1 parent
871ca31
commit 669c52e
Showing
56 changed files
with
292 additions
and
53 deletions.
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8
crates/next-custom-transforms/tests/fixture/server-actions/client-graph/3/input.js
This file contains 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
'use server' | ||
|
||
export const { sampleFunction } = someObject | ||
export const { sampleFunction2 } = fn() | ||
export let { 0: sampleFunction3, 1: sampleFunction4 } = [g(), g()] | ||
import ANYTHING from 'anything' | ||
|
||
export const { sampleFunction } = ANYTHING | ||
export const { sampleFunction2 } = ANYTHING() | ||
export let { 0: sampleFunction3, 1: sampleFunction4 } = [ANYTHING(), ANYTHING()] |
This file contains 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
2 changes: 2 additions & 0 deletions
2
crates/next-custom-transforms/tests/fixture/server-actions/index.ts
This file contains 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 @@ | ||
/// <reference types="./next" /> | ||
/// <reference types="./modules" /> |
51 changes: 51 additions & 0 deletions
51
crates/next-custom-transforms/tests/fixture/server-actions/modules.d.ts
This file contains 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,51 @@ | ||
declare module 'database' { | ||
export const db: { | ||
serialize<T>(cb: () => T): T | ||
run( | ||
statement: string, | ||
args: Record<string, any>, | ||
onComplete: (this: { lastID: string }, error?: Error) => void | ||
): void | ||
} | ||
} | ||
|
||
declare module 'db' { | ||
export default function deleteFromDb(arg: any, ...args: any[]): Promise<void> | ||
} | ||
|
||
declare module 'auth' { | ||
export function validator<TFn extends (...args: any[]) => any>(fn: TFn): TFn | ||
export function another<TFn extends (...args: any[]) => any>(fn: TFn): TFn | ||
} | ||
|
||
declare module 'anything' { | ||
const ANYTHING: any | ||
export default ANYTHING | ||
} | ||
|
||
declare module 'foo' { | ||
const f: any | ||
export default f | ||
export const f1: any | ||
export const f2: any | ||
} | ||
|
||
declare module 'components' { | ||
import React from 'react' | ||
|
||
export function Button( | ||
props: { | ||
action: () => Promise<any> | ||
} & React.ComponentProps<'button'> | ||
): React.ReactNode | ||
|
||
export function Form( | ||
props: React.PropsWithChildren<{ action: () => Promise<any> }> | ||
): React.ReactNode | ||
|
||
export function Client(props: Record<string, any>): React.ReactNode | ||
} | ||
|
||
declare module 'navigation' { | ||
export function redirect(href: string): void | ||
} |
49 changes: 49 additions & 0 deletions
49
crates/next-custom-transforms/tests/fixture/server-actions/next.d.ts
This file contains 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,49 @@ | ||
declare module 'private-next-rsc-action-encryption' { | ||
export function encryptActionBoundArgs( | ||
actionId: string, | ||
...args: any[] | ||
): Promise<string> | ||
|
||
export function decryptActionBoundArgs( | ||
actionId: string, | ||
encryptedPromise: Promise<string> | ||
): Promise<any[]> | ||
} | ||
|
||
declare module 'private-next-rsc-server-reference' { | ||
export function registerServerReference<T extends (...args: any[]) => any>( | ||
reference: T, | ||
id: string, | ||
exportName: string | null | ||
): T | ||
} | ||
|
||
declare module 'private-next-rsc-action-client-wrapper' { | ||
export function callServer( | ||
actionId: string, | ||
actionArgs: unknown[] | ||
): Promise<unknown> | ||
|
||
export function findSourceMapURL(filename: string): string | null | ||
|
||
const createServerReference: ( | ||
id: string, | ||
callServer: any, | ||
encodeFormAction?: any, | ||
findSourceMapURL?: any, | ||
functionName?: string | ||
) => (...args: unknown[]) => Promise<unknown> | ||
} | ||
|
||
declare module 'private-next-rsc-action-validate' { | ||
function ensureServerEntryExports(actions: unknown[]): void | ||
} | ||
|
||
declare module 'private-next-rsc-cache-wrapper' { | ||
export function cache<TFn extends (...args: any[]) => Promise<any>>( | ||
kind: string, | ||
id: string, | ||
boundArgsLength: number, | ||
fn: TFn | ||
): TFn | ||
} |
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/1/input.js
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { Button } from 'components' | ||
import deleteFromDb from 'db' | ||
|
||
export function Item({ id1, id2 }) { | ||
|
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/1/output.js
This file contains 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 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 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
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/18/input.js
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { Button } from 'components' | ||
import deleteFromDb from 'db' | ||
|
||
const v1 = 'v1' | ||
|
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/18/output.js
This file contains 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
2 changes: 2 additions & 0 deletions
2
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/19/input.js
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { Button } from 'components' | ||
|
||
export function Item({ value }) { | ||
return ( | ||
<> | ||
|
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/19/output.js
This file contains 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 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 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
1 change: 1 addition & 0 deletions
1
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/20/input.js
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
'use server' | ||
|
||
/** @type {[any]} */ | ||
const [foo] = [null] | ||
export default foo |
2 changes: 1 addition & 1 deletion
2
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/20/output.js
This file contains 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
3 changes: 2 additions & 1 deletion
3
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/21/input.js
This file contains 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
3 changes: 2 additions & 1 deletion
3
crates/next-custom-transforms/tests/fixture/server-actions/server-graph/21/output.js
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.