From c62387a5b480ee075e1e1a0e1b0655d1ff147463 Mon Sep 17 00:00:00 2001 From: capu Date: Mon, 27 Feb 2023 17:25:29 -0300 Subject: [PATCH] don't cause error on named global import fixes #414 update docs as well --- lib/rules/best-practises/no-global-import.js | 10 +++++++--- test/rules/best-practises/no-global-import.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/rules/best-practises/no-global-import.js b/lib/rules/best-practises/no-global-import.js index d9ae75fd..2f3e27b9 100644 --- a/lib/rules/best-practises/no-global-import.js +++ b/lib/rules/best-practises/no-global-import.js @@ -12,7 +12,11 @@ const meta = { { description: 'import all members from a file', code: 'import * from "foo.sol"' }, { description: 'import an entire file', code: 'import "foo.sol"' }, ], - good: [{ description: 'import names explicitly', code: 'import {A} from "./A.sol"' }], + good: [ + { description: 'import names explicitly', code: 'import {A} from "./A.sol"' }, + { description: 'import entire file into a name', code: 'import "./A.sol" as A' }, + { description: 'import entire file into a name', code: 'import * as A from "./A.sol"' }, + ], }, }, @@ -29,10 +33,10 @@ class NoGlobalImportsChecker extends BaseChecker { } ImportDirective(node) { - if (!node.symbolAliases) { + if (!(node.symbolAliases || node.unitAlias)) { this.error( node, - `global import of path ${node.path} is not allowed. Specify names to import individually` + `global import of path ${node.path} is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)` ) } } diff --git a/test/rules/best-practises/no-global-import.js b/test/rules/best-practises/no-global-import.js index 199f8c02..3ab26a1b 100644 --- a/test/rules/best-practises/no-global-import.js +++ b/test/rules/best-practises/no-global-import.js @@ -50,6 +50,22 @@ describe('Linter - no-global-import', () => { it('should not raise on import {identifier} from "path"', () => { const code = `import {A} from './A.sol';` + const report = linter.processStr(code, { + rules: { 'no-global-import': 'warn' }, + }) + assertNoWarnings(report) + }) + it('should not raise on import from "path" as NAME', () => { + const code = `import "../../src/Helpers.sol" as H;` + + const report = linter.processStr(code, { + rules: { 'no-global-import': 'warn' }, + }) + assertNoWarnings(report) + }) + it('should not raise on import * as NAME from "path" as NAME', () => { + const code = `import * as H from "../../src/Helpers.sol";` + const report = linter.processStr(code, { rules: { 'no-global-import': 'warn' }, })