Skip to content

Commit 26e7357

Browse files
committedJun 18, 2022
fix(check-line-alignment): if no types are present, avoid allocating extra space; fixes #891
1 parent 374daac commit 26e7357

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
 

‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,22 @@ const fn = ({ids}) => {}
26822682
*/
26832683
const fn = ( lorem, sit ) => {}
26842684
// "jsdoc/check-line-alignment": ["error"|"warn", "always"]
2685+
2686+
/**
2687+
* Function description.
2688+
*
2689+
* @param lorem Description.
2690+
* @param sit Description multi words.
2691+
*/
2692+
const fn = ( lorem, sit ) => {};
2693+
2694+
/**
2695+
* Function description.
2696+
*
2697+
* @return Return description.
2698+
*/
2699+
const fn2 = () => {}
2700+
// "jsdoc/check-line-alignment": ["error"|"warn", "always"]
26852701
````
26862702

26872703

‎src/alignTransform.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const alignTransform = ({
7979
let intoTags = false;
8080
let width;
8181

82-
const alignTokens = (tokens) => {
82+
const alignTokens = (tokens, hasNoTypes) => {
8383
const nothingAfter = {
8484
delim: false,
8585
name: false,
@@ -107,6 +107,11 @@ const alignTransform = ({
107107
}
108108
}
109109

110+
if (hasNoTypes) {
111+
nothingAfter.tag = true;
112+
tokens.postTag = '';
113+
}
114+
110115
// Todo: Avoid fixing alignment of blocks with multiline wrapping of type
111116
if (tokens.tag === '' && tokens.type) {
112117
return tokens;
@@ -137,7 +142,7 @@ const alignTransform = ({
137142
return tokens;
138143
};
139144

140-
const update = (line, index, source) => {
145+
const update = (line, index, source, hasNoTypes) => {
141146
const tokens = {
142147
...line.tokens,
143148
};
@@ -200,7 +205,7 @@ const alignTransform = ({
200205

201206
return {
202207
...line,
203-
tokens: alignTokens(tokens),
208+
tokens: alignTokens(tokens, hasNoTypes),
204209
};
205210
};
206211

@@ -211,11 +216,16 @@ const alignTransform = ({
211216
width = source.reduce(getWidth(tags), {
212217
...zeroWidth,
213218
});
219+
const hasNoTypes = fields.tags.every(({
220+
type,
221+
}) => {
222+
return !type;
223+
});
214224

215225
return rewireSource({
216226
...fields,
217227
source: source.map((line, index) => {
218-
return update(line, index, source);
228+
return update(line, index, source, hasNoTypes);
219229
}),
220230
});
221231
};

‎test/rules/assertions/checkLineAlignment.js

+21
Original file line numberDiff line numberDiff line change
@@ -1556,5 +1556,26 @@ export default {
15561556
'always',
15571557
],
15581558
},
1559+
{
1560+
code: `
1561+
/**
1562+
* Function description.
1563+
*
1564+
* @param lorem Description.
1565+
* @param sit Description multi words.
1566+
*/
1567+
const fn = ( lorem, sit ) => {};
1568+
1569+
/**
1570+
* Function description.
1571+
*
1572+
* @return Return description.
1573+
*/
1574+
const fn2 = () => {}
1575+
`,
1576+
options: [
1577+
'always',
1578+
],
1579+
},
15591580
],
15601581
};

0 commit comments

Comments
 (0)
Please sign in to comment.