-
Notifications
You must be signed in to change notification settings - Fork 341
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
Printing parenthesized JSX nodes #211
Comments
Probably related: #147 |
same here works for assign function App(props) {
//const div = ( // good
return ( // bad
<div className="app">
hello {props.name}
</div>
);
//return div;
} but only when i transform the AST good result: assign function App(props) {
const div = (
<div class="app">
hello {props.name}
</div>
);
return div;
} bad result: return function App(props) {
return (
(<div class="app">hello{props.name}
</div>)
);
} note: also whitespace in demo const putout = require("putout")
// good: assign
var source = `
function App(props) {
const div = (
<div className="app">
hello {props.name}
</div>
);
return div;
}
`;
// bad: return
var source = `
function App(props) {
return (
<div className="app">
hello {props.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) bug seems to be in Lines 1305 to 1322 in f41dd8b
from return (
<div class="app">
hello {props.name}
</div>
); the whitespace before
Line 1326 in f41dd8b
in the "good" case, this code is not reached i guess the slow path is caused by adding parens at when parens are missing
then it should create a parens-block, indented by 2 spaces
|
nevermind. im moving on to eslint as its more popular |
Is it currently possible to print JSX nodes like so:
They seem to always print like:
http://felix-kling.de/esprima_ast_explorer/#/984sneJJn2
Thanks!
The text was updated successfully, but these errors were encountered: