Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-import-module-exports.js
829 views
1
import path from 'path';
2
import { RuleTester } from 'eslint';
3
4
import { test, testVersion } from '../utils';
5
6
const ruleTester = new RuleTester({
7
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
8
});
9
const rule = require('rules/no-import-module-exports');
10
11
const error = {
12
message: `Cannot use import declarations in modules that export using CommonJS ` +
13
`(module.exports = 'foo' or exports.bar = 'hi')`,
14
type: 'ImportDeclaration',
15
};
16
17
ruleTester.run('no-import-module-exports', rule, {
18
valid: [].concat(
19
test({
20
code: `
21
const thing = require('thing')
22
module.exports = thing
23
`,
24
}),
25
test({
26
code: `
27
import thing from 'otherthing'
28
console.log(thing.module.exports)
29
`,
30
}),
31
test({
32
code: `
33
import thing from 'other-thing'
34
export default thing
35
`,
36
}),
37
test({
38
code: `
39
const thing = require('thing')
40
exports.foo = bar
41
`,
42
}),
43
test({
44
code: `
45
import foo from 'path';
46
module.exports = foo;
47
`,
48
// When the file matches the entry point defined in package.json
49
// See tests/files/package.json
50
filename: path.join(process.cwd(), 'tests/files/index.js'),
51
}),
52
test({
53
code: `
54
import foo from 'path';
55
module.exports = foo;
56
`,
57
filename: path.join(process.cwd(), 'tests/files/some/other/entry-point.js'),
58
options: [{ exceptions: ['**/*/other/entry-point.js'] }],
59
}),
60
test({
61
code: `
62
import * as process from 'process';
63
console.log(process.env);
64
`,
65
filename: path.join(process.cwd(), 'tests/files/missing-entrypoint/cli.js'),
66
}),
67
testVersion('>= 6', () => ({
68
code: `
69
import fs from 'fs/promises';
70
71
const subscriptions = new Map();
72
73
export default async (client) => {
74
/**
75
* loads all modules and their subscriptions
76
*/
77
const modules = await fs.readdir('./src/modules');
78
79
await Promise.all(
80
modules.map(async (moduleName) => {
81
// Loads the module
82
const module = await import(\`./modules/\${moduleName}/module.js\`);
83
// skips the module, in case it is disabled.
84
if (module.enabled) {
85
// Loads each of it's subscriptions into their according list.
86
module.subscriptions.forEach((fun, event) => {
87
if (!subscriptions.has(event)) {
88
subscriptions.set(event, []);
89
}
90
subscriptions.get(event).push(fun);
91
});
92
}
93
})
94
);
95
96
/**
97
* Setting up all events.
98
* binds all events inside the subscriptions map to call all functions provided
99
*/
100
subscriptions.forEach((funs, event) => {
101
client.on(event, (...args) => {
102
funs.forEach(async (fun) => {
103
try {
104
await fun(client, ...args);
105
} catch (e) {
106
client.emit('error', e);
107
}
108
});
109
});
110
});
111
};
112
`,
113
parserOptions: {
114
ecmaVersion: 2020,
115
},
116
})) || [],
117
),
118
invalid: [
119
test({
120
code: `
121
import { stuff } from 'starwars'
122
module.exports = thing
123
`,
124
errors: [error],
125
}),
126
test({
127
code: `
128
import thing from 'starwars'
129
const baz = module.exports = thing
130
console.log(baz)
131
`,
132
errors: [error],
133
}),
134
test({
135
code: `
136
import * as allThings from 'starwars'
137
exports.bar = thing
138
`,
139
errors: [error],
140
}),
141
test({
142
code: `
143
import thing from 'other-thing'
144
exports.foo = bar
145
`,
146
errors: [error],
147
}),
148
test({
149
code: `
150
import foo from 'path';
151
module.exports = foo;
152
`,
153
filename: path.join(process.cwd(), 'tests/files/some/other/entry-point.js'),
154
options: [{ exceptions: ['**/*/other/file.js'] }],
155
errors: [error],
156
}),
157
],
158
});
159
160