Skip to content

Commit

Permalink
Fix: show custom message for namespace import (fixes #11580) (#11791)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and platinumazure committed Jun 2, 2019
1 parent 37e5193 commit cb1922b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
32 changes: 18 additions & 14 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -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
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -152,7 +154,7 @@ module.exports = {

context.report({
node,
message: "'{{importSource}}' import is restricted from being used by a pattern.",
messageId: "patterns",
data: {
importSource
}
Expand All @@ -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
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -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"
}]
},
Expand Down Expand Up @@ -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"
}]
},
Expand All @@ -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"
}]
}
Expand Down

0 comments on commit cb1922b

Please sign in to comment.