-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-absolute-path
rule (fixes #530)
#538
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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], | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does 'node.js' mean here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That means typo 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed