Skip to content

Commit

Permalink
New: implement rule "no-console" (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Feb 9, 2019
1 parent 268e441 commit 71988af
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
48 changes: 48 additions & 0 deletions lib/rules/no-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @fileoverview Add fixer to rule "no-console"
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

const ruleComposer = require("eslint-rule-composer");
const utils = require("../utils");

const rule = utils.getFixableRule("no-console");

module.exports = ruleComposer.mapReports(
rule,
problem => {
problem.fix = fixer => {
const { node } = problem;
const parentType = node.parent.type;
const isMethodCall = parentType === "CallExpression";

if (parentType === "VariableDeclarator") {
return null;
}

if (isMethodCall && !node.parent.arguments.every(arg => arg.type === "Literal" || arg.type === "Identifier")) {
return null;
}

if (node.parent.parent) {
const grandType = node.parent.parent.type;
const maybeFlowType = isMethodCall && node.parent.parent.parent
? node.parent.parent.parent.type
: grandType;

if (
(maybeFlowType === "IfStatement" || maybeFlowType === "WhileStatement" || maybeFlowType === "ForStatement") &&
(isMethodCall ? grandType === "ExpressionStatement" : parentType === "ExpressionStatement")
) {
return isMethodCall ? fixer.replaceText(node.parent, "{}") : fixer.replaceText(node, "{}");
}
}

return isMethodCall ? fixer.remove(node.parent) : fixer.remove(problem.node);

};

return problem;
}
);
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ add prefix "autofix" to the rulename in eslintrc:
## Supported rules

+ 🛠no-debugger
+ 🛠no-console(wip)
+ 🛠no-console
+ 🛠prefer-spread

## Acknowledgement
Expand Down
77 changes: 77 additions & 0 deletions tests/lib/rules/no-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @fileoverview Tests for rule "no-console"
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

const rule = require("../../../lib/rules/no-console");
const RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();

ruleTester.run("no-console", rule, {
valid: [
"console;",
"if (true) console;"
],
invalid: [
{
code: "console.log",
output: "",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "console.log()",
output: "",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "if (true) console.log",
output: "if (true) {}",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "if (true) { console.log }",
output: "if (true) { }",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "if (true) console.log()",
output: "if (true) {}",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "if (true) { console.log() }",
output: "if (true) { }",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "console.log(foo)",
output: "",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "console.log(233)",
output: "",
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "console.log(foo())",
output: null,
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
},
{
code: "var log = console.log",
output: null,
errors: [{ messageId: "unexpected", type: "MemberExpression" }]
}
]
});

0 comments on commit 71988af

Please sign in to comment.