Skip to content

Commit

Permalink
Merge pull request #366 from lo1tuma/commonjs
Browse files Browse the repository at this point in the history
Drop support for commonjs in no-exports rule
  • Loading branch information
lo1tuma authored Jan 23, 2025
2 parents a263ff2 + f61b2dd commit c9fdcb5
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 133 deletions.
8 changes: 1 addition & 7 deletions docs/rules/no-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
17 changes: 0 additions & 17 deletions lib/rules/no-exports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createMochaVisitors } from '../ast/mochaVisitors.js';
import { getNodeName } from '../util/ast.js';

export const noExportsRule = {
meta: {
Expand All @@ -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) {
Expand All @@ -46,12 +35,6 @@ export const noExportsRule = {
node
) {
exportNodes.push(node);
},

AssignmentExpression(node) {
if (isCommonJsExport(node.left)) {
exportNodes.push(node);
}
}
});
}
Expand Down
7 changes: 0 additions & 7 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
102 changes: 0 additions & 102 deletions test/rules/no-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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' },
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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' },
Expand Down

0 comments on commit c9fdcb5

Please sign in to comment.