Skip to content

Commit

Permalink
Chore: refactor fix of semi
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Feb 24, 2017
1 parent f298d6b commit d376127
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
27 changes: 13 additions & 14 deletions lib/rules/semi.js
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -71,6 +77,7 @@ module.exports = {
*/
function report(node, missing) {
const lastToken = sourceCode.getLastToken(node);
const nextToken = sourceCode.getTokenAfter(lastToken);
let message,
fix,
loc = lastToken.loc;
Expand All @@ -83,9 +90,11 @@ module.exports = {
};
} else {
message = "Extra semicolon.";
loc = loc.start;
fix = function(fixer) {
return fixer.remove(lastToken);
return [
fixer.remove(lastToken),
fixer.keep(nextToken)
];
};
}

Expand All @@ -95,16 +104,6 @@ module.exports = {
message,
fix
});

}

/**
* Checks whether a token is a semicolon punctuator.
* @param {Token} token The token.
* @returns {boolean} True if token is a semicolon punctuator.
*/
function isSemicolon(token) {
return (token.type === "Punctuator" && token.value === ";");
}

/**
Expand All @@ -115,7 +114,7 @@ module.exports = {
* @returns {boolean} whether the semicolon is unnecessary.
*/
function isUnnecessarySemicolon(lastToken) {
if (!isSemicolon(lastToken)) {
if (!astUtils.isSemicolonToken(lastToken)) {
return false;
}

Expand Down Expand Up @@ -164,7 +163,7 @@ module.exports = {
report(node, true);
}
} else {
if (!isSemicolon(lastToken)) {
if (!astUtils.isSemicolonToken(lastToken)) {
if (!exceptOneLine || !isOneLinerBlock(node)) {
report(node);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/testers/rule-tester.js
Expand Up @@ -450,9 +450,11 @@ RuleTester.prototype = {
util.format("Should have %d error%s but had %d: %s",
item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages)));

const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);

for (let i = 0, l = item.errors.length; i < l; i++) {
assert.ok(!("fatal" in messages[i]), `A fatal parsing error occurred: ${messages[i].message}`);
assert.equal(messages[i].ruleId, ruleName, "Error rule name should be the same as the name of the rule being tested");
assert(!messages[i].fatal, `A fatal parsing error occurred: ${messages[i].message}`);
assert(hasMessageOfThisRule, "Error rule name should be the same as the name of the rule being tested");

if (typeof item.errors[i] === "string") {

Expand Down
21 changes: 20 additions & 1 deletion tests/lib/rules/semi.js
Expand Up @@ -169,6 +169,25 @@ ruleTester.run("semi", rule, {
{ code: "export default (foo) => foo.bar();", output: "export default (foo) => foo.bar()", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
{ code: "export default foo = 42;", output: "export default foo = 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
{ code: "export default foo += 42;", output: "export default foo += 42", options: ["never"], parserOptions: { sourceType: "module" }, errors: [{ message: "Extra semicolon.", type: "ExportDefaultDeclaration" }] },
{ code: "a;\n++b", output: "a\n++b", options: ["never"], errors: [{ message: "Extra semicolon." }] }
{ code: "a;\n++b", output: "a\n++b", options: ["never"], errors: [{ message: "Extra semicolon." }] },

// https://github.com/eslint/eslint/issues/7928
{
options: ["never"],
code: [
"/*eslint no-extra-semi: error */",
"foo();",
";[0,1,2].forEach(bar)"
].join("\n"),
errors: [
"Extra semicolon.",
"Unnecessary semicolon."
],
output: [
"/*eslint no-extra-semi: error */",
"foo()",
";[0,1,2].forEach(bar)"
].join("\n")
}
]
});

0 comments on commit d376127

Please sign in to comment.