Skip to content

Commit

Permalink
Add "no-throw-literal" (#37)
Browse files Browse the repository at this point in the history
close #31
  • Loading branch information
g-plane committed Feb 25, 2019
1 parent 5d0a3d1 commit 803c818
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/rules/no-throw-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @fileoverview Add fixer to rule no-throw-literal.
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

const rule = utils.getFixableRule("no-throw-literal", false);

module.exports = ruleComposer.mapReports(
rule,
(problem, { sourceCode }) => {
problem.fix = fixer => {
const { node } = problem;
const { argument } = node;

if (argument.type === "Identifier") {
return null;
}
return fixer.replaceText(node, `throw new Error(${sourceCode.getText(argument)});`);
};
return problem;
}
);
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Name | ✔️ | 🛠 | Description
[no-plusplus](https://eslint.org/docs/rules/no-plusplus) | ✔️ | 🛠 | disallow the unary operators `++` and `--`
[no-proto](https://eslint.org/docs/rules/no-proto) | | 🛠 | disallow the use of the `__proto__` property
[no-prototype-builtins](https://eslint.org/docs/rules/no-prototype-builtins) | | 🛠 | disallow calling some `Object.prototype` methods directly on objects
[no-throw-literal](https://eslint.org/docs/rules/no-throw-literal) | | 🛠 | disallow throwing literals as exceptions
[no-useless-concat](https://eslint.org/docs/rules/no-useless-concat) | | 🛠 | disallow unnecessary concatenation of literals or template literals
[prefer-spread](https://eslint.org/docs/rules/prefer-spread) | | 🛠 | require spread operators instead of `.apply()`
[radix](https://eslint.org/docs/rules/radix) | | 🛠 | enforce the consistent use of the radix argument when using `parseInt()`
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-throw-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @fileoverview Tests for rule no-throw-literal.
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

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

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

const ruleTester = new RuleTester();
const errors = [{ type: "ThrowStatement" }];

ruleTester.run("no-throw-literal", rule, {
valid: [
"throw new Error()",
"throw error"
],
invalid: [
{
code: "throw 'error'",
output: "throw new Error('error');",
errors
},
{
code: "throw ''",
output: "throw new Error('');",
errors
},
{
code: "throw 0",
output: "throw new Error(0);",
errors
}
]
});

0 comments on commit 803c818

Please sign in to comment.