-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathno-style-display.js
151 lines (131 loc) · 4.22 KB
/
no-style-display.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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
module.exports = function (context) {
const setStyleCall = 'CallExpression[callee.name=setStyle]';
const setStylesCall =
'CallExpression[callee.name=setStyles], CallExpression[callee.name=setImportantStyles]';
const resetStylesCall = 'CallExpression[callee.name=resetStyles]';
const displayMessage = [
'Do not set the display property using setStyle.',
'Only the `toggle` helper in `src/style.js` is permitted to change the `display: none` style of an element.',
'Or use `setInitialDisplay` to setup an initial `display: block`, `inline-block`, etc., if it is not possible to do so in CSS.',
].join('\n\t');
return {
[setStyleCall]: function (node) {
const filePath = context.getFilename();
if (filePath.endsWith('src/style.js')) {
return;
}
const arg = node.arguments[1];
if (!arg) {
return;
}
if (arg.type !== 'Literal' || typeof arg.value !== 'string') {
if (arg.type === 'CallExpression') {
const {callee} = arg;
if (
callee.type === 'Identifier' &&
callee.name === 'assertNotDisplay'
) {
return;
}
}
return context.report({
node: arg,
message:
'property argument (the second argument) to setStyle must be a string literal',
});
}
if (arg.value === 'display') {
context.report({
node: arg,
message: displayMessage,
});
}
},
[setStylesCall]: function (node) {
const callName = node.callee.name;
const arg = node.arguments[1];
if (!arg) {
return;
}
if (arg.type !== 'ObjectExpression') {
if (arg.type === 'CallExpression') {
const {callee} = arg;
if (
callee.type === 'Identifier' &&
callee.name === 'assertDoesNotContainDisplay'
) {
return;
}
}
return context.report({
node: arg,
message: `styles argument (the second argument) to ${callName} must be an object literal. You may also pass in an explicit call to assertDoesNotContainDisplay`,
});
}
const {properties} = arg;
for (let i = 0; i < properties.length; i++) {
const prop = properties[i];
if (prop.computed) {
context.report({
node: prop,
message: 'Style names must not be computed',
});
continue;
}
const {key} = prop;
// `"display": "none"`, and `display: none` use two different AST keys.
if (key.value === 'display' || key.name === 'display') {
context.report({
node: prop,
message: displayMessage,
});
}
}
},
[resetStylesCall]: function (node) {
const arg = node.arguments[1];
if (!arg) {
return;
}
if (arg.type !== 'ArrayExpression') {
return context.report({
node: arg,
message: `styles argument (the second argument) to resetStyles must be an array literal`,
});
}
const {elements} = arg;
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
if (el.type !== 'Literal' || typeof el.value !== 'string') {
context.report({
node: el,
message: 'Style names must be string literals',
});
continue;
}
if (el.value === 'display') {
context.report({
node: el,
message: displayMessage,
});
}
}
},
};
};