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: show custom message for namespace import (fixes #11580) #11791

Merged
merged 1 commit into from Jun 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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