Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for commonjs in no-exports rule #366

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading