Skip to content

Commit

Permalink
Simplified no-require. Close #88.
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Nov 9, 2015
1 parent f8b557e commit 9a19d1d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This plugin intends to support linting of ES2015+ (ES6+) import/export syntax, a

Helpful warnings:

* Report CommonJS `require` of ES6 module. ([`no-require`](#no-require))
* Report CommonJS `require` calls. ([`no-require`](#no-require))
* Report use of exported name as identifier of default export ([`no-named-as-default`](#no-named-as-default))
* Report repeated import of the same module in multiple places ([`no-duplicates`](#no-duplicates), warning by default)

Expand Down Expand Up @@ -106,10 +106,10 @@ For [ES7], reports if an exported namespace would be empty (no names exported fr

### `no-require`

Reports `require` of modules with ES named or default exports. Off by default.
Reports `require([string])` function calls. Will not report if >1 argument,
or single argument is not a literal string.

Will warn on core modules and unresolved modules, but will not warn on a userland
module that is obviously CommonJS.
Intended for temporary use when migrating to pure ES6 modules.

Given:
```js
Expand All @@ -121,19 +121,11 @@ export function bar() { return foo }
exports.something = 'whatever'
```

...this is valid:
```js
import { foo, bar } from './mod'
const { something } = require('common')

import fs from 'fs' // core module
import { whatever } from './not-found'
```

...and this would be reported:
This would be reported:

```js
var mod = require('./mod')
, common = require('./common')
, fs = require('fs')
, whateverModule = require('./not-found')
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"mocha": "^2.2.1"
},
"peerDependencies": {
"eslint": ">=0.16.0"
"eslint": ">=1.4.0"
},
"dependencies": {
"babel-runtime": "5.x",
Expand Down
12 changes: 5 additions & 7 deletions src/rules/no-require.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Exports from '../core/getExports'

module.exports = function (context) {
return {
'CallExpression': function (call) {
Expand All @@ -12,11 +10,11 @@ module.exports = function (context) {
if (module.type !== 'Literal') return
if (typeof module.value !== 'string') return

var imports = Exports.get(module.value, context)
if (!imports || imports.hasDefault || imports.hasNamed) {
context.report(call.callee,
'CommonJS require of ES module \'' + module.value + '\'.')
}
// keeping it simple: all 1-string-arg `require` calls are reported
context.report({
node: call.callee,
message: `CommonJS require of module '${module.value}'.`
})
}
}
}
22 changes: 13 additions & 9 deletions tests/src/rules/no-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ const ruleTester = new RuleTester()

ruleTester.run('no-require', rule, {
valid:
[ test({ code: "var foo = require('./common');" })
, test({ code: "var bar = require('./bar', true);" })
[ test({ code: "var bar = require('./bar', true);" })
, test({ code: "var bar = proxyquire('./bar');" })
, test({ code: "var bar = require('./ba' + 'r');" })
, test({ code: 'var zero = require(0);' })

, test({ code: 'function () { var bar = require("./bar"); }' })
, test({ code: 'var bar = require("./bar")'
, settings: { 'import/ignore': ['bar'] } })
],

invalid:
[ test({ code: "var bar = require('./bar');"
[ test({ code: "var foo = require('./common');"
, errors: 1 })
// future: maybe a rule option that doesn't report on anything
// but top-level scoped requires?
, test({ code: 'function () { var bar = require("./bar"); }'
, errors: 1 })
, test({ code: 'var bar = require("./bar")'
, settings: { 'import/ignore': ['bar'] }
, errors: 1 })
, test({ code: "var bar = require('./bar');"
, errors:
[ { message: "CommonJS require of ES module './bar'."
[ { message: "CommonJS require of module './bar'."
, type: 'Identifier'
}
]
})
, test({ code: "(function () { var bar = require('./bar'); }());"
, errors:
[ { message: "CommonJS require of ES module './bar'."
[ { message: "CommonJS require of module './bar'."
, type: 'Identifier'
}
]
Expand Down

0 comments on commit 9a19d1d

Please sign in to comment.