-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
putout(source, options) - show more options in readme #116
Comments
also ... how can i replace jsx? input const div = <div className="asdf"></div> output const div = <div class="asdf"></div> i tried module.exports.replace = () => ({
'<__a className="__b">': '<__a class="__b">',
}) but babel throws
edit: module.exports.rules = ({
'jsx-classname-to-class': {
report: () => `Use class instead of className attribute`,
include: () => [
'JSXOpeningElement',
],
filter: (path) => {
return !!path.node.attributes.find(attr => attr.name.name == "className")
},
fix: (path) => {
const attr = path.node.attributes.find(attr => attr.name.name == "className")
attr.name.name = "class"
// TODO handle collisions: class attribute can exist already
},
},
}) |
How exactly I can help you :)? About collision, filter should be improved a bit, to filter out nodes that was processed already. There is no support of JSX for Replacer, Includer does the job.
-const a: string = 'hello';
+const a = 'hello'; Type is striped, because it’s always string, in such case and it works the same, but easier to read. Anyways you can disable any plugin or any count of rules from the plugin, if you don’t need them. |
the readme section for currently i see putout(source: any, opts: any): {
code: any;
places: any;
} |
Are you up for a PR to improve docs? |
sure : ) two seconds ... |
demo plugin: transform tsx to tsx const putout = require("putout")
const source = `
function App(props: { name?: string }) {
const name = props?.name || "world"
return (
<div className="app">
hello {name}
</div>
)
}
`;
console.log("input:\n" + source)
/* based on
https://github.com/coderaiser/putout/blob/master/packages/plugin-react-router/lib/convert-switch-to-routes/index.js
https://github.com/coderaiser/putout/blob/master/packages/plugin-react-hooks/lib/apply-short-fragment/index.js
*/
const myPlugin = {
rules: {
'jsx-classname-to-class': {
report: () => `Use class instead of className attribute`,
include: () => [
'JSXOpeningElement',
],
fix: (path) => {
const attr = path.node.attributes.find(attr => attr.name.name == "className")
attr.name.name = "class"
},
filter: (path) => {
const hasClassName = !!path.node.attributes.find(attr => attr.name.name == "className")
const hasClass = !!path.node.attributes.find(attr => attr.name.name == "class")
return hasClassName && !hasClass
},
},
}
}
const res = putout(source, {
isTS: true,
isJSX: true,
//sourceFileName: 'input.tsx',
processors: [
//'typescript', // @putout/processor-typescript type checking for TypeScript code
],
plugins: [
//'typescript', // @putout/plugin-typescript transform TypeScript code
['my-plugin', myPlugin],
],
rules: {
// default: all rules are on
//"my-plugin/jsx-classname-to-class": "on",
}
});
console.log("output:\n" + res.code) result
im not happy with the missing whitespace in |
It looks like a bug in recast. |
edit: benjamn/recast#211 |
nevermind. im moving on to eslint as its more popular this issue can be fixed by #117 feel free to close |
The best way is using both: ESLint and 🐊Putout. They have different rules set that do not overlap. Also 🐊Putout has ability to show progress bar and run ESLint for each transformed file. |
Just landed base support of JSX to Replacer |
Just landed ability to use |
example: transform tsx to tsx
not perfect, because it should return typescripthad to disable the
typescript
plugin, which was removing the: string
example result
The text was updated successfully, but these errors were encountered: