Skip to content

Commit 5bf26ae

Browse files
author
shioju
committed
Add auto fix for jsx-indent-props
1 parent a3eccd8 commit 5bf26ae

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ The plugin has a [recommended configuration](#user-content-recommended-configura
102102
* [jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
103103
* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes
104104
* [jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
105-
* [jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX
105+
* [jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
106106
* [jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation
107107
* [jsx-key](docs/rules/jsx-key.md): Validate JSX has key prop when in array or iterator
108108
* [jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md): Limit maximum of props on a single line in JSX

docs/rules/jsx-indent-props.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This option validates a specific indentation style for props.
44

5+
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
6+
57
## Rule Details
68

79
This rule is aimed to enforce consistent indentation style. The default style is `4 spaces`.

lib/rules/jsx-indent-props.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ module.exports = function(context) {
7878
context.report({
7979
node: node,
8080
message: MESSAGE,
81-
data: msgContext
81+
data: msgContext,
82+
fix: function(fixer) {
83+
return fixer.replaceTextRange([node.start - node.loc.start.column, node.start],
84+
Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
85+
}
8286
});
8387
}
8488
}

tests/lib/rules/jsx-indent-props.js

+29
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ ruleTester.run('jsx-indent-props', rule, {
7070
' foo',
7171
'/>'
7272
].join('\n'),
73+
output: [
74+
'<App',
75+
' foo',
76+
'/>'
77+
].join('\n'),
7378
parserOptions: parserOptions,
7479
errors: [{message: 'Expected indentation of 4 space characters but found 2.'}]
7580
}, {
@@ -78,6 +83,11 @@ ruleTester.run('jsx-indent-props', rule, {
7883
' foo',
7984
'/>'
8085
].join('\n'),
86+
output: [
87+
'<App',
88+
' foo',
89+
'/>'
90+
].join('\n'),
8191
options: [2],
8292
parserOptions: parserOptions,
8393
errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]
@@ -87,8 +97,27 @@ ruleTester.run('jsx-indent-props', rule, {
8797
' foo',
8898
'/>'
8999
].join('\n'),
100+
output: [
101+
'<App',
102+
'\tfoo',
103+
'/>'
104+
].join('\n'),
90105
options: ['tab'],
91106
parserOptions: parserOptions,
92107
errors: [{message: 'Expected indentation of 1 tab character but found 0.'}]
108+
}, {
109+
code: [
110+
'<App',
111+
'\t\t\tfoo',
112+
'/>'
113+
].join('\n'),
114+
output: [
115+
'<App',
116+
'\tfoo',
117+
'/>'
118+
].join('\n'),
119+
options: ['tab'],
120+
parserOptions: parserOptions,
121+
errors: [{message: 'Expected indentation of 1 tab character but found 3.'}]
93122
}]
94123
});

0 commit comments

Comments
 (0)