Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Alignment fixer should use indentation character from sourceCode. #412

Merged
merged 3 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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