From 34866bc988a1a26fab9c4cf3c89fadb4c7d0494f Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 1 Oct 2024 17:28:26 +0800 Subject: [PATCH] fix(`check-alignment`): handle zero indent; fixes #1322 --- docs/rules/check-alignment.md | 7 +++++++ src/iterateJsdoc.js | 8 +++++++- test/rules/assertions/checkAlignment.js | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/rules/check-alignment.md b/docs/rules/check-alignment.md index 22f21988a..fdbf534ba 100644 --- a/docs/rules/check-alignment.md +++ b/docs/rules/check-alignment.md @@ -110,6 +110,13 @@ class Foo { quux(a) {} } // Message: Expected JSDoc block to be aligned. + +export const myVar = {/** + * This is JSDoc + */ + myProperty: 'hello' +} +// Message: Expected JSDoc block to be aligned. ```` diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index b5f93a3cf..eec9a974c 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -1999,7 +1999,13 @@ const getIndentAndJSDoc = function (lines, jsdocNode) { /** @type {import('estree').SourceLocation} */ (jsdocNode.loc).start.line - 1 ]; - const indnt = sourceLine.charAt(0).repeat( + + let indentChar = sourceLine.charAt(0); + if (indentChar !== ' ' && indentChar !== '\t') { + indentChar = ' '; + } + + const indnt = indentChar.repeat( /** @type {import('estree').SourceLocation} */ (jsdocNode.loc).start.column, ); diff --git a/test/rules/assertions/checkAlignment.js b/test/rules/assertions/checkAlignment.js index 2c395f1e4..ca6e64027 100644 --- a/test/rules/assertions/checkAlignment.js +++ b/test/rules/assertions/checkAlignment.js @@ -236,6 +236,28 @@ function quux (foo) { } `, }, + { + code: ` +export const myVar = {/** + * This is JSDoc + */ + myProperty: 'hello' +} +`, + errors: [ + { + line: 3, + message: 'Expected JSDoc block to be aligned.', + }, + ], + output: ` +export const myVar = {/** + * This is JSDoc + */ + myProperty: 'hello' +} +`, + }, ], valid: [ {