Skip to content

Commit

Permalink
fix(transformer/typescript): create Reference for require (#8355)
Browse files Browse the repository at this point in the history
Create a `Reference` when generating new `IdentifierReference` for `require`.
  • Loading branch information
overlookmotel committed Jan 9, 2025
1 parent 6c7acac commit 0df1866
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 657 deletions.
9 changes: 8 additions & 1 deletion crates/oxc_transformer/src/typescript/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ impl<'a> TypeScriptModule<'a, '_> {
self.ctx.error(diagnostics::import_equals_cannot_be_used_in_esm(decl_span));
}

let callee = ctx.ast.expression_identifier_reference(SPAN, "require");
let require_symbol_id =
ctx.scopes().find_binding(ctx.current_scope_id(), "require");
let callee = ctx.create_ident_expr(
SPAN,
Atom::from("require"),
require_symbol_id,
ReferenceFlags::Read,
);
let arguments =
ctx.ast.vec1(Argument::StringLiteral(ctx.alloc(reference.expression.clone())));
(
Expand Down
28 changes: 0 additions & 28 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,9 @@ rebuilt : []

tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/import-require/input.js
semantic error: Missing SymbolId: "x"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js
semantic error: Scope children mismatch:
Expand Down Expand Up @@ -740,23 +736,15 @@ rebuilt : ScopeId(0): [SymbolId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require/input.ts
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
semantic error: Missing SymbolId: "A"
Expand All @@ -766,23 +754,15 @@ rebuilt : ScopeId(0): [SymbolId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-require/input.ts
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-as-identifier/input.ts
semantic error: Missing SymbolId: "type"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-require/input.ts
semantic error: Bindings mismatch:
Expand All @@ -791,16 +771,12 @@ rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-named-import-require/input.ts
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Reference symbol mismatch for "a":
after transform: SymbolId(0) "a"
rebuilt : SymbolId(0) "a"
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts
semantic error: Bindings mismatch:
Expand All @@ -824,13 +800,9 @@ rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-type-as-identifier/input.ts
semantic error: Missing SymbolId: "type"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/internal-comments/input.ts
semantic error: Bindings mismatch:
Expand Down
Loading

0 comments on commit 0df1866

Please sign in to comment.