Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "no-throw-literal" #37

Merged
merged 3 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -42,6 +42,7 @@ Name | ✔️ | 🛠 | Description
[no-new-symbol](https://eslint.org/docs/rules/no-new-symbol) | | 🛠 | disallow `new` operators with the `Symbol` object
[no-plusplus](https://eslint.org/docs/rules/no-plusplus) | ✔️ | 🛠 | disallow the unary operators `++` and `--`
[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
33 changes: 33 additions & 0 deletions tests/lib/rules/no-throw-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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
}
]
});