diff --git a/README.md b/README.md index dc47479b4..4c977a2fe 100644 --- a/README.md +++ b/README.md @@ -338,7 +338,31 @@ The following patterns are considered problems: * @param {Number} foo */ function quux (foo) { + // with spaces +} +// Message: Expected JSDoc block to be aligned. + +/** + * @param {Number} foo + */ +function quux (foo) { + // with tabs +} +// Message: Expected JSDoc block to be aligned. +/** + * @param {Number} foo + */ +function quux (foo) { + // with spaces +} +// Message: Expected JSDoc block to be aligned. + +/** +* @param {Number} foo +*/ +function quux (foo) { + // with spaces } // Message: Expected JSDoc block to be aligned. diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index bdcf7fc74..200ccb09f 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -422,7 +422,8 @@ const iterateAllJsdocs = (iterator, ruleConfig) => { return; } - const indent = ' '.repeat(comment.loc.start.column); + const sourceLine = sourceCode.lines[comment.loc.start.line - 1]; + const indent = sourceLine.charAt(0).repeat(comment.loc.start.column); const jsdoc = parseComment(comment, indent, !ruleConfig.noTrim); const settings = getSettings(context); const report = makeReport(context, comment); @@ -498,7 +499,9 @@ export default function iterateJsdoc (iterator, ruleConfig) { return; } - const indent = ' '.repeat(jsdocNode.loc.start.column); + const sourceLine = sourceCode.lines[jsdocNode.loc.start.line - 1]; + + const indent = sourceLine.charAt(0).repeat(jsdocNode.loc.start.column); const jsdoc = parseComment(jsdocNode, indent); diff --git a/src/rules/checkAlignment.js b/src/rules/checkAlignment.js index aa29c8f13..63d052b4a 100644 --- a/src/rules/checkAlignment.js +++ b/src/rules/checkAlignment.js @@ -1,6 +1,9 @@ -import _ from 'lodash'; import iterateJsdoc from '../iterateJsdoc'; +const trimStart = (string) => { + return string.replace(/^\s+/, ''); +}; + export default iterateJsdoc(({ sourceCode, jsdocNode, @@ -15,16 +18,16 @@ export default iterateJsdoc(({ return line.split('*')[0]; }) .filter((line) => { - return !line.trim().length; + return !trimStart(line).length; }); const fix = (fixer) => { const replacement = sourceCode.getText(jsdocNode).split('\n') .map((line, index) => { // Ignore the first line and all lines not starting with `*` - const ignored = !index || line.split('*')[0].trim().length; + const ignored = !index || trimStart(line.split('*')[0]).length; - return ignored ? line : `${indent} ${_.trimStart(line)}`; + return ignored ? line : `${indent} ${trimStart(line)}`; }) .join('\n'); diff --git a/test/rules/assertions/checkAlignment.js b/test/rules/assertions/checkAlignment.js index e232a6758..a63c8da97 100644 --- a/test/rules/assertions/checkAlignment.js +++ b/test/rules/assertions/checkAlignment.js @@ -6,7 +6,7 @@ export default { * @param {Number} foo */ function quux (foo) { - + // with spaces } `, errors: [ @@ -20,10 +20,82 @@ export default { * @param {Number} foo */ function quux (foo) { - + // with spaces } `, }, + { + code: ` +\t\t\t\t/** +\t\t\t\t * @param {Number} foo +\t\t\t\t */ +\t\t\t\tfunction quux (foo) { +\t\t\t\t\t// with tabs +\t\t\t\t} + `, + errors: [ + { + line: 3, + message: 'Expected JSDoc block to be aligned.', + }, + ], + output: ` +\t\t\t\t/** +\t\t\t\t * @param {Number} foo +\t\t\t\t */ +\t\t\t\tfunction quux (foo) { +\t\t\t\t\t// with tabs +\t\t\t\t} + `, + }, + { + code: ` +/** + * @param {Number} foo + */ +function quux (foo) { + // with spaces +} + `, + errors: [ + { + line: 3, + message: 'Expected JSDoc block to be aligned.', + }, + ], + output: ` +/** + * @param {Number} foo + */ +function quux (foo) { + // with spaces +} + `, + }, + { + code: ` +/** +* @param {Number} foo +*/ +function quux (foo) { + // with spaces +} + `, + errors: [ + { + line: 3, + message: 'Expected JSDoc block to be aligned.', + }, + ], + output: ` +/** + * @param {Number} foo + */ +function quux (foo) { + // with spaces +} + `, + }, { code: ` /**