Skip to content

Commit

Permalink
Merge branch 'no-absolute-path-perf'
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jul 6, 2017
2 parents d9b712a + f70d127 commit 0dc4451
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 41 deletions.
21 changes: 21 additions & 0 deletions docs/rules/no-absolute-path.md
Expand Up @@ -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`
```
2 changes: 1 addition & 1 deletion src/core/importType.js
Expand Up @@ -8,7 +8,7 @@ function constant(value) {
return () => value
}

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

Expand Down
27 changes: 10 additions & 17 deletions src/rules/no-absolute-path.js
@@ -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
Expand Up @@ -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: [
Expand All @@ -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],
Expand All @@ -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],
}),
],
})

0 comments on commit 0dc4451

Please sign in to comment.