Skip to content

File tree

5 files changed

+46
-202
lines changed

5 files changed

+46
-202
lines changed

crates/oxc_semantic/src/checker/javascript.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,37 @@ fn invalid_let_declaration(x0: &str, span1: Span) -> OxcDiagnostic {
107107

108108
pub fn check_binding_identifier(ident: &BindingIdentifier, ctx: &SemanticBuilder<'_>) {
109109
let strict_mode = ctx.strict_mode();
110-
// It is a Diagnostic if the StringValue of a BindingIdentifier is "eval" or "arguments" within strict mode code.
110+
// In strict mode, `eval` and `arguments` are banned as identifiers.
111111
if strict_mode && matches!(ident.name.as_str(), "eval" | "arguments") {
112-
return ctx.error(unexpected_identifier_assign(&ident.name, ident.span));
112+
// `eval` and `arguments` are allowed as the names of declare functions as well as their arguments.
113+
//
114+
// declare function eval(): void; // OK
115+
// declare function arguments(): void; // OK
116+
// declare function f(eval: number, arguments: number): number; // OK
117+
// declare function f(...eval): number; // OK
118+
// declare function f(...arguments): number; // OK
119+
// declare function g({eval, arguments}: {eval: number, arguments: number}): number; // Error
120+
// declare function h([eval, arguments]: [number, number]): number; // Error
121+
let is_declare_function = |kind: &AstKind| {
122+
kind.as_function()
123+
.is_some_and(|func| matches!(func.r#type, FunctionType::TSDeclareFunction))
124+
};
125+
126+
let parent = ctx.nodes.parent_node(ctx.current_node_id);
127+
let is_ok = match parent.kind() {
128+
AstKind::Function(func) => matches!(func.r#type, FunctionType::TSDeclareFunction),
129+
AstKind::FormalParameter(_) => is_declare_function(&ctx.nodes.parent_kind(parent.id())),
130+
AstKind::BindingRestElement(_) => {
131+
let grand_parent = ctx.nodes.parent_node(parent.id());
132+
matches!(grand_parent.kind(), AstKind::FormalParameters(_))
133+
&& is_declare_function(&ctx.nodes.parent_kind(grand_parent.id()))
134+
}
135+
_ => false,
136+
};
137+
138+
if !is_ok {
139+
return ctx.error(unexpected_identifier_assign(&ident.name, ident.span));
140+
}
113141
}
114142

115143
// LexicalDeclaration : LetOrConst BindingList ;

tasks/coverage/snapshots/parser_babel.snap

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 1d4546bc
22

33
parser_babel Summary:
44
AST Parsed : 2351/2362 (99.53%)
5-
Positive Passed: 2328/2362 (98.56%)
5+
Positive Passed: 2332/2362 (98.73%)
66
Negative Passed: 1600/1698 (94.23%)
77
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-startindex-and-startline-specified-without-startcolumn/input.js
88

@@ -560,142 +560,6 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
560560
6 │ }
561561
╰────
562562

563-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts
564-
565-
× Cannot assign to 'eval' in strict mode
566-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts:3:25]
567-
2 │ export namespace ns {
568-
3 │ export function eval(): void;
569-
· ────
570-
4 │ export function arguments(): void;
571-
╰────
572-
573-
× Cannot assign to 'arguments' in strict mode
574-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts:4:25]
575-
3 │ export function eval(): void;
576-
4 │ export function arguments(): void;
577-
· ─────────
578-
5 │ }
579-
╰────
580-
581-
× Cannot assign to 'eval' in strict mode
582-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts:8:18]
583-
7 │
584-
8 │ declare function eval(): void;
585-
· ────
586-
9 │ declare function arguments(): void;
587-
╰────
588-
589-
× Cannot assign to 'arguments' in strict mode
590-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts:9:18]
591-
8 │ declare function eval(): void;
592-
9 │ declare function arguments(): void;
593-
· ─────────
594-
10 │
595-
╰────
596-
597-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts
598-
599-
× Cannot assign to 'eval' in strict mode
600-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts:3:25]
601-
2 │ export namespace ns {
602-
3 │ export function eval(): void;
603-
· ────
604-
4 │ export function arguments(): void;
605-
╰────
606-
607-
× Cannot assign to 'arguments' in strict mode
608-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts:4:25]
609-
3 │ export function eval(): void;
610-
4 │ export function arguments(): void;
611-
· ─────────
612-
5 │ }
613-
╰────
614-
615-
× Cannot assign to 'eval' in strict mode
616-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts:8:18]
617-
7 │
618-
8 │ declare function eval(): void;
619-
· ────
620-
9 │ declare function arguments(): void;
621-
╰────
622-
623-
× Cannot assign to 'arguments' in strict mode
624-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts:9:18]
625-
8 │ declare function eval(): void;
626-
9 │ declare function arguments(): void;
627-
· ─────────
628-
10 │
629-
╰────
630-
631-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts
632-
633-
× Cannot assign to 'eval' in strict mode
634-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts:4:25]
635-
3 │ export namespace ns {
636-
4 │ export function eval(): void;
637-
· ────
638-
5 │ export function arguments(): void;
639-
╰────
640-
641-
× Cannot assign to 'arguments' in strict mode
642-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts:5:25]
643-
4 │ export function eval(): void;
644-
5 │ export function arguments(): void;
645-
· ─────────
646-
6 │ }
647-
╰────
648-
649-
× Cannot assign to 'eval' in strict mode
650-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts:9:18]
651-
8 │
652-
9 │ declare function eval(): void;
653-
· ────
654-
10 │ declare function arguments(): void;
655-
╰────
656-
657-
× Cannot assign to 'arguments' in strict mode
658-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts:10:18]
659-
9 │ declare function eval(): void;
660-
10 │ declare function arguments(): void;
661-
· ─────────
662-
11 │
663-
╰────
664-
665-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts
666-
667-
× Cannot assign to 'eval' in strict mode
668-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts:4:25]
669-
3 │ export namespace ns {
670-
4 │ export function eval(): void;
671-
· ────
672-
5 │ export function arguments(): void;
673-
╰────
674-
675-
× Cannot assign to 'arguments' in strict mode
676-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts:5:25]
677-
4 │ export function eval(): void;
678-
5 │ export function arguments(): void;
679-
· ─────────
680-
6 │ }
681-
╰────
682-
683-
× Cannot assign to 'eval' in strict mode
684-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts:9:18]
685-
8 │
686-
9 │ declare function eval(): void;
687-
· ────
688-
10 │ declare function arguments(): void;
689-
╰────
690-
691-
× Cannot assign to 'arguments' in strict mode
692-
╭─[babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts:10:18]
693-
9 │ declare function eval(): void;
694-
10 │ declare function arguments(): void;
695-
· ─────────
696-
11 │
697-
╰────
698-
699563
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/explicit-resource-management/valid-for-using-declaration-binding-of/input.js
700564

701565
× Unexpected token

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 81c95189
22

33
parser_typescript Summary:
44
AST Parsed : 6693/6700 (99.90%)
5-
Positive Passed: 6680/6700 (99.70%)
5+
Positive Passed: 6681/6700 (99.72%)
66
Negative Passed: 1422/5598 (25.40%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ExportAssignment7.ts
88

@@ -8463,40 +8463,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/elidedEmbeddedSt
84638463
24 │ const enum H {}
84648464
╰────
84658465

8466-
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts
8467-
8468-
× Cannot assign to 'eval' in strict mode
8469-
╭─[typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts:3:25]
8470-
2 │ export namespace ns {
8471-
3 │ export function eval(): void;
8472-
· ────
8473-
4 │ export function arguments(): void;
8474-
╰────
8475-
8476-
× Cannot assign to 'arguments' in strict mode
8477-
╭─[typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts:4:25]
8478-
3 │ export function eval(): void;
8479-
4 │ export function arguments(): void;
8480-
· ─────────
8481-
5 │ }
8482-
╰────
8483-
8484-
× Cannot assign to 'eval' in strict mode
8485-
╭─[typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts:8:18]
8486-
7 │
8487-
8 │ declare function eval(): void;
8488-
· ────
8489-
9 │ declare function arguments(): void;
8490-
╰────
8491-
8492-
× Cannot assign to 'arguments' in strict mode
8493-
╭─[typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts:9:18]
8494-
8 │ declare function eval(): void;
8495-
9 │ declare function arguments(): void;
8496-
· ─────────
8497-
10 │
8498-
╰────
8499-
85008466
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/moduleResolutionWithExtensions_unexpected2.ts
85018467

85028468
× Expected a semicolon or an implicit semicolon after a statement, but found none

tasks/coverage/snapshots/semantic_babel.snap

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 1d4546bc
22

33
semantic_babel Summary:
44
AST Parsed : 2362/2362 (100.00%)
5-
Positive Passed: 1951/2362 (82.60%)
5+
Positive Passed: 1953/2362 (82.68%)
66
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/decorators/decorators-after-export/input.js
77
Symbol span mismatch for "C":
88
after transform: SymbolId(0): Span { start: 65, end: 66 }
@@ -563,28 +563,20 @@ after transform: ScopeId(0): ["x", "y"]
563563
rebuilt : ScopeId(0): []
564564
565565
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval/input.ts
566-
Cannot assign to 'eval' in strict mode
567-
Cannot assign to 'arguments' in strict mode
568-
Cannot assign to 'eval' in strict mode
569-
Cannot assign to 'arguments' in strict mode
566+
Bindings mismatch:
567+
after transform: ScopeId(0): ["arguments", "eval"]
568+
rebuilt : ScopeId(0): []
569+
Scope children mismatch:
570+
after transform: ScopeId(0): [ScopeId(1), ScopeId(5), ScopeId(6)]
571+
rebuilt : ScopeId(0): []
570572
571573
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-babel-7/input.ts
572-
Cannot assign to 'eval' in strict mode
573-
Cannot assign to 'arguments' in strict mode
574-
Cannot assign to 'eval' in strict mode
575-
Cannot assign to 'arguments' in strict mode
576-
577-
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts/input.ts
578-
Cannot assign to 'eval' in strict mode
579-
Cannot assign to 'arguments' in strict mode
580-
Cannot assign to 'eval' in strict mode
581-
Cannot assign to 'arguments' in strict mode
582-
583-
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/eval-dts-babel-7/input.ts
584-
Cannot assign to 'eval' in strict mode
585-
Cannot assign to 'arguments' in strict mode
586-
Cannot assign to 'eval' in strict mode
587-
Cannot assign to 'arguments' in strict mode
574+
Bindings mismatch:
575+
after transform: ScopeId(0): ["arguments", "eval"]
576+
rebuilt : ScopeId(0): []
577+
Scope children mismatch:
578+
after transform: ScopeId(0): [ScopeId(1), ScopeId(5), ScopeId(6)]
579+
rebuilt : ScopeId(0): []
588580
589581
semantic Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/function-rest-trailing-comma/input.ts
590582
Bindings mismatch:

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commit: 81c95189
22

33
semantic_typescript Summary:
44
AST Parsed : 6537/6537 (100.00%)
5-
Positive Passed: 2824/6537 (43.20%)
5+
Positive Passed: 2825/6537 (43.22%)
66
semantic Error: tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts
77
Symbol reference IDs mismatch for "Cell":
88
after transform: SymbolId(0): [ReferenceId(1)]
@@ -13006,12 +13006,6 @@ semantic Error: tasks/coverage/typescript/tests/cases/compiler/escapedIdentifier
1300613006
Namespaces exporting non-const are not supported by Babel. Change to const or see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
1300713007
Namespaces exporting non-const are not supported by Babel. Change to const or see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
1300813008

13009-
semantic Error: tasks/coverage/typescript/tests/cases/compiler/evalOrArgumentsInDeclarationFunctions.ts
13010-
Cannot assign to 'eval' in strict mode
13011-
Cannot assign to 'arguments' in strict mode
13012-
Cannot assign to 'eval' in strict mode
13013-
Cannot assign to 'arguments' in strict mode
13014-
1301513009
semantic Error: tasks/coverage/typescript/tests/cases/compiler/eventEmitterPatternWithRecordOfFunction.ts
1301613010
Scope children mismatch:
1301713011
after transform: ScopeId(0): [ScopeId(1), ScopeId(3), ScopeId(6), ScopeId(7)]

0 commit comments

Comments
 (0)