-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 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
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,37 @@ | ||
# Forbid unassigned imports | ||
|
||
With both CommonJS' `require` and the ES6 modules' `import` syntax, it is possible to import a module but not to use its result. This can be done explicitly by not assigning the module to as variable. Doing so can mean either of the following things: | ||
- The module is imported but not used | ||
- The module has side-effects (like [`should`](https://www.npmjs.com/package/should)). Having side-effects, makes it hard to know whether the module is actually used or can be removed. It can also make it harder to test or mock parts of your application. | ||
|
||
This rule aims to remove modules with side-effects by reporting when a module is imported but not assigned. | ||
|
||
## Fail | ||
|
||
```js | ||
import 'should' | ||
require('should') | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
import _ from 'foo' | ||
import _, {foo} from 'foo' | ||
import _, {foo as bar} from 'foo' | ||
import {foo as bar} from 'foo' | ||
import * as _ from 'foo' | ||
|
||
const _ = require('foo') | ||
const {foo} = require('foo') | ||
const {foo: bar} = require('foo') | ||
const [a, b] = require('foo') | ||
const _ = require('foo') | ||
|
||
// Module is not assigned, but it is used | ||
bar(require('foo')) | ||
require('foo').bar | ||
require('foo').bar() | ||
require('foo')() | ||
``` |
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 @@ | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
function report(context, node) { | ||
context.report({ | ||
node, | ||
message: 'Imported module should be assigned', | ||
}) | ||
} | ||
|
||
function create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
if (node.specifiers.length === 0) { | ||
report(context, node) | ||
} | ||
}, | ||
ExpressionStatement(node) { | ||
if (node.expression.type === 'CallExpression' && isStaticRequire(node.expression)) { | ||
report(context, node.expression) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: {}, | ||
schema: [ | ||
{ | ||
'type': 'object', | ||
'properties': { | ||
'devDependencies': { 'type': ['boolean', 'array'] }, | ||
'optionalDependencies': { 'type': ['boolean', 'array'] }, | ||
'peerDependencies': { 'type': ['boolean', 'array'] }, | ||
}, | ||
'additionalProperties': false, | ||
}, | ||
], | ||
}, | ||
} |
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,43 @@ | ||
import { test } from '../utils' | ||
import * as path from 'path' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-unassigned-import') | ||
|
||
const error = { | ||
ruleId: 'no-unassigned-import', | ||
message: 'Imported module should be assigned' | ||
} | ||
|
||
ruleTester.run('no-unassigned-import', rule, { | ||
valid: [ | ||
test({ code: 'import _ from "lodash"'}), | ||
test({ code: 'import _, {foo} from "lodash"'}), | ||
test({ code: 'import _, {foo as bar} from "lodash"'}), | ||
test({ code: 'import {foo as bar} from "lodash"'}), | ||
test({ code: 'import * as _ from "lodash"'}), | ||
test({ code: 'import _ from "./"'}), | ||
test({ code: 'const _ = require("lodash")'}), | ||
test({ code: 'const {foo} = require("lodash")'}), | ||
test({ code: 'const {foo: bar} = require("lodash")'}), | ||
test({ code: 'const [a, b] = require("lodash")'}), | ||
test({ code: 'const _ = require("lodash")'}), | ||
test({ code: 'const _ = require("./")'}), | ||
test({ code: 'foo(require("lodash"))'}), | ||
test({ code: 'require("lodash").foo'}), | ||
test({ code: 'require("lodash").foo()'}), | ||
test({ code: 'require("lodash")()'}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import "lodash"', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'require("lodash")', | ||
errors: [error], | ||
}), | ||
], | ||
}) |