-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip type-only TS namespaces (#16071)
- Loading branch information
Showing
5 changed files
with
49 additions
and
23 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/babel-plugin-transform-typescript/src/global-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { NodePath, Scope } from "@babel/traverse"; | ||
|
||
export const GLOBAL_TYPES = new WeakMap<Scope, Set<string>>(); | ||
|
||
export function isGlobalType({ scope }: NodePath, name: string) { | ||
if (scope.hasBinding(name)) return false; | ||
if (GLOBAL_TYPES.get(scope).has(name)) return true; | ||
|
||
console.warn( | ||
`The exported identifier "${name}" is not declared in Babel's scope tracker\n` + | ||
`as a JavaScript value binding, and "@babel/plugin-transform-typescript"\n` + | ||
`never encountered it as a TypeScript type declaration.\n` + | ||
`It will be treated as a JavaScript value.\n\n` + | ||
`This problem is likely caused by another plugin injecting\n` + | ||
`"${name}" without registering it in the scope tracker. If you are the author\n` + | ||
` of that plugin, please use "scope.registerDeclaration(declarationPath)".`, | ||
); | ||
|
||
return false; | ||
} | ||
|
||
export function registerGlobalType(programScope: Scope, name: string) { | ||
GLOBAL_TYPES.get(programScope).add(name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/babel-plugin-transform-typescript/test/fixtures/namespace/export-type-only/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// #15940 | ||
namespace Platform { | ||
export type Name = "web"; | ||
|
||
export namespace Data { | ||
export type Type = string; | ||
export declare namespace Empty {} | ||
} | ||
} | ||
|
||
export { Platform }; |
1 change: 1 addition & 0 deletions
1
...ges/babel-plugin-transform-typescript/test/fixtures/namespace/export-type-only/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |