Skip to content

Commit

Permalink
Merge pull request #412 from kaidjohnson/fix/alignment-fixer
Browse files Browse the repository at this point in the history
fix(`check-alignment`): alignment fixer should use indentation character from `sourceCode`.
  • Loading branch information
brettz9 committed Oct 29, 2019
2 parents 9b36869 + a0897c1 commit fc46821
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 5 additions & 2 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 7 additions & 4 deletions src/rules/checkAlignment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import _ from 'lodash';
import iterateJsdoc from '../iterateJsdoc';

const trimStart = (string) => {
return string.replace(/^\s+/, '');
};

export default iterateJsdoc(({
sourceCode,
jsdocNode,
Expand All @@ -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');

Expand Down
76 changes: 74 additions & 2 deletions test/rules/assertions/checkAlignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
* @param {Number} foo
*/
function quux (foo) {
// with spaces
}
`,
errors: [
Expand All @@ -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: `
/**
Expand Down

0 comments on commit fc46821

Please sign in to comment.