Skip to content

Commit 784ce62

Browse files
authored
feat(check-alignment): add innerIndent integer option; fixes #1351 (#1471)
1 parent 5fbab65 commit 784ce62

File tree

3 files changed

+170
-17
lines changed

3 files changed

+170
-17
lines changed

docs/rules/check-alignment.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ export const myVar = {/**
117117
myProperty: 'hello'
118118
}
119119
// Message: Expected JSDoc block to be aligned.
120+
121+
/**
122+
* @param {Number} foo
123+
* @access private
124+
*/
125+
function quux (foo) {
126+
// with spaces
127+
}
128+
// "jsdoc/check-alignment": ["error"|"warn", {"innerIndent":0}]
129+
// Message: Expected JSDoc block to be aligned.
130+
131+
/**
132+
Some desc.
133+
@param {Number} foo
134+
@access private
135+
*/
136+
function quux (foo) {
137+
// with spaces
138+
}
139+
// "jsdoc/check-alignment": ["error"|"warn", {"innerIndent":0}]
140+
// Message: Expected JSDoc block to be aligned.
120141
````
121142

122143

@@ -172,5 +193,24 @@ function quux (foo) {
172193
// with spaces
173194
}
174195
// Settings: {"jsdoc":{"ignorePrivate":true}}
196+
197+
/**
198+
* @param {Number} foo
199+
* @access private
200+
*/
201+
function quux (foo) {
202+
// with spaces
203+
}
204+
// "jsdoc/check-alignment": ["error"|"warn", {"innerIndent":0}]
205+
206+
/**
207+
Some desc.
208+
@param {Number} foo
209+
@access private
210+
*/
211+
function quux (foo) {
212+
// with spaces
213+
}
214+
// "jsdoc/check-alignment": ["error"|"warn", {"innerIndent":0}]
175215
````
176216

src/rules/checkAlignment.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
import iterateJsdoc from '../iterateJsdoc.js';
22

3-
/**
4-
* @param {string} string
5-
* @returns {string}
6-
*/
7-
const trimStart = (string) => {
8-
return string.replace(/^\s+/v, '');
9-
};
10-
113
export default iterateJsdoc(({
4+
context,
125
indent,
136
jsdocNode,
147
report,
158
sourceCode,
169
}) => {
10+
const {
11+
innerIndent = 1,
12+
} = context.options[0] || {};
13+
1714
// `indent` is whitespace from line 1 (`/**`), so slice and account for "/".
18-
const indentLevel = indent.length + 1;
15+
const indentLevel = indent.length + innerIndent;
1916
const sourceLines = sourceCode.getText(jsdocNode).split('\n')
2017
.slice(1)
21-
.map((line) => {
22-
return line.split('*')[0];
18+
.map((line, number) => {
19+
return {
20+
line: line.split('*')[0],
21+
number,
22+
};
2323
})
24-
.filter((line) => {
25-
return !trimStart(line).length;
24+
.filter(({
25+
line,
26+
}) => {
27+
return !line.trimStart().length;
2628
});
2729

2830
/** @type {import('eslint').Rule.ReportFixer} */
2931
const fix = (fixer) => {
3032
const replacement = sourceCode.getText(jsdocNode).split('\n')
3133
.map((line, index) => {
3234
// Ignore the first line and all lines not starting with `*`
33-
const ignored = !index || trimStart(line.split('*')[0]).length;
35+
const ignored = !index || line.split('*')[0].trimStart().length;
3436

35-
return ignored ? line : `${indent} ${trimStart(line)}`;
37+
return ignored ? line : `${indent}${''.padStart(innerIndent, ' ')}${line.trimStart()}`;
3638
})
3739
.join('\n');
3840

3941
return fixer.replaceText(jsdocNode, replacement);
4042
};
4143

42-
sourceLines.some((line, lineNum) => {
44+
sourceLines.some(({
45+
line,
46+
number,
47+
}) => {
4348
if (line.length !== indentLevel) {
4449
report('Expected JSDoc block to be aligned.', fix, {
45-
line: lineNum + 1,
50+
line: number + 1,
4651
});
4752

4853
return true;
@@ -58,6 +63,17 @@ export default iterateJsdoc(({
5863
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-alignment.md#repos-sticky-header',
5964
},
6065
fixable: 'code',
66+
schema: [
67+
{
68+
additionalProperties: false,
69+
properties: {
70+
innerIndent: {
71+
default: 1,
72+
type: 'integer',
73+
},
74+
},
75+
},
76+
],
6177
type: 'layout',
6278
},
6379
});

test/rules/assertions/checkAlignment.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,70 @@ export const myVar = {/**
258258
}
259259
`,
260260
},
261+
{
262+
code: `
263+
/**
264+
* @param {Number} foo
265+
* @access private
266+
*/
267+
function quux (foo) {
268+
// with spaces
269+
}
270+
`,
271+
errors: [
272+
{
273+
line: 3,
274+
message: 'Expected JSDoc block to be aligned.',
275+
},
276+
],
277+
options: [
278+
{
279+
innerIndent: 0,
280+
},
281+
],
282+
output: `
283+
/**
284+
* @param {Number} foo
285+
* @access private
286+
*/
287+
function quux (foo) {
288+
// with spaces
289+
}
290+
`,
291+
},
292+
{
293+
code: `
294+
/**
295+
Some desc.
296+
@param {Number} foo
297+
@access private
298+
*/
299+
function quux (foo) {
300+
// with spaces
301+
}
302+
`,
303+
errors: [
304+
{
305+
line: 6,
306+
message: 'Expected JSDoc block to be aligned.',
307+
},
308+
],
309+
options: [
310+
{
311+
innerIndent: 0,
312+
},
313+
],
314+
output: `
315+
/**
316+
Some desc.
317+
@param {Number} foo
318+
@access private
319+
*/
320+
function quux (foo) {
321+
// with spaces
322+
}
323+
`,
324+
},
261325
],
262326
valid: [
263327
{
@@ -328,5 +392,38 @@ export const myVar = {/**
328392
},
329393
},
330394
},
395+
{
396+
code: `
397+
/**
398+
* @param {Number} foo
399+
* @access private
400+
*/
401+
function quux (foo) {
402+
// with spaces
403+
}
404+
`,
405+
options: [
406+
{
407+
innerIndent: 0,
408+
},
409+
],
410+
},
411+
{
412+
code: `
413+
/**
414+
Some desc.
415+
@param {Number} foo
416+
@access private
417+
*/
418+
function quux (foo) {
419+
// with spaces
420+
}
421+
`,
422+
options: [
423+
{
424+
innerIndent: 0,
425+
},
426+
],
427+
},
331428
],
332429
});

0 commit comments

Comments
 (0)