-
-
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.
- Loading branch information
Showing
6 changed files
with
105 additions
and
0 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,31 @@ | ||
# No Node.js builtin modules | ||
|
||
Forbid the use of Node.js builtin modules. Can be useful for client-side web projects that do not have access to those modules. | ||
|
||
## Rule Details | ||
|
||
### Fail | ||
|
||
```js | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
var fs = require('fs'); | ||
var path = require('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'); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you have a project that is run mainly or partially using Node.js. |
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) === 'builtin') { | ||
context.report(node, 'Do not import Node.js builtin modules') | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
, rule = require('rules/no-nodejs-modules') | ||
|
||
const errors = [{ | ||
ruleId: 'no-nodejs-modules', | ||
message: 'Do not import Node.js builtin modules', | ||
}] | ||
|
||
ruleTester.run('no-nodejs-modules', 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")'}), | ||
], | ||
invalid: [ | ||
test({ | ||
code: 'import path from "path"', | ||
errors, | ||
}), | ||
test({ | ||
code: 'import fs from "fs"', | ||
errors, | ||
}), | ||
test({ | ||
code: 'var path = require("path")', | ||
errors, | ||
}), | ||
test({ | ||
code: 'var fs = require("fs")', | ||
errors, | ||
}), | ||
], | ||
}) |