Skip to content

Commit

Permalink
Fix: update rule message of no-throw-literal (fixes #11637) (#11638)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and not-an-aardvark committed Apr 30, 2019
1 parent 67c08b6 commit 776b0fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
11 changes: 8 additions & 3 deletions lib/rules/no-throw-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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" });
}
}

Expand Down
31 changes: 19 additions & 12 deletions tests/lib/rules/no-throw-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}]
},
Expand All @@ -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"
}]
},
Expand All @@ -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"
}]
},
Expand All @@ -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"
}]
},
Expand All @@ -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"
}]
},
Expand All @@ -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"
}]
},
Expand All @@ -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"

}]
Expand Down

0 comments on commit 776b0fe

Please sign in to comment.