@atlaskit/eslint-plugin-ui-styling-standard 0.10.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +2 -0
- package/constellation/index/overview.mdx +2 -0
- package/constellation/no-exported-styles/usage.mdx +87 -0
- package/constellation/no-unsafe-values/usage.mdx +222 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/index.codegen.js +6 -2
- package/dist/cjs/rules/no-exported-styles/index.js +39 -0
- package/dist/cjs/rules/no-unsafe-values/index.js +85 -0
- package/dist/cjs/rules/no-unsafe-values/messages.js +24 -0
- package/dist/cjs/rules/no-unsafe-values/utils.js +437 -0
- package/dist/es2019/presets/all.codegen.js +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/index.codegen.js +6 -2
- package/dist/es2019/rules/no-exported-styles/index.js +33 -0
- package/dist/es2019/rules/no-unsafe-values/index.js +80 -0
- package/dist/es2019/rules/no-unsafe-values/messages.js +72 -0
- package/dist/es2019/rules/no-unsafe-values/utils.js +350 -0
- package/dist/esm/presets/all.codegen.js +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/index.codegen.js +6 -2
- package/dist/esm/rules/no-exported-styles/index.js +33 -0
- package/dist/esm/rules/no-unsafe-values/index.js +79 -0
- package/dist/esm/rules/no-unsafe-values/messages.js +17 -0
- package/dist/esm/rules/no-unsafe-values/utils.js +430 -0
- package/dist/types/index.codegen.d.ts +4 -0
- package/dist/types/presets/all.codegen.d.ts +3 -1
- package/dist/types/presets/recommended.codegen.d.ts +3 -1
- package/dist/types/rules/index.codegen.d.ts +2 -0
- package/dist/types/rules/no-exported-styles/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types/rules/no-unsafe-values/utils.d.ts +28 -0
- package/dist/types-ts4.5/index.codegen.d.ts +4 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-exported-styles/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/utils.d.ts +28 -0
- package/package.json +4 -4
- package/src/presets/all.codegen.tsx +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/index.codegen.tsx +5 -1
- package/src/rules/no-exported-styles/README.md +85 -0
- package/src/rules/no-exported-styles/__tests__/rule.test.tsx +225 -0
- package/src/rules/no-exported-styles/index.tsx +52 -0
- package/src/rules/no-unsafe-values/README.md +220 -0
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +27 -0
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +37 -0
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +229 -0
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +267 -0
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +52 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +106 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +44 -0
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +48 -0
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +75 -0
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +121 -0
- package/src/rules/no-unsafe-values/index.tsx +96 -0
- package/src/rules/no-unsafe-values/messages.tsx +86 -0
- package/src/rules/no-unsafe-values/utils.tsx +399 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallExpression,
|
|
3
|
+
Expression,
|
|
4
|
+
Identifier,
|
|
5
|
+
Property,
|
|
6
|
+
ObjectExpression,
|
|
7
|
+
Node,
|
|
8
|
+
} from 'estree-jsx';
|
|
9
|
+
import type { Rule } from 'eslint';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
getAllowedDynamicKeys,
|
|
13
|
+
getAllowedFunctionCalls,
|
|
14
|
+
isAllowListedVariable,
|
|
15
|
+
type AllowList,
|
|
16
|
+
} from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
17
|
+
import type { Variable } from 'eslint-scope';
|
|
18
|
+
import { findIdentifierNode } from '@atlaskit/eslint-utils/find-identifier-node';
|
|
19
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
20
|
+
|
|
21
|
+
import type { MessageId } from './messages';
|
|
22
|
+
|
|
23
|
+
function isReferenceToCssVar(variable: Variable) {
|
|
24
|
+
const definitions = variable.defs;
|
|
25
|
+
|
|
26
|
+
return definitions.every((definition) => {
|
|
27
|
+
if (definition.type !== 'Variable') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const initializer = definition.node.init;
|
|
32
|
+
if (!initializer) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
initializer.type === 'Literal' &&
|
|
38
|
+
typeof initializer.value === 'string' &&
|
|
39
|
+
initializer.value.startsWith('--')
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class Linter {
|
|
45
|
+
private readonly allowedDynamicKeys: AllowList;
|
|
46
|
+
private readonly allowedFunctionCalls: AllowList;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly context: Rule.RuleContext,
|
|
50
|
+
private readonly baseNode: CallExpression,
|
|
51
|
+
) {
|
|
52
|
+
this.allowedDynamicKeys = getAllowedDynamicKeys(context.options);
|
|
53
|
+
this.allowedFunctionCalls = getAllowedFunctionCalls(context.options);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wrapper around `context.report` to provide type checking on the message ID.
|
|
58
|
+
*/
|
|
59
|
+
private report(descriptor: { node: Node; messageId: MessageId }) {
|
|
60
|
+
this.context.report(descriptor);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Fallback case to lint any identifier that is not otherwise handled.
|
|
65
|
+
*/
|
|
66
|
+
private lintUnhandledIdentifier(identifier: Identifier) {
|
|
67
|
+
this.report({ node: identifier, messageId: 'no-variables' });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private lintIdentifier(value: Identifier) {
|
|
71
|
+
if (value.name === 'undefined') {
|
|
72
|
+
/**
|
|
73
|
+
* `undefined` gets caught implicitly anyway by other checks below,
|
|
74
|
+
* but we are explicitly handling it here.
|
|
75
|
+
*
|
|
76
|
+
* We could potentially introduce a distinct message for it as well.
|
|
77
|
+
*/
|
|
78
|
+
this.lintUnhandledIdentifier(value);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const variable = findVariable({
|
|
83
|
+
identifier: value,
|
|
84
|
+
sourceCode: this.context.sourceCode,
|
|
85
|
+
});
|
|
86
|
+
if (!variable) {
|
|
87
|
+
this.lintUnhandledIdentifier(value);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Allows using whitelisted imported values
|
|
93
|
+
*/
|
|
94
|
+
if (
|
|
95
|
+
isAllowListedVariable({
|
|
96
|
+
allowList: this.allowedFunctionCalls,
|
|
97
|
+
variable,
|
|
98
|
+
})
|
|
99
|
+
) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const definitions = variable.defs;
|
|
104
|
+
if (definitions.length === 0) {
|
|
105
|
+
this.lintUnhandledIdentifier(value);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
definitions.forEach((definition) => {
|
|
110
|
+
if (definition.type === 'Variable') {
|
|
111
|
+
const initializer = definition.node.init;
|
|
112
|
+
if (!initializer) {
|
|
113
|
+
this.lintUnhandledIdentifier(value);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Variables can be used if they resolve to a literal.
|
|
119
|
+
*/
|
|
120
|
+
if (initializer.type === 'Literal') {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Not allowing tagged template expressions through variables.
|
|
126
|
+
*/
|
|
127
|
+
if (initializer.type === 'TaggedTemplateExpression') {
|
|
128
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Variables can be used if they resolve to an allowed function call.
|
|
134
|
+
*/
|
|
135
|
+
if (initializer.type === 'CallExpression') {
|
|
136
|
+
if (this.isAllowedFunctionCall(initializer)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (initializer.type === 'TemplateLiteral') {
|
|
144
|
+
for (const expression of initializer.expressions) {
|
|
145
|
+
this.lintValue(expression);
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* This branch is only here for clarity.
|
|
152
|
+
*
|
|
153
|
+
* We could fallback to `no-variables` for it too.
|
|
154
|
+
*/
|
|
155
|
+
if (initializer.type === 'ObjectExpression') {
|
|
156
|
+
this.report({ node: value, messageId: 'no-object-references' });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.lintUnhandledIdentifier(value);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private isAllowedDynamicKey(expression: Expression) {
|
|
166
|
+
const identifier = findIdentifierNode(expression);
|
|
167
|
+
if (!identifier) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const variable = findVariable({
|
|
172
|
+
identifier,
|
|
173
|
+
sourceCode: this.context.sourceCode,
|
|
174
|
+
});
|
|
175
|
+
if (!variable) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
isReferenceToCssVar(variable) ||
|
|
181
|
+
isAllowListedVariable({ allowList: this.allowedDynamicKeys, variable })
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private lintKey(property: Property) {
|
|
186
|
+
/**
|
|
187
|
+
* If it's not computed then it must be a plain string.
|
|
188
|
+
*
|
|
189
|
+
* Private identifiers cannot be computed,
|
|
190
|
+
* but we need the extra check for type narrowing.
|
|
191
|
+
*/
|
|
192
|
+
if (!property.computed || property.key.type === 'PrivateIdentifier') {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Some dynamic keys are explicitly allowed.
|
|
198
|
+
*/
|
|
199
|
+
if (this.isAllowedDynamicKey(property.key)) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.report({ node: property.key, messageId: 'no-dynamic-keys' });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private isAllowedFunctionCall(value: CallExpression) {
|
|
207
|
+
const identifier = findIdentifierNode(value);
|
|
208
|
+
if (!identifier) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const variable = findVariable({
|
|
213
|
+
identifier,
|
|
214
|
+
sourceCode: this.context.sourceCode,
|
|
215
|
+
});
|
|
216
|
+
if (!variable) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return isAllowListedVariable({
|
|
221
|
+
allowList: this.allowedFunctionCalls,
|
|
222
|
+
variable,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private lintFunctionCall(value: CallExpression) {
|
|
227
|
+
if (this.isAllowedFunctionCall(value)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private lintObjectExpression(expression: ObjectExpression) {
|
|
235
|
+
for (const property of expression.properties) {
|
|
236
|
+
if (property.type === 'SpreadElement') {
|
|
237
|
+
this.report({
|
|
238
|
+
node: property,
|
|
239
|
+
messageId: 'no-spread-elements',
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
this.lintKey(property);
|
|
243
|
+
this.lintValue(property.value);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private lintUnhandledValue(value: Node) {
|
|
249
|
+
/**
|
|
250
|
+
* Default to blocking value if it isn't explictly handled.
|
|
251
|
+
*/
|
|
252
|
+
this.report({ node: value, messageId: 'no-unsafe-values' });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private lintValue(value: Property['value']): void {
|
|
256
|
+
/**
|
|
257
|
+
* Literals are always allowed by this rule.
|
|
258
|
+
*/
|
|
259
|
+
if (value.type === 'Literal') {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Allow negative numbers.
|
|
265
|
+
*/
|
|
266
|
+
if (
|
|
267
|
+
value.type === 'UnaryExpression' &&
|
|
268
|
+
value.operator === '-' &&
|
|
269
|
+
value.argument.type === 'Literal'
|
|
270
|
+
) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (value.type === 'TemplateLiteral') {
|
|
275
|
+
for (const expression of value.expressions) {
|
|
276
|
+
this.lintValue(expression);
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (
|
|
282
|
+
value.type === 'ConditionalExpression' ||
|
|
283
|
+
value.type === 'LogicalExpression'
|
|
284
|
+
) {
|
|
285
|
+
this.report({ node: value, messageId: 'no-conditional-values' });
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (value.type === 'BinaryExpression') {
|
|
290
|
+
this.lintValue(value.left);
|
|
291
|
+
this.lintValue(value.right);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (value.type === 'ObjectExpression') {
|
|
296
|
+
this.lintObjectExpression(value);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (value.type === 'Identifier') {
|
|
301
|
+
this.lintIdentifier(value);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (value.type === 'TaggedTemplateExpression') {
|
|
306
|
+
this.report({ node: value, messageId: 'no-function-calls' });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (value.type === 'CallExpression') {
|
|
311
|
+
this.lintFunctionCall(value);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (
|
|
316
|
+
value.type === 'ArrowFunctionExpression' ||
|
|
317
|
+
value.type === 'FunctionExpression'
|
|
318
|
+
) {
|
|
319
|
+
/**
|
|
320
|
+
* Function values are handled by `no-dynamic-styles` instead
|
|
321
|
+
*/
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (value.type === 'MemberExpression') {
|
|
326
|
+
this.report({ node: value, messageId: 'no-object-access' });
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (value.type === 'ArrayExpression') {
|
|
331
|
+
this.report({ node: value, messageId: 'no-array-values' });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.lintUnhandledValue(value);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private lintArgument(argument: CallExpression['arguments'][number]) {
|
|
339
|
+
if (argument.type === 'ArrowFunctionExpression') {
|
|
340
|
+
if (argument.body.type === 'ObjectExpression') {
|
|
341
|
+
// Something in this form:
|
|
342
|
+
//
|
|
343
|
+
// const styles = styled.div(
|
|
344
|
+
// (props) => ({ color: props.height }),
|
|
345
|
+
// );
|
|
346
|
+
this.lintArgument(argument.body);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (argument.type === 'ArrayExpression') {
|
|
352
|
+
for (const element of argument.elements) {
|
|
353
|
+
if (element === null) {
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
this.lintArgument(element);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (argument.type === 'ObjectExpression') {
|
|
362
|
+
this.lintObjectExpression(argument);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (argument.type === 'CallExpression') {
|
|
367
|
+
this.lintFunctionCall(argument);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (
|
|
372
|
+
argument.type === 'ConditionalExpression' ||
|
|
373
|
+
argument.type === 'LogicalExpression'
|
|
374
|
+
) {
|
|
375
|
+
this.report({ node: argument, messageId: 'no-conditional-values' });
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (
|
|
380
|
+
argument.type === 'Identifier' ||
|
|
381
|
+
argument.type === 'MemberExpression'
|
|
382
|
+
) {
|
|
383
|
+
this.report({ node: argument, messageId: 'no-identifier-arguments' });
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
this.lintUnhandledValue(argument);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
private lintArguments(args: CallExpression['arguments']) {
|
|
391
|
+
for (const argument of args) {
|
|
392
|
+
this.lintArgument(argument);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
run(): void {
|
|
397
|
+
this.lintArguments(this.baseNode.arguments);
|
|
398
|
+
}
|
|
399
|
+
}
|