Skip to content

Commit b017f09

Browse files
authored
fix: correct no-restricted-import messages (#20374)
1 parent e593aa0 commit b017f09

File tree

2 files changed

+278
-25
lines changed

2 files changed

+278
-25
lines changed

lib/rules/no-restricted-imports.js

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@
1010

1111
const astUtils = require("./utils/ast-utils");
1212

13+
//------------------------------------------------------------------------------
14+
// Helpers
15+
//------------------------------------------------------------------------------
16+
17+
/**
18+
* Format import names for error messages.
19+
* @param {string[]} importNames The import names to format.
20+
* @returns {string} The formatted import names.
21+
*/
22+
function formatImportNames(importNames) {
23+
return new Intl.ListFormat("en-US").format(
24+
importNames.map(name => `'${name}'`),
25+
);
26+
}
27+
28+
/**
29+
* Returns "is" or "are" based on the number of import names.
30+
* @param {string[]} importNames The import names to check.
31+
* @returns {string} "is" if one import name, otherwise "are".
32+
*/
33+
function isOrAre(importNames) {
34+
return importNames.length === 1 ? "is" : "are";
35+
}
36+
1337
//------------------------------------------------------------------------------
1438
// Rule Definition
1539
//------------------------------------------------------------------------------
@@ -175,22 +199,22 @@ module.exports = {
175199
"'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
176200

177201
patternAndEverything:
178-
"* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.",
202+
"* import is invalid because {{importNames}} from '{{importSource}}' {{isOrAre}} restricted from being used by a pattern.",
179203

180204
patternAndEverythingWithRegexImportName:
181205
"* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used.",
182206
patternAndEverythingWithCustomMessage:
183207
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
184-
"* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
208+
"* import is invalid because {{importNames}} from '{{importSource}}' {{isOrAre}} restricted from being used by a pattern. {{customMessage}}",
185209
patternAndEverythingWithRegexImportNameAndCustomMessage:
186210
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
187211
"* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used. {{customMessage}}",
188212

189213
everything:
190-
"* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.",
214+
"* import is invalid because {{importNames}} from '{{importSource}}' {{isOrAre}} restricted.",
191215
everythingWithCustomMessage:
192216
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
193-
"* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}",
217+
"* import is invalid because {{importNames}} from '{{importSource}}' {{isOrAre}} restricted. {{customMessage}}",
194218

195219
importName:
196220
"'{{importName}}' import from '{{importSource}}' is restricted.",
@@ -199,16 +223,16 @@ module.exports = {
199223
"'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}",
200224

201225
allowedImportName:
202-
"'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed.",
226+
"'{{importName}}' import from '{{importSource}}' is restricted because only {{allowedImportNames}} {{isOrAre}} allowed.",
203227
allowedImportNameWithCustomMessage:
204228
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
205-
"'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed. {{customMessage}}",
229+
"'{{importName}}' import from '{{importSource}}' is restricted because only {{allowedImportNames}} {{isOrAre}} allowed. {{customMessage}}",
206230

207231
everythingWithAllowImportNames:
208-
"* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed.",
232+
"* import is invalid because only {{allowedImportNames}} from '{{importSource}}' {{isOrAre}} allowed.",
209233
everythingWithAllowImportNamesAndCustomMessage:
210234
// eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
211-
"* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed. {{customMessage}}",
235+
"* import is invalid because only {{allowedImportNames}} from '{{importSource}}' {{isOrAre}} allowed. {{customMessage}}",
212236

213237
allowedImportNamePattern:
214238
"'{{importName}}' import from '{{importSource}}' is restricted because only imports that match the pattern '{{allowedImportNamePattern}}' are allowed from '{{importSource}}'.",
@@ -452,7 +476,10 @@ module.exports = {
452476
loc: specifier.loc,
453477
data: {
454478
importSource,
455-
importNames: restrictedImportNames,
479+
importNames: formatImportNames(
480+
restrictedImportNames,
481+
),
482+
isOrAre: isOrAre(restrictedImportNames),
456483
customMessage,
457484
},
458485
});
@@ -465,7 +492,11 @@ module.exports = {
465492
loc: specifier.loc,
466493
data: {
467494
importSource,
468-
allowedImportNames,
495+
allowedImportNames:
496+
formatImportNames(
497+
allowedImportNames,
498+
),
499+
isOrAre: isOrAre(allowedImportNames),
469500
customMessage,
470501
},
471502
});
@@ -525,7 +556,11 @@ module.exports = {
525556
importSource,
526557
customMessage,
527558
importName,
528-
allowedImportNames,
559+
allowedImportNames:
560+
formatImportNames(
561+
allowedImportNames,
562+
),
563+
isOrAre: isOrAre(allowedImportNames),
529564
},
530565
});
531566
});
@@ -612,7 +647,10 @@ module.exports = {
612647
loc: specifier.loc,
613648
data: {
614649
importSource,
615-
importNames: restrictedImportNames,
650+
importNames: formatImportNames(
651+
restrictedImportNames,
652+
),
653+
isOrAre: isOrAre(restrictedImportNames),
616654
customMessage,
617655
},
618656
});
@@ -625,7 +663,9 @@ module.exports = {
625663
loc: specifier.loc,
626664
data: {
627665
importSource,
628-
allowedImportNames,
666+
allowedImportNames:
667+
formatImportNames(allowedImportNames),
668+
isOrAre: isOrAre(allowedImportNames),
629669
customMessage,
630670
},
631671
});
@@ -713,7 +753,9 @@ module.exports = {
713753
importSource,
714754
customMessage,
715755
importName,
716-
allowedImportNames,
756+
allowedImportNames:
757+
formatImportNames(allowedImportNames),
758+
isOrAre: isOrAre(allowedImportNames),
717759
},
718760
});
719761
});

0 commit comments

Comments
 (0)