From 803c818b594a7756ddb0d749f84627559f0312da Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 25 Feb 2019 15:26:53 +0800 Subject: [PATCH] Add "no-throw-literal" (#37) close #31 --- lib/rules/no-throw-literal.js | 26 +++++++++++++++++ readme.md | 1 + tests/lib/rules/no-throw-literal.js | 43 +++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/rules/no-throw-literal.js create mode 100644 tests/lib/rules/no-throw-literal.js diff --git a/lib/rules/no-throw-literal.js b/lib/rules/no-throw-literal.js new file mode 100644 index 0000000..880efef --- /dev/null +++ b/lib/rules/no-throw-literal.js @@ -0,0 +1,26 @@ +/** + * @fileoverview Add fixer to rule no-throw-literal. + * @author Pig Fang + */ +"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; + } +); diff --git a/readme.md b/readme.md index 71ca23a..58c76d9 100644 --- a/readme.md +++ b/readme.md @@ -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()` diff --git a/tests/lib/rules/no-throw-literal.js b/tests/lib/rules/no-throw-literal.js new file mode 100644 index 0000000..1980576 --- /dev/null +++ b/tests/lib/rules/no-throw-literal.js @@ -0,0 +1,43 @@ +/** + * @fileoverview Tests for rule no-throw-literal. + * @author Pig Fang + */ +"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 + } + ] +});