From 776b0fe3d93da958517ac7752682091f22eb30b4 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Tue, 30 Apr 2019 12:49:59 +0800 Subject: [PATCH] Fix: update rule message of no-throw-literal (fixes #11637) (#11638) --- lib/rules/no-throw-literal.js | 11 +++++++--- tests/lib/rules/no-throw-literal.js | 31 ++++++++++++++++++----------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/rules/no-throw-literal.js b/lib/rules/no-throw-literal.js index c4a6b86bfb4..bf16927024c 100644 --- a/lib/rules/no-throw-literal.js +++ b/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" }); } } diff --git a/tests/lib/rules/no-throw-literal.js b/tests/lib/rules/no-throw-literal.js index 45e1ec658ee..d27e7c7c5db 100644 --- a/tests/lib/rules/no-throw-literal.js +++ b/tests/lib/rules/no-throw-literal.js @@ -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" }]