2.0.1 • Published 6 years ago

typescript-magic-variable-plugin v2.0.1

Weekly downloads
3
License
UNLICENSED
Repository
github
Last release
6 years ago

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-plugin

Quick Start

  1. 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"
      ]
    }
  2. write code use the magic variables:
    export class Foo extends React.Component {
      static displayName = __CLASS__;
    }
  3. transform the code: use ttypescript with your configurations, and it will convert the variables to its value.

Full list of the variables

nametypedescription
__NAMESPACE__stringthe full name of the current namespace, use . to join the parents
__CLASS__stringthe class name of the declaring class
__METHOD__stringthe method name of the declaring method
__FUNCTION__stringthe function name of the declaring function
ClassName.classstringthe full name of the class, include the namespace
__DIR__stringthe current directory of the code
__FILE__stringthe current file full name of the code
__LINE__numberthe 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;
}

License

MIT

2.0.1

6 years ago

2.0.0

6 years ago

1.0.0

7 years ago