Skip to content
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

Merged
merged 5 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions docs/rules/jsx-indent-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 prop for each line with the tag's first prop.
Note that using the `"first"` option allows very inconsistent indentation unless you also enable a rule that enforces the position of the first prop.

```js
...
"react/jsx-indent-props": [<enabled>, 'tab'|<number>]
"react/jsx-indent-props": [<enabled>, 'tab'|<number>|'first']
...
```

Expand All @@ -51,6 +52,13 @@ The following patterns are considered warnings:
<Hello
firstName="John"
/>

// aligned with first prop
// [2, 'first']
<Hello
firstName="John"
lastName="Doe"
/>
```

The following patterns are **not** warnings:
Expand All @@ -77,6 +85,21 @@ The following patterns are **not** warnings:
<Hello
firstName="John"
/>

// aligned with first prop
// [2, 'first']
<Hello
firstName="John"
lastName="Doe"
/>

<Hello
firstName="John"
lastName="Doe"
/>

<Hello firstName="Jane"
lastName="Doe" />
```

## When not to use
Expand Down
74 changes: 33 additions & 41 deletions lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {

schema: [{
oneOf: [{
enum: ['tab']
enum: ['tab', 'first']
}, {
type: 'integer'
}]
Expand All @@ -64,7 +64,10 @@ module.exports = {
const sourceCode = context.getSourceCode();

if (context.options.length) {
if (context.options[0] === 'tab') {
if (context.options[0] === 'first') {
indentSize = 'first';
indentType = 'space';
} else if (context.options[0] === 'tab') {
indentSize = 1;
indentType = 'tab';
} else if (typeof context.options[0] === 'number') {
Expand All @@ -78,62 +81,41 @@ module.exports = {
* @param {ASTNode} node Node violating the indent rule
* @param {Number} needed Expected indentation character count
* @param {Number} gotten Indentation character count in the actual node/code
* @param {Object=} loc Error line and column location
*/
function report(node, needed, gotten, loc) {
Copy link
Member

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?

Copy link
Contributor Author

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.

function report(node, needed, gotten) {
const msgContext = {
needed: needed,
type: indentType,
characters: needed === 1 ? 'character' : 'characters',
gotten: gotten
};

if (loc) {
context.report({
node: node,
loc: loc,
message: MESSAGE,
data: msgContext
});
} else {
context.report({
node: node,
message: MESSAGE,
data: msgContext,
fix: function(fixer) {
return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]],
Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
}
});
}
context.report({
node: node,
message: MESSAGE,
data: msgContext,
fix: function(fixer) {
return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]],
Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
}
});
}

/**
* Get node indent
* @param {ASTNode} node Node to examine
* @param {Boolean} byLastLine get indent of node's last line
* @param {Boolean} excludeCommas skip comma on start of line
* @return {Number} Indent
*/
function getNodeIndent(node, byLastLine, excludeCommas) {
byLastLine = byLastLine || false;
excludeCommas = excludeCommas || false;

function getNodeIndent(node) {
let src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
const lines = src.split('\n');
if (byLastLine) {
src = lines[lines.length - 1];
} else {
src = lines[0];
}

const skip = excludeCommas ? ',' : '';
src = lines[0];

let regExp;
if (indentType === 'space') {
regExp = new RegExp(`^[ ${skip}]+`);
regExp = /^[ ]+/;
} else {
regExp = new RegExp(`^[\t${skip}]+`);
regExp = /^[\t]+/;
}

const indent = regExp.exec(src);
Expand All @@ -146,9 +128,9 @@ module.exports = {
* @param {Number} indent needed indent
* @param {Boolean} excludeCommas skip comma on start of line
*/
function checkNodesIndent(nodes, indent, excludeCommas) {
function checkNodesIndent(nodes, indent) {
nodes.forEach(node => {
const nodeIndent = getNodeIndent(node, false, excludeCommas);
const nodeIndent = getNodeIndent(node);
if (
node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression' &&
nodeIndent !== indent && astUtil.isNodeFirstInLine(context, node)
Expand All @@ -160,8 +142,18 @@ module.exports = {

return {
JSXOpeningElement: function(node) {
const elementIndent = getNodeIndent(node);
checkNodesIndent(node.attributes, elementIndent + indentSize);
if (!node.attributes.length) {
return;
}
let propIndent;
if (indentSize === 'first') {
const firstPropNode = node.attributes[0];
propIndent = firstPropNode.loc.start.column;
} else {
const elementIndent = getNodeIndent(node);
propIndent = elementIndent + indentSize;
}
checkNodesIndent(node.attributes, propIndent);
}
};
}
Expand Down
138 changes: 138 additions & 0 deletions tests/lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,83 @@ ruleTester.run('jsx-indent-props', rule, {
'/>'
].join('\n'),
options: ['tab']
}, {
code: [
'<App/>'
].join('\n'),
options: ['first']
}, {
code: [
'<App aaa',
' b',
' cc',
'/>'
].join('\n'),
options: ['first']
}, {
code: [
'<App aaa',
' b',
' cc',
'/>'
].join('\n'),
options: ['first']
}, {
code: [
'const test = <App aaa',
' b',
' cc',
' />'
].join('\n'),
options: ['first']
}, {
code: [
'<App aaa x',
' b y',
' cc',
'/>'
].join('\n'),
options: ['first']
}, {
code: [
'const test = <App aaa x',
' b y',
' cc',
' />'
].join('\n'),
options: ['first']
}, {
code: [
'<App aaa',
' b',
'>',
' <Child c',
' d/>',
'</App>'
].join('\n'),
options: ['first']
}, {
code: [
'<Fragment>',
' <App aaa',
' b',
' cc',
' />',
' <OtherApp a',
' bbb',
' c',
' />',
'</Fragment>'
].join('\n'),
options: ['first']
}, {
code: [
'<App',
' a',
' b',
'/>'
].join('\n'),
options: ['first']
}],

invalid: [{
Expand Down Expand Up @@ -112,5 +189,66 @@ ruleTester.run('jsx-indent-props', rule, {
].join('\n'),
options: ['tab'],
errors: [{message: 'Expected indentation of 1 tab character but found 3.'}]
}, {
code: [
'<App a',
' b',
'/>'
].join('\n'),
output: [
'<App a',
' b',
'/>'
].join('\n'),
options: ['first'],
errors: [{message: 'Expected indentation of 5 space characters but found 2.'}]
}, {
code: [
'<App a',
' b',
'/>'
].join('\n'),
output: [
'<App a',
' b',
'/>'
].join('\n'),
options: ['first'],
errors: [{message: 'Expected indentation of 6 space characters but found 3.'}]
}, {
code: [
'<App',
' a',
' b',
'/>'
].join('\n'),
output: [
'<App',
' a',
' b',
'/>'
].join('\n'),
options: ['first'],
errors: [{message: 'Expected indentation of 6 space characters but found 3.'}]
}, {
code: [
'<App',
' a',
' b',
' c',
'/>'
].join('\n'),
output: [
'<App',
' a',
' b',
' c',
'/>'
].join('\n'),
options: ['first'],
errors: [
{message: 'Expected indentation of 2 space characters but found 1.'},
{message: 'Expected indentation of 2 space characters but found 3.'}
]
}]
});