Skip to content

Commit fc46821

Browse files
authored
Merge pull request #412 from kaidjohnson/fix/alignment-fixer
fix(`check-alignment`): alignment fixer should use indentation character from `sourceCode`.
2 parents 9b36869 + a0897c1 commit fc46821

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,31 @@ The following patterns are considered problems:
338338
* @param {Number} foo
339339
*/
340340
function quux (foo) {
341+
// with spaces
342+
}
343+
// Message: Expected JSDoc block to be aligned.
344+
345+
/**
346+
* @param {Number} foo
347+
*/
348+
function quux (foo) {
349+
// with tabs
350+
}
351+
// Message: Expected JSDoc block to be aligned.
341352

353+
/**
354+
* @param {Number} foo
355+
*/
356+
function quux (foo) {
357+
// with spaces
358+
}
359+
// Message: Expected JSDoc block to be aligned.
360+
361+
/**
362+
* @param {Number} foo
363+
*/
364+
function quux (foo) {
365+
// with spaces
342366
}
343367
// Message: Expected JSDoc block to be aligned.
344368

src/iterateJsdoc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ const iterateAllJsdocs = (iterator, ruleConfig) => {
422422
return;
423423
}
424424

425-
const indent = ' '.repeat(comment.loc.start.column);
425+
const sourceLine = sourceCode.lines[comment.loc.start.line - 1];
426+
const indent = sourceLine.charAt(0).repeat(comment.loc.start.column);
426427
const jsdoc = parseComment(comment, indent, !ruleConfig.noTrim);
427428
const settings = getSettings(context);
428429
const report = makeReport(context, comment);
@@ -498,7 +499,9 @@ export default function iterateJsdoc (iterator, ruleConfig) {
498499
return;
499500
}
500501

501-
const indent = ' '.repeat(jsdocNode.loc.start.column);
502+
const sourceLine = sourceCode.lines[jsdocNode.loc.start.line - 1];
503+
504+
const indent = sourceLine.charAt(0).repeat(jsdocNode.loc.start.column);
502505

503506
const jsdoc = parseComment(jsdocNode, indent);
504507

src/rules/checkAlignment.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import _ from 'lodash';
21
import iterateJsdoc from '../iterateJsdoc';
32

3+
const trimStart = (string) => {
4+
return string.replace(/^\s+/, '');
5+
};
6+
47
export default iterateJsdoc(({
58
sourceCode,
69
jsdocNode,
@@ -15,16 +18,16 @@ export default iterateJsdoc(({
1518
return line.split('*')[0];
1619
})
1720
.filter((line) => {
18-
return !line.trim().length;
21+
return !trimStart(line).length;
1922
});
2023

2124
const fix = (fixer) => {
2225
const replacement = sourceCode.getText(jsdocNode).split('\n')
2326
.map((line, index) => {
2427
// Ignore the first line and all lines not starting with `*`
25-
const ignored = !index || line.split('*')[0].trim().length;
28+
const ignored = !index || trimStart(line.split('*')[0]).length;
2629

27-
return ignored ? line : `${indent} ${_.trimStart(line)}`;
30+
return ignored ? line : `${indent} ${trimStart(line)}`;
2831
})
2932
.join('\n');
3033

test/rules/assertions/checkAlignment.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
* @param {Number} foo
77
*/
88
function quux (foo) {
9-
9+
// with spaces
1010
}
1111
`,
1212
errors: [
@@ -20,10 +20,82 @@ export default {
2020
* @param {Number} foo
2121
*/
2222
function quux (foo) {
23-
23+
// with spaces
2424
}
2525
`,
2626
},
27+
{
28+
code: `
29+
\t\t\t\t/**
30+
\t\t\t\t * @param {Number} foo
31+
\t\t\t\t */
32+
\t\t\t\tfunction quux (foo) {
33+
\t\t\t\t\t// with tabs
34+
\t\t\t\t}
35+
`,
36+
errors: [
37+
{
38+
line: 3,
39+
message: 'Expected JSDoc block to be aligned.',
40+
},
41+
],
42+
output: `
43+
\t\t\t\t/**
44+
\t\t\t\t * @param {Number} foo
45+
\t\t\t\t */
46+
\t\t\t\tfunction quux (foo) {
47+
\t\t\t\t\t// with tabs
48+
\t\t\t\t}
49+
`,
50+
},
51+
{
52+
code: `
53+
/**
54+
* @param {Number} foo
55+
*/
56+
function quux (foo) {
57+
// with spaces
58+
}
59+
`,
60+
errors: [
61+
{
62+
line: 3,
63+
message: 'Expected JSDoc block to be aligned.',
64+
},
65+
],
66+
output: `
67+
/**
68+
* @param {Number} foo
69+
*/
70+
function quux (foo) {
71+
// with spaces
72+
}
73+
`,
74+
},
75+
{
76+
code: `
77+
/**
78+
* @param {Number} foo
79+
*/
80+
function quux (foo) {
81+
// with spaces
82+
}
83+
`,
84+
errors: [
85+
{
86+
line: 3,
87+
message: 'Expected JSDoc block to be aligned.',
88+
},
89+
],
90+
output: `
91+
/**
92+
* @param {Number} foo
93+
*/
94+
function quux (foo) {
95+
// with spaces
96+
}
97+
`,
98+
},
2799
{
28100
code: `
29101
/**

0 commit comments

Comments
 (0)