babel-plugin-transform-class-inject-directive
v4.0.0
Published
Detect an inject directive on a class constructor and define a static property with the list of dependencies.
Downloads
11
Readme
babel-plugin-transform-class-inject-directive
This is a utility for projects that relay heavily on classes and dependency injection. If a class constructor uses an 'inject'
directive, the list of parameter names will be copied as strings and placed on an inject
static property, so your DI tool can read them.
Example
In
class MyService {
constructor(depOne, depTwo) {
'inject';
...
}
}
Out
class MyService {
constructor(depOne, depTwo) {
'inject';
...
}
}
MyService.inject = ['depOne', 'depTwo']
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["transform-class-inject-directive"]
}
Via CLI
babel --plugins transform-class-inject-directive script.js
Via Node API
require('@babel/core').transform('code', {
plugins: ['transform-class-inject-directive'],
});
Options
directive
string
, defaults to inject
.
Tells the plugin which is the name of the directive it should find in order to apply the transformation.
property
string
, defaults to inject
.
This is the name of the property the plugin will create in order to define the list of dependencies.
Warning
This only works on classes and regular functions (no arrow). The idea was for it to only work on classes, and the only reason it also works with functions is in order to add support in case you are transforming classes.
Development
NPM/Yarn Tasks
| Task | Description |
|----------------|-------------------------------------|
| test
| Run the project unit tests. |
| run lint
| Lint the modified files. |
| run lint:all
| Lint the project code. |
| run docs
| Generate the project documentation. |
| run todo
| List all the pending to-do's. |
Repository hooks
I use husky
to automatically install the repository hooks so the code will be tested and linted before any commit, and the dependencies updated after every merge.
Commits convention
I use conventional commits with commitlint
in order to support semantic releases. The one that sets it up is actually husky, that installs a script that runs commitlint
on the git commit
command.
The configuration is on the commitlint
property of the package.json
.
Releases
I use semantic-release
and a GitHub action to automatically release on NPM everything that gets merged to main.
The configuration for semantic-release
is on ./releaserc
and the workflow for the release is on ./.github/workflow/release.yml
.
Testing
I use Jest to test the project.
The configuration file is on ./.jestrc.js
, the tests are on ./tests
and the script that runs it is on ./utils/scripts/test
.
Linting && Formatting
I use ESlint with my own custom configuration to validate all the JS code. The configuration file for the project code is on ./.eslintrc
and the one for the tests is on ./tests/.eslintrc
. There's also an ./.eslintignore
to exclude some files on the process. The script that runs it is on ./utils/scripts/lint-all
.
For formatting I use Prettier with my custom configuration. The configuration file for the project code is on ./.prettierrc
.
Documentation
I use JSDoc to generate an HTML documentation site for the project.
The configuration file is on ./.jsdoc.js
and the script that runs it is on ./utils/scripts/docs
.
To-Dos
I use @todo
comments to write all the pending improvements and fixes, and Leasot to generate a report. The script that runs it is on ./utils/scripts/todo
.