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