-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Add custom eslint plugin to check fbjs imports #13014
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,14 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
const fbjsImportRule = require('./rules/fbjs-import'); | ||
|
||
module.exports.rules = { | ||
'fbjs-import': fbjsImportRule, | ||
}; |
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,17 @@ | ||
{ | ||
"name": "eslint-plugin-custom", | ||
"version": "1.0.0", | ||
"license": "BSD-3-Clause", | ||
"description": "Custom ESLint rules used in the React Native repo", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/react-native.git" | ||
}, | ||
"dependencies": { | ||
"eslint-module-utils": "^2.0.0" | ||
} | ||
} |
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,57 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/** | ||
* ESLint rule to avoid importing fbjs modules using haste, they should be imported | ||
* using `fbjs/lib/<module>` otherwise it won't work in OSS. | ||
*/ | ||
const fs = require('fs'); | ||
const moduleVisitor = require('eslint-module-utils/moduleVisitor').default; | ||
const path = require('path'); | ||
|
||
// Note that this file will be in the `<root>/node_modules/eslint-plugin-custom/rules` | ||
// folder when installed so file paths are relative to that. | ||
// Find modules that could be imported from fbjs. | ||
const fbjsFiles = fs | ||
.readdirSync(path.resolve(__dirname, '../../fbjs/lib')) | ||
.filter(file => file.match(/\.js$/g)); | ||
|
||
// Find RN dependencies to avoid false positive if there is a commonjs module with | ||
// the same name as one of the fbjs modules. | ||
const reactNativePackageJSON = JSON.parse( | ||
fs.readFileSync(path.resolve(__dirname, '../../../package.json')) | ||
); | ||
|
||
const fbjsModules = new Set( | ||
fbjsFiles | ||
.map(file => file.replace('.js', '')) | ||
.filter(module => !reactNativePackageJSON.dependencies[module]) | ||
); | ||
|
||
module.exports = { | ||
create: context => { | ||
const visitor = source => { | ||
const moduleName = source.value; | ||
if (fbjsModules.has(moduleName)) { | ||
context.report({ | ||
node: source, | ||
message: `fbjs module \`${moduleName}\` should not be imported using haste`, | ||
fix: fixer => { | ||
return fixer.insertTextBeforeRange( | ||
[source.start + 1, source.end], | ||
'fbjs/lib/' | ||
); | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
return moduleVisitor(visitor, { commonjs: true }); | ||
}, | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this will work with our internal Yarn offline cache that well, unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like nuclide uses this to register custom plugins, that would probably work instead or using a file dependency. https://github.com/facebook/nuclide/blob/693623fd6e2124b094ab4d4d5fad15a67496c6b7/.eslintrc.js#L513-L514