Skip to content

Commit 67f1dd2

Browse files
committed
jsx-indent rule autofixer
1 parent e169799 commit 67f1dd2

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lib/rules/jsx-indent.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = {
3939
category: 'Stylistic Issues',
4040
recommended: false
4141
},
42-
42+
fixable: 'whitespace',
4343
schema: [{
4444
oneOf: [{
4545
enum: ['tab']
@@ -69,6 +69,30 @@ module.exports = {
6969
}
7070
}
7171

72+
var indentChar = indentType === 'space' ? ' ' : '\u0009';
73+
74+
/**
75+
* Responsible for fixing the indentation issue fix
76+
* @param {ASTNode} node Node violating the indent rule
77+
* @param {Number} needed Expected indentation character count
78+
* @param {Number} gotten Indentation character count in the actual node/code
79+
* @returns {Function} function to be executed by the fixer
80+
* @private
81+
*/
82+
function getFixerFunction(node, needed, gotten) {
83+
if (needed > gotten) {
84+
var spaces = indentChar.repeat(needed - gotten);
85+
86+
return function(fixer) {
87+
return fixer.insertTextBeforeRange([node.range[0], node.range[0]], spaces);
88+
};
89+
}
90+
91+
return function(fixer) {
92+
return fixer.removeRange([node.range[0] - (gotten - needed), node.range[0]]);
93+
};
94+
}
95+
7296
/**
7397
* Reports a given indent violation and properly pluralizes the message
7498
* @param {ASTNode} node Node violating the indent rule
@@ -89,13 +113,15 @@ module.exports = {
89113
node: node,
90114
loc: loc,
91115
message: MESSAGE,
92-
data: msgContext
116+
data: msgContext,
117+
fix: getFixerFunction(node, needed, gotten)
93118
});
94119
} else {
95120
context.report({
96121
node: node,
97122
message: MESSAGE,
98-
data: msgContext
123+
data: msgContext,
124+
fix: getFixerFunction(node, needed, gotten)
99125
});
100126
}
101127
}

tests/lib/rules/jsx-indent.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ ruleTester.run('jsx-indent', rule, {
107107
' <Foo />',
108108
'</App>'
109109
].join('\n'),
110+
output: [
111+
'<App>',
112+
' <Foo />',
113+
'</App>'
114+
].join('\n'),
110115
parserOptions: parserOptions,
111116
errors: [{message: 'Expected indentation of 4 space characters but found 2.'}]
112117
}, {
@@ -115,6 +120,11 @@ ruleTester.run('jsx-indent', rule, {
115120
' <Foo />',
116121
'</App>'
117122
].join('\n'),
123+
output: [
124+
'<App>',
125+
' <Foo />',
126+
'</App>'
127+
].join('\n'),
118128
options: [2],
119129
parserOptions: parserOptions,
120130
errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]
@@ -135,6 +145,13 @@ ruleTester.run('jsx-indent', rule, {
135145
' </App>;',
136146
'}'
137147
].join('\n'),
148+
output: [
149+
'function App() {',
150+
' return <App>',
151+
' <Foo />',
152+
' </App>;',
153+
'}'
154+
].join('\n'),
138155
options: [2],
139156
parserOptions: parserOptions,
140157
errors: [{message: 'Expected indentation of 2 space characters but found 9.'}]
@@ -146,6 +163,13 @@ ruleTester.run('jsx-indent', rule, {
146163
' </App>);',
147164
'}'
148165
].join('\n'),
166+
output: [
167+
'function App() {',
168+
' return (<App>',
169+
' <Foo />',
170+
' </App>);',
171+
'}'
172+
].join('\n'),
149173
options: [2],
150174
parserOptions: parserOptions,
151175
errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]

0 commit comments

Comments
 (0)