Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-commonjs.js
829 views
1
import { RuleTester } from 'eslint';
2
import eslintPkg from 'eslint/package.json';
3
import semver from 'semver';
4
5
const EXPORT_MESSAGE = 'Expected "export" or "export default"';
6
const IMPORT_MESSAGE = 'Expected "import" instead of "require()"';
7
8
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, sourceType: 'module' } });
9
10
ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
11
valid: [
12
13
// imports
14
{ code: 'import "x";', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
15
{ code: 'import x from "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
16
{ code: 'import { x } from "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
17
18
// exports
19
{ code: 'export default "x"', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
20
{ code: 'export function house() {}', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
21
{
22
code:
23
'function someFunc() {\n'+
24
' const exports = someComputation();\n'+
25
'\n'+
26
' expect(exports.someProp).toEqual({ a: \'value\' });\n'+
27
'}',
28
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
29
},
30
31
// allowed requires
32
{ code: 'function a() { var x = require("y"); }' }, // nested requires allowed
33
{ code: 'var a = c && require("b")' }, // conditional requires allowed
34
{ code: 'require.resolve("help")' }, // methods of require are allowed
35
{ code: 'require.ensure([])' }, // webpack specific require.ensure is allowed
36
{ code: 'require([], function(a, b, c) {})' }, // AMD require is allowed
37
{ code: "var bar = require('./bar', true);" },
38
{ code: "var bar = proxyquire('./bar');" },
39
{ code: "var bar = require('./ba' + 'r');" },
40
{ code: 'var bar = require(`x${1}`);', parserOptions: { ecmaVersion: 2015 } },
41
{ code: 'var zero = require(0);' },
42
{ code: 'require("x")', options: [{ allowRequire: true }] },
43
44
// commonJS doesn't care how the path is built. You can use a function to
45
// dynamically build the module path.t st
46
{ code: 'require(rootRequire("x"))', options: [{ allowRequire: true }] },
47
{ code: 'require(String("x"))', options: [{ allowRequire: true }] },
48
{ code: 'require(["x", "y", "z"].join("/"))', options: [{ allowRequire: true }] },
49
50
// commonJS rules should be scoped to commonJS spec. `rootRequire` is not
51
// recognized by this commonJS plugin.
52
{ code: 'rootRequire("x")', options: [{ allowRequire: true }] },
53
{ code: 'rootRequire("x")', options: [{ allowRequire: false }] },
54
55
{ code: 'module.exports = function () {}', options: ['allow-primitive-modules'] },
56
{ code: 'module.exports = function () {}', options: [{ allowPrimitiveModules: true }] },
57
{ code: 'module.exports = "foo"', options: ['allow-primitive-modules'] },
58
{ code: 'module.exports = "foo"', options: [{ allowPrimitiveModules: true }] },
59
60
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: true }] },
61
{ code: 'if (typeof window !== "undefined") require("x")', options: [{ allowRequire: false }] },
62
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: true }] },
63
{ code: 'if (typeof window !== "undefined") { require("x") }', options: [{ allowRequire: false }] },
64
65
{ code: 'try { require("x") } catch (error) {}' },
66
],
67
68
invalid: [
69
70
// imports
71
...(semver.satisfies(eslintPkg.version, '< 4.0.0') ? [] : [
72
{ code: 'var x = require("x")', output: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
73
{ code: 'x = require("x")', output: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
74
{ code: 'require("x")', output: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },
75
{ code: 'require(`x`)',
76
parserOptions: { ecmaVersion: 2015 },
77
output: 'require(`x`)',
78
errors: [ { message: IMPORT_MESSAGE }],
79
},
80
81
{ code: 'if (typeof window !== "undefined") require("x")',
82
options: [{ allowConditionalRequire: false }],
83
output: 'if (typeof window !== "undefined") require("x")',
84
errors: [ { message: IMPORT_MESSAGE }],
85
},
86
{ code: 'if (typeof window !== "undefined") { require("x") }',
87
options: [{ allowConditionalRequire: false }],
88
output: 'if (typeof window !== "undefined") { require("x") }',
89
errors: [ { message: IMPORT_MESSAGE }],
90
},
91
{ code: 'try { require("x") } catch (error) {}',
92
options: [{ allowConditionalRequire: false }],
93
output: 'try { require("x") } catch (error) {}',
94
errors: [ { message: IMPORT_MESSAGE }],
95
},
96
]),
97
98
// exports
99
{ code: 'exports.face = "palm"', output: 'exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
100
{ code: 'module.exports.face = "palm"', output: 'module.exports.face = "palm"', errors: [ { message: EXPORT_MESSAGE }] },
101
{ code: 'module.exports = face', output: 'module.exports = face', errors: [ { message: EXPORT_MESSAGE }] },
102
{ code: 'exports = module.exports = {}', output: 'exports = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
103
{ code: 'var x = module.exports = {}', output: 'var x = module.exports = {}', errors: [ { message: EXPORT_MESSAGE }] },
104
{ code: 'module.exports = {}',
105
options: ['allow-primitive-modules'],
106
output: 'module.exports = {}',
107
errors: [ { message: EXPORT_MESSAGE }],
108
},
109
{ code: 'var x = module.exports',
110
options: ['allow-primitive-modules'],
111
output: 'var x = module.exports',
112
errors: [ { message: EXPORT_MESSAGE }],
113
},
114
],
115
});
116
117