Awesome
undecorate-codemod
A small codemod to "undecorate" Class Declarations that have decorators, for use with JSCodeshift.
Why?
For more on why this was created, see my blog post: Undecorate Your Decorators
Setup & Run
npm install -g jscodeshift
git clone https://github.com/tizmagik/undecorate-codemod.git
- Run
npm install
in the undecorate-codemod directory (or,yarn
) jscodeshift -t undecorate-codemod/transforms/undecorate.js <path>
- Use the
-d
option for a dry-run and use-p
to print the output for comparison
What's Supported
This codemod undecorates Class Declarations, for any number of decorators and works whether or not the class is a named export, default export or not exported at all. For example:
Example input:
@withParam('myParam')
@noParam
@taggedTemplate`my tagged template string`
class MyClass { }
Converts to:
const MyClass = withParam('myParam')(noParam(taggedTemplate`my tagged template string`(class MyClass { })));
Things to be aware of
- Only supports Class Declaration decorators. It does not (yet?) support Class Property decorators.
- It does not "desugar" exactly according to the spec, instead it "undecorates". The difference being it tries to convert the code to what you probably would have written by hand if decorators weren't available (and you weren't using a currying library).
Credit & Thanks
- react-codemod - For examples and where this repo structure is based off of.
- ASTExplorer - For an excellent playground.
- Yehuda Katz and Brian Terlson - For the decorator proposal, of course.