From f61b2dd03536b855455158da2d95d9954d35d888 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Thu, 23 Jan 2025 13:29:22 +0100 Subject: [PATCH] Drop support for commonjs in no-exports rule --- docs/rules/no-exports.md | 8 +-- lib/rules/no-exports.js | 17 ------- lib/util/ast.js | 7 --- test/rules/no-exports.js | 102 --------------------------------------- 4 files changed, 1 insertion(+), 133 deletions(-) diff --git a/docs/rules/no-exports.md b/docs/rules/no-exports.md index 114a910..454b087 100644 --- a/docs/rules/no-exports.md +++ b/docs/rules/no-exports.md @@ -8,17 +8,11 @@ Test files should have only one purpose, which is testing a specific unit. Using ## Rule Details -This rule looks for CommonJS or ESM export statements and flags them as a problem when the same file also contains a use of a mocha function. +This rule looks for ESM export statements and flags them as a problem when the same file also contains a use of a mocha function. The following patterns are considered warnings: ```js -describe(function () {/* ... */}); -module.exports = 'foo'; - -it('works', function () {/* ... */}); -exports.foo = 'bar'; - beforeEach(function () {/* ... */}); export default 'foo'; diff --git a/lib/rules/no-exports.js b/lib/rules/no-exports.js index 5a7ce66..7631bbf 100644 --- a/lib/rules/no-exports.js +++ b/lib/rules/no-exports.js @@ -1,5 +1,4 @@ import { createMochaVisitors } from '../ast/mochaVisitors.js'; -import { getNodeName } from '../util/ast.js'; export const noExportsRule = { meta: { @@ -17,16 +16,6 @@ export const noExportsRule = { const exportNodes = []; let hasTestCase = false; - function isCommonJsExport(node) { - if (node.type === 'MemberExpression') { - const name = getNodeName(node); - - return name === 'module.exports' || name.startsWith('exports.'); - } - - return false; - } - return createMochaVisitors(context, { 'Program:exit'() { if (hasTestCase && exportNodes.length > 0) { @@ -46,12 +35,6 @@ export const noExportsRule = { node ) { exportNodes.push(node); - }, - - AssignmentExpression(node) { - if (isCommonJsExport(node.left)) { - exportNodes.push(node); - } } }); } diff --git a/lib/util/ast.js b/lib/util/ast.js index d8cfa8c..6bdb0cf 100644 --- a/lib/util/ast.js +++ b/lib/util/ast.js @@ -22,13 +22,6 @@ function getPropertyName(property) { return property.name || property.value; } -export function getNodeName(node) { - if (node.type === 'MemberExpression') { - return `${getNodeName(node.object)}.${getPropertyName(node.property)}`; - } - return node.name; -} - function isSuiteConfigExpression(node) { if (node.type !== 'MemberExpression') { return false; diff --git a/test/rules/no-exports.js b/test/rules/no-exports.js index dc542b6..e3e3d0d 100644 --- a/test/rules/no-exports.js +++ b/test/rules/no-exports.js @@ -14,9 +14,6 @@ ruleTester.run('no-exports', plugin.rules['no-exports'], { 'it("", function() {});', 'test("", function() {});', 'specify("", function() {});', - 'it("", function() {}); notModule.exports = "foo"', - 'notIt("", function() {}); module.exports = "foo"', - 'it("", function() {}); exports = "foo"', { code: 'describe(function () {})', languageOptions: { ecmaVersion: 2019, sourceType: 'module' } @@ -32,39 +29,6 @@ ruleTester.run('no-exports', plugin.rules['no-exports'], { ], invalid: [ - { - code: 'describe(function() {}); module.exports = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 26, - line: 1 - }] - }, - { - code: 'describe(function() {}); module["exports"] = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 26, - line: 1 - }] - }, - { - code: 'describe(function() {}); exports.foo = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 26, - line: 1 - }] - }, - { - code: 'describe(function() {}); module.exports = "foo"', - languageOptions: { ecmaVersion: 2019, sourceType: 'module' }, - errors: [{ - message: 'Unexpected export from a test file', - column: 26, - line: 1 - }] - }, { code: 'describe(function() {}); export default "foo"', languageOptions: { ecmaVersion: 2019, sourceType: 'module' }, @@ -119,39 +83,6 @@ ruleTester.run('no-exports', plugin.rules['no-exports'], { line: 1 }] }, - { - code: 'it("", function() {}); module.exports = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 24, - line: 1 - }] - }, - { - code: 'it("", function() {}); module["exports"] = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 24, - line: 1 - }] - }, - { - code: 'it("", function() {}); exports.foo = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 24, - line: 1 - }] - }, - { - code: 'it("", function() {}); module.exports = "foo"', - languageOptions: { ecmaVersion: 2019, sourceType: 'module' }, - errors: [{ - message: 'Unexpected export from a test file', - column: 24, - line: 1 - }] - }, { code: 'it("", function() {}); export default "foo"', languageOptions: { ecmaVersion: 2019, sourceType: 'module' }, @@ -206,39 +137,6 @@ ruleTester.run('no-exports', plugin.rules['no-exports'], { line: 1 }] }, - { - code: 'beforeEach(function() {}); module.exports = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 28, - line: 1 - }] - }, - { - code: 'beforeEach(function() {}); module["exports"] = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 28, - line: 1 - }] - }, - { - code: 'beforeEach(function() {}); exports.foo = "foo"', - errors: [{ - message: 'Unexpected export from a test file', - column: 28, - line: 1 - }] - }, - { - code: 'beforeEach(function() {}); module.exports = "foo"', - languageOptions: { ecmaVersion: 2019, sourceType: 'module' }, - errors: [{ - message: 'Unexpected export from a test file', - column: 28, - line: 1 - }] - }, { code: 'beforeEach(function() {}); export default "foo"', languageOptions: { ecmaVersion: 2019, sourceType: 'module' },