Skip to content

Commit

Permalink
Forbid super in static class methods with server function directives
Browse files Browse the repository at this point in the history
Static class methods that are annotated with `"use cache"` or `"use
server"` must not call `super`.
  • Loading branch information
unstubbable committed Nov 25, 2024
1 parent 241806a commit 2833fd6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
10 changes: 10 additions & 0 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,16 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
}

fn visit_mut_super(&mut self, n: &mut Super) {
if let ThisStatus::Forbidden { directive } = &self.this_status {
self.emit_error(ServerActionsErrorKind::ForbiddenExpression {
span: n.span,
expr: "super".into(),
directive: directive.clone(),
});
}
}

fn visit_mut_ident(&mut self, n: &mut Ident) {
if n.sym == *"arguments" {
if let ThisStatus::Forbidden { directive } = &self.this_status {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
export class MyClass {
import { BaseClass } from './base'

export class MyClass extends BaseClass {
static async foo() {
// super is allowed here
super.foo()

return fetch('https://example.com').then((res) => res.json())
}
static async bar() {
'use cache'

if (Math.random() > 0.5) {
// super is not allowed here
return [super.bar()]
}

// arguments is not allowed here
console.log(arguments)
// this is not allowed here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export class MyClass {
import { BaseClass } from './base';
export class MyClass extends BaseClass {
static async foo() {
// super is allowed here
super.foo();
return fetch('https://example.com').then((res)=>res.json());
}
static async bar() {
if (Math.random() > 0.5) {
// super is not allowed here
return [
super.bar()
];
}
// arguments is not allowed here
console.log(arguments);
// this is not allowed here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
x "use cache" functions cannot use `super`.
|
,-[input.js:15:1]
14 | // super is not allowed here
15 | return [super.bar()]
: ^^^^^
16 | }
`----
x "use cache" functions cannot use `arguments`.
|
,-[input.js:9:1]
8 | // arguments is not allowed here
9 | console.log(arguments)
,-[input.js:19:1]
18 | // arguments is not allowed here
19 | console.log(arguments)
: ^^^^^^^^^
10 | // this is not allowed here
20 | // this is not allowed here
`----
x "use cache" functions cannot use `this`.
|
,-[input.js:11:1]
10 | // this is not allowed here
11 | return this.foo()
,-[input.js:21:1]
20 | // this is not allowed here
21 | return this.foo()
: ^^^^
12 | }
22 | }
`----

0 comments on commit 2833fd6

Please sign in to comment.