forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create lint rule preventing uses of CommonJS exports (facebook#49079)
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
1 parent
c3ea606
commit 5c21537
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |