Skip to content

Commit

Permalink
fix(check-line-alignment): if no types are present, avoid allocatin…
Browse files Browse the repository at this point in the history
…g extra space; fixes #891
  • Loading branch information
brettz9 committed Jun 18, 2022
1 parent 374daac commit 26e7357
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -2682,6 +2682,22 @@ const fn = ({ids}) => {}
*/
const fn = ( lorem, sit ) => {}
// "jsdoc/check-line-alignment": ["error"|"warn", "always"]

/**
* Function description.
*
* @param lorem Description.
* @param sit Description multi words.
*/
const fn = ( lorem, sit ) => {};

/**
* Function description.
*
* @return Return description.
*/
const fn2 = () => {}
// "jsdoc/check-line-alignment": ["error"|"warn", "always"]
````


Expand Down
18 changes: 14 additions & 4 deletions src/alignTransform.js
Expand Up @@ -79,7 +79,7 @@ const alignTransform = ({
let intoTags = false;
let width;

const alignTokens = (tokens) => {
const alignTokens = (tokens, hasNoTypes) => {
const nothingAfter = {
delim: false,
name: false,
Expand Down Expand Up @@ -107,6 +107,11 @@ const alignTransform = ({
}
}

if (hasNoTypes) {
nothingAfter.tag = true;
tokens.postTag = '';
}

// Todo: Avoid fixing alignment of blocks with multiline wrapping of type
if (tokens.tag === '' && tokens.type) {
return tokens;
Expand Down Expand Up @@ -137,7 +142,7 @@ const alignTransform = ({
return tokens;
};

const update = (line, index, source) => {
const update = (line, index, source, hasNoTypes) => {
const tokens = {
...line.tokens,
};
Expand Down Expand Up @@ -200,7 +205,7 @@ const alignTransform = ({

return {
...line,
tokens: alignTokens(tokens),
tokens: alignTokens(tokens, hasNoTypes),
};
};

Expand All @@ -211,11 +216,16 @@ const alignTransform = ({
width = source.reduce(getWidth(tags), {
...zeroWidth,
});
const hasNoTypes = fields.tags.every(({
type,
}) => {
return !type;
});

return rewireSource({
...fields,
source: source.map((line, index) => {
return update(line, index, source);
return update(line, index, source, hasNoTypes);
}),
});
};
Expand Down
21 changes: 21 additions & 0 deletions test/rules/assertions/checkLineAlignment.js
Expand Up @@ -1556,5 +1556,26 @@ export default {
'always',
],
},
{
code: `
/**
* Function description.
*
* @param lorem Description.
* @param sit Description multi words.
*/
const fn = ( lorem, sit ) => {};
/**
* Function description.
*
* @return Return description.
*/
const fn2 = () => {}
`,
options: [
'always',
],
},
],
};

0 comments on commit 26e7357

Please sign in to comment.