Skip to content

Commit

Permalink
fix(semantic): report error for super property appearing in function …
Browse files Browse the repository at this point in the history
…body (#8376)

Missing error that super property inside plain function.

```js
class C {
  constructor() {
    function g() {
      // * It is a Syntax Error if PropName of MethodDefinition is not "constructor" and HasDirectSuper of MethodDefinition is true.
      super();
    }
  }
  method() {
    function func() {
      // It is a Syntax Error if FunctionBody Contains SuperProperty is true.
      super.good();
    }
  }
}
```

I am not sure why test262 doesn't cover tests like that
  • Loading branch information
Dunqing committed Jan 9, 2025
1 parent 5516f7f commit 79a8fc6
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 9 deletions.
9 changes: 9 additions & 0 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,15 @@ pub fn check_super<'a>(sup: &Super, node: &AstNode<'a>, ctx: &SemanticBuilder<'a
// super references are allowed in method
break;
}
// * It is a Syntax Error if FunctionBody Contains SuperProperty is true.
AstKind::Function(_) => {
if !matches!(ctx.nodes.parent_kind(node_id), Some(AstKind::MethodDefinition(_))) {
return super_call_span.map_or_else(
|| ctx.error(unexpected_super_reference(sup.span)),
|super_call_span| ctx.error(unexpected_super_call(super_call_span)),
);
}
}
// FieldDefinition : ClassElementName Initializer opt
// * It is a Syntax Error if Initializer is present and Initializer Contains SuperCall is true.
// PropertyDefinition : MethodDefinition
Expand Down
11 changes: 9 additions & 2 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 54a8389f
parser_babel Summary:
AST Parsed : 2205/2218 (99.41%)
Positive Passed: 2184/2218 (98.47%)
Negative Passed: 1522/1634 (93.15%)
Negative Passed: 1523/1634 (93.21%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.1-sloppy-labeled-functions-if-body/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-if/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-loop/input.js
Expand All @@ -25,7 +25,6 @@ Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/comma-after-spread-nested/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/no-pattern-in-rest/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2018/object-rest-spread/no-pattern-in-rest-with-ts/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-properties/super-inside-function/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-properties/yield-in-class-property-in-generator/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/class-static-block/invalid-decorators/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/private-in/invalid-private-followed-by-in-2/input.js
Expand Down Expand Up @@ -8248,6 +8247,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
5 │ }
╰────

× 'super' can only be referenced in members of derived classes or object literal expressions.
╭─[babel/packages/babel-parser/test/fixtures/es2022/class-properties/super-inside-function/input.js:3:5]
2 │ foo = function fn() {
3 │ super.x();
· ─────
4 │ }
╰────

× Cannot use `await` as an identifier in an async context
╭─[babel/packages/babel-parser/test/fixtures/es2022/class-static-block/await-binding-in-async-arrow-function-in-static-block/input.js:3:29]
2 │ // await is not allowed in async arrow
Expand Down
Loading

0 comments on commit 79a8fc6

Please sign in to comment.