Skip to content

Commit b201de1

Browse files
committed
feat(parser): use BindingIdentifier for namespace declaration names
Use `BindingIdentifier` instead of `IdentifierName` so that AST visitors can access the bound symbol id for the namespace's name.
1 parent 09a24cd commit b201de1

File tree

10 files changed

+77
-40
lines changed

10 files changed

+77
-40
lines changed

crates/oxc_ast/src/ast/ts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ pub enum TSModuleDeclarationKind {
13881388
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
13891389
#[serde(untagged)]
13901390
pub enum TSModuleDeclarationName<'a> {
1391-
Identifier(IdentifierName<'a>) = 0,
1391+
Identifier(BindingIdentifier<'a>) = 0,
13921392
StringLiteral(StringLiteral<'a>) = 1,
13931393
}
13941394

crates/oxc_ast/src/generated/assert_layouts.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,19 +1091,19 @@ const _: () = {
10911091
assert!(size_of::<TSTypePredicateName>() == 16usize);
10921092
assert!(align_of::<TSTypePredicateName>() == 8usize);
10931093

1094-
assert!(size_of::<TSModuleDeclaration>() == 64usize);
1094+
assert!(size_of::<TSModuleDeclaration>() == 72usize);
10951095
assert!(align_of::<TSModuleDeclaration>() == 8usize);
10961096
assert!(offset_of!(TSModuleDeclaration, span) == 0usize);
10971097
assert!(offset_of!(TSModuleDeclaration, id) == 8usize);
1098-
assert!(offset_of!(TSModuleDeclaration, body) == 40usize);
1099-
assert!(offset_of!(TSModuleDeclaration, kind) == 56usize);
1100-
assert!(offset_of!(TSModuleDeclaration, declare) == 57usize);
1101-
assert!(offset_of!(TSModuleDeclaration, scope_id) == 60usize);
1098+
assert!(offset_of!(TSModuleDeclaration, body) == 48usize);
1099+
assert!(offset_of!(TSModuleDeclaration, kind) == 64usize);
1100+
assert!(offset_of!(TSModuleDeclaration, declare) == 65usize);
1101+
assert!(offset_of!(TSModuleDeclaration, scope_id) == 68usize);
11021102

11031103
assert!(size_of::<TSModuleDeclarationKind>() == 1usize);
11041104
assert!(align_of::<TSModuleDeclarationKind>() == 1usize);
11051105

1106-
assert!(size_of::<TSModuleDeclarationName>() == 32usize);
1106+
assert!(size_of::<TSModuleDeclarationName>() == 40usize);
11071107
assert!(align_of::<TSModuleDeclarationName>() == 8usize);
11081108

11091109
assert!(size_of::<TSModuleDeclarationBody>() == 16usize);
@@ -2646,19 +2646,19 @@ const _: () = {
26462646
assert!(size_of::<TSTypePredicateName>() == 12usize);
26472647
assert!(align_of::<TSTypePredicateName>() == 4usize);
26482648

2649-
assert!(size_of::<TSModuleDeclaration>() == 44usize);
2649+
assert!(size_of::<TSModuleDeclaration>() == 48usize);
26502650
assert!(align_of::<TSModuleDeclaration>() == 4usize);
26512651
assert!(offset_of!(TSModuleDeclaration, span) == 0usize);
26522652
assert!(offset_of!(TSModuleDeclaration, id) == 8usize);
2653-
assert!(offset_of!(TSModuleDeclaration, body) == 28usize);
2654-
assert!(offset_of!(TSModuleDeclaration, kind) == 36usize);
2655-
assert!(offset_of!(TSModuleDeclaration, declare) == 37usize);
2656-
assert!(offset_of!(TSModuleDeclaration, scope_id) == 40usize);
2653+
assert!(offset_of!(TSModuleDeclaration, body) == 32usize);
2654+
assert!(offset_of!(TSModuleDeclaration, kind) == 40usize);
2655+
assert!(offset_of!(TSModuleDeclaration, declare) == 41usize);
2656+
assert!(offset_of!(TSModuleDeclaration, scope_id) == 44usize);
26572657

26582658
assert!(size_of::<TSModuleDeclarationKind>() == 1usize);
26592659
assert!(align_of::<TSModuleDeclarationKind>() == 1usize);
26602660

2661-
assert!(size_of::<TSModuleDeclarationName>() == 20usize);
2661+
assert!(size_of::<TSModuleDeclarationName>() == 24usize);
26622662
assert!(align_of::<TSModuleDeclarationName>() == 4usize);
26632663

26642664
assert!(size_of::<TSModuleDeclarationBody>() == 8usize);

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11423,27 +11423,27 @@ impl<'a> AstBuilder<'a> {
1142311423
///
1142411424
/// ## Parameters
1142511425
/// - span: The [`Span`] covering this node
11426-
/// - name
11426+
/// - name: The identifier name being bound.
1142711427
#[inline]
11428-
pub fn ts_module_declaration_name_identifier_name<A>(
11428+
pub fn ts_module_declaration_name_binding_identifier<A>(
1142911429
self,
1143011430
span: Span,
1143111431
name: A,
1143211432
) -> TSModuleDeclarationName<'a>
1143311433
where
1143411434
A: IntoIn<'a, Atom<'a>>,
1143511435
{
11436-
TSModuleDeclarationName::Identifier(self.identifier_name(span, name))
11436+
TSModuleDeclarationName::Identifier(self.binding_identifier(span, name))
1143711437
}
1143811438

11439-
/// Convert a [`IdentifierName`] into a [`TSModuleDeclarationName::Identifier`]
11439+
/// Convert a [`BindingIdentifier`] into a [`TSModuleDeclarationName::Identifier`]
1144011440
#[inline]
11441-
pub fn ts_module_declaration_name_from_identifier_name<T>(
11441+
pub fn ts_module_declaration_name_from_binding_identifier<T>(
1144211442
self,
1144311443
inner: T,
1144411444
) -> TSModuleDeclarationName<'a>
1144511445
where
11446-
T: IntoIn<'a, IdentifierName<'a>>,
11446+
T: IntoIn<'a, BindingIdentifier<'a>>,
1144711447
{
1144811448
TSModuleDeclarationName::Identifier(inner.into_in(self.allocator))
1144911449
}

crates/oxc_ast/src/generated/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3888,7 +3888,7 @@ pub mod walk {
38883888
it: &TSModuleDeclarationName<'a>,
38893889
) {
38903890
match it {
3891-
TSModuleDeclarationName::Identifier(it) => visitor.visit_identifier_name(it),
3891+
TSModuleDeclarationName::Identifier(it) => visitor.visit_binding_identifier(it),
38923892
TSModuleDeclarationName::StringLiteral(it) => visitor.visit_string_literal(it),
38933893
}
38943894
}

crates/oxc_ast/src/generated/visit_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,7 @@ pub mod walk_mut {
41094109
it: &mut TSModuleDeclarationName<'a>,
41104110
) {
41114111
match it {
4112-
TSModuleDeclarationName::Identifier(it) => visitor.visit_identifier_name(it),
4112+
TSModuleDeclarationName::Identifier(it) => visitor.visit_binding_identifier(it),
41134113
TSModuleDeclarationName::StringLiteral(it) => visitor.visit_string_literal(it),
41144114
}
41154115
}

crates/oxc_parser/src/ts/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'a> ParserImpl<'a> {
300300
);
301301
let id = match self.cur_kind() {
302302
Kind::Str => self.parse_literal_string().map(TSModuleDeclarationName::StringLiteral),
303-
_ => self.parse_identifier_name().map(TSModuleDeclarationName::Identifier),
303+
_ => self.parse_binding_identifier().map(TSModuleDeclarationName::Identifier),
304304
}?;
305305

306306
let body = if self.eat(Kind::Dot) {

crates/oxc_semantic/src/binder.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,19 @@ impl<'a> Binder<'a> for TSModuleDeclaration<'a> {
414414
// At declaration time a module has no value declaration it is only when a value declaration
415415
// is made inside a the scope of a module that the symbol is modified
416416
let ambient = if self.declare { SymbolFlags::Ambient } else { SymbolFlags::None };
417-
builder.declare_symbol(
417+
let symbol_id = builder.declare_symbol(
418418
self.id.span(),
419419
self.id.name().as_str(),
420420
SymbolFlags::NameSpaceModule | ambient,
421421
SymbolFlags::None,
422422
);
423+
424+
// do not bind `global` for `declare global { ... }`
425+
if !self.kind.is_global() {
426+
if let TSModuleDeclarationName::Identifier(id) = &self.id {
427+
id.symbol_id.set(Some(symbol_id));
428+
}
429+
}
423430
}
424431
}
425432

crates/oxc_transformer/src/typescript/namespace.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ impl<'a> TypeScriptNamespace<'a> {
151151

152152
let mut names: FxHashSet<Atom<'a>> = FxHashSet::default();
153153

154-
let TSModuleDeclarationName::Identifier(IdentifierName { name: real_name, .. }) = decl.id
154+
let TSModuleDeclarationName::Identifier(BindingIdentifier { name: real_name, .. }) =
155+
decl.id
155156
else {
156157
return None;
157158
};

crates/oxc_traverse/src/generated/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4987,7 +4987,7 @@ pub(crate) unsafe fn walk_ts_module_declaration_name<'a, Tr: Traverse<'a>>(
49874987
traverser.enter_ts_module_declaration_name(&mut *node, ctx);
49884988
match &mut *node {
49894989
TSModuleDeclarationName::Identifier(node) => {
4990-
walk_identifier_name(traverser, node as *mut _, ctx)
4990+
walk_binding_identifier(traverser, node as *mut _, ctx)
49914991
}
49924992
TSModuleDeclarationName::StringLiteral(node) => {
49934993
walk_string_literal(traverser, node as *mut _, ctx)

tasks/coverage/snapshots/parser_typescript.snap

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ commit: a709f989
33
parser_typescript Summary:
44
AST Parsed : 6469/6479 (99.85%)
55
Positive Passed: 6458/6479 (99.68%)
6-
Negative Passed: 1226/5715 (21.45%)
6+
Negative Passed: 1227/5715 (21.47%)
77
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration10.ts
88
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration11.ts
99
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration13.ts
@@ -1910,7 +1910,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/statics.ts
19101910
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/staticsNotInScopeInClodule.ts
19111911
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictFunctionTypesErrors.ts
19121912
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictModeInConstructor.ts
1913-
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictModeReservedWordInModuleDeclaration.ts
19141913
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictNullEmptyDestructuring.ts
19151914
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictNullNotNullIndexTypeNoLib.ts
19161915
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/strictOptionalProperties3.ts
@@ -11845,6 +11844,37 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
1184511844
· ──────
1184611845
╰────
1184711846

11847+
× The keyword 'public' is reserved
11848+
╭─[typescript/tests/cases/compiler/strictModeReservedWordInModuleDeclaration.ts:2:8]
11849+
1 │ "use strict"
11850+
2 │ module public { }
11851+
· ──────
11852+
3 │ module private { }
11853+
╰────
11854+
11855+
× The keyword 'private' is reserved
11856+
╭─[typescript/tests/cases/compiler/strictModeReservedWordInModuleDeclaration.ts:3:8]
11857+
2 │ module public { }
11858+
3 │ module private { }
11859+
· ───────
11860+
4 │ module public.whatever {
11861+
╰────
11862+
11863+
× The keyword 'public' is reserved
11864+
╭─[typescript/tests/cases/compiler/strictModeReservedWordInModuleDeclaration.ts:4:8]
11865+
3 │ module private { }
11866+
4 │ module public.whatever {
11867+
· ──────
11868+
5 │ }
11869+
╰────
11870+
11871+
× The keyword 'private' is reserved
11872+
╭─[typescript/tests/cases/compiler/strictModeReservedWordInModuleDeclaration.ts:6:8]
11873+
5 │ }
11874+
6 │ module private.public.foo { }
11875+
· ───────
11876+
╰────
11877+
1184811878
× The keyword 'package' is reserved
1184911879
╭─[typescript/tests/cases/compiler/strictModeWordInImportDeclaration.ts:2:13]
1185011880
1 │ "use strict"
@@ -12865,21 +12895,20 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
1286512895
3 │
1286612896
╰────
1286712897

12868-
× Expected a semicolon or an implicit semicolon after a statement, but found none
12869-
╭─[typescript/tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts:11:8]
12870-
10
12871-
11 │ declare module debugger {} // still an error
12872-
·
12873-
╰────
12874-
help: Try insert a semicolon here
12898+
× Unexpected token
12899+
╭─[typescript/tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts:3:23]
12900+
2
12901+
3 │ declare module chrome.debugger {
12902+
· ────────
12903+
4 │ declare var tabId: number;
12904+
╰────
1287512905

12876-
× Expected a semicolon or an implicit semicolon after a statement, but found none
12877-
╭─[typescript/tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts:9:8]
12878-
8
12879-
9 │ declare namespace debugger {} // still an error
12880-
·
12906+
× Unexpected token
12907+
╭─[typescript/tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts:1:26]
12908+
1declare namespace chrome.debugger {
12909+
· ────────
12910+
2 │ declare var tabId: number;
1288112911
╰────
12882-
help: Try insert a semicolon here
1288312912

1288412913
× Cannot use `await` as an identifier in an async context
1288512914
╭─[typescript/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts:1:18]

0 commit comments

Comments
 (0)