-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathgutenberg-phase.js
174 lines (158 loc) · 4.42 KB
/
gutenberg-phase.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Traverse up through the chain of parent AST nodes returning the first parent
* the predicate returns a truthy value for.
*
* @param {Object} sourceNode The AST node to search from.
* @param {Function} predicate A predicate invoked for each parent.
*
* @return {Object | undefined} The first encountered parent node where the predicate
* returns a truthy value.
*/
function findParent( sourceNode, predicate ) {
if ( ! sourceNode.parent ) {
return;
}
if ( predicate( sourceNode.parent ) ) {
return sourceNode.parent;
}
return findParent( sourceNode.parent, predicate );
}
/**
* Tests whether the GUTENBERG_PHASE variable is accessed via
* `process.env.GUTENBERG_PHASE`.
*
* @example
* ```js
* // good
* if ( process.env.GUTENBERG_PHASE === 2 ) {
*
* // bad
* if ( GUTENBERG_PHASE === 2 ) {
* ```
*
* @param {Object} node The GUTENBERG_PHASE identifier node.
* @param {Object} context The eslint context object.
*/
function testIsAccessedViaProcessEnv( node, context ) {
const parent = node.parent;
if (
parent &&
parent.type === 'MemberExpression' &&
context.getSource( parent ) === 'process.env.GUTENBERG_PHASE'
) {
return;
}
context.report(
node,
'The `GUTENBERG_PHASE` constant should be accessed using `process.env.GUTENBERG_PHASE`.'
);
}
/**
* Tests whether the GUTENBERG_PHASE variable is used in a strict binary
* equality expression in a comparison with a number, triggering a
* violation if not.
*
* @example
* ```js
* // good
* if ( process.env.GUTENBERG_PHASE === 2 ) {
*
* // bad
* if ( process.env.GUTENBERG_PHASE >= '2' ) {
* ```
*
* @param {Object} node The GUTENBERG_PHASE identifier node.
* @param {Object} context The eslint context object.
*/
function testIsUsedInStrictBinaryExpression( node, context ) {
const parent = findParent(
node,
( candidate ) => candidate.type === 'BinaryExpression'
);
if ( parent ) {
const comparisonNode =
node.parent.type === 'MemberExpression' ? node.parent : node;
// Test for process.env.GUTENBERG_PHASE === <number> or <number> === process.env.GUTENBERG_PHASE.
const hasCorrectOperator = [ '===', '!==' ].includes( parent.operator );
const hasCorrectOperands =
( parent.left === comparisonNode &&
typeof parent.right.value === 'number' ) ||
( parent.right === comparisonNode &&
typeof parent.left.value === 'number' );
if ( hasCorrectOperator && hasCorrectOperands ) {
return;
}
}
context.report(
node,
'The `GUTENBERG_PHASE` constant should only be used in a strict equality comparison with a primitive number.'
);
}
/**
* Tests whether the GUTENBERG_PHASE variable is used as the condition for an
* if statement, triggering a violation if not.
*
* @example
* ```js
* // good
* if ( process.env.GUTENBERG_PHASE === 2 ) {
*
* // bad
* const isFeatureActive = process.env.GUTENBERG_PHASE === 2;
* ```
*
* @param {Object} node The GUTENBERG_PHASE identifier node.
* @param {Object} context The eslint context object.
*/
function testIsUsedInIfOrTernary( node, context ) {
const conditionalParent = findParent( node, ( candidate ) =>
[ 'IfStatement', 'ConditionalExpression' ].includes( candidate.type )
);
const binaryParent = findParent(
node,
( candidate ) => candidate.type === 'BinaryExpression'
);
if (
conditionalParent &&
binaryParent &&
conditionalParent.test &&
conditionalParent.test.range[ 0 ] === binaryParent.range[ 0 ] &&
conditionalParent.test.range[ 1 ] === binaryParent.range[ 1 ]
) {
return;
}
context.report(
node,
'The `GUTENBERG_PHASE` constant should only be used as part of the condition in an if statement or ternary expression.'
);
}
module.exports = {
meta: {
type: 'problem',
schema: [],
deprecated: true,
replacedBy: '@wordpress/is-gutenberg-plugin',
},
create( context ) {
return {
Identifier( node ) {
// Bypass any identifiers with a node name different to `GUTENBERG_PHASE`.
if ( node.name !== 'GUTENBERG_PHASE' ) {
return;
}
testIsAccessedViaProcessEnv( node, context );
testIsUsedInStrictBinaryExpression( node, context );
testIsUsedInIfOrTernary( node, context );
},
Literal( node ) {
// Bypass any identifiers with a node value different to `GUTENBERG_PHASE`.
if ( node.value !== 'GUTENBERG_PHASE' ) {
return;
}
if ( node.parent && node.parent.type === 'MemberExpression' ) {
testIsAccessedViaProcessEnv( node, context );
}
},
};
},
};