-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
no-commonjs.js
115 lines (100 loc) · 5.52 KB
/
no-commonjs.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
const EXPORT_MESSAGE = 'Expected "export" or "export default"';
const IMPORT_MESSAGE = 'Expected "import" instead of "require()"';
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, sourceType: 'module' } });
ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
valid: [
// imports
{ code: 'import "x";', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import x from "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import { x } from "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
// exports
{ code: 'export default "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'export function house() {}', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{
code:
'function someFunc() {\n'+
' const exports = someComputation();\n'+
'\n'+
' expect(exports.someProp).toEqual({ a: \'value\' });\n'+
'}',
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
// allowed requires
{ code: 'function a() { var x = require("y"); }' }, // nested requires allowed
{ code: 'var a = c && require("b")' }, // conditional requires allowed
{ code: 'require.resolve("help")' }, // methods of require are allowed
{ code: 'require.ensure([])' }, // webpack specific require.ensure is allowed
{ code: 'require([], function(a, b, c) {})' }, // AMD require is allowed
{ code: "var bar = require('./bar', true);" },
{ code: "var bar = proxyquire('./bar');" },
{ code: "var bar = require('./ba' + 'r');" },
{ code: 'var bar = require(`x${1}`);', parserOptions: { ecmaVersion: 2015 } },
{ code: 'var zero = require(0);' },
{ code: 'require("x")', options: [{ allowRequire: true }] },
// commonJS doesn't care how the path is built. You can use a function to
// dynamically build the module path.t st
{ code: 'require(rootRequire("x"))', options: [{ allowRequire: true }] },
{ code: 'require(String("x"))', options: [{ allowRequire: true }] },
{ code: 'require(["x", "y", "z"].join("/"))', options: [{ allowRequire: true }] },
// commonJS rules should be scoped to commonJS spec. `rootRequire` is not
// recognized by this commonJS plugin.
{ code: 'rootRequire("x")', options: [{ allowRequire: true }] },
{ code: 'rootRequire("x")', options: [{ allowRequire: false }] },
{ code: 'module.exports = function () {}', options: ['allow-primitive-modules'] },
{ code: 'module.exports = function () {}', options: [{ allowPrimitiveModules: true }] },
{ code: 'module.exports = "foo"', options: ['allow-primitive-modules'] },
{ code: 'module.exports = "foo"', options: [{ allowPrimitiveModules: true }] },
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: true }] },
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: false }] },
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: true }] },
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: false }] },
{ code: 'try { require("x") } catch (error) {}' },
],
invalid: [
// imports
...(semver.satisfies(eslintPkg.version, '< 4.0.0') ? [] : [
{ code: 'var x = require("x")', output: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'x = require("x")', output: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', output: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require(`x`)',
parserOptions: { ecmaVersion: 2015 },
output: 'require(`x`)',
errors: [ { message: IMPORT_MESSAGE }],
},
{ code: 'if (typeof window !== "undefined") require("x")',
options: [{ allowConditionalRequire: false }],
output: 'if (typeof window !== "undefined") require("x")',
errors: [ { message: IMPORT_MESSAGE }],
},
{ code: 'if (typeof window !== "undefined") { require("x") }',
options: [{ allowConditionalRequire: false }],
output: 'if (typeof window !== "undefined") { require("x") }',
errors: [ { message: IMPORT_MESSAGE }],
},
{ code: 'try { require("x") } catch (error) {}',
options: [{ allowConditionalRequire: false }],
output: 'try { require("x") } catch (error) {}',
errors: [ { message: IMPORT_MESSAGE }],
},
]),
// exports
{ code: 'exports.face = "palm"', output: 'exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports.face = "palm"', output: 'module.exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports = face', output: 'module.exports = face', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'exports = module.exports = {}', output: 'exports = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'var x = module.exports = {}', output: 'var x = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
{ code: 'module.exports = {}',
options: ['allow-primitive-modules'],
output: 'module.exports = {}',
errors: [ { message: EXPORT_MESSAGE }],
},
{ code: 'var x = module.exports',
options: ['allow-primitive-modules'],
output: 'var x = module.exports',
errors: [ { message: EXPORT_MESSAGE }],
},
],
});