Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Add ignoreComments option to no-trailing-spaces (#8061)
  • Loading branch information
Jake Roussel authored and kaicataldo committed Jun 21, 2017
1 parent b58ae2e commit 879688c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
18 changes: 18 additions & 0 deletions docs/rules/no-trailing-spaces.md
Expand Up @@ -31,6 +31,8 @@ This rule has an object option:

* `"skipBlankLines": false` (default) disallows trailing whitespace on empty lines
* `"skipBlankLines": true` allows trailing whitespace on empty lines
* `"ignoreComments": false` (default) disallows trailing whitespace in comment blocks
* `"ignoreComments": true` allows trailing whitespace in comment blocks

### skipBlankLines

Expand All @@ -43,3 +45,19 @@ var foo = 0;
var baz = 5;
//•••••
```

### ignoreComments

Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option:

```js
/*eslint no-trailing-spaces: ["error", { "ignoreComments": true }]*/

//foo•
//•••••
/**
*•baz
*••
*•bar
*/
```
32 changes: 29 additions & 3 deletions lib/rules/no-trailing-spaces.js
Expand Up @@ -30,6 +30,9 @@ module.exports = {
properties: {
skipBlankLines: {
type: "boolean"
},
ignoreComments: {
type: "boolean"
}
},
additionalProperties: false
Expand All @@ -45,7 +48,8 @@ module.exports = {
NONBLANK = `${BLANK_CLASS}+$`;

const options = context.options[0] || {},
skipBlankLines = options.skipBlankLines || false;
skipBlankLines = options.skipBlankLines || false,
ignoreComments = typeof options.ignoreComments === "undefined" || options.ignoreComments;

/**
* Report the error message
Expand All @@ -72,6 +76,22 @@ module.exports = {
});
}

/**
* Given a list of comment nodes, return the line numbers for those comments.
* @param {Array} comments An array of comment nodes.
* @returns {number[]} An array of line numbers containing comments.
*/
function getCommentLineNumbers(comments) {
const lines = new Set();

comments.forEach(comment => {
for (let i = comment.loc.start.line; i <= comment.loc.end.line; i++) {
lines.add(i);
}
});

return lines;
}

//--------------------------------------------------------------------------
// Public
Expand All @@ -87,7 +107,10 @@ module.exports = {
const re = new RegExp(NONBLANK),
skipMatch = new RegExp(SKIP_BLANK),
lines = sourceCode.lines,
linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher());
linebreaks = sourceCode.getText().match(astUtils.createGlobalLinebreakMatcher()),
comments = sourceCode.getAllComments(),
commentLineNumbers = getCommentLineNumbers(comments);

let totalLength = 0,
fixRange = [];

Expand Down Expand Up @@ -125,7 +148,10 @@ module.exports = {
}

fixRange = [rangeStart, rangeEnd];
report(node, location, fixRange);

if (!ignoreComments || !commentLineNumbers.has(location.line)) {
report(node, location, fixRange);
}
}

totalLength += lineLength;
Expand Down
72 changes: 72 additions & 0 deletions tests/lib/rules/no-trailing-spaces.js
Expand Up @@ -70,6 +70,18 @@ ruleTester.run("no-trailing-spaces", rule, {
code: "let str = `${a}\n \n${b}`;\n \n ",
parserOptions: { ecmaVersion: 6 },
options: [{ skipBlankLines: true }]
},
{
code: "// Trailing comment test. ",
options: [{ ignoreComments: true }]
},
{
code: "/* \nTrailing comments test. \n*/",
options: [{ ignoreComments: true }]
},
{
code: "#!/usr/bin/env node ",
options: [{ ignoreComments: true }]
}
],

Expand Down Expand Up @@ -416,6 +428,66 @@ ruleTester.run("no-trailing-spaces", rule, {
column: 8
}
]
},

// Tests for ignoreComments flag.
{
code: "var foo = 'bar'; ",
output: "var foo = 'bar';",
options: [{ ignoreComments: true }],
errors: [
{
message: "Trailing spaces not allowed.",
type: "Program",
line: 1,
column: 17
}
]
},
{
code: "// Trailing comment test. ",
output: "// Trailing comment test.",
options: [{ ignoreComments: false }],
errors: [
{
message: "Trailing spaces not allowed.",
type: "Program",
line: 1,
column: 26
}
]
},
{
code: "/* \nTrailing comments test. \n*/",
output: "/*\nTrailing comments test.\n*/",
options: [{ ignoreComments: false }],
errors: [
{
message: "Trailing spaces not allowed.",
type: "Program",
line: 1,
column: 3
},
{
message: "Trailing spaces not allowed.",
type: "Program",
line: 2,
column: 24
}
]
},
{
code: "#!/usr/bin/env node ",
output: "#!/usr/bin/env node",
options: [{ ignoreComments: false }],
errors: [
{
message: "Trailing spaces not allowed.",
type: "Program",
line: 1,
column: 20
}
]
}
]
});

0 comments on commit 879688c

Please sign in to comment.