From 58a32fe0068e8e09e04d2ea1beca4f23200fbb2e Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 30 May 2019 16:15:13 +0800 Subject: [PATCH] Fix: show custom message for namespace import (fixes #11580) --- lib/rules/no-restricted-imports.js | 32 +++++++++++++----------- tests/lib/rules/no-restricted-imports.js | 6 ++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/rules/no-restricted-imports.js b/lib/rules/no-restricted-imports.js index b8917ee9c56..6f3d2158c8d 100644 --- a/lib/rules/no-restricted-imports.js +++ b/lib/rules/no-restricted-imports.js @@ -4,13 +4,6 @@ */ "use strict"; -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -const DEFAULT_MESSAGE_TEMPLATE = "'{{importSource}}' import is restricted from being used."; -const CUSTOM_MESSAGE_TEMPLATE = "'{{importSource}}' import is restricted from being used. {{customMessage}}"; - //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -62,6 +55,18 @@ module.exports = { url: "https://eslint.org/docs/rules/no-restricted-imports" }, + messages: { + path: "'{{importSource}}' import is restricted from being used.", + // eslint-disable-next-line eslint-plugin/report-message-format + pathWithCustomMessage: "'{{importSource}}' import is restricted from being used. {{customMessage}}", + + patterns: "'{{importSource}}' import is restricted from being used by a pattern.", + + everything: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.", + // eslint-disable-next-line eslint-plugin/report-message-format + everythingWithCustomMessage: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}" + }, + schema: { anyOf: [ arrayOfStringsOrObjects, @@ -127,13 +132,10 @@ module.exports = { function reportPath(node) { const importSource = node.source.value.trim(); const customMessage = restrictedPathMessages[importSource] && restrictedPathMessages[importSource].message; - const message = customMessage - ? CUSTOM_MESSAGE_TEMPLATE - : DEFAULT_MESSAGE_TEMPLATE; context.report({ node, - message, + messageId: customMessage ? "pathWithCustomMessage" : "path", data: { importSource, customMessage @@ -152,7 +154,7 @@ module.exports = { context.report({ node, - message: "'{{importSource}}' import is restricted from being used by a pattern.", + messageId: "patterns", data: { importSource } @@ -168,13 +170,15 @@ module.exports = { */ function reportPathForEverythingImported(importSource, node) { const importNames = restrictedPathMessages[importSource].importNames; + const customMessage = restrictedPathMessages[importSource] && restrictedPathMessages[importSource].message; context.report({ node, - message: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.", + messageId: customMessage ? "everythingWithCustomMessage" : "everything", data: { importSource, - importNames + importNames, + customMessage } }); } diff --git a/tests/lib/rules/no-restricted-imports.js b/tests/lib/rules/no-restricted-imports.js index a77adcc267b..ac97bc137db 100644 --- a/tests/lib/rules/no-restricted-imports.js +++ b/tests/lib/rules/no-restricted-imports.js @@ -272,7 +272,7 @@ ruleTester.run("no-restricted-imports", rule, { }] }], errors: [{ - message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted.", + message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.", type: "ImportDeclaration" }] }, @@ -398,7 +398,7 @@ ruleTester.run("no-restricted-imports", rule, { }] }], errors: [{ - message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted.", + message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.", type: "ImportDeclaration" }] }, @@ -412,7 +412,7 @@ ruleTester.run("no-restricted-imports", rule, { }] }], errors: [{ - message: "* import is invalid because 'DisallowedObject,DisallowedObjectTwo' from 'foo' is restricted.", + message: "* import is invalid because 'DisallowedObject,DisallowedObjectTwo' from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead.", type: "ImportDeclaration" }] }