Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: import-js/eslint-plugin-import
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.12.1
Choose a base ref
...
head repository: import-js/eslint-plugin-import
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.12.2
Choose a head ref

Commits on Aug 20, 2015

  1. Initial commit

    xjamundx committed Aug 20, 2015
    Copy the full SHA
    9387625 View commit details

Commits on Sep 2, 2015

  1. first commit

    Jamund Ferguson committed Sep 2, 2015
    Copy the full SHA
    c67fb35 View commit details
  2. webstorm added to .gitignore

    Jamund Ferguson committed Sep 2, 2015
    Copy the full SHA
    d67702f View commit details
  3. Catch module.exports typos (fixes #1)

    Jamund Ferguson committed Sep 2, 2015
    Copy the full SHA
    ab9dce2 View commit details
  4. readme

    Jamund Ferguson committed Sep 2, 2015
    Copy the full SHA
    c0d3d2a View commit details

Commits on Oct 9, 2015

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    a5ca81f View commit details
  2. Merge pull request #3 from eventualbuddha/no-mix-default-named

    Add a rule to disallow mixed named and default exports in ES6.
    xjamundx committed Oct 9, 2015
    Copy the full SHA
    2572646 View commit details

Commits on Oct 13, 2015

  1. 1.1.0

    Jamund Ferguson committed Oct 13, 2015
    Copy the full SHA
    f663af0 View commit details
  2. 1.1.1

    Jamund Ferguson committed Oct 13, 2015
    Copy the full SHA
    14fd35a View commit details

Commits on Dec 24, 2015

  1. Copy the full SHA
    a5adc01 View commit details
  2. Merge pull request #152 from benmosher/benmosher-patch-1

    update: add an ignore example for CSS modules
    benmosher committed Dec 24, 2015
    Copy the full SHA
    b617f8f View commit details
  3. Copy the full SHA
    ea43531 View commit details
  4. renames to match layout

    benmosher committed Dec 24, 2015
    Copy the full SHA
    70a3ee7 View commit details
  5. Copy the full SHA
    7f622b2 View commit details

Commits on Dec 25, 2015

  1. Copy the full SHA
    2c08f6b View commit details
  2. Copy the full SHA
    01930a2 View commit details
  3. Copy the full SHA
    ccd9408 View commit details
  4. coverage bump

    benmosher committed Dec 25, 2015
    Copy the full SHA
    8fbdf63 View commit details
  5. Merge pull request #154 from benmosher/modules-merge

    Merge no-cjs, no-define from eslint-plugin-modules
    benmosher committed Dec 25, 2015
    Copy the full SHA
    ed26c39 View commit details

Commits on Dec 26, 2015

  1. first draft: makefile

    benmosher committed Dec 26, 2015
    Copy the full SHA
    3500a27 View commit details
  2. clean extra lib files

    benmosher committed Dec 26, 2015
    Copy the full SHA
    71358dd View commit details
  3. clean extra test files

    benmosher committed Dec 26, 2015
    Copy the full SHA
    9c8c31d View commit details

Commits on Dec 28, 2015

  1. Copy the full SHA
    0bce8c7 View commit details

Commits on Jan 17, 2016

  1. reverted Makefile changes to .editorconfig

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    8813fd5 View commit details
  2. updated npm scripts to hit gulp directly

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    96ed73a View commit details
  3. added tests/src|lib to wipe-extras

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    0c6731b View commit details
  4. moved tests / watch to gulpfile (which now works!)

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    ca08366 View commit details
  5. disabling coveralls until able to resolve

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    85a0455 View commit details
  6. don't read test files

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    53df4e7 View commit details
  7. appveyor fail

    Ben Mosher committed Jan 17, 2016
    Copy the full SHA
    8be5929 View commit details
  8. Merge pull request #156 from benmosher/makefile

    ~Makefile~ gulpfile!
    benmosher committed Jan 17, 2016
    Copy the full SHA
    ff0ef21 View commit details

Commits on Feb 6, 2016

  1. 0.12.2

    benmosher committed Feb 6, 2016
    Copy the full SHA
    de9da58 View commit details
Showing with 392 additions and 122 deletions.
  1. +3 −2 .travis.yml
  2. +7 −4 README.md
  3. +1 −3 appveyor.yml
  4. +35 −0 docs/rules/no-amd.md
  5. +61 −0 docs/rules/no-commonjs.md
  6. +0 −35 docs/rules/no-require.md
  7. +88 −0 gulpfile.js
  8. +11 −8 package.json
  9. +4 −2 src/index.js
  10. +33 −0 src/rules/no-amd.js
  11. +59 −0 src/rules/no-commonjs.js
  12. +0 −20 src/rules/no-require.js
  13. +34 −0 tests/src/rules/no-amd.js
  14. +56 −0 tests/src/rules/no-commonjs.js
  15. +0 −48 tests/src/rules/no-require.js
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -17,5 +17,6 @@ script:
- "npm run-script ci-test"
- "for resolver in ./resolvers/*; do cd $resolver && npm test && cd ../..; done"

after_success:
- npm run coveralls
# something broke remap-istanbul :-(
# after_success:
# - npm run coveralls
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -31,11 +31,13 @@ Helpful warnings:

Style guide:

* Report CommonJS `require` calls. ([`no-require`])
* Report CommonJS `require` calls and `module.exports` or `exports.*`. ([`no-commonjs`])
* Report AMD `require` and `define` calls. ([`no-amd`])
* Ensure all imports appear before other statements ([`imports-first`])
* Report repeated import of the same module in multiple places ([`no-duplicates`])

[`no-require`]: ./docs/rules/no-require.md
[`no-commonjs`]: ./docs/rules/no-commonjs.md
[`no-amd`]: ./docs/rules/no-amd.md
[`imports-first`]: ./docs/rules/imports-first.md
[`no-duplicates`]: ./docs/rules/no-duplicates.md

@@ -172,8 +174,9 @@ may need to include it in your own list if you still want to ignore it. Example:
```yaml
settings:
import/ignore:
- node_modules # mostly CommonJS (ignored by default)
- \.coffee$ # fraught with parse errors
- node_modules # mostly CommonJS (ignored by default)
- \.coffee$ # fraught with parse errors
- \.(scss|less|css)$ # can't parse unprocessed CSS modules, either
```

[`jsnext:main`]: https://github.com/rollup/rollup/wiki/jsnext:main
4 changes: 1 addition & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -38,10 +38,8 @@ test_script:
- npm --version
# Lint
- ./node_modules/.bin/eslint ./src
# Compile
- npm run-script pretest
# core tests
- ./node_modules/.bin/mocha tests/lib/ --recursive --reporter dot
- ./node_modules/.bin/gulp test
# resolver tests
- cd .\resolvers\webpack && npm test && cd ..\..
- cd .\resolvers\node && npm test && cd ..\..
35 changes: 35 additions & 0 deletions docs/rules/no-amd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# no-amd

Reports `require([array], ...)` and `define([array], ...) function calls at the
module scope. Will not report if !=2 arguments, or first argument is not a literal array.

Intended for temporary use when migrating to pure ES6 modules.

## Rule Details

This will be reported:

```js
define(["a", "b"], function (a, b) { /* ... */ })

require(["b", "c"], function (b, c) { /* ... */ })
```

CommonJS `require` is still valid.

## When Not To Use It

If you don't mind mixing module systems (sometimes this is useful), you probably
don't want this rule.

It is also fairly noisy if you have a larger codebase that is being transitioned
from AMD to ES6 modules.

## Contributors

Special thanks to @xjamundx for donating his no-define rule as a start to this.

## Further Reading

- [`no-commonjs`](./no-commonjs.md): report CommonJS `require` and `exports`
- Source: https://github.com/xjamundx/eslint-plugin-modules
61 changes: 61 additions & 0 deletions docs/rules/no-commonjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# no-commonjs

Reports `require([string])` function calls. Will not report if >1 argument,
or single argument is not a literal string.

Reports `module.exports` or `exports.*`, also.

Intended for temporary use when migrating to pure ES6 modules.

## Rule Details

This will be reported:

```js
var mod = require('./mod')
, common = require('./common')
, fs = require('fs')
, whateverModule = require('./not-found')

module.exports = { a: "b" }
exports.c = "d"
```

If `allow-primitive-modules` is provided as an option, the following is valid:

```js
/*eslint no-commonjs: [2, "allow-primitive-modules"]*/

module.exports = "foo"
module.exports = function rule(context) { return { /* ... */ } }
```

but this is still reported:

```js
/*eslint no-commonjs: [2, "allow-primitive-modules"]*/

module.exports = { x: "y" }
exports.z = function boop() { /* ... */ }
```

This is useful for things like ESLint rule modules, which must export a function as
the module.

## When Not To Use It

If you don't mind mixing module systems (sometimes this is useful), you probably
don't want this rule.

It is also fairly noisy if you have a larger codebase that is being transitioned
from CommonJS to ES6 modules.


## Contributors

Special thanks to @xjamundx for donating the module.exports and exports.* bits.

## Further Reading

- [`no-amd`](./no-amd.md): report on AMD `require`, `define`
- Source: https://github.com/xjamundx/eslint-plugin-modules
35 changes: 0 additions & 35 deletions docs/rules/no-require.md

This file was deleted.

88 changes: 88 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var gulp = require('gulp')
, changed = require('gulp-changed')
, babel = require('gulp-babel')
, mocha = require('gulp-mocha')
, path = require('path')
, glob = require('glob')
, fs = require('fs')

var SRC = 'src/**/*.js'
, DEST = 'lib'

gulp.task('src', function () {
return gulp.src(SRC)
.pipe(changed(DEST))
.pipe(babel())
.pipe(gulp.dest(DEST))
})

/**
* Delete any file under `dest` that has no corresponding file in `src`.
* I.E. remove generated files that have been orphaned via deletion of their source.
* @param {string} src
* @param {string} dest
* @param {Function} done - callback upon completion
*/
function wipeExtras(src, dest, done) {
// glob into 'lib' and delete whatever isn't there
glob(dest + '/**/*.js', function (err, files) {
if (err) {
done(err); return
}

function checkFile(index) {
if (index >= files.length) {
done(); return
}

var libFilename = files[index]
, srcFilename = path.resolve(src, path.relative(path.resolve(dest), libFilename))

fs.stat(srcFilename, function (err) {
if (err) {
fs.unlink(libFilename, function () {
checkFile(index + 1)
})
} else {
checkFile(index + 1)
}
})
}


checkFile(0)
})
}

gulp.task('wipe-extras', function (done) {
var unfinished = 2
function megadone(err) {
if (err) { done(err); return }
if (--unfinished === 0) done()
}
wipeExtras('src', DEST, megadone)
wipeExtras('tests/src', 'tests/lib', megadone)
})

gulp.task('prepublish', ['src', 'wipe-extras'])

gulp.task('tests', function () {
return gulp.src('tests/src/**/*.js')
.pipe(changed('tests/lib'))
.pipe(babel())
.pipe(gulp.dest('tests/lib'))
})

// used externally by Istanbul, too
gulp.task('pretest', ['src', 'tests', 'wipe-extras'])

gulp.task('test', ['pretest'], function () {
return gulp.src('tests/lib/**/*.js', { read: false })
.pipe(mocha({ reporter: 'dot' }))
// NODE_PATH=./lib mocha --recursive --reporter dot tests/lib/
})

gulp.task('watch-test', function () {
gulp.watch(SRC, ['test'])
gulp.watch('tests/' + SRC, ['test'])
})
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"name": "eslint-plugin-import",
"version": "0.12.1",
"version": "0.12.2",
"description": "Import with sanity.",
"main": "lib/index.js",
"directories": {
"test": "tests"
},
"scripts": {
"watch": "babel -d lib/ src/ --watch & mocha --recursive --reporter dot --compilers js:babel/register --watch tests/src/",
"cover": "npm run pretest && NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot; remap-istanbul -i reports/coverage/coverage.json -o reports/coverage/lcov-report --type html",
"test": "NODE_PATH=./lib mocha --recursive --reporter dot tests/lib/",
"ci-test": "eslint ./src && npm run-script pretest && istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot",
"watch": "NODE_PATH=./lib gulp watch-test",
"cover": "gulp pretest && NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive -R progress",
"test": "NODE_PATH=./lib gulp test",
"ci-test": "eslint ./src && gulp pretest && istanbul cover --report lcovonly --dir reports/coverage _mocha tests/lib/ -- --recursive --reporter dot",
"debug": "NODE_PATH=./lib mocha debug --recursive --reporter dot tests/lib/",
"compile": "rm -rf lib/ && babel -d lib/ src/",
"prepublish": "eslint ./src && npm run compile",
"pretest": "npm run compile && babel -d tests/lib/ tests/src/",
"prepublish": "eslint ./src && gulp prepublish",
"coveralls": "remap-istanbul -i reports/coverage/coverage.json -o reports/coverage/lcov.info --type lcovonly && cat ./reports/coverage/lcov.info | coveralls"
},
"repository": {
@@ -44,6 +42,11 @@
"eslint": ">=1.8.0",
"eslint-import-resolver-node": "file:./resolvers/node",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
"glob": "^6.0.2",
"gulp": "^3.9.0",
"gulp-babel": "5.x",
"gulp-changed": "^1.3.0",
"gulp-mocha": "^2.2.0",
"istanbul": "^0.4.0",
"mocha": "^2.2.1",
"redux": "^3.0.4",
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@ export const rules = {

'no-named-as-default': require('./rules/no-named-as-default'),

'no-require': require('./rules/no-require'),
'no-commonjs': require('./rules/no-commonjs'),
'no-amd': require('./rules/no-amd'),
'no-duplicates': require('./rules/no-duplicates'),
'imports-first': require('./rules/imports-first'),
}
@@ -21,7 +22,8 @@ export const rulesConfig = {

'no-named-as-default': 0,

'no-require': 0,
'no-commonjs': 0,
'no-amd': 0,
'no-duplicates': 0,
'imports-first': 0,
}
33 changes: 33 additions & 0 deletions src/rules/no-amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @fileoverview Rule to prefer imports to AMD
* @author Jamund Ferguson
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function (context) {

return {

'CallExpression': function (node) {
if (context.getScope().type !== 'module') return

if (node.callee.type !== 'Identifier') return
if (node.callee.name !== 'require' &&
node.callee.name !== 'define') return

// todo: capture define((require, module, exports) => {}) form?
if (node.arguments.length !== 2) return

const modules = node.arguments[0]
if (modules.type !== 'ArrayExpression') return

// todo: check second arg type? (identifier or callback)

context.report(node, `Expected imports instead of AMD ${node.callee.name}().`)
},
}

}
59 changes: 59 additions & 0 deletions src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @fileoverview Rule to prefer ES6 to CJS
* @author Jamund Ferguson
*/

const EXPORT_MESSAGE = 'Expected "export" or "export default"'
, IMPORT_MESSAGE = 'Expected "import" instead of "require()"'

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------


module.exports = function (context) {

return {

'MemberExpression': function (node) {

// module.exports
if (node.object.name === 'module' && node.property.name === 'exports') {
if (allowPrimitive(node, context)) return
context.report({ node, message: EXPORT_MESSAGE })
}

// exports.
if (node.object.name === 'exports') {
context.report({ node, message: EXPORT_MESSAGE })
}

},
'CallExpression': function (call) {
if (context.getScope().type !== 'module') return

if (call.callee.type !== 'Identifier') return
if (call.callee.name !== 'require') return

if (call.arguments.length !== 1) return
var module = call.arguments[0]

if (module.type !== 'Literal') return
if (typeof module.value !== 'string') return

// keeping it simple: all 1-string-arg `require` calls are reported
context.report({
node: call.callee,
message: IMPORT_MESSAGE,
})
},
}

}

// allow non-objects as module.exports
function allowPrimitive(node, context) {
if (context.options.indexOf('allow-primitive-modules') < 0) return false
if (node.parent.type !== 'AssignmentExpression') return false
return (node.parent.right.type !== 'ObjectExpression')
}
20 changes: 0 additions & 20 deletions src/rules/no-require.js

This file was deleted.

34 changes: 34 additions & 0 deletions tests/src/rules/no-amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { RuleTester } from 'eslint'

var ruleTester = new RuleTester()

ruleTester.run('no-amd', require('rules/no-amd'), {
valid: [
{ code: 'import "x";', ecmaFeatures: { modules: true } },
{ code: 'import x from "x"', ecmaFeatures: { modules: true } },
'var x = require("x")',
'require("x")',
// 2-args, not an array
'require("x", "y")',
// random other function
'setTimeout(foo, 100)',
// non-identifier callee
'(a || b)(1, 2, 3)',

// nested scope is fine
'function x() { define(["a"], function (a) {}) }',
'function x() { require(["a"], function (a) {}) }',

// unmatched arg types/number
'define(0, 1, 2)',
'define("a")',
],

invalid: [
{ code: 'define([], function() {})', errors: [ { message: 'Expected imports instead of AMD define().' }] },
{ code: 'define(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD define().' }] },

{ code: 'require([], function() {})', errors: [ { message: 'Expected imports instead of AMD require().' }] },
{ code: 'require(["a"], function(a) { console.log(a); })', errors: [ { message: 'Expected imports instead of AMD require().' }] },
],
})
56 changes: 56 additions & 0 deletions tests/src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { RuleTester } from 'eslint'

const EXPORT_MESSAGE = 'Expected "export" or "export default"'
, IMPORT_MESSAGE = 'Expected "import" instead of "require()"'

const ruleTester = new RuleTester()

ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
valid: [

// imports
{ code: 'import "x";', ecmaFeatures: { modules: true } },
{ code: 'import x from "x"', ecmaFeatures: { modules: true } },
{ code: 'import x from "x"', ecmaFeatures: { modules: true } },
{ code: 'import { x } from "x"', ecmaFeatures: { modules: true } },

// exports
{ code: 'export default "x"', ecmaFeatures: { modules: true } },
{ code: 'export function house() {}', ecmaFeatures: { modules: true } },

// allowed requires
{ code: 'function a() { var x = require("y"); }' }, // nested requires allowed
{ code: 'require.resolve("help")' }, // methods of require are allowed
{ code: 'require.ensure([])' }, // webpack specific require.ensure is allowed
{ code: 'require([], function(a, b, c) {})' }, // AMD require is allowed
{ code: "var bar = require('./bar', true);" },
{ code: "var bar = proxyquire('./bar');" },
{ code: "var bar = require('./ba' + 'r');" },
{ code: 'var zero = require(0);' },

{ code: 'module.exports = function () {}', options: ['allow-primitive-modules'] },
{ code: 'module.exports = "foo"', options: ['allow-primitive-modules'] },
],

invalid: [

// imports
{ code: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },

// exports
{ code: 'exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports = face', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'exports = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'var x = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports = {}',
options: ['allow-primitive-modules'],
errors: [ { message: EXPORT_MESSAGE }],
},
{ code: 'var x = module.exports',
options: ['allow-primitive-modules'],
errors: [ { message: EXPORT_MESSAGE }],
},
],
})
48 changes: 0 additions & 48 deletions tests/src/rules/no-require.js

This file was deleted.