Skip to content

Commit

Permalink
Update: Fix lines-around-directive semicolon handling (fixes #7450) (
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and nzakas committed Nov 1, 2016
1 parent e58cead commit 8a3e717
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/rules/lines-around-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,32 @@ module.exports = {
return node.loc.start.line - tokenLineBefore >= 2;
}

/**
* Gets the last token of a node that is on the same line as the rest of the node.
* This will usually be the last token of the node, but it will be the second-to-last token if the node has a trailing
* semicolon on a different line.
* @param {ASTNode} node A directive node
* @returns {Token} The last token of the node on the line
*/
function getLastTokenOnLine(node) {
const lastToken = sourceCode.getLastToken(node);
const secondToLastToken = sourceCode.getTokenBefore(lastToken);

return lastToken.type === "Punctuator" && lastToken.value === ";" && lastToken.loc.start.line > secondToLastToken.loc.end.line
? secondToLastToken
: lastToken;
}

/**
* Check if node is followed by a blank newline.
* @param {ASTNode} node Node to check.
* @returns {boolean} Whether or not the passed in node is followed by a blank newline.
*/
function hasNewlineAfter(node) {
const tokenAfter = sourceCode.getTokenOrCommentAfter(node);
const lastToken = getLastTokenOnLine(node);
const tokenAfter = sourceCode.getTokenOrCommentAfter(lastToken);

return tokenAfter.loc.start.line - node.loc.end.line >= 2;
return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
}

/**
Expand All @@ -91,10 +108,12 @@ module.exports = {
location
},
fix(fixer) {
const lastToken = getLastTokenOnLine(node);

if (expected) {
return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(node, "\n");
return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(lastToken, "\n");
}
return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [node.range[1], node.range[1] + 1]);
return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [lastToken.range[1], lastToken.range[1] + 1]);
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/lines-around-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,16 @@ ruleTester.run("lines-around-directive", rule, {
parserOptions: { ecmaVersion: 6 },
options: [{ before: "always", after: "never" }]
},

// https://github.com/eslint/eslint/issues/7450
{
code: "'use strict'\n\n;foo();",
options: [{ before: "never", after: "always" }]
},
{
code: "'use strict'\n;foo();",
options: [{ before: "never", after: "never" }]
}
],

invalid: [
Expand Down Expand Up @@ -1598,6 +1608,27 @@ ruleTester.run("lines-around-directive", rule, {
"Expected newline before \"use strict\" directive.",
"Unexpected newline after \"use asm\" directive."
]
},

// https://github.com/eslint/eslint/issues/7450
{

code: "'use strict'\n\n;foo();",
output: "'use strict'\n;foo();",
options: [{ before: "never", after: "never" }],
errors: ["Unexpected newline after \"use strict\" directive."]
},
{
code: "'use strict'\n;foo();",
output: "'use strict'\n\n;foo();",
options: [{ before: "never", after: "always" }],
errors: ["Expected newline after \"use strict\" directive."]
},
{
code: "'use strict'\n;\nfoo();",
output: "'use strict'\n\n;\nfoo();",
options: [{ before: "never", after: "always" }],
errors: ["Expected newline after \"use strict\" directive."]
}
]
});

0 comments on commit 8a3e717

Please sign in to comment.