-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #390 from juanpcapurro/no-global-imports
[new rule] No global imports
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
warning: "This is a dynamically generated file. Do not edit manually." | ||
layout: "default" | ||
title: "no-global-import | Solhint" | ||
--- | ||
|
||
# no-global-import | ||
![Recommended Badge](https://img.shields.io/badge/-Recommended-brightgreen) | ||
![Category Badge](https://img.shields.io/badge/-Best%20Practise%20Rules-informational) | ||
![Default Severity Badge warn](https://img.shields.io/badge/Default%20Severity-warn-yellow) | ||
> The {"extends": "solhint:recommended"} property in a configuration file enables this rule. | ||
|
||
## Description | ||
Import statement includes an entire file instead of selected symbols | ||
|
||
## Options | ||
This rule accepts a string option of rule severity. Must be one of "error", "warn", "off". Default to warn. | ||
|
||
### Example Config | ||
```json | ||
{ | ||
"rules": { | ||
"no-global-import": "warn" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Examples | ||
### 👍 Examples of **correct** code for this rule | ||
|
||
#### import names explicitly | ||
|
||
```solidity | ||
import {A} from "./A.sol" | ||
``` | ||
|
||
### 👎 Examples of **incorrect** code for this rule | ||
|
||
#### import all members from a file | ||
|
||
```solidity | ||
import * from "foo.sol" | ||
``` | ||
|
||
#### import an entire file | ||
|
||
```solidity | ||
import "foo.sol" | ||
``` | ||
|
||
## Version | ||
This rule is introduced in the latest version. | ||
|
||
## Resources | ||
- [Rule source](https://github.com/protofire/solhint/tree/master/lib/rules/best-practises/no-global-import.js) | ||
- [Document source](https://github.com/protofire/solhint/tree/master/docs/rules/best-practises/no-global-import.md) | ||
- [Test cases](https://github.com/protofire/solhint/tree/master/test/rules/best-practises/no-global-import.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const BaseChecker = require('../base-checker') | ||
|
||
const ruleId = 'no-global-import' | ||
const meta = { | ||
type: 'best-practises', | ||
|
||
docs: { | ||
description: 'Import statement includes an entire file instead of selected symbols', | ||
category: 'Best Practise Rules', | ||
examples: { | ||
bad: [ | ||
{ 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"' }], | ||
}, | ||
}, | ||
|
||
isDefault: false, | ||
recommended: true, | ||
defaultSetup: 'warn', | ||
|
||
schema: null, | ||
} | ||
|
||
class NoGlobalImportsChecker extends BaseChecker { | ||
constructor(reporter) { | ||
super(reporter, ruleId, meta) | ||
} | ||
|
||
ImportDirective(node) { | ||
if (!node.symbolAliases) { | ||
this.error( | ||
node, | ||
`global import of path ${node.path} is not allowed. Specify names to import individually` | ||
) | ||
} | ||
} | ||
} | ||
|
||
module.exports = NoGlobalImportsChecker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const linter = require('../../../lib/index') | ||
const { | ||
assertNoWarnings, | ||
assertLineNumber, | ||
assertErrorMessage, | ||
assertWarnsCount, | ||
} = require('../../common/asserts') | ||
|
||
describe('Linter - no-global-import', () => { | ||
it('should raise on import * from "path"', () => { | ||
const code = `import * from './A.sol';` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'no-global-import': 'warn' }, | ||
}) | ||
assertWarnsCount(report, 1) | ||
assertErrorMessage(report, 'global import') | ||
assertErrorMessage(report, 'Specify names to import individually') | ||
}) | ||
it('should raise on import "path"', () => { | ||
const code = `import './A.sol';` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'no-global-import': 'warn' }, | ||
}) | ||
assertWarnsCount(report, 1) | ||
assertErrorMessage(report, 'global import') | ||
assertErrorMessage(report, 'Specify names to import individually') | ||
}) | ||
it('should raise warning when using solhint:recommended', () => { | ||
const code = `pragma solidity ^0.5.8; import "./A.sol";` | ||
|
||
const report = linter.processStr(code, { | ||
extends: 'solhint:recommended', | ||
}) | ||
assertWarnsCount(report, 1) | ||
assertErrorMessage(report, 'global import') | ||
assertErrorMessage(report, 'Specify names to import individually') | ||
}) | ||
it('should report correct line', () => { | ||
const code = `import {A} from './A.sol'; | ||
import './A.sol';` | ||
|
||
const report = linter.processStr(code, { | ||
rules: { 'no-global-import': 'warn' }, | ||
}) | ||
assertLineNumber(report.reports[0], 2) | ||
assertWarnsCount(report, 1) | ||
}) | ||
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) | ||
}) | ||
}) |