-
-
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.
Browse files
Browse the repository at this point in the history
* Add `no-absolute-path` rule (fixes #530) * fix typo
- Loading branch information
Showing
8 changed files
with
155 additions
and
2 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,27 @@ | ||
# Forbid import of modules using absolute paths | ||
|
||
Node.js allows the import of modules using an absolute path such as `/home/xyz/file.js`. That is a bad practice as it ties the code using it to your computer, and therefore makes it unusable in packages distributed on `npm` for instance. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
|
||
```js | ||
import f from '/foo'; | ||
import f from '/some/path'; | ||
|
||
var f = require('/foo'); | ||
var f = require('/some/path'); | ||
``` | ||
|
||
### Pass | ||
|
||
```js | ||
import _ from 'lodash'; | ||
import foo from 'foo'; | ||
import foo from './foo'; | ||
|
||
var _ = require('lodash'); | ||
var foo = require('foo'); | ||
var foo = 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
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,21 @@ | ||
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') | ||
} | ||
} | ||
|
||
module.exports = 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) | ||
} | ||
}, | ||
} | ||
} |
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,86 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-absolute-path') | ||
|
||
const error = { | ||
ruleId: 'no-absolute-path', | ||
message: 'Do not import modules using an absolute path', | ||
} | ||
|
||
ruleTester.run('no-absolute-path', rule, { | ||
valid: [ | ||
test({ code: 'import _ from "lodash"'}), | ||
test({ code: 'import find from "lodash.find"'}), | ||
test({ code: 'import foo from "./foo"'}), | ||
test({ code: 'import foo from "../foo"'}), | ||
test({ code: 'import foo from "foo"'}), | ||
test({ code: 'import foo from "./"'}), | ||
test({ code: 'import foo from "@scope/foo"'}), | ||
test({ code: 'var _ = require("lodash")'}), | ||
test({ code: 'var find = require("lodash.find")'}), | ||
test({ code: 'var foo = require("./foo")'}), | ||
test({ code: 'var foo = require("../foo")'}), | ||
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 path from "path"', | ||
options: [{ | ||
allow: ['path'], | ||
}], | ||
}), | ||
test({ | ||
code: 'var events = require("events")', | ||
options: [{ | ||
allow: ['events'], | ||
}], | ||
}), | ||
test({ | ||
code: 'var path = require("path")', | ||
options: [{ | ||
allow: ['path'], | ||
}], | ||
}), | ||
test({ | ||
code: 'import path from "path";import events from "events"', | ||
options: [{ | ||
allow: ['path', 'events'], | ||
}], | ||
}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import f from "/foo"', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'import f from "/foo/path"', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'import f from "/some/path"', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'var f = require("/foo")', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'var f = require("/foo/path")', | ||
errors: [error], | ||
}), | ||
test({ | ||
code: 'var f = require("/some/path")', | ||
errors: [error], | ||
}), | ||
], | ||
}) |