Skip to content

Commit b2db226

Browse files
author
Brett Zamir
committed
fix(check-line-alignment): implement default never options; fixes #483
1 parent 577863b commit b2db226

File tree

4 files changed

+181
-21
lines changed

4 files changed

+181
-21
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,14 +2025,43 @@ const config = {
20252025
// Message: Expected JSDoc block lines to be aligned.
20262026

20272027
/**
2028-
* Not implemented yet.
2028+
* My function.
2029+
*
2030+
* @param {string} lorem Description.
2031+
* @param {int} sit Description multi words.
2032+
*/
2033+
const fn = ( lorem, sit ) => {}
2034+
// Options: ["never"]
2035+
// Message: Expected JSDoc block lines to not be aligned.
2036+
2037+
/**
2038+
* My function.
20292039
*
20302040
* @param {string} lorem Description.
2031-
* @param {int} sit Description multi words.
2041+
* @param {int} sit Description multi words.
2042+
*/
2043+
const fn = ( lorem, sit ) => {}
2044+
// Options: ["never"]
2045+
// Message: Expected JSDoc block lines to not be aligned.
2046+
2047+
/**
2048+
* My function.
2049+
*
2050+
* @param {string} lorem Description.
2051+
* @param {int} sit
20322052
*/
20332053
const fn = ( lorem, sit ) => {}
20342054
// Options: ["never"]
2035-
// Message: The `never` option is not yet implemented for this rule.
2055+
// Message: Expected JSDoc block lines to not be aligned.
2056+
2057+
/**
2058+
* My function.
2059+
*
2060+
* @param {string} lorem Description.
2061+
* @param {int} sit
2062+
*/
2063+
const fn = ( lorem, sit ) => {}
2064+
// Message: Expected JSDoc block lines to not be aligned.
20362065
````
20372066

20382067
The following patterns are not considered problems:

src/iterateJsdoc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const getUtils = (
240240
});
241241
};
242242

243-
utils.setTag = (tag) => {
243+
utils.setTag = (tag, tokens) => {
244244
tag.source = [{
245245
// Or tag.source[0].number?
246246
number: tag.line,
@@ -249,6 +249,7 @@ const getUtils = (
249249
postDelimiter: ' ',
250250
start: indent + ' ',
251251
tag: '@' + tag.tag,
252+
...tokens,
252253
}),
253254
}];
254255
};

src/rules/checkLineAlignment.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const createFixer = (comment, expectedPositions, partsMatrix, lineRegExp, tagInd
111111
* @param {string} tagIndentation Tag indentation.
112112
* @param {Function} report Report function.
113113
*/
114-
const checkCommentPerTag = (comment, tag, tagIndentation, report) => {
114+
const checkAlignedPerTag = (comment, tag, tagIndentation, report) => {
115115
const lineRegExp = new RegExp(`.*@${tag}[\\s].*`, 'gm');
116116
const lines = comment.value.match(lineRegExp);
117117

@@ -171,32 +171,65 @@ const checkCommentPerTag = (comment, tag, tagIndentation, report) => {
171171
}
172172
};
173173

174+
const spacers = [
175+
['postDelimiter', ['tag']],
176+
['postTag', ['type', 'name', 'description']],
177+
['postType', ['name', 'description']],
178+
['postName', ['description']],
179+
];
180+
181+
const checkNotAlignedPerTag = (utils, tag) => {
182+
// If checking alignment on multiple lines, need to check other `source` items
183+
const ok = spacers.every(([prop, checkProps]) => {
184+
const hasCheckProp = checkProps.some((checkProp) => {
185+
return tag.source[0].tokens[checkProp];
186+
});
187+
188+
return !hasCheckProp || (/^[\t ]$/).test(tag.source[0].tokens[prop]);
189+
});
190+
if (ok) {
191+
return;
192+
}
193+
const fix = () => {
194+
const tokens = tag.source[0].tokens;
195+
spacers.forEach(([prop, checkProps]) => {
196+
const hasCheckProp = checkProps.some((checkProp) => {
197+
return tag.source[0].tokens[checkProp];
198+
});
199+
tokens[prop] = hasCheckProp ? ' ' : '';
200+
});
201+
202+
utils.setTag(tag, tokens);
203+
};
204+
utils.reportJSDoc('Expected JSDoc block lines to not be aligned.', tag, fix, true);
205+
};
206+
174207
export default iterateJsdoc(({
175208
jsdocNode,
176209
report,
177210
context,
178211
indent,
212+
utils,
179213
}) => {
180-
if (context.options[0] === 'never') {
181-
report('The `never` option is not yet implemented for this rule.');
214+
if (context.options[0] === 'always') {
215+
// Skip if it contains only a single line.
216+
if (!jsdocNode.value.includes('\n')) {
217+
return;
218+
}
182219

183-
return;
184-
}
220+
// `indent` is whitespace from line 1 (`/**`), so slice and account for "/".
221+
const tagIndentation = indent + ' ';
185222

186-
if (context.options[0] !== 'always') {
187-
return;
188-
}
223+
['param', 'arg', 'argument', 'property', 'prop'].forEach((tag) => {
224+
checkAlignedPerTag(jsdocNode, tag, tagIndentation, report);
225+
});
189226

190-
// Skip if it contains only a single line.
191-
if (!jsdocNode.value.includes('\n')) {
192227
return;
193228
}
194229

195-
// `indent` is whitespace from line 1 (`/**`), so slice and account for "/".
196-
const tagIndentation = indent + ' ';
197-
198-
['param', 'arg', 'argument', 'property', 'prop'].forEach((tag) => {
199-
checkCommentPerTag(jsdocNode, tag, tagIndentation, report);
230+
const paramTags = utils.getPresentTags(['param', 'arg', 'argument', 'property', 'prop']);
231+
paramTags.forEach((tag) => {
232+
checkNotAlignedPerTag(utils, tag, report);
200233
});
201234
}, {
202235
iterateAllJsdocs: true,

test/rules/assertions/checkLineAlignment.js

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,22 +402,119 @@ export default {
402402
{
403403
code: `
404404
/**
405-
* Not implemented yet.
405+
* My function.
406+
*
407+
* @param {string} lorem Description.
408+
* @param {int} sit Description multi words.
409+
*/
410+
const fn = ( lorem, sit ) => {}
411+
`,
412+
errors: [
413+
{
414+
message: 'Expected JSDoc block lines to not be aligned.',
415+
type: 'Block',
416+
},
417+
{
418+
message: 'Expected JSDoc block lines to not be aligned.',
419+
type: 'Block',
420+
},
421+
],
422+
options: [
423+
'never',
424+
],
425+
output: `
426+
/**
427+
* My function.
428+
*
429+
* @param {string} lorem Description.
430+
* @param {int} sit Description multi words.
431+
*/
432+
const fn = ( lorem, sit ) => {}
433+
`,
434+
},
435+
{
436+
code: `
437+
/**
438+
* My function.
439+
*
440+
* @param {string} lorem Description.
441+
* @param {int} sit Description multi words.
442+
*/
443+
const fn = ( lorem, sit ) => {}
444+
`,
445+
errors: [
446+
{
447+
message: 'Expected JSDoc block lines to not be aligned.',
448+
type: 'Block',
449+
},
450+
],
451+
options: [
452+
'never',
453+
],
454+
output: `
455+
/**
456+
* My function.
406457
*
407458
* @param {string} lorem Description.
408459
* @param {int} sit Description multi words.
409460
*/
410461
const fn = ( lorem, sit ) => {}
411462
`,
463+
},
464+
{
465+
code: `
466+
/**
467+
* My function.
468+
*
469+
* @param {string} lorem Description.
470+
* @param {int} sit
471+
*/
472+
const fn = ( lorem, sit ) => {}
473+
`,
412474
errors: [
413475
{
414-
message: 'The `never` option is not yet implemented for this rule.',
476+
message: 'Expected JSDoc block lines to not be aligned.',
415477
type: 'Block',
416478
},
417479
],
418480
options: [
419481
'never',
420482
],
483+
output: `
484+
/**
485+
* My function.
486+
*
487+
* @param {string} lorem Description.
488+
* @param {int} sit
489+
*/
490+
const fn = ( lorem, sit ) => {}
491+
`,
492+
},
493+
{
494+
code: `
495+
/**
496+
* My function.
497+
*
498+
* @param {string} lorem Description.
499+
* @param {int} sit
500+
*/
501+
const fn = ( lorem, sit ) => {}
502+
`,
503+
errors: [
504+
{
505+
message: 'Expected JSDoc block lines to not be aligned.',
506+
type: 'Block',
507+
},
508+
],
509+
output: `
510+
/**
511+
* My function.
512+
*
513+
* @param {string} lorem Description.
514+
* @param {int} sit
515+
*/
516+
const fn = ( lorem, sit ) => {}
517+
`,
421518
},
422519
],
423520
valid: [

0 commit comments

Comments
 (0)