-
-
Notifications
You must be signed in to change notification settings - Fork 260
feat(rsc): expose directive function validation #1412
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
Draft
hi-ogawa
wants to merge
13
commits into
main
Choose a base branch
from
fix/issue-1335
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2b9e36a
feat(rsc): support inline directive methods
hi-ogawa ac815e7
fix(rsc): preserve __proto__ directive methods
hi-ogawa e539a43
fix(rsc): guard generated method names
hi-ogawa 68930e2
fix(rsc): lower static constructor methods safely
hi-ogawa caf4ef5
fix(rsc): preserve computed method key mappings
hi-ogawa dc29e54
fix(rsc): respect nested class directive boundaries
hi-ogawa bd96a38
fix(rsc): narrow jsxDEV this exemption
hi-ogawa 0e0488a
fix(rsc): ignore arguments labels
hi-ogawa bb22b37
test(rsc): cover static method captures
hi-ogawa 8733ef1
fix(rsc): preserve computed __proto__ keys
hi-ogawa 4a968cf
fix(rsc): avoid inferred computed method names
hi-ogawa 0db3859
fix(rsc): prioritize unsupported method diagnostics
hi-ogawa 5ee499b
refactor(rsc): split inline directive method support
hi-ogawa 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
111 changes: 111 additions & 0 deletions
111
packages/plugin-rsc/src/transforms/directive-function.test.ts
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,111 @@ | ||
| import type { Node, Program } from 'estree' | ||
| import { parseAstAsync } from 'vite' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { validateDirectiveFunction } from './directive-function' | ||
|
|
||
| async function getFunction(input: string): Promise<Node> { | ||
| const ast = (await parseAstAsync(input)) as unknown as Program | ||
| const statement = ast.body[0]! | ||
| if (statement.type === 'FunctionDeclaration') return statement | ||
| if (statement.type === 'ClassDeclaration') { | ||
| const member = statement.body.body[0]! | ||
| if (member.type === 'MethodDefinition') return member.value | ||
| } | ||
| throw new Error('unsupported test input') | ||
| } | ||
|
|
||
| describe(validateDirectiveFunction, () => { | ||
| test.each([ | ||
| [`async function action() { this.value }`, 'this'], | ||
| [`async function action() { arguments }`, 'arguments'], | ||
| [`async function action(value = arguments) {}`, 'arguments'], | ||
| [`class A extends B { static async action() { super.action() } }`, 'super'], | ||
| ])('rejects %s', async (input, expression) => { | ||
| const node = await getFunction(input) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).toThrow( | ||
| `"use server" functions cannot use "${expression}".`, | ||
| ) | ||
| }) | ||
|
|
||
| test('follows arrow lexical bindings', async () => { | ||
| const node = await getFunction(` | ||
| async function action() { | ||
| const nested = () => arguments | ||
| } | ||
| `) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).toThrow( | ||
| /arguments/, | ||
| ) | ||
| }) | ||
|
|
||
| test('stops at nested normal functions', async () => { | ||
| const node = await getFunction(` | ||
| async function action() { | ||
| function nested() { | ||
| this.value | ||
| arguments | ||
| const arrow = () => this.value | ||
| } | ||
| } | ||
| `) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).not.toThrow() | ||
| }) | ||
|
|
||
| test('ignores syntax-only arguments identifiers and jsxDEV metadata', async () => { | ||
| const node = await getFunction(` | ||
| async function action() { | ||
| const value = { arguments: 1 } | ||
| return _jsxDEV(Component, {}, undefined, false, undefined, this) | ||
| } | ||
| `) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).not.toThrow() | ||
| }) | ||
|
|
||
| test('ignores unknown and non-function values', async () => { | ||
| const ast = (await parseAstAsync( | ||
| 'export default action', | ||
| )) as unknown as Program | ||
| const statement = ast.body[0]! | ||
| expect(statement.type).toBe('ExportDefaultDeclaration') | ||
| if (statement.type !== 'ExportDefaultDeclaration') return | ||
| expect(() => | ||
| validateDirectiveFunction(statement.declaration, 'use server'), | ||
| ).not.toThrow() | ||
| expect(() => | ||
| validateDirectiveFunction(undefined, 'use server'), | ||
| ).not.toThrow() | ||
| }) | ||
|
|
||
| test('ignores arguments labels', async () => { | ||
| const node = await getFunction(` | ||
| async function action() { | ||
| arguments: while (true) { | ||
| break arguments | ||
| } | ||
| } | ||
| `) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).not.toThrow() | ||
| }) | ||
|
|
||
| test.each([ | ||
| `custom_jsxDEV(this)`, | ||
| `_jsxDEV(this)`, | ||
| `_jsxDEV(Component, {}, undefined, false, this, undefined)`, | ||
| ])('does not exempt user this expressions: %s', async (expression) => { | ||
| const node = await getFunction(`async function action() { ${expression} }`) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).toThrow(/this/) | ||
| }) | ||
|
|
||
| test('stops at nested classes', async () => { | ||
| const node = await getFunction(` | ||
| async function action() { | ||
| class Nested extends Base { | ||
| value = this.value | ||
| method() { return super.method() } | ||
| static { this.initialize() } | ||
| } | ||
| } | ||
| `) | ||
| expect(() => validateDirectiveFunction(node, 'use server')).not.toThrow() | ||
| }) | ||
| }) |
110 changes: 110 additions & 0 deletions
110
packages/plugin-rsc/src/transforms/directive-function.ts
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,110 @@ | ||
| import type { | ||
| ArrowFunctionExpression, | ||
| FunctionDeclaration, | ||
| FunctionExpression, | ||
| Identifier, | ||
| Node, | ||
| } from 'estree' | ||
| import { walk } from 'estree-walker' | ||
|
|
||
| export type DirectiveFunction = | ||
| | ArrowFunctionExpression | ||
| | FunctionDeclaration | ||
| | FunctionExpression | ||
|
|
||
| /** | ||
| * Applies the function restrictions used by Next.js Server Functions. | ||
| * Unknown and non-function values are ignored so transform callbacks can pass | ||
| * `meta.valueNode` directly. | ||
| */ | ||
| export function validateDirectiveFunction( | ||
|
Comment on lines
+15
to
+20
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: next.js reference |
||
| valueNode: { type: string } | null | undefined, | ||
| directive: string, | ||
| ): void { | ||
| if (!valueNode) return | ||
| const node = valueNode as Node | ||
| if (!isFunction(node)) return | ||
|
|
||
| walk(node, { | ||
| enter(child, parent) { | ||
| if ( | ||
| child !== node && | ||
| (child.type === 'FunctionDeclaration' || | ||
| child.type === 'FunctionExpression' || | ||
| child.type === 'ClassDeclaration' || | ||
| child.type === 'ClassExpression') | ||
| ) { | ||
| this.skip() | ||
| return | ||
| } | ||
|
|
||
| const expression = | ||
| child.type === 'ThisExpression' && !isJsxDevSourceThis(child, parent) | ||
| ? 'this' | ||
| : child.type === 'Super' | ||
| ? 'super' | ||
| : child.type === 'Identifier' && | ||
| child.name === 'arguments' && | ||
| isReferenceIdentifier(child, parent) | ||
| ? 'arguments' | ||
| : undefined | ||
| if (expression) { | ||
| throw Object.assign( | ||
| new Error( | ||
| `${JSON.stringify(directive)} functions cannot use ${JSON.stringify(expression)}.`, | ||
| ), | ||
| { pos: child.start }, | ||
| ) | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function isFunction(node: Node): node is DirectiveFunction { | ||
| return ( | ||
| node.type === 'ArrowFunctionExpression' || | ||
| node.type === 'FunctionDeclaration' || | ||
| node.type === 'FunctionExpression' | ||
| ) | ||
| } | ||
|
|
||
| function isJsxDevSourceThis(node: Node, parent: Node | null): boolean { | ||
| return ( | ||
| node.type === 'ThisExpression' && | ||
| parent?.type === 'CallExpression' && | ||
| parent.arguments.length === 6 && | ||
| parent.arguments[5] === node && | ||
| parent.callee.type === 'Identifier' && | ||
| (parent.callee.name === 'jsxDEV' || parent.callee.name === '_jsxDEV') | ||
| ) | ||
| } | ||
|
|
||
| function isReferenceIdentifier(node: Identifier, parent: Node | null): boolean { | ||
| if (!parent) return true | ||
| if ( | ||
| parent.type === 'MemberExpression' && | ||
| parent.property === node && | ||
| !parent.computed | ||
| ) { | ||
| return false | ||
| } | ||
| if ( | ||
| (parent.type === 'Property' || | ||
| parent.type === 'MethodDefinition' || | ||
| parent.type === 'PropertyDefinition') && | ||
| parent.key === node && | ||
| !parent.computed && | ||
| !(parent.type === 'Property' && parent.shorthand) | ||
| ) { | ||
| return false | ||
| } | ||
| if ( | ||
| (parent.type === 'LabeledStatement' || | ||
| parent.type === 'BreakStatement' || | ||
| parent.type === 'ContinueStatement') && | ||
| parent.label === node | ||
| ) { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. just unify
wrapandruntime.