From b251b76a8ddffb99ec038f348c74d97b1777257c Mon Sep 17 00:00:00 2001 From: Mhammed Emrabet Date: Tue, 19 May 2026 17:26:49 +0200 Subject: [PATCH 1/2] fix(no-import-all-from-library): allow named imports from forbidden libraries --- eslint-plugin/lib/rules/no-import-all-from-library.js | 8 +++++++- .../tests/lib/rules/no-import-all-from-library.test.js | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/eslint-plugin/lib/rules/no-import-all-from-library.js b/eslint-plugin/lib/rules/no-import-all-from-library.js index 3073e0e..af966c1 100644 --- a/eslint-plugin/lib/rules/no-import-all-from-library.js +++ b/eslint-plugin/lib/rules/no-import-all-from-library.js @@ -84,7 +84,13 @@ module.exports = { ImportDeclaration(node) { const currentLibrary = node.source.value; - const forbiddenByName = notAllowedLibraries.includes(currentLibrary); + const forbiddenByName = + notAllowedLibraries.includes(currentLibrary) && + node.specifiers.some( + (specifier) => + specifier.type === "ImportDefaultSpecifier" || + specifier.type === "ImportNamespaceSpecifier", + ); const forbiddenByNamespace = importByNamespaceNotAllowedLibraries.includes(currentLibrary) && node.specifiers.some( diff --git a/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js b/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js index e04fc92..f1a44e2 100644 --- a/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js +++ b/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js @@ -54,6 +54,12 @@ const tests = { ` import map from 'underscore/modules/map.js'; `, + ` + import { memoize, omitBy, isNil } from 'lodash'; + `, + ` + import { isEmpty } from 'underscore'; + `, ], invalid: [ From f09a3a78d31b544db48c1f47f3db6cf110807e34 Mon Sep 17 00:00:00 2001 From: utarwyn Date: Sun, 19 Jul 2026 11:10:42 +0200 Subject: [PATCH 2/2] fix(no-import-all-from-library): update error messages for clarity --- CHANGELOG.md | 1 + .../lib/rules/no-import-all-from-library.js | 32 +++++++++++-------- .../rules/no-import-all-from-library.test.js | 22 +++++-------- .../src/modular-import-from-library.js | 6 ++++ 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73b4322..e8767d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [#112](https://github.com/green-code-initiative/creedengo-javascript/pull/112) Clarify rule GCI9 "no-import-all-from-library" - [#113](https://github.com/green-code-initiative/creedengo-javascript/pull/113) Extend rule GCI530 "no-torch" to detect HTML5 Web API usage ## [3.1.0] - 2026-05-10 diff --git a/eslint-plugin/lib/rules/no-import-all-from-library.js b/eslint-plugin/lib/rules/no-import-all-from-library.js index 60f059f..9b6f2a7 100644 --- a/eslint-plugin/lib/rules/no-import-all-from-library.js +++ b/eslint-plugin/lib/rules/no-import-all-from-library.js @@ -28,8 +28,10 @@ module.exports = { recommended: "warn", }, messages: { - ShouldNotImportAllFromLibrary: - "You should not import all from library {{library}}", + doNotImportFromLibrary: + "Avoid importing from the main library path {{library}}, use specific subpaths instead", + doNotUseNamespaceImport: + "Avoid namespace imports from {{library}}, use named imports instead", }, schema: [ { @@ -74,9 +76,9 @@ module.exports = { } if (option.importByNamespaceNotAllowedLibraries) { - notAllowedLibraries.push( - ...option.importByNamespaceNotAllowedLibraries, - ); + for (const lib of option.importByNamespaceNotAllowedLibraries) { + importByNamespaceNotAllowedLibraries.add(lib); + } } } @@ -84,23 +86,25 @@ module.exports = { ImportDeclaration(node) { const currentLibrary = node.source.value; - const forbiddenByName = - notAllowedLibraries.includes(currentLibrary) && - node.specifiers.some( - (specifier) => - specifier.type === "ImportDefaultSpecifier" || - specifier.type === "ImportNamespaceSpecifier", - ); + const forbiddenByName = notAllowedLibraries.includes(currentLibrary); const forbiddenByNamespace = importByNamespaceNotAllowedLibraries.has(currentLibrary) && node.specifiers.some( (specifier) => specifier.type === "ImportNamespaceSpecifier", ); - if (forbiddenByName || forbiddenByNamespace) { + if (forbiddenByName) { + context.report({ + node, + messageId: "doNotImportFromLibrary", + data: { library: currentLibrary }, + }); + } + + if (forbiddenByNamespace) { context.report({ node, - messageId: "ShouldNotImportAllFromLibrary", + messageId: "doNotUseNamespaceImport", data: { library: currentLibrary }, }); } diff --git a/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js b/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js index f1a44e2..7287498 100644 --- a/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js +++ b/eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js @@ -36,9 +36,9 @@ const ruleTester = new RuleTester({ sourceType: "module", }, }); -const expectedError = { - messageId: "ShouldNotImportAllFromLibrary", -}; + +const doNotImportFromLibraryError = { messageId: "doNotImportFromLibrary" }; +const doNotUseNamespaceImportError = { messageId: "doNotUseNamespaceImport" }; const tests = { valid: [ @@ -54,36 +54,30 @@ const tests = { ` import map from 'underscore/modules/map.js'; `, - ` - import { memoize, omitBy, isNil } from 'lodash'; - `, - ` - import { isEmpty } from 'underscore'; - `, ], invalid: [ { code: "import lodash from 'lodash';", - errors: [expectedError], + errors: [doNotImportFromLibraryError], }, { code: "import * as lodash from 'lodash';", - errors: [expectedError], + errors: [doNotImportFromLibraryError], }, { code: "import * as lodash from 'lodash-es';", - errors: [expectedError], + errors: [doNotUseNamespaceImportError], }, { code: "import someLib from 'some-lib';", options: [{ notAllowedLibraries: ["some-lib"] }], - errors: [expectedError], + errors: [doNotImportFromLibraryError], }, { code: "import * as someLib from 'some-lib';", options: [{ importByNamespaceNotAllowedLibraries: ["some-lib"] }], - errors: [expectedError], + errors: [doNotUseNamespaceImportError], }, ], }; diff --git a/test-project/src/modular-import-from-library.js b/test-project/src/modular-import-from-library.js index 7db1abd..cd041f8 100644 --- a/test-project/src/modular-import-from-library.js +++ b/test-project/src/modular-import-from-library.js @@ -1,3 +1,9 @@ +import { orderBy } from "lodash"; // Non-compliant: import of the main path of a bad library + +import * as _ from "underscore"; // Non-compliant: import by namespace of a bad library + import isEmpty from "lodash/isEmpty"; // Compliant +orderBy([], [], []); +_.allKeys([]); isEmpty("");