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 fc2cfee..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); + } } } @@ -91,10 +93,18 @@ module.exports = { (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 e04fc92..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: [ @@ -59,25 +59,25 @@ const tests = { 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("");