Skip to content

Commit

Permalink
Create lint rule preventing uses of CommonJS exports (facebook#49079)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#49079

The rule disallows using CommonJS exports in react-native and assets/registry package.

## Changelog:
[Internal] - Created a lint rule that prevents using CommonJS exports

Reviewed By: huntie

Differential Revision: D68951212

fbshipit-source-id: 1c9a1581af951d2a876b348981f0e5a81c99109a
  • Loading branch information
coado authored and facebook-github-bot committed Jan 31, 2025
1 parent c3ea606 commit 5c21537
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ module.exports = {
'no-undef': 0,
},
},
{
files: [
'./packages/react-native/**/*.{js,flow}',
'./packages/assets/registry.js',
],
parser: 'hermes-eslint',
rules: {
'lint/no-commonjs-exports': 1,
},
},
{
files: ['package.json'],
parser: 'jsonc-eslint-parser',
Expand Down
44 changes: 44 additions & 0 deletions tools/eslint/rules/__tests__/no-commonjs-exports-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

const rule = require('../no-commonjs-exports.js');
const {RuleTester} = require('eslint');

const ruleTester = new RuleTester({
parser: require.resolve('hermes-eslint'),
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

ruleTester.run('module.exports', rule, {
valid: [
{
code: `export default function foo() {}`,
},
{
code: `export function foo() {}`,
},
],
invalid: [
{
code: `module.exports = function foo() {}`,
errors: [{messageId: 'moduleExports'}],
output: null, // Expect no autofix to be suggested.
},
{
code: `exports.foo = function foo() {}`,
errors: [{messageId: 'exports'}],
output: null, // Expect no autofix to be suggested.
},
],
});
54 changes: 54 additions & 0 deletions tools/eslint/rules/no-commonjs-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow CommonJS `module.exports` syntax',
},
messages: {
moduleExports:
'Use `export` syntax instead of CommonJS `module.exports`.',
exports: 'Use `export` syntax instead of CommonJS `exports`.',
},
schema: [],
},

create(context) {
return {
MemberExpression(node) {
if (
node.object.type === 'Identifier' &&
node.object.name === 'module' &&
node.property.type === 'Identifier' &&
node.property.name === 'exports'
) {
context.report({
node,
messageId: 'moduleExports',
});
return;
}

if (
node.object.type === 'Identifier' &&
node.object.name === 'exports'
) {
context.report({
node,
messageId: 'exports',
});
}
},
};
},
};

0 comments on commit 5c21537

Please sign in to comment.