coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement 💪 with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
712 stars 40 forks source link

question: how can I use putout to transform unicode regexes (/../u)? #179

Closed retorquere closed 1 year ago

retorquere commented 1 year ago

I have to maintain code that is targeted both at environments that support unicode regexes as some that don't; it'd be great if I could use putout to rewrite the code towards plain regexp (not incredibly hard), but I have to find and parse the regexes themselves. If I can find the regexes, I could use https://www.npmjs.com/package/regexp-to-ast to parse them and return the rewritten ones.

coderaiser commented 1 year ago

Could you please provide code example: before and after?

coderaiser commented 1 year ago

Traverser

One of the ways to transform regular expressions is using Traverser:

export const report = () => `Remove regular expression`;

export const fix = (path) => {
    path.remove();
};

export const traverse = () => ({
    RegExpLiteral(path) {
        push(path);
    }
});

Check out in 🐊Putout Editor

Replacer

You can also use Replacer:

export const report = () => `Drop regexp`;

export const replace = () => ({
    '/__a/': ({__a}, path) => {
        return '';
    }
});

Check out in 🐊Putout Editor

You can find more examples in sources of @putout/plugin-regexp.

retorquere commented 1 year ago

I'm going to give this a try -- super thanks! This will make maintenance much easier.

coderaiser commented 1 year ago

I'll close it if all questions resolved, feel free to reopen if you have any questions :)