@atlaskit/eslint-plugin-design-system 13.17.2 → 13.17.3
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 +6 -0
- package/README.md +1 -0
- package/dist/cjs/presets/all-flat.codegen.js +2 -1
- package/dist/cjs/presets/all.codegen.js +2 -1
- package/dist/cjs/rules/enforce-inline-styles-in-select/index.js +76 -0
- package/dist/cjs/rules/enforce-inline-styles-in-select/utils.js +231 -0
- package/dist/cjs/rules/index.codegen.js +3 -1
- package/dist/cjs/rules/no-nested-styles/index.js +27 -20
- package/dist/es2019/presets/all-flat.codegen.js +2 -1
- package/dist/es2019/presets/all.codegen.js +2 -1
- package/dist/es2019/rules/enforce-inline-styles-in-select/index.js +68 -0
- package/dist/es2019/rules/enforce-inline-styles-in-select/utils.js +217 -0
- package/dist/es2019/rules/index.codegen.js +3 -1
- package/dist/es2019/rules/no-nested-styles/index.js +27 -18
- package/dist/esm/presets/all-flat.codegen.js +2 -1
- package/dist/esm/presets/all.codegen.js +2 -1
- package/dist/esm/rules/enforce-inline-styles-in-select/index.js +70 -0
- package/dist/esm/rules/enforce-inline-styles-in-select/utils.js +225 -0
- package/dist/esm/rules/index.codegen.js +3 -1
- package/dist/esm/rules/no-nested-styles/index.js +27 -20
- package/dist/types/index.codegen.d.ts +5 -0
- package/dist/types/presets/all-flat.codegen.d.ts +1 -0
- package/dist/types/presets/all.codegen.d.ts +1 -0
- package/dist/types/rules/enforce-inline-styles-in-select/index.d.ts +3 -0
- package/dist/types/rules/enforce-inline-styles-in-select/utils.d.ts +2 -0
- package/dist/types/rules/index.codegen.d.ts +1 -0
- package/dist/types-ts4.5/index.codegen.d.ts +5 -0
- package/dist/types-ts4.5/presets/all-flat.codegen.d.ts +1 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +1 -0
- package/dist/types-ts4.5/rules/enforce-inline-styles-in-select/index.d.ts +3 -0
- package/dist/types-ts4.5/rules/enforce-inline-styles-in-select/utils.d.ts +2 -0
- package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
const unsupportedSelectors = [':',
|
|
3
|
+
// pseudo-classes/elements
|
|
4
|
+
'[',
|
|
5
|
+
// attribute selectors
|
|
6
|
+
'>',
|
|
7
|
+
// child combinator
|
|
8
|
+
'+',
|
|
9
|
+
// adjacent sibling combinator
|
|
10
|
+
'~',
|
|
11
|
+
// general sibling combinator
|
|
12
|
+
' ',
|
|
13
|
+
// descendant combinator
|
|
14
|
+
'*',
|
|
15
|
+
// universal selector
|
|
16
|
+
'#',
|
|
17
|
+
// ID selector
|
|
18
|
+
'.',
|
|
19
|
+
// class selector
|
|
20
|
+
'@',
|
|
21
|
+
// at-rules
|
|
22
|
+
'&',
|
|
23
|
+
// parent selector
|
|
24
|
+
'|',
|
|
25
|
+
// namespace separator
|
|
26
|
+
'^',
|
|
27
|
+
// starts with
|
|
28
|
+
'$',
|
|
29
|
+
// ends with
|
|
30
|
+
'=' // equals
|
|
31
|
+
];
|
|
32
|
+
function checkForPseudoClasses(node, objectExpression, context) {
|
|
33
|
+
if (!isNodeOfType(objectExpression, 'ObjectExpression')) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
objectExpression.properties.forEach(prop => {
|
|
37
|
+
if (isNodeOfType(prop, 'Property')) {
|
|
38
|
+
// Check if property key is a pseudo-class
|
|
39
|
+
let keyValue = null;
|
|
40
|
+
if (isNodeOfType(prop.key, 'Literal')) {
|
|
41
|
+
keyValue = prop.key.value;
|
|
42
|
+
} else if (isNodeOfType(prop.key, 'Identifier')) {
|
|
43
|
+
keyValue = prop.key.name;
|
|
44
|
+
}
|
|
45
|
+
if (keyValue && typeof keyValue === 'string' && unsupportedSelectors.some(selector => keyValue.includes(selector))) {
|
|
46
|
+
context.report({
|
|
47
|
+
node: prop.key,
|
|
48
|
+
messageId: 'noPseudoClass',
|
|
49
|
+
data: {
|
|
50
|
+
pseudo: keyValue
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Recursively check nested objects (for function return values)
|
|
56
|
+
if (isNodeOfType(prop.value, 'ArrowFunctionExpression') || isNodeOfType(prop.value, 'FunctionExpression')) {
|
|
57
|
+
// Check the function body for returned object expressions
|
|
58
|
+
const body = prop.value.body;
|
|
59
|
+
if (isNodeOfType(body, 'ObjectExpression')) {
|
|
60
|
+
checkForPseudoClasses(node, body, context);
|
|
61
|
+
} else if (isNodeOfType(body, 'BlockStatement')) {
|
|
62
|
+
// Look for return statements
|
|
63
|
+
body.body.forEach(stmt => {
|
|
64
|
+
if (isNodeOfType(stmt, 'ReturnStatement') && stmt.argument && isNodeOfType(stmt.argument, 'ObjectExpression')) {
|
|
65
|
+
checkForPseudoClasses(node, stmt.argument, context);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
} else if (isNodeOfType(prop.value, 'ObjectExpression')) {
|
|
70
|
+
checkForPseudoClasses(node, prop.value, context);
|
|
71
|
+
}
|
|
72
|
+
} else if (isNodeOfType(prop, 'SpreadElement')) {
|
|
73
|
+
// Handle spread elements like ...styles or ...conditionalStyles
|
|
74
|
+
checkSpreadElement(node, prop, context);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function checkSpreadElement(node, spreadElement, context) {
|
|
79
|
+
if (!isNodeOfType(spreadElement, 'SpreadElement')) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const argument = spreadElement.argument;
|
|
83
|
+
|
|
84
|
+
// Handle direct identifier (e.g., ...styles)
|
|
85
|
+
if (isNodeOfType(argument, 'Identifier')) {
|
|
86
|
+
const scope = context.getScope();
|
|
87
|
+
let variable = null;
|
|
88
|
+
let currentScope = scope;
|
|
89
|
+
|
|
90
|
+
// Search through scope chain
|
|
91
|
+
while (currentScope && !variable) {
|
|
92
|
+
variable = currentScope.variables.find(v => v.name === argument.name);
|
|
93
|
+
currentScope = currentScope.upper;
|
|
94
|
+
}
|
|
95
|
+
if (variable && variable.defs.length > 0) {
|
|
96
|
+
const def = variable.defs[0];
|
|
97
|
+
if (isNodeOfType(def.node, 'VariableDeclarator') && def.node.init) {
|
|
98
|
+
if (isNodeOfType(def.node.init, 'ObjectExpression')) {
|
|
99
|
+
checkForPseudoClasses(node, def.node.init, context);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Handle conditional expressions (e.g., ...(condition ? { ':hover': ... } : undefined))
|
|
105
|
+
else if (isNodeOfType(argument, 'ConditionalExpression')) {
|
|
106
|
+
// Check both consequent and alternate
|
|
107
|
+
if (isNodeOfType(argument.consequent, 'ObjectExpression')) {
|
|
108
|
+
checkForPseudoClasses(node, argument.consequent, context);
|
|
109
|
+
}
|
|
110
|
+
if (argument.alternate && isNodeOfType(argument.alternate, 'ObjectExpression')) {
|
|
111
|
+
checkForPseudoClasses(node, argument.alternate, context);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// Handle logical expressions (e.g., ...condition && { ':hover': ... })
|
|
115
|
+
else if (isNodeOfType(argument, 'LogicalExpression')) {
|
|
116
|
+
if (isNodeOfType(argument.right, 'ObjectExpression')) {
|
|
117
|
+
checkForPseudoClasses(node, argument.right, context);
|
|
118
|
+
}
|
|
119
|
+
if (isNodeOfType(argument.left, 'ObjectExpression')) {
|
|
120
|
+
checkForPseudoClasses(node, argument.left, context);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Handle direct object expressions (e.g., ...{ ':hover': ... })
|
|
124
|
+
else if (isNodeOfType(argument, 'ObjectExpression')) {
|
|
125
|
+
checkForPseudoClasses(node, argument, context);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export function checkStylesObject(node, stylesValue, context) {
|
|
129
|
+
if (!stylesValue) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (isNodeOfType(stylesValue, 'ObjectExpression')) {
|
|
133
|
+
stylesValue.properties.forEach(prop => {
|
|
134
|
+
if (!isNodeOfType(prop, 'Property') || !prop.value || !isNodeOfType(prop.value, 'ArrowFunctionExpression') && !isNodeOfType(prop.value, 'FunctionExpression')) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const body = prop.value.body;
|
|
138
|
+
if (isNodeOfType(body, 'ObjectExpression')) {
|
|
139
|
+
checkForPseudoClasses(node, body, context);
|
|
140
|
+
} else if (isNodeOfType(body, 'BlockStatement')) {
|
|
141
|
+
const visitor = {
|
|
142
|
+
ReturnStatement(returnStmt) {
|
|
143
|
+
if (returnStmt.argument && isNodeOfType(returnStmt.argument, 'ObjectExpression')) {
|
|
144
|
+
checkForPseudoClasses(node, returnStmt.argument, context);
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
AssignmentExpression(assignExpr) {
|
|
148
|
+
// Handle cases like styles[':hover'] = { ... }
|
|
149
|
+
const left = assignExpr.left;
|
|
150
|
+
if (!isNodeOfType(left, 'MemberExpression')) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const property = left.property;
|
|
154
|
+
if (!isNodeOfType(property, 'Literal')) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const value = property.value;
|
|
158
|
+
if (typeof value !== 'string') {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (unsupportedSelectors.some(selector => value.includes(selector))) {
|
|
162
|
+
context.report({
|
|
163
|
+
node: property,
|
|
164
|
+
messageId: 'noPseudoClass',
|
|
165
|
+
data: {
|
|
166
|
+
pseudo: value
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
body.body.forEach(stmt => {
|
|
173
|
+
if (isNodeOfType(stmt, 'ReturnStatement')) {
|
|
174
|
+
visitor.ReturnStatement(stmt);
|
|
175
|
+
} else if (isNodeOfType(stmt, 'ExpressionStatement') && isNodeOfType(stmt.expression, 'AssignmentExpression')) {
|
|
176
|
+
visitor.AssignmentExpression(stmt.expression);
|
|
177
|
+
} else if (isNodeOfType(stmt, 'IfStatement')) {
|
|
178
|
+
const checkBlock = block => {
|
|
179
|
+
if (isNodeOfType(block, 'BlockStatement')) {
|
|
180
|
+
block.body.forEach(innerStmt => {
|
|
181
|
+
if (isNodeOfType(innerStmt, 'ExpressionStatement') && isNodeOfType(innerStmt.expression, 'AssignmentExpression')) {
|
|
182
|
+
visitor.AssignmentExpression(innerStmt.expression);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
} else if (isNodeOfType(block, 'ExpressionStatement') && isNodeOfType(block.expression, 'AssignmentExpression')) {
|
|
186
|
+
visitor.AssignmentExpression(block.expression);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
if (stmt.consequent) {
|
|
190
|
+
checkBlock(stmt.consequent);
|
|
191
|
+
}
|
|
192
|
+
if (stmt.alternate) {
|
|
193
|
+
checkBlock(stmt.alternate);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
} else if (isNodeOfType(stylesValue, 'Identifier')) {
|
|
200
|
+
// track the variable
|
|
201
|
+
const scope = context.getScope();
|
|
202
|
+
let variable = null;
|
|
203
|
+
let currentScope = scope;
|
|
204
|
+
|
|
205
|
+
// Search through scope chain
|
|
206
|
+
while (currentScope && !variable) {
|
|
207
|
+
variable = currentScope.variables.find(v => v.name === stylesValue.name);
|
|
208
|
+
currentScope = currentScope.upper;
|
|
209
|
+
}
|
|
210
|
+
if (variable && variable.defs.length > 0) {
|
|
211
|
+
const def = variable.defs[0];
|
|
212
|
+
if (isNodeOfType(def.node, 'VariableDeclarator') && def.node.init) {
|
|
213
|
+
checkStylesObject(node, def.node.init, context);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::c3ee5646fc648d2510a70195a04d7fe9>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
import consistentCssPropUsage from './consistent-css-prop-usage';
|
|
7
|
+
import enforceInlineStylesInSelect from './enforce-inline-styles-in-select';
|
|
7
8
|
import ensureDesignTokenUsage from './ensure-design-token-usage';
|
|
8
9
|
import ensureDesignTokenUsagePreview from './ensure-design-token-usage-preview';
|
|
9
10
|
import ensureIconColor from './ensure-icon-color';
|
|
@@ -68,6 +69,7 @@ import useTokensTypography from './use-tokens-typography';
|
|
|
68
69
|
import useVisuallyHidden from './use-visually-hidden';
|
|
69
70
|
export const rules = {
|
|
70
71
|
'consistent-css-prop-usage': consistentCssPropUsage,
|
|
72
|
+
'enforce-inline-styles-in-select': enforceInlineStylesInSelect,
|
|
71
73
|
'ensure-design-token-usage': ensureDesignTokenUsage,
|
|
72
74
|
'ensure-design-token-usage/preview': ensureDesignTokenUsagePreview,
|
|
73
75
|
'ensure-icon-color': ensureIconColor,
|
|
@@ -50,16 +50,21 @@ const getKeyValue = (node, context) => {
|
|
|
50
50
|
}
|
|
51
51
|
return '';
|
|
52
52
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
|
|
54
|
+
// const isWidthMediaQuery = (rawSelector: string): boolean => {
|
|
55
|
+
// const selectors = parseSelector(rawSelector);
|
|
56
|
+
|
|
57
|
+
// if (selectors[0].startsWith('@')) {
|
|
58
|
+
// // If the selector includes a min-width/max-width query, return false - the primitives media object should be used instead:
|
|
59
|
+
// // https://staging.atlassian.design/components/primitives/responsive/breakpoints/examples
|
|
60
|
+
// // Otherwise return true, non-width queries are acceptable
|
|
61
|
+
// return selectors.some(
|
|
62
|
+
// (selector) => selector.includes('min-width') || selector.includes('max-width'),
|
|
63
|
+
// );
|
|
64
|
+
// }
|
|
65
|
+
// return false;
|
|
66
|
+
// };
|
|
67
|
+
|
|
63
68
|
const isAllowedNestedSelector = rawSelector => {
|
|
64
69
|
if (rawSelector.trim() === '&') {
|
|
65
70
|
// This can be written without the nest.
|
|
@@ -87,7 +92,8 @@ const rule = createLintRule({
|
|
|
87
92
|
severity: 'error'
|
|
88
93
|
},
|
|
89
94
|
messages: {
|
|
90
|
-
noWidthQueries:
|
|
95
|
+
// noWidthQueries:
|
|
96
|
+
// 'Media queries that target min-width or max-width are not allowed. Use the media object provided by the Atlassian Design System instead. https://staging.atlassian.design/components/primitives/responsive/breakpoints/examples',
|
|
91
97
|
noNestedStyles: 'Nested styles are not allowed as they can change unexpectedly when child markup changes and result in duplicates when extracting to CSS.',
|
|
92
98
|
noDirectNestedStyles: `Styles applied with data attributes are not allowed, split them into discrete CSS declarations and apply them conditionally with JavaScript.
|
|
93
99
|
|
|
@@ -112,13 +118,16 @@ const disabledStyles = css({ opacity: 0.5 });
|
|
|
112
118
|
});
|
|
113
119
|
return;
|
|
114
120
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
|
|
122
|
+
// if (isWidthMediaQuery(getKeyValue(node.key as Rule.Node, context))) {
|
|
123
|
+
// context.report({
|
|
124
|
+
// node,
|
|
125
|
+
// messageId: 'noWidthQueries',
|
|
126
|
+
// });
|
|
127
|
+
|
|
128
|
+
// return;
|
|
129
|
+
// }
|
|
130
|
+
|
|
122
131
|
if (!isAllowedNestedSelector(getKeyValue(node.key, context))) {
|
|
123
132
|
context.report({
|
|
124
133
|
node,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::a8f7f6388f0499adc9db0228b7426aee>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -9,6 +9,7 @@ export default {
|
|
|
9
9
|
plugins: {},
|
|
10
10
|
rules: {
|
|
11
11
|
'@atlaskit/design-system/consistent-css-prop-usage': 'error',
|
|
12
|
+
'@atlaskit/design-system/enforce-inline-styles-in-select': 'error',
|
|
12
13
|
'@atlaskit/design-system/ensure-design-token-usage': 'error',
|
|
13
14
|
'@atlaskit/design-system/ensure-design-token-usage/preview': 'warn',
|
|
14
15
|
'@atlaskit/design-system/ensure-icon-color': 'error',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::aaf4f1ee2f2db236ebed7b77de426181>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -8,6 +8,7 @@ export default {
|
|
|
8
8
|
plugins: ['@atlaskit/design-system'],
|
|
9
9
|
rules: {
|
|
10
10
|
'@atlaskit/design-system/consistent-css-prop-usage': 'error',
|
|
11
|
+
'@atlaskit/design-system/enforce-inline-styles-in-select': 'error',
|
|
11
12
|
'@atlaskit/design-system/ensure-design-token-usage': 'error',
|
|
12
13
|
'@atlaskit/design-system/ensure-design-token-usage/preview': 'warn',
|
|
13
14
|
'@atlaskit/design-system/ensure-icon-color': 'error',
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import { createLintRule } from '../utils/create-rule';
|
|
3
|
+
import { checkStylesObject } from './utils';
|
|
4
|
+
var rule = createLintRule({
|
|
5
|
+
meta: {
|
|
6
|
+
name: 'enforce-inline-styles-in-select',
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'Disallow unsupported CSS selectors in styles prop for @atlaskit/select and require inline styles only',
|
|
9
|
+
recommended: false,
|
|
10
|
+
severity: 'error'
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
noPseudoClass: "This selector '{{pseudo}}' is not allowed in styles for @atlaskit/select. Please use the `components` API in select with `xcss` props.",
|
|
14
|
+
noVariableStyles: 'Variable-defined styles are not allowed for @atlaskit/select. Please use inline styles object or the `components` API with `xcss` props.'
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
create: function create(context) {
|
|
18
|
+
// Track imports of @atlaskit/select
|
|
19
|
+
var atlaskitSelectImports = new Set();
|
|
20
|
+
return {
|
|
21
|
+
ImportDeclaration: function ImportDeclaration(node) {
|
|
22
|
+
if (node.source.value !== '@atlaskit/select') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
node.specifiers.forEach(function (spec) {
|
|
26
|
+
if (isNodeOfType(spec, 'ImportDefaultSpecifier')) {
|
|
27
|
+
atlaskitSelectImports.add(spec.local.name);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
JSXElement: function JSXElement(node) {
|
|
32
|
+
if (!isNodeOfType(node, 'JSXElement')) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Check if this is a Select component from @atlaskit/select
|
|
37
|
+
if (isNodeOfType(node.openingElement.name, 'JSXIdentifier') && atlaskitSelectImports.has(node.openingElement.name.name)) {
|
|
38
|
+
// Look for styles prop
|
|
39
|
+
var stylesAttr = node.openingElement.attributes.find(function (attr) {
|
|
40
|
+
return isNodeOfType(attr, 'JSXAttribute') && isNodeOfType(attr.name, 'JSXIdentifier') && attr.name.name === 'styles';
|
|
41
|
+
});
|
|
42
|
+
if (stylesAttr && isNodeOfType(stylesAttr, 'JSXAttribute') && stylesAttr.value) {
|
|
43
|
+
if (isNodeOfType(stylesAttr.value, 'JSXExpressionContainer')) {
|
|
44
|
+
var expression = stylesAttr.value.expression;
|
|
45
|
+
|
|
46
|
+
// Check if it's an inline object expression
|
|
47
|
+
if (isNodeOfType(expression, 'ObjectExpression')) {
|
|
48
|
+
// This is an inline styles object - check for unsupported selectors
|
|
49
|
+
checkStylesObject(node, expression, context);
|
|
50
|
+
} else if (isNodeOfType(expression, 'Identifier')) {
|
|
51
|
+
// This is a variable reference - not allowed
|
|
52
|
+
context.report({
|
|
53
|
+
node: expression,
|
|
54
|
+
messageId: 'noVariableStyles'
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
// Any other expression type (function calls, member expressions, etc.) - not allowed
|
|
58
|
+
context.report({
|
|
59
|
+
node: expression,
|
|
60
|
+
messageId: 'noVariableStyles'
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
export default rule;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
var unsupportedSelectors = [':',
|
|
3
|
+
// pseudo-classes/elements
|
|
4
|
+
'[',
|
|
5
|
+
// attribute selectors
|
|
6
|
+
'>',
|
|
7
|
+
// child combinator
|
|
8
|
+
'+',
|
|
9
|
+
// adjacent sibling combinator
|
|
10
|
+
'~',
|
|
11
|
+
// general sibling combinator
|
|
12
|
+
' ',
|
|
13
|
+
// descendant combinator
|
|
14
|
+
'*',
|
|
15
|
+
// universal selector
|
|
16
|
+
'#',
|
|
17
|
+
// ID selector
|
|
18
|
+
'.',
|
|
19
|
+
// class selector
|
|
20
|
+
'@',
|
|
21
|
+
// at-rules
|
|
22
|
+
'&',
|
|
23
|
+
// parent selector
|
|
24
|
+
'|',
|
|
25
|
+
// namespace separator
|
|
26
|
+
'^',
|
|
27
|
+
// starts with
|
|
28
|
+
'$',
|
|
29
|
+
// ends with
|
|
30
|
+
'=' // equals
|
|
31
|
+
];
|
|
32
|
+
function checkForPseudoClasses(node, objectExpression, context) {
|
|
33
|
+
if (!isNodeOfType(objectExpression, 'ObjectExpression')) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
objectExpression.properties.forEach(function (prop) {
|
|
37
|
+
if (isNodeOfType(prop, 'Property')) {
|
|
38
|
+
// Check if property key is a pseudo-class
|
|
39
|
+
var keyValue = null;
|
|
40
|
+
if (isNodeOfType(prop.key, 'Literal')) {
|
|
41
|
+
keyValue = prop.key.value;
|
|
42
|
+
} else if (isNodeOfType(prop.key, 'Identifier')) {
|
|
43
|
+
keyValue = prop.key.name;
|
|
44
|
+
}
|
|
45
|
+
if (keyValue && typeof keyValue === 'string' && unsupportedSelectors.some(function (selector) {
|
|
46
|
+
return keyValue.includes(selector);
|
|
47
|
+
})) {
|
|
48
|
+
context.report({
|
|
49
|
+
node: prop.key,
|
|
50
|
+
messageId: 'noPseudoClass',
|
|
51
|
+
data: {
|
|
52
|
+
pseudo: keyValue
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Recursively check nested objects (for function return values)
|
|
58
|
+
if (isNodeOfType(prop.value, 'ArrowFunctionExpression') || isNodeOfType(prop.value, 'FunctionExpression')) {
|
|
59
|
+
// Check the function body for returned object expressions
|
|
60
|
+
var body = prop.value.body;
|
|
61
|
+
if (isNodeOfType(body, 'ObjectExpression')) {
|
|
62
|
+
checkForPseudoClasses(node, body, context);
|
|
63
|
+
} else if (isNodeOfType(body, 'BlockStatement')) {
|
|
64
|
+
// Look for return statements
|
|
65
|
+
body.body.forEach(function (stmt) {
|
|
66
|
+
if (isNodeOfType(stmt, 'ReturnStatement') && stmt.argument && isNodeOfType(stmt.argument, 'ObjectExpression')) {
|
|
67
|
+
checkForPseudoClasses(node, stmt.argument, context);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
} else if (isNodeOfType(prop.value, 'ObjectExpression')) {
|
|
72
|
+
checkForPseudoClasses(node, prop.value, context);
|
|
73
|
+
}
|
|
74
|
+
} else if (isNodeOfType(prop, 'SpreadElement')) {
|
|
75
|
+
// Handle spread elements like ...styles or ...conditionalStyles
|
|
76
|
+
checkSpreadElement(node, prop, context);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function checkSpreadElement(node, spreadElement, context) {
|
|
81
|
+
if (!isNodeOfType(spreadElement, 'SpreadElement')) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
var argument = spreadElement.argument;
|
|
85
|
+
|
|
86
|
+
// Handle direct identifier (e.g., ...styles)
|
|
87
|
+
if (isNodeOfType(argument, 'Identifier')) {
|
|
88
|
+
var scope = context.getScope();
|
|
89
|
+
var variable = null;
|
|
90
|
+
var currentScope = scope;
|
|
91
|
+
|
|
92
|
+
// Search through scope chain
|
|
93
|
+
while (currentScope && !variable) {
|
|
94
|
+
variable = currentScope.variables.find(function (v) {
|
|
95
|
+
return v.name === argument.name;
|
|
96
|
+
});
|
|
97
|
+
currentScope = currentScope.upper;
|
|
98
|
+
}
|
|
99
|
+
if (variable && variable.defs.length > 0) {
|
|
100
|
+
var def = variable.defs[0];
|
|
101
|
+
if (isNodeOfType(def.node, 'VariableDeclarator') && def.node.init) {
|
|
102
|
+
if (isNodeOfType(def.node.init, 'ObjectExpression')) {
|
|
103
|
+
checkForPseudoClasses(node, def.node.init, context);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Handle conditional expressions (e.g., ...(condition ? { ':hover': ... } : undefined))
|
|
109
|
+
else if (isNodeOfType(argument, 'ConditionalExpression')) {
|
|
110
|
+
// Check both consequent and alternate
|
|
111
|
+
if (isNodeOfType(argument.consequent, 'ObjectExpression')) {
|
|
112
|
+
checkForPseudoClasses(node, argument.consequent, context);
|
|
113
|
+
}
|
|
114
|
+
if (argument.alternate && isNodeOfType(argument.alternate, 'ObjectExpression')) {
|
|
115
|
+
checkForPseudoClasses(node, argument.alternate, context);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Handle logical expressions (e.g., ...condition && { ':hover': ... })
|
|
119
|
+
else if (isNodeOfType(argument, 'LogicalExpression')) {
|
|
120
|
+
if (isNodeOfType(argument.right, 'ObjectExpression')) {
|
|
121
|
+
checkForPseudoClasses(node, argument.right, context);
|
|
122
|
+
}
|
|
123
|
+
if (isNodeOfType(argument.left, 'ObjectExpression')) {
|
|
124
|
+
checkForPseudoClasses(node, argument.left, context);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// Handle direct object expressions (e.g., ...{ ':hover': ... })
|
|
128
|
+
else if (isNodeOfType(argument, 'ObjectExpression')) {
|
|
129
|
+
checkForPseudoClasses(node, argument, context);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export function checkStylesObject(node, stylesValue, context) {
|
|
133
|
+
if (!stylesValue) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (isNodeOfType(stylesValue, 'ObjectExpression')) {
|
|
137
|
+
stylesValue.properties.forEach(function (prop) {
|
|
138
|
+
if (!isNodeOfType(prop, 'Property') || !prop.value || !isNodeOfType(prop.value, 'ArrowFunctionExpression') && !isNodeOfType(prop.value, 'FunctionExpression')) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
var body = prop.value.body;
|
|
142
|
+
if (isNodeOfType(body, 'ObjectExpression')) {
|
|
143
|
+
checkForPseudoClasses(node, body, context);
|
|
144
|
+
} else if (isNodeOfType(body, 'BlockStatement')) {
|
|
145
|
+
var visitor = {
|
|
146
|
+
ReturnStatement: function ReturnStatement(returnStmt) {
|
|
147
|
+
if (returnStmt.argument && isNodeOfType(returnStmt.argument, 'ObjectExpression')) {
|
|
148
|
+
checkForPseudoClasses(node, returnStmt.argument, context);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
AssignmentExpression: function AssignmentExpression(assignExpr) {
|
|
152
|
+
// Handle cases like styles[':hover'] = { ... }
|
|
153
|
+
var left = assignExpr.left;
|
|
154
|
+
if (!isNodeOfType(left, 'MemberExpression')) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
var property = left.property;
|
|
158
|
+
if (!isNodeOfType(property, 'Literal')) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
var value = property.value;
|
|
162
|
+
if (typeof value !== 'string') {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (unsupportedSelectors.some(function (selector) {
|
|
166
|
+
return value.includes(selector);
|
|
167
|
+
})) {
|
|
168
|
+
context.report({
|
|
169
|
+
node: property,
|
|
170
|
+
messageId: 'noPseudoClass',
|
|
171
|
+
data: {
|
|
172
|
+
pseudo: value
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
body.body.forEach(function (stmt) {
|
|
179
|
+
if (isNodeOfType(stmt, 'ReturnStatement')) {
|
|
180
|
+
visitor.ReturnStatement(stmt);
|
|
181
|
+
} else if (isNodeOfType(stmt, 'ExpressionStatement') && isNodeOfType(stmt.expression, 'AssignmentExpression')) {
|
|
182
|
+
visitor.AssignmentExpression(stmt.expression);
|
|
183
|
+
} else if (isNodeOfType(stmt, 'IfStatement')) {
|
|
184
|
+
var checkBlock = function checkBlock(block) {
|
|
185
|
+
if (isNodeOfType(block, 'BlockStatement')) {
|
|
186
|
+
block.body.forEach(function (innerStmt) {
|
|
187
|
+
if (isNodeOfType(innerStmt, 'ExpressionStatement') && isNodeOfType(innerStmt.expression, 'AssignmentExpression')) {
|
|
188
|
+
visitor.AssignmentExpression(innerStmt.expression);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
} else if (isNodeOfType(block, 'ExpressionStatement') && isNodeOfType(block.expression, 'AssignmentExpression')) {
|
|
192
|
+
visitor.AssignmentExpression(block.expression);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
if (stmt.consequent) {
|
|
196
|
+
checkBlock(stmt.consequent);
|
|
197
|
+
}
|
|
198
|
+
if (stmt.alternate) {
|
|
199
|
+
checkBlock(stmt.alternate);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
} else if (isNodeOfType(stylesValue, 'Identifier')) {
|
|
206
|
+
// track the variable
|
|
207
|
+
var scope = context.getScope();
|
|
208
|
+
var variable = null;
|
|
209
|
+
var currentScope = scope;
|
|
210
|
+
|
|
211
|
+
// Search through scope chain
|
|
212
|
+
while (currentScope && !variable) {
|
|
213
|
+
variable = currentScope.variables.find(function (v) {
|
|
214
|
+
return v.name === stylesValue.name;
|
|
215
|
+
});
|
|
216
|
+
currentScope = currentScope.upper;
|
|
217
|
+
}
|
|
218
|
+
if (variable && variable.defs.length > 0) {
|
|
219
|
+
var def = variable.defs[0];
|
|
220
|
+
if (isNodeOfType(def.node, 'VariableDeclarator') && def.node.init) {
|
|
221
|
+
checkStylesObject(node, def.node.init, context);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::c3ee5646fc648d2510a70195a04d7fe9>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
import consistentCssPropUsage from './consistent-css-prop-usage';
|
|
7
|
+
import enforceInlineStylesInSelect from './enforce-inline-styles-in-select';
|
|
7
8
|
import ensureDesignTokenUsage from './ensure-design-token-usage';
|
|
8
9
|
import ensureDesignTokenUsagePreview from './ensure-design-token-usage-preview';
|
|
9
10
|
import ensureIconColor from './ensure-icon-color';
|
|
@@ -68,6 +69,7 @@ import useTokensTypography from './use-tokens-typography';
|
|
|
68
69
|
import useVisuallyHidden from './use-visually-hidden';
|
|
69
70
|
export var rules = {
|
|
70
71
|
'consistent-css-prop-usage': consistentCssPropUsage,
|
|
72
|
+
'enforce-inline-styles-in-select': enforceInlineStylesInSelect,
|
|
71
73
|
'ensure-design-token-usage': ensureDesignTokenUsage,
|
|
72
74
|
'ensure-design-token-usage/preview': ensureDesignTokenUsagePreview,
|
|
73
75
|
'ensure-icon-color': ensureIconColor,
|