Skip to content

Commit a05741a

Browse files
jasminexiebrettz9
authored andcommitted
feat(check-alignment): add fixer
1 parent 188dbe8 commit a05741a

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/rules/checkAlignment.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import iterateJsdoc from '../iterateJsdoc';
23

34
export default iterateJsdoc(({
@@ -17,14 +18,28 @@ export default iterateJsdoc(({
1718
return !line.trim().length;
1819
});
1920

21+
const fix = (fixer) => {
22+
const replacement = sourceCode.getText(jsdocNode).split('\n')
23+
.map((line, index) => {
24+
// Ignore the first line and all lines not starting with `*`
25+
const ignored = !index || line.split('*')[0].trim().length;
26+
27+
return ignored ? line : indent + ' ' + _.trimStart(line);
28+
})
29+
.join('\n');
30+
31+
return fixer.replaceText(jsdocNode, replacement);
32+
};
33+
2034
for (const line of sourceLines) {
2135
if (line.length !== indentLevel) {
22-
report('Expected JSDoc block to be aligned.');
36+
report('Expected JSDoc block to be aligned.', fix);
2337
break;
2438
}
2539
}
2640
}, {
2741
meta: {
42+
fixable: 'code',
2843
type: 'layout'
2944
}
3045
});

test/rules/assertions/checkAlignment.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ export default {
1313
{
1414
message: 'Expected JSDoc block to be aligned.'
1515
}
16-
]
16+
],
17+
output: `
18+
/**
19+
* @param {Number} foo
20+
*/
21+
function quux (foo) {
22+
23+
}
24+
`
1725
},
1826
{
1927
code: `
@@ -28,7 +36,15 @@ export default {
2836
{
2937
message: 'Expected JSDoc block to be aligned.'
3038
}
31-
]
39+
],
40+
output: `
41+
/**
42+
* @param {Number} foo
43+
*/
44+
function quux (foo) {
45+
46+
}
47+
`
3248
},
3349
{
3450
code: `
@@ -43,7 +59,15 @@ export default {
4359
{
4460
message: 'Expected JSDoc block to be aligned.'
4561
}
46-
]
62+
],
63+
output: `
64+
/**
65+
* @param {Number} foo
66+
*/
67+
function quux (foo) {
68+
69+
}
70+
`
4771
},
4872
{
4973
code: `
@@ -58,7 +82,15 @@ export default {
5882
{
5983
message: 'Expected JSDoc block to be aligned.'
6084
}
61-
]
85+
],
86+
output: `
87+
/**
88+
* @param {Number} foo
89+
*/
90+
function quux (foo) {
91+
92+
}
93+
`
6294
},
6395
{
6496
code: `
@@ -73,7 +105,15 @@ export default {
73105
{
74106
message: 'Expected JSDoc block to be aligned.'
75107
}
76-
]
108+
],
109+
output: `
110+
/**
111+
* @param {Number} foo
112+
*/
113+
function quux (foo) {
114+
115+
}
116+
`
77117
}
78118
],
79119
valid: [

0 commit comments

Comments
 (0)