Skip to content
Permalink
Browse files

Fix: update rule message of no-throw-literal (fixes #11637) (#11638)

  • Loading branch information...
g-plane authored and not-an-aardvark committed Apr 30, 2019
1 parent 67c08b6 commit 776b0fe3d93da958517ac7752682091f22eb30b4
Showing with 27 additions and 15 deletions.
  1. +8 −3 lib/rules/no-throw-literal.js
  2. +19 −12 tests/lib/rules/no-throw-literal.js
@@ -22,7 +22,12 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-throw-literal"
},

schema: []
schema: [],

messages: {
object: "Expected an error object to be thrown.",
undef: "Do not throw undefined."
}
},

create(context) {
@@ -31,10 +36,10 @@ module.exports = {

ThrowStatement(node) {
if (!astUtils.couldBeError(node.argument)) {
context.report({ node, message: "Expected an object to be thrown." });
context.report({ node, messageId: "object" });
} else if (node.argument.type === "Identifier") {
if (node.argument.name === "undefined") {
context.report({ node, message: "Do not throw undefined." });
context.report({ node, messageId: "undef" });
}
}

@@ -44,35 +44,42 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw 'error';",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw 0;",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw false;",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw null;",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw {};",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw undefined;",
errors: [{
message: "Do not throw undefined.",
messageId: "undef",
type: "ThrowStatement"
}]
},
@@ -81,14 +88,14 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw 'a' + 'b';",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "var b = new Error(); throw 'a' + b;",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
@@ -97,7 +104,7 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw foo = 'error';",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
@@ -106,7 +113,7 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw new Error(), 1, 2, 3;",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
@@ -115,7 +122,7 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw 'literal' && 'not an Error';",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
@@ -124,7 +131,7 @@ ruleTester.run("no-throw-literal", rule, {
{
code: "throw foo ? 'not an Error' : 'literal';",
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"
}]
},
@@ -134,7 +141,7 @@ ruleTester.run("no-throw-literal", rule, {
code: "throw `${err}`;",
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Expected an object to be thrown.",
messageId: "object",
type: "ThrowStatement"

}]

0 comments on commit 776b0fe

Please sign in to comment.
You can’t perform that action at this time.