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.
This commit is contained in:
Andrew Clark 2020-04-09 18:11:34 -07:00 committed by GitHub
parent 50bdd75a60
commit af1b039bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 2 deletions

View File

@ -98,6 +98,7 @@ module.exports = {
'react-internal/invariant-args': ERROR, 'react-internal/invariant-args': ERROR,
'react-internal/warning-args': ERROR, 'react-internal/warning-args': ERROR,
'react-internal/no-production-logging': ERROR, 'react-internal/no-production-logging': ERROR,
'react-internal/no-cross-fork-imports': ERROR,
}, },
overrides: [ overrides: [
@ -161,8 +162,8 @@ module.exports = {
{ {
files: ['packages/react-flight-dom-webpack/**/*.js'], files: ['packages/react-flight-dom-webpack/**/*.js'],
globals: { globals: {
'__webpack_chunk_load__': true, __webpack_chunk_load__: true,
'__webpack_require__': true, __webpack_require__: true,
}, },
}, },
], ],

View File

@ -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.',
},
],
},
],
});

View File

@ -7,5 +7,6 @@ module.exports = {
'warning-args': require('./warning-args'), 'warning-args': require('./warning-args'),
'invariant-args': require('./invariant-args'), 'invariant-args': require('./invariant-args'),
'no-production-logging': require('./no-production-logging'), 'no-production-logging': require('./no-production-logging'),
'no-cross-fork-imports': require('./no-cross-fork-imports'),
}, },
}; };

View File

@ -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 {};
};