Skip to content

Commit

Permalink
refactor(semantic): add strict mode in scope flags for class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jul 10, 2024
1 parent 2203143 commit e88e957
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 56 deletions.
7 changes: 6 additions & 1 deletion crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ impl<'a> SemanticBuilder<'a> {

pub fn strict_mode(&self) -> bool {
self.current_scope_flags().is_strict_mode()
|| self.current_node_flags.contains(NodeFlags::Class)
}

pub fn set_function_node_flag(&mut self, flag: NodeFlags) {
Expand Down Expand Up @@ -422,6 +421,12 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
if flags.contains(ScopeFlags::Top) { None } else { Some(self.current_scope_id) };

let mut flags = flags;

if !flags.is_strict_mode() && self.current_node_flags.has_class() {
// NOTE A class definition is always strict mode code.
flags |= ScopeFlags::StrictMode;
};

if let Some(parent_scope_id) = parent_scope_id {
flags = self.scope.get_new_scope_flags(flags, parent_scope_id);
}
Expand Down
9 changes: 2 additions & 7 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 12619ffe
parser_babel Summary:
AST Parsed : 2093/2101 (99.62%)
Positive Passed: 2083/2101 (99.14%)
Negative Passed: 1380/1501 (91.94%)
Negative Passed: 1379/1501 (91.87%)
Expect Syntax Error: "annex-b/disabled/1.1-html-comments-close/input.js"
Expect Syntax Error: "annex-b/disabled/3.1-sloppy-labeled-functions/input.js"
Expect Syntax Error: "annex-b/disabled/3.1-sloppy-labeled-functions-if-body/input.js"
Expand Down Expand Up @@ -39,6 +39,7 @@ Expect Syntax Error: "esprima/es2015-arrow-function/invalid-param-strict-mode/in
Expect Syntax Error: "esprima/es2015-generator/generator-parameter-binding-property-reserved/input.js"
Expect Syntax Error: "esprima/invalid-syntax/migrated_0101/input.js"
Expect Syntax Error: "esprima/invalid-syntax/migrated_0276/input.js"
Expect Syntax Error: "esprima/invalid-syntax/migrated_0278/input.js"
Expect Syntax Error: "typescript/cast/satisfies-const-error/input.ts"
Expect Syntax Error: "typescript/cast/unparenthesized-assert-and-assign/input.ts"
Expect Syntax Error: "typescript/cast/unparenthesized-type-assertion-and-assign/input.ts"
Expand Down Expand Up @@ -9688,12 +9689,6 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ────
╰────

× The keyword 'static' is reserved
╭─[esprima/invalid-syntax/migrated_0278/input.js:1:18]
1 │ class A {static [static](){};}
· ──────
╰────

× A 'set' accessor cannot have rest parameter.
╭─[esprima/rest-parameter/invalid-setter-rest/input.js:1:13]
1 │ x = { set f(...y) {} }
Expand Down
53 changes: 5 additions & 48 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: d8086f14
parser_typescript Summary:
AST Parsed : 5279/5283 (99.92%)
Positive Passed: 5272/5283 (99.79%)
Negative Passed: 1094/4875 (22.44%)
Negative Passed: 1090/4875 (22.36%)
Expect Syntax Error: "compiler/ClassDeclaration10.ts"
Expect Syntax Error: "compiler/ClassDeclaration11.ts"
Expect Syntax Error: "compiler/ClassDeclaration13.ts"
Expand Down Expand Up @@ -2321,6 +2321,8 @@ Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames38
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames38_ES6.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames39_ES5.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames39_ES6.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames3_ES5.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames3_ES6.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames42_ES5.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames42_ES6.ts"
Expect Syntax Error: "conformance/es6/computedProperties/computedPropertyNames43_ES5.ts"
Expand Down Expand Up @@ -3287,7 +3289,9 @@ Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parse
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName31.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName32.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName34.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName36.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName37.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName38.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName4.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName40.ts"
Expect Syntax Error: "conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName41.ts"
Expand Down Expand Up @@ -9690,21 +9694,6 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
16 │ }
╰────

× The keyword 'package' is reserved
╭─[compiler/strictModeReservedWordInClassDeclaration.ts:27:17]
26 │ class F1 implements public.private.implements { }
27 │ class G extends package { }
· ───────
28 │ class H extends package.A { }
╰────

× The keyword 'package' is reserved
╭─[compiler/strictModeReservedWordInClassDeclaration.ts:28:17]
27 │ class G extends package { }
28 │ class H extends package.A { }
· ───────
╰────

× The keyword 'public' is reserved
╭─[compiler/strictModeReservedWordInDestructuring.ts:2:6]
1 │ "use strict"
Expand Down Expand Up @@ -13073,22 +13062,6 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
5 │ }
╰────

× Delete of an unqualified identifier in strict mode.
╭─[conformance/es6/computedProperties/computedPropertyNames3_ES5.ts:5:17]
4 │ static [() => { }]() { }
5 │ get [delete id]() { }
· ──
6 │ set [[0, 1]](v) { }
╰────

× Delete of an unqualified identifier in strict mode.
╭─[conformance/es6/computedProperties/computedPropertyNames3_ES6.ts:5:17]
4 │ static [() => { }]() { }
5 │ get [delete id]() { }
· ──
6 │ set [[0, 1]](v) { }
╰────

× Identifier `` has already been declared
╭─[conformance/es6/computedProperties/computedPropertyNames40_ES5.ts:8:6]
7 │ // Computed properties
Expand Down Expand Up @@ -19143,22 +19116,6 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
3 │ }
╰────

× The keyword 'public' is reserved
╭─[conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName36.ts:2:6]
1 │ class C {
2 │ [public ]: string;
· ──────
3 │ }
╰────

× The keyword 'public' is reserved
╭─[conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName38.ts:2:6]
1 │ class C {
2 │ [public]() { }
· ──────
3 │ }
╰────

× The keyword 'public' is reserved
╭─[conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName39.ts:3:6]
2 │ class C {
Expand Down

0 comments on commit e88e957

Please sign in to comment.