-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add JSX prop alignment based on the first prop & between-prop space consistency #1729
Add JSX prop alignment based on the first prop & between-prop space consistency #1729
Conversation
lib/rules/jsx-indent-props.js
Outdated
// any space between the name and the first attribute | ||
const afterNameLength = Math.max(1, firstPropNode.loc.start.column - node.name.loc.end.column); | ||
if (afterNameLength !== 1) { | ||
// this is not easily fixable, so we just report another error for it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went for the lazy option here - the default fixing logic in report()
cannot handle the case where the property isn't the first thing in the line and checkNodesIndent
would need further changes as well.
If you think having an automated fixer for this is important I can have another look though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it's very important to have an automated fixer for as many things as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I'll have another look at this
b6ff4ed
to
f69ae31
Compare
lib/rules/jsx-indent-props.js
Outdated
@@ -47,7 +47,7 @@ module.exports = { | |||
|
|||
schema: [{ | |||
oneOf: [{ | |||
enum: ['tab'] | |||
enum: ['tab', 'aligned'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aligned with what? opening tag, closing tag, first used prop name, tag name, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any better suggestion? Naming is hard :p
My first idea was tag-aligned
but jsx-closing-bracket-location
usess that term with a different meaning, and just tag
is too similar to tab
IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it's aligned with the first prop?
What happens with:
<C a b c
d
e
f
/>
Which is properly aligned, d, e, or f?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, d
. So yeah, first-prop
sounds actually a good name.
Just thinking.. should fixing double-space between tag and prop (the stuff from the other comment below) even be part of this rule or a completely new rule? In that case I'd look into creating such a rule and just remove the afterNameLength !== 1
error here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this rule should only cover prop indentation. I'm not sure what your specific question is tho, could you provide a code example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<Foo bar>
-->
<Foo bar>
So this should then be handled by a separate rule (or is there already one which does that?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's not prop indentation - indentation is only "start of line + whitespace + thing"
lib/rules/jsx-indent-props.js
Outdated
// any space between the name and the first attribute | ||
const afterNameLength = Math.max(1, firstPropNode.loc.start.column - node.name.loc.end.column); | ||
if (afterNameLength !== 1) { | ||
// this is not easily fixable, so we just report another error for it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it's very important to have an automated fixer for as many things as possible.
Awesome! indent: [2, 2, {
'FunctionDeclaration': {
'parameters': 'first',
'body': 1,
},
}] we can call it first |
d098513
to
f426e5c
Compare
PR updated. Also removed a bunch of unused code from the rule in a separate commit (probably copy&paste from somewhere else). |
docs/rules/jsx-indent-props.md
Outdated
@@ -29,11 +29,12 @@ firstName="John" | |||
|
|||
## Rule Options | |||
|
|||
It takes an option as the second parameter which can be `"tab"` for tab-based indentation or a positive number for space indentations. | |||
It takes an option as the second parameter which can be `"tab"` for tab-based indentation, a positive number for space indentations or `"first"` for aligning the first attribute for each line with the tag's first attribute. | |||
Note that using the `"first"` option allows very inconsistent indentation unless you also enable a rule that enforces the position of the first prop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this rule does not exist yet; I'll add it to the PR later
PR updated with the rule to deal with excessive spacing between tag/prop or props in the same line |
*/ | ||
function report(node, needed, gotten, loc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the loc
no longer required because eslint gets it off the node?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was never provided when calling the function - probably just a copy&paste leftover from somewhere else.
@@ -0,0 +1,29 @@ | |||
# Disallow multiple spaces between inline JSX props |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure what this rule is, but please remove it from this PR :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the one I mentioned here: #1729 (comment)
I can move it to a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, ok. yes, thanks, a separate PR for it would be great.
This is the default style used by the JetBrains IDEs. closes jsx-eslint#398
Just to make sure this still works properly.
71d52d8
to
c854e1d
Compare
PR updated |
Switch back to a release once these PRs are merged and in a release: - jsx-eslint/eslint-plugin-react#1729 - jsx-eslint/eslint-plugin-react#1755 - jsx-eslint/eslint-plugin-react#1735
Switch back to a release once these PRs are merged and in a release: - jsx-eslint/eslint-plugin-react#1729 - jsx-eslint/eslint-plugin-react#1755 - jsx-eslint/eslint-plugin-react#1735
Switch back to a release once these PRs are merged and in a release: - jsx-eslint/eslint-plugin-react#1729 - jsx-eslint/eslint-plugin-react#1755 - jsx-eslint/eslint-plugin-react#1735
Any chance to get it merged so it makes it into the next version? |
Released in @ThiefMaster Is there a workaround for this? Probably i'm missing something in config 'react/jsx-indent-props': [2, 'first'],
'indent': [2, 2, {
'SwitchCase': 1,
'VariableDeclarator': {'var': 2, 'let': 2, 'const': 3},
'outerIIFEBody': 1,
'MemberExpression': 1,
'FunctionDeclaration': {
'parameters': 'first',
'body': 1,
},
'FunctionExpression': {
'parameters': 'first',
'body': 1,
},
'CallExpression': {
'arguments': 1,
},
'ArrayExpression': 1,
'ObjectExpression': 1,
'ImportDeclaration': 1,
'flatTernaryExpressions': true,
'ignoredNodes': [],
'ignoreComments': false,
}], |
You have to exclude the JSX nodes from the indent rule:
|
@ThiefMaster Thank you for the quick response, that works! |
This adds support for the JSX prop style suggested in #398 which is the default of the various JetBrains IDEs.
I haven't updated the docs yet since writing docs isn't fun so I'd rather know first that the PR has a chance of getting accepted before doing that.
Fixes #398.