-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move babel-plugin-transform-typescript-metadata in plugins
- Loading branch information
1 parent
47ab269
commit b03cf3f
Showing
9 changed files
with
565 additions
and
41 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
47 changes: 47 additions & 0 deletions
47
src/plugins/babel-plugin-transform-typescript-metadata/index.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,47 @@ | ||
import { PluginObj } from '@babel/core'; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { parameterVisitor } from './parameter/parameter-visitor'; | ||
import { metadataVisitor } from './metadata/metadata-visitor'; | ||
|
||
export default declare( | ||
(api: any): PluginObj => { | ||
api.assertVersion(7); | ||
|
||
return { | ||
visitor: { | ||
Program(programPath) { | ||
/** | ||
* We need to traverse the program right here since | ||
* `@babel/preset-typescript` removes imports at this level. | ||
* | ||
* Since we need to convert some typings into **bindings**, used in | ||
* `Reflect.metadata` calls, we need to process them **before** | ||
* the typescript preset. | ||
*/ | ||
programPath.traverse({ | ||
ClassDeclaration(path) { | ||
for (const field of path.get('body').get('body')) { | ||
if ( | ||
field.type !== 'ClassMethod' && | ||
field.type !== 'ClassProperty' | ||
) { | ||
continue; | ||
} | ||
|
||
parameterVisitor(path, field as any); | ||
metadataVisitor(path, field as any); | ||
} | ||
|
||
/** | ||
* We need to keep binding in order to let babel know where imports | ||
* are used as a Value (and not just as a type), so that | ||
* `babel-transform-typescript` do not strip the import. | ||
*/ | ||
(path.parentPath.scope as any).crawl(); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
); |
Oops, something went wrong.