-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #31
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
"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; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @fileoverview Tests for rule no-throw-literal. | ||
* @author Pig Fang <[email protected]> | ||
*/ | ||
"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 | ||
} | ||
] | ||
}); |