Skip to content
Permalink
Browse files

Fix: show custom message for namespace import (fixes #11580) (#11791)

  • Loading branch information...
g-plane authored and platinumazure committed Jun 2, 2019
1 parent 37e5193 commit cb1922bdc07e58de0e55c13fd992dd8faf3292a4
Showing with 21 additions and 17 deletions.
  1. +18 −14 lib/rules/no-restricted-imports.js
  2. +3 −3 tests/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
}
});
}
@@ -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"
}]
}

0 comments on commit cb1922b

Please sign in to comment.
You can’t perform that action at this time.