@samitouri / QOS-React-2 / commits / af1b039bdd

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.

Andrew Clark committed Apr 9, 2020 at 18:11 UTC af1b039bdd5a8b5def5d51acad00b79e9b7b377c
4 files changed +152 -2
.eslintrc.js
+3 -2
@@ -98,6 +98,7 @@ module.exports = {
98 'react-internal/invariant-args': ERROR,
99 'react-internal/warning-args': ERROR,
100 'react-internal/no-production-logging': ERROR,
101 + 'react-internal/no-cross-fork-imports': ERROR,
102 },
103
104 overrides: [
@@ -161,8 +162,8 @@ module.exports = {
162 {
163 files: ['packages/react-flight-dom-webpack/**/*.js'],
164 globals: {
164 - '__webpack_chunk_load__': true,
165 - '__webpack_require__': true,
165 + __webpack_chunk_load__: true,
166 + __webpack_require__: true,
167 },
168 },
169 ],
scripts/eslint-rules/__tests__/no-cross-fork-imports-test.internal.js new
+94
@@ -0,0 +1,94 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + *
7 + * @emails react-core
8 + */
9 +
10 +'use strict';
11 +
12 +const rule = require('../no-cross-fork-imports');
13 +const RuleTester = require('eslint').RuleTester;
14 +const ruleTester = new RuleTester({
15 + parserOptions: {
16 + ecmaVersion: 8,
17 + sourceType: 'module',
18 + },
19 +});
20 +
21 +ruleTester.run('eslint-rules/no-cross-fork-imports', rule, {
22 + valid: [
23 + {
24 + code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop';",
25 + filename: 'ReactFiberWorkLoop.js',
26 + },
27 + {
28 + code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';",
29 + filename: 'ReactFiberWorkLoop.new.js',
30 + },
31 + {
32 + code:
33 + "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new.js';",
34 + filename: 'ReactFiberWorkLoop.new.js',
35 + },
36 + {
37 + code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old';",
38 + filename: 'ReactFiberWorkLoop.old.js',
39 + },
40 + {
41 + code:
42 + "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old.js';",
43 + filename: 'ReactFiberWorkLoop.old.js',
44 + },
45 + ],
46 + invalid: [
47 + {
48 + code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new';",
49 + filename: 'ReactFiberWorkLoop.old.js',
50 + errors: [
51 + {
52 + message:
53 + 'A module that belongs to the old fork cannot import a module ' +
54 + 'from the new fork.',
55 + },
56 + ],
57 + },
58 + {
59 + code:
60 + "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.new.js';",
61 + filename: 'ReactFiberWorkLoop.old.js',
62 + errors: [
63 + {
64 + message:
65 + 'A module that belongs to the old fork cannot import a module ' +
66 + 'from the new fork.',
67 + },
68 + ],
69 + },
70 + {
71 + code: "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old';",
72 + filename: 'ReactFiberWorkLoop.new.js',
73 + errors: [
74 + {
75 + message:
76 + 'A module that belongs to the new fork cannot import a module ' +
77 + 'from the old fork.',
78 + },
79 + ],
80 + },
81 + {
82 + code:
83 + "import {scheduleUpdateOnFiber} from './ReactFiberWorkLoop.old.js';",
84 + filename: 'ReactFiberWorkLoop.new.js',
85 + errors: [
86 + {
87 + message:
88 + 'A module that belongs to the new fork cannot import a module ' +
89 + 'from the old fork.',
90 + },
91 + ],
92 + },
93 + ],
94 +});
scripts/eslint-rules/index.js
+1
@@ -7,5 +7,6 @@ module.exports = {
7 'warning-args': require('./warning-args'),
8 'invariant-args': require('./invariant-args'),
9 'no-production-logging': require('./no-production-logging'),
10 + 'no-cross-fork-imports': require('./no-cross-fork-imports'),
11 },
12 };
scripts/eslint-rules/no-cross-fork-imports.js new
+54
@@ -0,0 +1,54 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
5 + * LICENSE file in the root directory of this source tree.
6 + *
7 + * @emails react-core
8 + */
9 +
10 +'use strict';
11 +
12 +function isOldFork(filename) {
13 + return filename.endsWith('.old.js') || filename.endsWith('.old');
14 +}
15 +
16 +function isNewFork(filename) {
17 + return filename.endsWith('.new.js') || filename.endsWith('.new');
18 +}
19 +
20 +module.exports = context => {
21 + const sourceFilename = context.getFilename();
22 +
23 + if (isOldFork(sourceFilename)) {
24 + return {
25 + ImportDeclaration(node) {
26 + const importFilename = node.source.value;
27 + if (isNewFork(importFilename)) {
28 + context.report(
29 + node,
30 + 'A module that belongs to the old fork cannot import a module ' +
31 + 'from the new fork.'
32 + );
33 + }
34 + },
35 + };
36 + }
37 +
38 + if (isNewFork(sourceFilename)) {
39 + return {
40 + ImportDeclaration(node) {
41 + const importFilename = node.source.value;
42 + if (isOldFork(importFilename)) {
43 + context.report(
44 + node,
45 + 'A module that belongs to the new fork cannot import a module ' +
46 + 'from the old fork.'
47 + );
48 + }
49 + },
50 + };
51 + }
52 +
53 + return {};
54 +};