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

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

Merged
merged 2 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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