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