You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix#14860.
### The problem
`TSModuleDeclaration` type was used to represent 4 different syntaxes:
* `namespace X {}`
* `module X {}`
* `module "x" {}`
* `global {}`
(and all the above prefixed with `declare`)
`global {}` is quite different from the others in a few ways:
* It has no identifier (`global` is a keyword, not an identifier).
* It has no symbol.
* The `body` block is not optional.
* Directives are not allowed in the `body` block.
* It is always ambient context (regardless of whether it has `declare` modifier).
In particular, it was causing panics because `symbol_id` field for `TSModuleDeclaration`s representing `global {}` was `None`.
### Solution
Rather than trying to make `TSModuleDeclaration` type encode all these differences, and make Oxc's code reason about them, split `global {}` into a new type `TSGlobalDeclaration`.
The rest of the diff is just adjusting code to also handle `TSGlobalDeclaration` where it was dealing with `TSModuleDeclaration`s.
This change simplifies the parser quite a bit.
`oxc_semantic`'s `SymbolDeclarationTest` tester now tests that all `TSModuleDeclaration`s with `id: BindingIdentifier` do have a symbol:
https://github.com/oxc-project/oxc/pull/15712/files#diff-ce544d71b839faa898887ac920237e563e59a708e7fca86b53023498ab67a611L101-R110
### Formatter bug
There is one regression in the formatter for `typescript/module/global.ts` case.
https://github.com/oxc-project/oxc/pull/15712/files#diff-f248b1d49b8c482864810055632d7322cdebc895dd4dfe05fbefcf5e23ba12c9R51
Input:
```ts
declare /* module */ global {}
declare /* namespace */ global {}
```
Output:
```diff
- declare /* module */ global {}
- declare /* namespace */ global {}
+ declare global /* module */ {}
+ declare global /* namespace */ {}
```
I assume this is happening because there's no longer a `BindingIdentifier` for the `global` keyword for the comment to get attached to.
I am not sure how to fix this. Maybe the `global_span` field can help?
@Dunqing This PR touches a *lot* of files. Can I propose that we merge it before it gets conflicts, and if you can give me some guidance, I'll do a follow-up PR to fix it?
0 commit comments