Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions eslint-plugin/lib/rules/no-import-all-from-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -74,9 +76,9 @@ module.exports = {
}

if (option.importByNamespaceNotAllowedLibraries) {
notAllowedLibraries.push(
...option.importByNamespaceNotAllowedLibraries,
);
for (const lib of option.importByNamespaceNotAllowedLibraries) {
importByNamespaceNotAllowedLibraries.add(lib);
}
}
}

Expand All @@ -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 },
});
}
Expand Down
16 changes: 8 additions & 8 deletions eslint-plugin/tests/lib/rules/no-import-all-from-library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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],
},
],
};
Expand Down
6 changes: 6 additions & 0 deletions test-project/src/modular-import-from-library.js
Original file line number Diff line number Diff line change
@@ -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("");