From 26e7357171ee1abd73dc16191725a786e7108cf0 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 18 Jun 2022 11:39:01 +0800 Subject: [PATCH] fix(`check-line-alignment`): if no types are present, avoid allocating extra space; fixes #891 --- README.md | 16 ++++++++++++++++ src/alignTransform.js | 18 ++++++++++++++---- test/rules/assertions/checkLineAlignment.js | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3f1a16114..c084aa51d 100644 --- a/README.md +++ b/README.md @@ -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"] ```` diff --git a/src/alignTransform.js b/src/alignTransform.js index 04163a4ba..ef76c7946 100644 --- a/src/alignTransform.js +++ b/src/alignTransform.js @@ -79,7 +79,7 @@ const alignTransform = ({ let intoTags = false; let width; - const alignTokens = (tokens) => { + const alignTokens = (tokens, hasNoTypes) => { const nothingAfter = { delim: false, name: false, @@ -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; @@ -137,7 +142,7 @@ const alignTransform = ({ return tokens; }; - const update = (line, index, source) => { + const update = (line, index, source, hasNoTypes) => { const tokens = { ...line.tokens, }; @@ -200,7 +205,7 @@ const alignTransform = ({ return { ...line, - tokens: alignTokens(tokens), + tokens: alignTokens(tokens, hasNoTypes), }; }; @@ -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); }), }); }; diff --git a/test/rules/assertions/checkLineAlignment.js b/test/rules/assertions/checkLineAlignment.js index d9bc03e8e..e2d45d121 100644 --- a/test/rules/assertions/checkLineAlignment.js +++ b/test/rules/assertions/checkLineAlignment.js @@ -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', + ], + }, ], };