forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
global-require.js
65 lines (61 loc) · 2.03 KB
/
global-require.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @author Jamund Ferguson
* See LICENSE file in root directory for full license.
*/
"use strict"
const RuleTester = require("#test-helpers").RuleTester
const rule = require("../../../lib/rules/global-require")
const ERROR = { messageId: "unexpected", type: "CallExpression" }
new RuleTester().run("global-require", rule, {
valid: [
"var x = require('y');",
"if (x) { x.require('y'); }",
"var x;\nx = require('y');",
"var x = 1, y = require('y');",
"var x = require('y'), y = require('y'), z = require('z');",
"var x = require('y').foo;",
"require('y').foo();",
"require('y');",
"function x(){}\n\n\nx();\n\n\nif (x > y) {\n\tdoSomething()\n\n}\n\nvar x = require('y').foo;",
"var logger = require(DEBUG ? 'dev-logger' : 'logger');",
"var logger = DEBUG ? require('dev-logger') : require('logger');",
"function localScopedRequire(require) { require('y'); }",
"var someFunc = require('./someFunc'); someFunc(function(require) { return('bananas'); });",
],
invalid: [
// block statements
{
code: "if (process.env.NODE_ENV === 'DEVELOPMENT') {\n\trequire('debug');\n}",
errors: [ERROR],
},
{
code: "var x; if (y) { x = require('debug'); }",
errors: [ERROR],
},
{
code: "var x; if (y) { x = require('debug').baz; }",
errors: [ERROR],
},
{
code: "function x() { require('y') }",
errors: [ERROR],
},
{
code: "try { require('x'); } catch (e) { console.log(e); }",
errors: [ERROR],
},
// non-block statements
{
code: "var getModule = x => require(x);",
errors: [ERROR],
},
{
code: "var x = (x => require(x))('weird')",
errors: [ERROR],
},
{
code: "switch(x) { case '1': require('1'); break; }",
errors: [ERROR],
},
],
})