@atlaskit/eslint-plugin-ui-styling-standard 0.18.0 → 0.18.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 +8 -0
- package/package.json +1 -1
- package/afm-cc/tsconfig.json +0 -28
- package/afm-jira/tsconfig.json +0 -27
- package/build/tsconfig.json +0 -18
- package/constellation/atlaskit-theme/usage.mdx +0 -132
- package/constellation/consistent-css-prop-usage/usage.mdx +0 -7
- package/constellation/convert-props-syntax/usage.mdx +0 -130
- package/constellation/enforce-style-prop/usage.mdx +0 -47
- package/constellation/index/migration-guide.mdx +0 -309
- package/constellation/index/overview.mdx +0 -250
- package/constellation/index/props.mdx +0 -30
- package/constellation/local-cx-xcss/usage.mdx +0 -53
- package/constellation/no-array-arguments/usage.mdx +0 -49
- package/constellation/no-classname-prop/usage.mdx +0 -49
- package/constellation/no-container-queries/usage.mdx +0 -101
- package/constellation/no-css-tagged-template-expression/usage.mdx +0 -8
- package/constellation/no-dynamic-styles/usage.mdx +0 -130
- package/constellation/no-empty-styled-expression/usage.mdx +0 -8
- package/constellation/no-exported-css/usage.mdx +0 -8
- package/constellation/no-exported-keyframes/usage.mdx +0 -8
- package/constellation/no-exported-styles/usage.mdx +0 -87
- package/constellation/no-global-styles/usage.mdx +0 -60
- package/constellation/no-important-styles/usage.mdx +0 -33
- package/constellation/no-imported-style-values/usage.mdx +0 -157
- package/constellation/no-invalid-css-map/usage.mdx +0 -8
- package/constellation/no-js-xcss/usage.mdx +0 -7
- package/constellation/no-keyframes-tagged-template-expression/usage.mdx +0 -8
- package/constellation/no-nested-selectors/usage.mdx +0 -95
- package/constellation/no-styled/usage.mdx +0 -92
- package/constellation/no-styled-tagged-template-expression/usage.mdx +0 -8
- package/constellation/no-suppress-xcss/usage.mdx +0 -28
- package/constellation/no-unsafe-selectors/usage.mdx +0 -370
- package/constellation/no-unsafe-values/usage.mdx +0 -318
- package/constellation/use-compiled/usage.mdx +0 -54
- package/index.js +0 -24
- package/scripts/codegen.tsx +0 -373
- package/src/index.codegen.tsx +0 -14
- package/src/index.tsx +0 -1
- package/src/presets/all.codegen.tsx +0 -79
- package/src/presets/recommended.codegen.tsx +0 -79
- package/src/rules/TEMPLATE.md +0 -29
- package/src/rules/__tests__/utils/_tester.tsx +0 -40
- package/src/rules/atlaskit-theme/README.md +0 -130
- package/src/rules/atlaskit-theme/__tests__/test.ts +0 -133
- package/src/rules/atlaskit-theme/index.ts +0 -134
- package/src/rules/convert-props-syntax/README.md +0 -128
- package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +0 -76
- package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +0 -584
- package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +0 -140
- package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +0 -69
- package/src/rules/convert-props-syntax/index.tsx +0 -414
- package/src/rules/enforce-style-prop/README.md +0 -45
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +0 -243
- package/src/rules/enforce-style-prop/index.tsx +0 -172
- package/src/rules/external-rules.tsx +0 -177
- package/src/rules/index.codegen.tsx +0 -42
- package/src/rules/local-cx-xcss/README.md +0 -51
- package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +0 -102
- package/src/rules/local-cx-xcss/index.tsx +0 -62
- package/src/rules/no-array-arguments/README.md +0 -47
- package/src/rules/no-array-arguments/__tests__/rule.test.tsx +0 -107
- package/src/rules/no-array-arguments/index.tsx +0 -86
- package/src/rules/no-classname-prop/README.md +0 -47
- package/src/rules/no-classname-prop/__tests__/rule.test.tsx +0 -39
- package/src/rules/no-classname-prop/index.tsx +0 -34
- package/src/rules/no-container-queries/README.md +0 -99
- package/src/rules/no-container-queries/__tests__/rule.test.tsx +0 -139
- package/src/rules/no-container-queries/index.tsx +0 -94
- package/src/rules/no-dynamic-styles/README.md +0 -128
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +0 -135
- package/src/rules/no-dynamic-styles/index.tsx +0 -60
- package/src/rules/no-exported-styles/README.md +0 -85
- package/src/rules/no-exported-styles/__tests__/rule.test.tsx +0 -216
- package/src/rules/no-exported-styles/index.tsx +0 -52
- package/src/rules/no-global-styles/README.md +0 -58
- package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +0 -33
- package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +0 -46
- package/src/rules/no-global-styles/index.tsx +0 -82
- package/src/rules/no-important-styles/README.md +0 -31
- package/src/rules/no-important-styles/__tests__/rule.test.tsx +0 -165
- package/src/rules/no-important-styles/index.tsx +0 -70
- package/src/rules/no-imported-style-values/README.md +0 -155
- package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +0 -491
- package/src/rules/no-imported-style-values/index.tsx +0 -188
- package/src/rules/no-nested-selectors/README.md +0 -93
- package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +0 -335
- package/src/rules/no-nested-selectors/index.tsx +0 -99
- package/src/rules/no-styled/README.md +0 -90
- package/src/rules/no-styled/__tests__/rule.test.tsx +0 -73
- package/src/rules/no-styled/index.tsx +0 -45
- package/src/rules/no-unsafe-selectors/README.md +0 -368
- package/src/rules/no-unsafe-selectors/__tests__/no-ambiguous-pseudos.test.tsx +0 -368
- package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +0 -57
- package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +0 -73
- package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +0 -40
- package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +0 -101
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +0 -131
- package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +0 -132
- package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +0 -25
- package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +0 -35
- package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +0 -257
- package/src/rules/no-unsafe-selectors/constants.tsx +0 -91
- package/src/rules/no-unsafe-selectors/index.tsx +0 -198
- package/src/rules/no-unsafe-selectors/lint-selector.tsx +0 -155
- package/src/rules/no-unsafe-selectors/source-position-utils.tsx +0 -55
- package/src/rules/no-unsafe-selectors/walk-css-map.tsx +0 -226
- package/src/rules/no-unsafe-values/README.md +0 -316
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +0 -27
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +0 -37
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +0 -229
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +0 -267
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +0 -52
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +0 -106
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +0 -44
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +0 -48
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +0 -75
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +0 -121
- package/src/rules/no-unsafe-values/index.tsx +0 -94
- package/src/rules/no-unsafe-values/messages.tsx +0 -86
- package/src/rules/no-unsafe-values/utils.tsx +0 -387
- package/src/rules/use-compiled/README.md +0 -52
- package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +0 -73
- package/src/rules/use-compiled/__tests__/css.test.tsx +0 -126
- package/src/rules/use-compiled/__tests__/imports.test.tsx +0 -158
- package/src/rules/use-compiled/__tests__/keyframes.test.tsx +0 -140
- package/src/rules/use-compiled/__tests__/pragma.test.tsx +0 -108
- package/src/rules/use-compiled/__tests__/styled.test.tsx +0 -114
- package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +0 -15
- package/src/rules/use-compiled/index.tsx +0 -252
- package/src/rules/use-compiled/is-safe.tsx +0 -159
- package/src/rules/use-compiled/utils.tsx +0 -33
- package/src/rules/utils/create-rule-with-typed-config.tsx +0 -86
- package/src/rules/utils/create-rule.tsx +0 -13
- package/tsconfig.app.json +0 -47
- package/tsconfig.dev.json +0 -45
- package/tsconfig.json +0 -25
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { createLintRule } from '../utils/create-rule';
|
|
2
|
-
import {
|
|
3
|
-
getImportSources,
|
|
4
|
-
hasStyleObjectArguments,
|
|
5
|
-
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
6
|
-
import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
7
|
-
import type { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree/dist/ts-estree';
|
|
8
|
-
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
9
|
-
import type { Rule } from 'eslint';
|
|
10
|
-
import {
|
|
11
|
-
type AllowList,
|
|
12
|
-
getAllowedDynamicKeys,
|
|
13
|
-
getAllowedFunctionCalls,
|
|
14
|
-
isAllowListedVariable,
|
|
15
|
-
} from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
16
|
-
import type * as ESTree from 'eslint-codemod-utils';
|
|
17
|
-
import esquery from 'esquery';
|
|
18
|
-
import estraverse from 'estraverse';
|
|
19
|
-
|
|
20
|
-
const schema: JSONSchema4 = [
|
|
21
|
-
{
|
|
22
|
-
type: 'object',
|
|
23
|
-
properties: {
|
|
24
|
-
importSources: {
|
|
25
|
-
type: 'array',
|
|
26
|
-
items: { type: 'string' },
|
|
27
|
-
uniqueItems: true,
|
|
28
|
-
},
|
|
29
|
-
allowedFunctionCalls: {
|
|
30
|
-
type: 'array',
|
|
31
|
-
items: {
|
|
32
|
-
type: 'array',
|
|
33
|
-
minItems: 2,
|
|
34
|
-
maxItems: 2,
|
|
35
|
-
items: [{ type: 'string' }, { type: 'string' }],
|
|
36
|
-
},
|
|
37
|
-
uniqueItems: true,
|
|
38
|
-
},
|
|
39
|
-
allowedDynamicKeys: {
|
|
40
|
-
type: 'array',
|
|
41
|
-
items: {
|
|
42
|
-
type: 'array',
|
|
43
|
-
minItems: 2,
|
|
44
|
-
maxItems: 2,
|
|
45
|
-
items: [{ type: 'string' }, { type: 'string' }],
|
|
46
|
-
},
|
|
47
|
-
uniqueItems: true,
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const isIdentifierImported = (
|
|
54
|
-
identifier: ESTree.Identifier,
|
|
55
|
-
context: Rule.RuleContext,
|
|
56
|
-
functionAllowList: AllowList,
|
|
57
|
-
keysAllowList: AllowList,
|
|
58
|
-
): boolean => {
|
|
59
|
-
const variable = findVariable({
|
|
60
|
-
identifier: identifier,
|
|
61
|
-
sourceCode: context.getSourceCode(),
|
|
62
|
-
});
|
|
63
|
-
if (!variable) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const isImported = variable.defs.some((definition) => definition.type === 'ImportBinding');
|
|
68
|
-
|
|
69
|
-
// check if imported variable is allowed
|
|
70
|
-
return (
|
|
71
|
-
isImported &&
|
|
72
|
-
!isAllowListedVariable({
|
|
73
|
-
allowList: functionAllowList,
|
|
74
|
-
variable,
|
|
75
|
-
}) &&
|
|
76
|
-
!isAllowListedVariable({ allowList: keysAllowList, variable })
|
|
77
|
-
);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const checkIdentifier = (
|
|
81
|
-
identifier: ESTree.Identifier,
|
|
82
|
-
context: Rule.RuleContext,
|
|
83
|
-
functionAllowList: AllowList,
|
|
84
|
-
keysAllowList: AllowList,
|
|
85
|
-
) => {
|
|
86
|
-
const { parent } = identifier as Rule.Node;
|
|
87
|
-
|
|
88
|
-
// Even though `member` in `object.member` is syntactically an identifier,
|
|
89
|
-
// it should be ignored as `.member` is syntax sugar for `['member']` here.
|
|
90
|
-
// We should still lint `object[member]` (when `parent.computed` === true)
|
|
91
|
-
if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Even though `key` in `{ key: 'value' }` is syntactically an identifier,
|
|
96
|
-
// it should be ignored as `key` is syntax sugar for `'key'` here.
|
|
97
|
-
// We should still lint `[key]` (when `parent.computed` === true)
|
|
98
|
-
if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
|
|
103
|
-
context.report({
|
|
104
|
-
node: identifier,
|
|
105
|
-
messageId: 'no-imported-style-values',
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
export const rule = createLintRule({
|
|
111
|
-
meta: {
|
|
112
|
-
name: 'no-imported-style-values',
|
|
113
|
-
docs: {
|
|
114
|
-
description: 'Disallows imports of style values',
|
|
115
|
-
recommended: true,
|
|
116
|
-
severity: 'error',
|
|
117
|
-
},
|
|
118
|
-
messages: {
|
|
119
|
-
'no-imported-style-values':
|
|
120
|
-
'Styles should not be imported to use in css, cssMap, styled, keyframes or xcss calls. All styles should be defined and used in the same file instead.',
|
|
121
|
-
},
|
|
122
|
-
type: 'problem',
|
|
123
|
-
schema,
|
|
124
|
-
},
|
|
125
|
-
create(context) {
|
|
126
|
-
const importSources = getImportSources(context);
|
|
127
|
-
const functionAllowList = getAllowedFunctionCalls(context.options);
|
|
128
|
-
const keysAllowList = getAllowedDynamicKeys(context.options);
|
|
129
|
-
|
|
130
|
-
const checkForIdentifiers = (node: ESTree.Node) => {
|
|
131
|
-
const matches = esquery(node, 'Identifier');
|
|
132
|
-
|
|
133
|
-
for (const match of matches) {
|
|
134
|
-
if (match.type === 'Identifier') {
|
|
135
|
-
checkIdentifier(match, context, functionAllowList, keysAllowList);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
return {
|
|
141
|
-
// Checking css/cssMap/keyframes/styled/xcss calls
|
|
142
|
-
CallExpression(node) {
|
|
143
|
-
const { references } = context.getScope();
|
|
144
|
-
|
|
145
|
-
if (!hasStyleObjectArguments(node.callee, references, importSources)) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
node.arguments.forEach(checkForIdentifiers);
|
|
149
|
-
},
|
|
150
|
-
'JSXAttribute[name.name=/^style|css$/]'(jsxAttribute: ESTree.Node) {
|
|
151
|
-
estraverse.traverse(jsxAttribute, {
|
|
152
|
-
fallback(node) {
|
|
153
|
-
// estraverse does not know about nodes not in the ESTree standard.
|
|
154
|
-
// This includes JSX nodes, so we must provide a fallback method to handle them.
|
|
155
|
-
// Using `fallback: 'iteration'` can cause cycles because it will iterate
|
|
156
|
-
// over the `parent` attribute on JSX nodes.
|
|
157
|
-
return Object.keys(node).filter((key) => key !== 'parent');
|
|
158
|
-
},
|
|
159
|
-
enter(node, parent) {
|
|
160
|
-
// Ignore type contexts
|
|
161
|
-
// Cast required because ESTree is not aware of TypeScript nodes
|
|
162
|
-
if ((node.type as AST_NODE_TYPES) === 'TSTypeReference') {
|
|
163
|
-
return estraverse.VisitorOption.Skip;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Ignore conditions of ternary expressions
|
|
167
|
-
if (parent?.type === 'ConditionalExpression' && node === parent.test) {
|
|
168
|
-
return estraverse.VisitorOption.Skip;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Ignore LHS of logical expressions (&&, ||)
|
|
172
|
-
if (parent?.type === 'LogicalExpression' && node === parent.left) {
|
|
173
|
-
return estraverse.VisitorOption.Skip;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (node.type !== 'Identifier') {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
checkIdentifier(node, context, functionAllowList, keysAllowList);
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
},
|
|
184
|
-
};
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
export default rule;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
Blocks nested selectors in style declarations.
|
|
2
|
-
|
|
3
|
-
Use styles which do not require context from other elements.
|
|
4
|
-
|
|
5
|
-
## Examples
|
|
6
|
-
|
|
7
|
-
### Incorrect
|
|
8
|
-
|
|
9
|
-
```tsx
|
|
10
|
-
import { css } from '@compiled/react';
|
|
11
|
-
import { token } from '@atlaskit/tokens';
|
|
12
|
-
|
|
13
|
-
const styles = css({
|
|
14
|
-
h2: {
|
|
15
|
-
font: token('font.heading.large'),
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const Component = () => (
|
|
20
|
-
<div css={styles}>
|
|
21
|
-
<h2>Heading</h2>
|
|
22
|
-
</div>
|
|
23
|
-
);
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
```tsx
|
|
27
|
-
import { styled } from '@compiled/react';
|
|
28
|
-
import { token } from '@atlaskit/tokens';
|
|
29
|
-
|
|
30
|
-
const Table = styled.table({
|
|
31
|
-
border: `1px solid ${token('color.border')}`,
|
|
32
|
-
td: {
|
|
33
|
-
padding: token('space.100'),
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
```tsx
|
|
39
|
-
import { styled } from '@compiled/react';
|
|
40
|
-
import { token } from '@atlaskit/tokens';
|
|
41
|
-
|
|
42
|
-
const Component = styled.div({
|
|
43
|
-
'.myclass': {
|
|
44
|
-
padding: token('space.100'),
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Correct
|
|
50
|
-
|
|
51
|
-
```tsx
|
|
52
|
-
import { css } from '@compiled/react';
|
|
53
|
-
import { token } from '@atlaskit/tokens';
|
|
54
|
-
|
|
55
|
-
const headingStyles = css({
|
|
56
|
-
font: token('font.heading.large'),
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const Component = () => (
|
|
60
|
-
<div>
|
|
61
|
-
<h2 css={headingStyles}>Heading</h2>
|
|
62
|
-
</div>
|
|
63
|
-
);
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
```tsx
|
|
67
|
-
import { css } from '@compiled/react';
|
|
68
|
-
import { token } from '@atlaskit/tokens';
|
|
69
|
-
|
|
70
|
-
const tableStyles = css({
|
|
71
|
-
border: `1px solid ${token('color.border')}`,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const tdStyles = css({
|
|
75
|
-
padding: token('space.100'),
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Options
|
|
80
|
-
|
|
81
|
-
### `importSources: string[]`
|
|
82
|
-
|
|
83
|
-
By default, this rule will check styles using:
|
|
84
|
-
|
|
85
|
-
- `@atlaskit/css`
|
|
86
|
-
- `@atlaskit/primitives`
|
|
87
|
-
- `@compiled/react`
|
|
88
|
-
- `@emotion/react`
|
|
89
|
-
- `@emotion/core`
|
|
90
|
-
- `@emotion/styled`
|
|
91
|
-
- `styled-components`
|
|
92
|
-
|
|
93
|
-
Override this list with the `importSources` option, which accepts an array of package names.
|
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
-
import rule from '../index';
|
|
3
|
-
|
|
4
|
-
typescriptEslintTester.run(
|
|
5
|
-
'no-nested-selectors',
|
|
6
|
-
// @ts-expect-error
|
|
7
|
-
rule,
|
|
8
|
-
{
|
|
9
|
-
valid: [
|
|
10
|
-
{
|
|
11
|
-
name: 'Basic valid test',
|
|
12
|
-
code: `
|
|
13
|
-
import { css } from '@compiled/react';
|
|
14
|
-
const styles = css({
|
|
15
|
-
display: 'flex',
|
|
16
|
-
flexDirection: 'column',
|
|
17
|
-
});
|
|
18
|
-
`,
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: 'Basic valid test using emotion',
|
|
22
|
-
code: `
|
|
23
|
-
import styled from '@emotion/styled';
|
|
24
|
-
const Container = styled.div({
|
|
25
|
-
display: 'flex',
|
|
26
|
-
flexDirection: 'column'
|
|
27
|
-
})
|
|
28
|
-
`,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
name: 'Custom import sources (subtractive)',
|
|
32
|
-
code: `
|
|
33
|
-
import { css } from '@compiled/react';
|
|
34
|
-
const styles = css({
|
|
35
|
-
h2: {
|
|
36
|
-
fontSize: '1.5rem',
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
`,
|
|
40
|
-
options: [
|
|
41
|
-
{
|
|
42
|
-
importSources: [],
|
|
43
|
-
},
|
|
44
|
-
],
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
name: 'Styled div with no selectors',
|
|
48
|
-
code: `
|
|
49
|
-
import { styled } from '@compiled/react';
|
|
50
|
-
const Component = styled.div({
|
|
51
|
-
padding: 0,
|
|
52
|
-
display: 'flex',
|
|
53
|
-
flexDirection: 'column',
|
|
54
|
-
boxSizing: 'border-box',
|
|
55
|
-
});
|
|
56
|
-
`,
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
name: 'Pseudo element selectors',
|
|
60
|
-
code: `
|
|
61
|
-
import { styled } from '@compiled/react';
|
|
62
|
-
const Component = styled.div({
|
|
63
|
-
'&:hover': {
|
|
64
|
-
color: 'red'
|
|
65
|
-
},
|
|
66
|
-
'&:focus, :last-child': {
|
|
67
|
-
color: 'yellow'
|
|
68
|
-
},
|
|
69
|
-
'&:first-of-type:last-child': {
|
|
70
|
-
color: 'green'
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
`,
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
name: 'Skips @ queries',
|
|
77
|
-
code: `
|
|
78
|
-
import { css } from '@compiled/react';
|
|
79
|
-
const styles = css({
|
|
80
|
-
'@container (width > 400px)': {
|
|
81
|
-
fontSize: '1.5rem',
|
|
82
|
-
},
|
|
83
|
-
'@media (max-width: 1200px)': {
|
|
84
|
-
gridTemplateColumns: 'repeat(1, 1fr)',
|
|
85
|
-
justifyItems: 'center',
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
`,
|
|
89
|
-
},
|
|
90
|
-
],
|
|
91
|
-
invalid: [
|
|
92
|
-
{
|
|
93
|
-
name: 'Universal selector',
|
|
94
|
-
code: `
|
|
95
|
-
import { styled } from '@compiled/react';
|
|
96
|
-
const Component = styled.div({
|
|
97
|
-
'*': {
|
|
98
|
-
padding: 0,
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
`,
|
|
102
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
name: 'Class selector',
|
|
106
|
-
code: `
|
|
107
|
-
import { styled } from '@compiled/react';
|
|
108
|
-
const Component = styled.div({
|
|
109
|
-
'.myclass': {
|
|
110
|
-
padding: 0,
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
`,
|
|
114
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
name: 'ID selector',
|
|
118
|
-
code: `
|
|
119
|
-
import { styled } from '@compiled/react';
|
|
120
|
-
const Component = styled.div({
|
|
121
|
-
'#myid': {
|
|
122
|
-
padding: 0,
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
`,
|
|
126
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: 'HTML element selector',
|
|
130
|
-
code: `
|
|
131
|
-
import { styled } from '@compiled/react';
|
|
132
|
-
const Component = styled.div({
|
|
133
|
-
h2: {
|
|
134
|
-
fontSize: '1.5rem',
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
`,
|
|
138
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
name: 'Data-component-selector',
|
|
142
|
-
code: `
|
|
143
|
-
import { styled } from '@compiled/react';
|
|
144
|
-
const Component = styled.div({
|
|
145
|
-
'[data-component-selector="my.button"]': {
|
|
146
|
-
color: 'blue',
|
|
147
|
-
},
|
|
148
|
-
});
|
|
149
|
-
`,
|
|
150
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
name: 'Grouping selector',
|
|
154
|
-
code: `
|
|
155
|
-
import { styled } from '@compiled/react';
|
|
156
|
-
const Component = styled.div({
|
|
157
|
-
'div, p, span': {
|
|
158
|
-
fontSize: '1.5rem',
|
|
159
|
-
},
|
|
160
|
-
color: 'blue',
|
|
161
|
-
backgroundColor: 'red',
|
|
162
|
-
});
|
|
163
|
-
`,
|
|
164
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
165
|
-
},
|
|
166
|
-
{
|
|
167
|
-
name: 'Selector including a variable',
|
|
168
|
-
code: `
|
|
169
|
-
import { styled } from '@compiled/react';
|
|
170
|
-
styled.div({
|
|
171
|
-
[\`\${SOME_VAR} > *\`]: {
|
|
172
|
-
color: 'red'
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
`,
|
|
176
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
177
|
-
},
|
|
178
|
-
{
|
|
179
|
-
name: 'Current element selector',
|
|
180
|
-
code: `
|
|
181
|
-
import { styled } from '@compiled/react';
|
|
182
|
-
styled.div({
|
|
183
|
-
color: 'darkorchid',
|
|
184
|
-
'.name &': {
|
|
185
|
-
color: 'orange'
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
`,
|
|
189
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
name: 'Child combinator',
|
|
193
|
-
code: `
|
|
194
|
-
import { styled } from '@compiled/react';
|
|
195
|
-
styled.div({
|
|
196
|
-
'& > &': {
|
|
197
|
-
color: 'orange'
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
`,
|
|
201
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
name: 'Child combinator without whitespace',
|
|
205
|
-
code: `
|
|
206
|
-
import { styled } from '@compiled/react';
|
|
207
|
-
styled.div({
|
|
208
|
-
'&>a': {
|
|
209
|
-
color: 'orange'
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
`,
|
|
213
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
name: 'Column combinator',
|
|
217
|
-
code: `
|
|
218
|
-
import { styled } from '@compiled/react';
|
|
219
|
-
styled.div({
|
|
220
|
-
'col.selected||td': {
|
|
221
|
-
color: 'orange'
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
`,
|
|
225
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
name: 'Namespace selector',
|
|
229
|
-
code: `
|
|
230
|
-
import { styled } from '@compiled/react';
|
|
231
|
-
styled.div({
|
|
232
|
-
'myNameSpace|a': {
|
|
233
|
-
color: 'orange'
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
`,
|
|
237
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
238
|
-
},
|
|
239
|
-
{
|
|
240
|
-
name: 'Descendant combinator',
|
|
241
|
-
code: `
|
|
242
|
-
import { styled } from '@compiled/react';
|
|
243
|
-
const Component = styled.div({
|
|
244
|
-
'& &:hover': {
|
|
245
|
-
padding: 0,
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
`,
|
|
249
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
250
|
-
},
|
|
251
|
-
{
|
|
252
|
-
name: 'cssMap API',
|
|
253
|
-
code: `
|
|
254
|
-
import { cssMap } from '@compiled/react';
|
|
255
|
-
|
|
256
|
-
const styles = cssMap({
|
|
257
|
-
success: {
|
|
258
|
-
'[data-component-selector="my.button"]': {
|
|
259
|
-
color: 'var(--ds-text-inverse)',
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
});
|
|
263
|
-
`,
|
|
264
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
265
|
-
},
|
|
266
|
-
{
|
|
267
|
-
name: 'keyframes API',
|
|
268
|
-
code: `
|
|
269
|
-
import { keyframes } from '@emotion/react';
|
|
270
|
-
|
|
271
|
-
const keyFrameStyles = keyframes({
|
|
272
|
-
'from, to': {
|
|
273
|
-
div: {
|
|
274
|
-
opacity: 0,
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
'50%': {
|
|
278
|
-
opacity: 1,
|
|
279
|
-
},
|
|
280
|
-
});
|
|
281
|
-
`,
|
|
282
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
283
|
-
},
|
|
284
|
-
{
|
|
285
|
-
name: 'Next sibling selector',
|
|
286
|
-
code: `
|
|
287
|
-
import { styled } from '@compiled/react';
|
|
288
|
-
styled.div({
|
|
289
|
-
'&+a': {
|
|
290
|
-
color: 'orange'
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
`,
|
|
294
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
295
|
-
},
|
|
296
|
-
{
|
|
297
|
-
name: 'Next sibling selector on non-HTML element',
|
|
298
|
-
code: `
|
|
299
|
-
import { styled } from '@compiled/react';
|
|
300
|
-
styled.div({
|
|
301
|
-
'& + &': {
|
|
302
|
-
color: 'orange'
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
`,
|
|
306
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
name: 'Subsequent sibling selector on non-HTML element',
|
|
310
|
-
code: `
|
|
311
|
-
import { styled } from '@compiled/react';
|
|
312
|
-
styled.div({
|
|
313
|
-
'& ~ &': {
|
|
314
|
-
color: 'orange'
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
`,
|
|
318
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
319
|
-
},
|
|
320
|
-
{
|
|
321
|
-
name: 'Nested selector inside styled(BaseComponent)()',
|
|
322
|
-
code: `
|
|
323
|
-
import { styled } from '@compiled/react';
|
|
324
|
-
import { BaseComponent } from './base-component';
|
|
325
|
-
styled(BaseComponent)({
|
|
326
|
-
'> span': {
|
|
327
|
-
color: 'orange'
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
`,
|
|
331
|
-
errors: [{ messageId: 'no-nested-selectors' }],
|
|
332
|
-
},
|
|
333
|
-
],
|
|
334
|
-
},
|
|
335
|
-
);
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type { Property } from 'estree';
|
|
2
|
-
import cssSelectorParser, { type Selector, isNesting, isPseudo } from 'postcss-selector-parser';
|
|
3
|
-
|
|
4
|
-
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
5
|
-
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
6
|
-
|
|
7
|
-
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
8
|
-
|
|
9
|
-
const getCssSelector = (key: Property['key']): string | null => {
|
|
10
|
-
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
11
|
-
return key.value;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (key.type === 'Identifier' && typeof key.name === 'string') {
|
|
15
|
-
return key.name;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (key.type === 'TemplateLiteral') {
|
|
19
|
-
return key.quasis.map((quasi) => quasi.value.raw).join(' ');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return null;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
function isAllowedSelector(selector: Selector): boolean {
|
|
26
|
-
const [head, ...tail] = selector.nodes;
|
|
27
|
-
|
|
28
|
-
// The first node must be a nesting selector (`&`) or pseudo-selector.
|
|
29
|
-
const isHeadAllowed = isNesting(head) || isPseudo(head);
|
|
30
|
-
|
|
31
|
-
// All remaining nodes must be pseudo selectors.
|
|
32
|
-
const isTailAllowed = tail.every(isPseudo);
|
|
33
|
-
|
|
34
|
-
return isHeadAllowed && isTailAllowed;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const cssSelectorProcessor = cssSelectorParser();
|
|
38
|
-
|
|
39
|
-
export const rule = createLintRuleWithTypedConfig({
|
|
40
|
-
meta: {
|
|
41
|
-
name: 'no-nested-selectors',
|
|
42
|
-
docs: {
|
|
43
|
-
description: 'Prevents usage of nested selectors within css styling',
|
|
44
|
-
recommended: true,
|
|
45
|
-
severity: 'error',
|
|
46
|
-
},
|
|
47
|
-
messages: {
|
|
48
|
-
'no-nested-selectors':
|
|
49
|
-
'Please avoid setting styles for child elements or elements that require context from other elements.',
|
|
50
|
-
},
|
|
51
|
-
type: 'problem',
|
|
52
|
-
schema: {
|
|
53
|
-
type: 'object',
|
|
54
|
-
properties: {
|
|
55
|
-
importSources,
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
create(context, { importSources }) {
|
|
60
|
-
return {
|
|
61
|
-
CallExpression(node) {
|
|
62
|
-
const { references } = context.getScope();
|
|
63
|
-
|
|
64
|
-
walkStyleProperties(node, references, importSources, ({ key, value }) => {
|
|
65
|
-
if (value.type !== 'ObjectExpression') {
|
|
66
|
-
// If the value is a CSS object, safe to assume we're at a CSS selector
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const selectorText = getCssSelector(key);
|
|
71
|
-
if (selectorText === null) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Ignore at-rules
|
|
76
|
-
if (selectorText.includes('@')) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
const selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
82
|
-
|
|
83
|
-
if (!selectorList.nodes.every(isAllowedSelector)) {
|
|
84
|
-
context.report({
|
|
85
|
-
messageId: 'no-nested-selectors',
|
|
86
|
-
node: key,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
} catch (_) {
|
|
90
|
-
// If it is not parsable then `no-unsafe-selectors` will give it an error,
|
|
91
|
-
// we can ignore it here.
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
export default rule;
|