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: v2.6.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: v2.7.0
Choose a head ref
  • 7 commits
  • 6 files changed
  • 2 contributors

Commits on May 24, 2017

  1. Extract isAbsolutePath from importTypes helper

    importTypes helper performs resolve() - which does not look needed for the no-absolute-path rule.
    
    Additionally, the absolute condition only seems like it was used for no-absolute-path.
    
    Finally, all the tests continue to pass.
    Joachim Seminck committed May 24, 2017
    Copy the full SHA
    3e8438e View commit details
  2. Rename funtion reportIfMissing to reportIfAbsolute

    The old name seems wrong. Probably copied from no-extraneuous-dependencies
    Joachim Seminck committed May 24, 2017
    Copy the full SHA
    5aa2fe0 View commit details

Commits on Jul 6, 2017

  1. PR note fixes

    benmosher committed Jul 6, 2017
    Copy the full SHA
    e4b8884 View commit details
  2. Copy the full SHA
    f70d127 View commit details
  3. Copy the full SHA
    0dc4451 View commit details
  4. changelog note for #843

    benmosher committed Jul 6, 2017
    Copy the full SHA
    ee5a986 View commit details
  5. bump to v2.7.0

    benmosher committed Jul 6, 2017
    Copy the full SHA
    c9dd91d View commit details
Showing with 81 additions and 44 deletions.
  1. +9 −1 CHANGELOG.md
  2. +21 −0 docs/rules/no-absolute-path.md
  3. +2 −2 package.json
  4. +1 −1 src/core/importType.js
  5. +10 −17 src/rules/no-absolute-path.js
  6. +38 −23 tests/src/rules/no-absolute-path.js
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]


## [2.7.0] - 2017-07-06
### Changed
- [`no-absolute-path`] picks up speed boost, optional AMD support ([#843], thansk [@jseminck])

## [2.6.1] - 2017-06-29
### Fixed
- update bundled node resolver dependency to latest version
@@ -415,6 +420,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#843]: https://github.com/benmosher/eslint-plugin-import/pull/843
[#871]: https://github.com/benmosher/eslint-plugin-import/pull/871
[#742]: https://github.com/benmosher/eslint-plugin-import/pull/742
[#737]: https://github.com/benmosher/eslint-plugin-import/pull/737
@@ -536,7 +542,8 @@ for info on changes for earlier releases.
[#119]: https://github.com/benmosher/eslint-plugin-import/issues/119
[#89]: https://github.com/benmosher/eslint-plugin-import/issues/89

[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.6.1...HEAD
[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.7.0...HEAD
[2.7.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.6.1...v2.7.0
[2.6.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.6.0...v2.6.1
[2.6.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.5.0...v2.6.0
[2.5.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.4.0...v2.5.0
@@ -624,3 +631,4 @@ for info on changes for earlier releases.
[@kevin940726]: https://github.com/kevin940726
[@eelyafi]: https://github.com/eelyafi
[@mastilver]: https://github.com/mastilver
[@jseminck]: https://github.com/jseminck
21 changes: 21 additions & 0 deletions docs/rules/no-absolute-path.md
Original file line number Diff line number Diff line change
@@ -25,3 +25,24 @@ var _ = require('lodash');
var foo = require('foo');
var foo = require('./foo');
```

### Options

By default, only ES6 imports and CommonJS `require` calls will have this rule enforced.

You may provide an options object providing true/false for any of

- `esmodule`: defaults to `true`
- `commonjs`: defaults to `true`
- `amd`: defaults to `false`

If `{ amd: true }` is provided, dependency paths for AMD-style `define` and `require`
calls will be resolved:

```js
/*eslint import/no-absolute-path: [2, { commonjs: false, amd: true }]*/
define(['/foo'], function (foo) { /*...*/ }) // reported
require(['/foo'], function (foo) { /*...*/ }) // reported

const foo = require('/foo') // ignored because of explicit `commonjs: false`
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "2.6.1",
"version": "2.7.0",
"description": "Import with sanity.",
"engines": {
"node": ">=4"
@@ -82,7 +82,7 @@
"debug": "^2.6.8",
"doctrine": "1.5.0",
"eslint-import-resolver-node": "^0.3.1",
"eslint-module-utils": "^2.0.0",
"eslint-module-utils": "^2.1.1",
"has": "^1.0.1",
"lodash.cond": "^4.3.0",
"minimatch": "^3.0.3",
2 changes: 1 addition & 1 deletion src/core/importType.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ function constant(value) {
return () => value
}

function isAbsolute(name) {
export function isAbsolute(name) {
return name.indexOf('/') === 0
}

27 changes: 10 additions & 17 deletions src/rules/no-absolute-path.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import importType from '../core/importType'
import isStaticRequire from '../core/staticRequire'

function reportIfMissing(context, node, name) {
if (importType(name, context) === 'absolute') {
context.report(node, 'Do not import modules using an absolute path')
}
}
import { isAbsolute } from '../core/importType'
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor'

module.exports = {
meta: {
docs: {},
schema: [ makeOptionsSchema() ],
},

create: function (context) {
return {
ImportDeclaration: function handleImports(node) {
reportIfMissing(context, node, node.source.value)
},
CallExpression: function handleRequires(node) {
if (isStaticRequire(node)) {
reportIfMissing(context, node, node.arguments[0].value)
}
},
function reportIfAbsolute(source) {
if (isAbsolute(source.value)) {
context.report(source, 'Do not import modules using an absolute path')
}
}

const options = Object.assign({ esmodule: true, commonjs: true }, context.options[0])
return moduleVisitor(reportIfAbsolute, options)
},
}
61 changes: 38 additions & 23 deletions tests/src/rules/no-absolute-path.js
Original file line number Diff line number Diff line change
@@ -26,35 +26,29 @@ ruleTester.run('no-absolute-path', rule, {
test({ code: 'var foo = require("foo")'}),
test({ code: 'var foo = require("./")'}),
test({ code: 'var foo = require("@scope/foo")'}),
test({
code: 'import events from "events"',
options: [{
allow: ['events'],
}],
}),

test({ code: 'import events from "events"' }),
test({ code: 'import path from "path"' }),
test({ code: 'var events = require("events")' }),
test({ code: 'var path = require("path")' }),
test({ code: 'import path from "path";import events from "events"' }),

// still works if only `amd: true` is provided
test({
code: 'import path from "path"',
options: [{
allow: ['path'],
}],
}),
test({
code: 'var events = require("events")',
options: [{
allow: ['events'],
}],
options: [{ amd: true }],
}),

// amd not enabled by default
test({ code: 'require(["/some/path"], function (f) { /* ... */ })' }),
test({ code: 'define(["/some/path"], function (f) { /* ... */ })' }),
test({
code: 'var path = require("path")',
options: [{
allow: ['path'],
}],
code: 'require(["./some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
}),
test({
code: 'import path from "path";import events from "events"',
options: [{
allow: ['path', 'events'],
}],
code: 'define(["./some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
}),
],
invalid: [
@@ -70,6 +64,11 @@ ruleTester.run('no-absolute-path', rule, {
code: 'import f from "/some/path"',
errors: [error],
}),
test({
code: 'import f from "/some/path"',
options: [{ amd: true }],
errors: [error],
}),
test({
code: 'var f = require("/foo")',
errors: [error],
@@ -82,5 +81,21 @@ ruleTester.run('no-absolute-path', rule, {
code: 'var f = require("/some/path")',
errors: [error],
}),
test({
code: 'var f = require("/some/path")',
options: [{ amd: true }],
errors: [error],
}),
// validate amd
test({
code: 'require(["/some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
errors: [error],
}),
test({
code: 'define(["/some/path"], function (f) { /* ... */ })',
options: [{ amd: true }],
errors: [error],
}),
],
})