2.0.1 • Published 7 years ago
typescript-magic-variable-plugin v2.0.1
typescript-magic-variable-plugin
A typescript transform plugin to transform magic variables in your typescript
project. Just like __CLASS__, __METHOD__, and so on, in PHP.
Install
yarn add typescript-magic-variable-plugin
# OR var npm
npm i -S typescript-magic-variable-pluginQuick Start
- configure your
tsconfig.json, add the following options:{ "compilerOptions": { "plugins": [ { "transform": "typescript-magic-variable-plugin" } ] }, "include": [ "./node_modules/typescript-magic-variable-plugin/types/globals.d.ts" ] } - write code use the magic variables:
export class Foo extends React.Component { static displayName = __CLASS__; } - transform the code: use
ttypescriptwith your configurations, and it will convert the variables to its value.
Full list of the variables
| name | type | description |
|---|---|---|
__NAMESPACE__ | string | the full name of the current namespace, use . to join the parents |
__CLASS__ | string | the class name of the declaring class |
__METHOD__ | string | the method name of the declaring method |
__FUNCTION__ | string | the function name of the declaring function |
ClassName.class | string | the full name of the class, include the namespace |
__DIR__ | string | the current directory of the code |
__FILE__ | string | the current file full name of the code |
__LINE__ | number | the current line number |
Configuration
export interface MagicVariableOptions {
// add display name for react Component/PureComponent class automatically
// if it is not set.
addDisplayName: boolean;
// the root directory for `__DIR__`, `__FILE__`, default is
// `compilerOptions.rootDir`.
rootDir: string;
}