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
+/* eslint-disable no-for-of-loops/no-for-of-loops */
11
+
12
+'use strict';
13
+
14
+function isOldFork(filename) {
15
+ return filename.endsWith('.old.js') || filename.endsWith('.old');
16
+}
17
+
18
+function isNewFork(filename) {
19
+ return filename.endsWith('.new.js') || filename.endsWith('.new');
20
+}
21
+
22
+function warnIfNewField(context, newFields, identifier) {
23
+ const name = identifier.name;
24
+ if (name.endsWith('_new') || (newFields !== null && newFields.has(name))) {
25
+ context.report({
26
+ node: identifier,
27
+ message:
28
+ 'Field cannot be accessed inside the old reconciler fork, only the ' +
29
+ 'new fork.',
30
+ });
31
+ }
32
+}
33
+
34
+function warnIfOldField(context, oldFields, identifier) {
35
+ const name = identifier.name;
36
+ if (name.endsWith('_old') || (oldFields !== null && oldFields.has(name))) {
37
+ context.report({
38
+ node: identifier,
39
+ message:
40
+ 'Field cannot be accessed inside the new reconciler fork, only the ' +
41
+ 'old fork.',
42
+ });
43
+ }
44
+}
45
+
46
+module.exports = {
47
+ meta: {
48
+ type: 'problem',
49
+ fixable: 'code',
50
+ },
51
+ create(context) {
52
+ const sourceFilename = context.getFilename();
53
+
54
+ if (isOldFork(sourceFilename)) {
55
+ const options = context.options;
56
+ let newFields = null;
57
+ if (options !== null) {
58
+ for (const option of options) {
59
+ if (option.new !== undefined) {
60
+ if (newFields === null) {
61
+ newFields = new Set(option.new);
62
+ } else {
63
+ for (const field of option.new) {
64
+ newFields.add(field);
65
+ }
66
+ }
67
+ }
68
+ }
69
+ }
70
+ return {
71
+ MemberExpression(node) {
72
+ const property = node.property;
73
+ if (property.type === 'Identifier') {
74
+ warnIfNewField(context, newFields, property);
75
+ }
76
+ },
77
+
78
+ ObjectPattern(node) {
79
+ for (const property of node.properties) {
80
+ const key = property.key;
81
+ if (key.type === 'Identifier') {
82
+ warnIfNewField(context, newFields, key);
83
+ }
84
+ }
85
+ },
86
+ };
87
+ }
88
+
89
+ if (isNewFork(sourceFilename)) {
90
+ const options = context.options;
91
+ let oldFields = null;
92
+ if (options !== null) {
93
+ for (const option of options) {
94
+ if (option.old !== undefined) {
95
+ if (oldFields === null) {
96
+ oldFields = new Set(option.old);
97
+ } else {
98
+ for (const field of option.new) {
99
+ oldFields.add(field);
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ return {
106
+ MemberExpression(node) {
107
+ const property = node.property;
108
+ if (property.type === 'Identifier') {
109
+ warnIfOldField(context, oldFields, property);
110
+ }
111
+ },
112
+
113
+ ObjectPattern(node) {
114
+ for (const property of node.properties) {
115
+ const key = property.key;
116
+ if (key.type === 'Identifier') {
117
+ warnIfOldField(context, oldFields, key);
118
+ }
119
+ }
120
+ },
121
+ };
122
+ }
123
+
124
+ return {};
125
+ },
126
+};