-
Notifications
You must be signed in to change notification settings - Fork 45.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESLint rule to forbid cross fork imports (#18568)
Modules that belong to one fork should not import modules that belong to the other fork. Helps make sure you correctly update imports when syncing changes across implementations. Also could help protect against code size regressions that might happen if one of the forks accidentally depends on two copies of the same module.
- Loading branch information
Showing
4 changed files
with
152 additions
and
2 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
94 changes: 94 additions & 0 deletions
94
scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js
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,94 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const rule = require('../no-cross-fork-imports'); | ||
const RuleTester = require('eslint').RuleTester; | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('eslint-rules/no-cross-fork-imports', rule, { | ||
valid: [ | ||
{ | ||
code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop';", | ||
filename: 'ReactFiberWorkLoop.js', | ||
}, | ||
{ | ||
code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';", | ||
filename: 'ReactFiberWorkLoop.new.js', | ||
}, | ||
{ | ||
code: | ||
"import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new.js';", | ||
filename: 'ReactFiberWorkLoop.new.js', | ||
}, | ||
{ | ||
code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old';", | ||
filename: 'ReactFiberWorkLoop.old.js', | ||
}, | ||
{ | ||
code: | ||
"import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old.js';", | ||
filename: 'ReactFiberWorkLoop.old.js', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';", | ||
filename: 'ReactFiberWorkLoop.old.js', | ||
errors: [ | ||
{ | ||
message: | ||
'A module that belongs to the old fork cannot import a module ' + | ||
'from the new fork.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
"import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new.js';", | ||
filename: 'ReactFiberWorkLoop.old.js', | ||
errors: [ | ||
{ | ||
message: | ||
'A module that belongs to the old fork cannot import a module ' + | ||
'from the new fork.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old';", | ||
filename: 'ReactFiberWorkLoop.new.js', | ||
errors: [ | ||
{ | ||
message: | ||
'A module that belongs to the new fork cannot import a module ' + | ||
'from the old fork.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: | ||
"import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old.js';", | ||
filename: 'ReactFiberWorkLoop.new.js', | ||
errors: [ | ||
{ | ||
message: | ||
'A module that belongs to the new fork cannot import a module ' + | ||
'from the old fork.', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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,54 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
function isOldFork(filename) { | ||
return filename.endsWith('.old.js') || filename.endsWith('.old'); | ||
} | ||
|
||
function isNewFork(filename) { | ||
return filename.endsWith('.new.js') || filename.endsWith('.new'); | ||
} | ||
|
||
module.exports = context => { | ||
const sourceFilename = context.getFilename(); | ||
|
||
if (isOldFork(sourceFilename)) { | ||
return { | ||
ImportDeclaration(node) { | ||
const importFilename = node.source.value; | ||
if (isNewFork(importFilename)) { | ||
context.report( | ||
node, | ||
'A module that belongs to the old fork cannot import a module ' + | ||
'from the new fork.' | ||
); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
if (isNewFork(sourceFilename)) { | ||
return { | ||
ImportDeclaration(node) { | ||
const importFilename = node.source.value; | ||
if (isOldFork(importFilename)) { | ||
context.report( | ||
node, | ||
'A module that belongs to the new fork cannot import a module ' + | ||
'from the old fork.' | ||
); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
return {}; | ||
}; |