@atlaskit/eslint-plugin-ui-styling-standard 0.15.1 → 0.16.0

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.
Files changed (99) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/constellation/index/migration-guide.mdx +309 -0
  3. package/constellation/index/overview.mdx +4 -0
  4. package/constellation/index/props.mdx +9 -5
  5. package/dist/cjs/rules/no-nested-selectors/index.js +68 -49
  6. package/dist/es2019/rules/no-nested-selectors/index.js +63 -45
  7. package/dist/esm/rules/no-nested-selectors/index.js +64 -49
  8. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
  9. package/index.js +9 -9
  10. package/package.json +3 -3
  11. package/scripts/codegen.tsx +223 -284
  12. package/src/rules/__tests__/utils/_tester.tsx +24 -24
  13. package/src/rules/atlaskit-theme/README.md +21 -19
  14. package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
  15. package/src/rules/atlaskit-theme/index.ts +107 -115
  16. package/src/rules/convert-props-syntax/README.md +32 -22
  17. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
  18. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
  19. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
  20. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
  21. package/src/rules/convert-props-syntax/index.tsx +356 -386
  22. package/src/rules/enforce-style-prop/README.md +21 -18
  23. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
  24. package/src/rules/enforce-style-prop/index.tsx +144 -160
  25. package/src/rules/external-rules.tsx +163 -164
  26. package/src/rules/local-cx-xcss/README.md +10 -6
  27. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
  28. package/src/rules/local-cx-xcss/index.tsx +46 -56
  29. package/src/rules/no-array-arguments/README.md +2 -1
  30. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
  31. package/src/rules/no-array-arguments/index.tsx +68 -69
  32. package/src/rules/no-classname-prop/README.md +7 -4
  33. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
  34. package/src/rules/no-classname-prop/index.tsx +25 -25
  35. package/src/rules/no-container-queries/README.md +11 -9
  36. package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
  37. package/src/rules/no-container-queries/index.tsx +75 -81
  38. package/src/rules/no-dynamic-styles/README.md +16 -19
  39. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
  40. package/src/rules/no-dynamic-styles/index.tsx +45 -48
  41. package/src/rules/no-exported-styles/README.md +17 -12
  42. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
  43. package/src/rules/no-exported-styles/index.tsx +36 -36
  44. package/src/rules/no-global-styles/README.md +15 -14
  45. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
  46. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
  47. package/src/rules/no-global-styles/index.tsx +59 -62
  48. package/src/rules/no-important-styles/README.md +5 -3
  49. package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
  50. package/src/rules/no-important-styles/index.tsx +49 -49
  51. package/src/rules/no-imported-style-values/README.md +23 -15
  52. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
  53. package/src/rules/no-imported-style-values/index.tsx +116 -120
  54. package/src/rules/no-nested-selectors/README.md +15 -13
  55. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +186 -149
  56. package/src/rules/no-nested-selectors/index.tsx +92 -105
  57. package/src/rules/no-styled/README.md +14 -9
  58. package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
  59. package/src/rules/no-styled/index.tsx +34 -41
  60. package/src/rules/no-unsafe-selectors/README.md +103 -93
  61. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
  62. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
  63. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
  64. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
  65. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
  66. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
  67. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
  68. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
  69. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +114 -114
  70. package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
  71. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
  72. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
  73. package/src/rules/no-unsafe-values/README.md +34 -40
  74. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
  75. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
  76. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
  77. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
  78. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
  79. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
  80. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
  81. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
  82. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
  83. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
  84. package/src/rules/no-unsafe-values/index.tsx +76 -78
  85. package/src/rules/no-unsafe-values/messages.tsx +12 -12
  86. package/src/rules/no-unsafe-values/utils.tsx +370 -382
  87. package/src/rules/use-compiled/README.md +11 -8
  88. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
  89. package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
  90. package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
  91. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
  92. package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
  93. package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
  94. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
  95. package/src/rules/use-compiled/index.tsx +203 -226
  96. package/src/rules/use-compiled/is-safe.tsx +113 -117
  97. package/src/rules/use-compiled/utils.tsx +14 -18
  98. package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
  99. package/src/rules/utils/create-rule.tsx +3 -6
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.16.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#109850](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/109850)
8
+ [`9a18d4ad09473`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/9a18d4ad09473) -
9
+ Added migration guide to styling standard documentation.
10
+
11
+ ## 0.15.2
12
+
13
+ ### Patch Changes
14
+
15
+ - [#105238](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/105238)
16
+ [`8840e6d3180d`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8840e6d3180d) -
17
+ Fixes `no-nested-selectors` to handle additional APIs (such as `cssMap` and `keyframes`) as well
18
+ as catch more cases of nested selectors.
19
+
3
20
  ## 0.15.1
4
21
 
5
22
  ### Patch Changes
@@ -0,0 +1,309 @@
1
+ ---
2
+ order: 3
3
+ ---
4
+
5
+ import SectionMessage from '@atlaskit/section-message';
6
+ import Tabs, { Tab, TabList, TabPanel } from '@atlaskit/tabs';
7
+
8
+ ## Patterns
9
+
10
+ All styles should be written using [Compiled](https://compiledcssinjs.com/) or
11
+ [Primitives](/components/primitives) and conform to the following patterns.
12
+
13
+ ### Static styles
14
+
15
+ For styles that are not affected by props or state, use the `css()` or `xcss()` API.
16
+
17
+ #### Compiled
18
+
19
+ When using Compiled, write static styles with `css()` and apply them with the `css` prop.
20
+
21
+ ```tsx
22
+ import { css } from '@compiled/react';
23
+ import { token } from '@atlaskit/tokens';
24
+
25
+ const cardStyles = css({
26
+ backgroundColor: token('elevation.surface.raised'),
27
+ boxShadow: token('elevation.shadow.raised'),
28
+ padding: token('space.100'),
29
+ });
30
+
31
+ const Card = () => <div css={cardStyles} />;
32
+ ```
33
+
34
+ #### Primitives
35
+
36
+ When using Primitives, write static styles with `xcss()` and apply them with the `xcss` prop.
37
+
38
+ ```tsx
39
+ import { Box, xcss } from '@atlaskit/primitives';
40
+
41
+ const cardStyles = xcss({
42
+ backgroundColor: 'elevation.surface.raised',
43
+ boxShadow: 'elevation.shadow.raised',
44
+ padding: 'space.100',
45
+ });
46
+
47
+ const Card = () => <Box xcss={cardStyles} />;
48
+ ```
49
+
50
+ ### Toggled styles
51
+
52
+ For styles that are toggled on and off, write them as [static styles](#static-styles) and then
53
+ conditionally apply them.
54
+
55
+ #### Compiled
56
+
57
+ When using Compiled, conditionally apply styles with the `css` prop.
58
+
59
+ ```tsx
60
+ import { css } from '@compiled/react';
61
+ import { token } from '@atlaskit/tokens';
62
+
63
+ const baseStyles = css({
64
+ backgroundColor: token('color.background.neutral'),
65
+ border: `1px solid ${token('color.border')}`,
66
+ });
67
+
68
+ const selectedStyles = css({
69
+ backgroundColor: token('color.background.selected'),
70
+ borderColor: token('color.border.selected'),
71
+ });
72
+
73
+ const Selectable = ({ isSelected = false }: { isSelected?: boolean }) => (
74
+ // The `selectedStyles` are conditionally applied
75
+ <div css={[baseStyles, isSelected && selectedStyles]} />
76
+ );
77
+ ```
78
+
79
+ #### Primitives
80
+
81
+ When using Primitives, conditionally apply styles with the `xcss` prop.
82
+
83
+ ```tsx
84
+ import { Box, xcss } from '@atlaskit/primitives';
85
+
86
+ const baseStyles = xcss({
87
+ backgroundColor: 'color.background.neutral',
88
+ borderWidth: '1px',
89
+ borderStyle: 'solid',
90
+ borderColor: 'color.border',
91
+ });
92
+
93
+ const selectedStyles = xcss({
94
+ backgroundColor: 'color.background.selected',
95
+ borderColor: 'color.border.selected',
96
+ });
97
+
98
+ const Selectable = ({ isSelected = false }: { isSelected?: boolean }) => (
99
+ // The `selectedStyles` are conditionally applied
100
+ <Box xcss={[baseStyles, isSelected && selectedStyles]} />
101
+ );
102
+ ```
103
+
104
+ ### Variant styles
105
+
106
+ For styles that compose a set of known variants, use `cssMap` or an object of `xcss()` styles and
107
+ then dynamically access them.
108
+
109
+ #### Compiled
110
+
111
+ When using Compiled, write variants with `cssMap` and apply them with the `css` prop.
112
+
113
+ ```tsx
114
+ import { css, cssMap } from '@compiled/react';
115
+ import { token } from '@atlaskit/tokens';
116
+
117
+ type Status = 'success' | 'warning' | 'danger';
118
+
119
+ const baseStyles = css({
120
+ font: token('font.body.large'),
121
+ padding: token('space.100'),
122
+ });
123
+
124
+ const statusStyles = cssMap({
125
+ success: {
126
+ color: token('color.text.success'),
127
+ backgroundColor: token('color.background.success'),
128
+ },
129
+ warning: {
130
+ color: token('color.text.warning'),
131
+ backgroundColor: token('color.background.warning'),
132
+ },
133
+ danger: {
134
+ color: token('color.text.danger'),
135
+ backgroundColor: token('color.background.danger'),
136
+ },
137
+ });
138
+
139
+ const StatusMessage = ({ message, status }: { message: string; status: Status }) => (
140
+ // The `statusStyles` are dynamically accessed
141
+ <div css={[baseStyles, statusStyles[status]]} />
142
+ );
143
+ ```
144
+
145
+ #### Primitives
146
+
147
+ When using Primitives, write variants using an object with `xcss()` values and apply them with the
148
+ `xcss` prop.
149
+
150
+ ```tsx
151
+ import { Box, xcss } from '@atlaskit/primitives';
152
+
153
+ type Status = 'success' | 'warning' | 'danger';
154
+
155
+ const baseStyles = xcss({
156
+ font: 'font.body.large',
157
+ padding: 'space.100',
158
+ });
159
+
160
+ const statusStyles = {
161
+ success: xcss({
162
+ color: 'color.text.success',
163
+ backgroundColor: 'color.background.success',
164
+ }),
165
+ warning: xcss({
166
+ color: 'color.text.warning',
167
+ backgroundColor: 'color.background.warning',
168
+ }),
169
+ danger: xcss({
170
+ color: 'color.text.danger',
171
+ backgroundColor: 'color.background.danger',
172
+ }),
173
+ };
174
+
175
+ const StatusMessage = ({ message, status }: { message: string; status: Status }) => (
176
+ // The `statusStyles` are dynamically accessed
177
+ <Box xcss={[baseStyles, statusStyles[status]]} />
178
+ );
179
+ ```
180
+
181
+ ### Dynamic styles
182
+
183
+ <SectionMessage appearance="warning" title="Use with caution">
184
+ Only a tiny percentage (~1%) of styles need to be written this way. If you are using it
185
+ frequently, you are likely doing something wrong!
186
+ </SectionMessage>
187
+
188
+ For styles that cannot be expressed statically, use the `style` prop.
189
+
190
+ This might be needed when:
191
+
192
+ - Styles are derived from user input
193
+ - Integrating with a 3rd party library that provides a `style` object
194
+
195
+ #### Compiled
196
+
197
+ ```tsx
198
+ import { css } from '@compiled/react';
199
+ import { token } from '@atlaskit/tokens';
200
+
201
+ const baseStyles = css({
202
+ height: '100%',
203
+ minWidth: '200px',
204
+ backgroundColor: token('color.background.neutral'),
205
+ });
206
+
207
+ const Resizable = ({ width }: { width: string }) => <div css={baseStyles} style={{ width }} />;
208
+ ```
209
+
210
+ #### Primitives
211
+
212
+ ```tsx
213
+ import { Box, xcss } from '@atlaskit/primitives';
214
+
215
+ const baseStyles = xcss({
216
+ height: '100%',
217
+ minWidth: '200px',
218
+ backgroundColor: 'color.background.neutral',
219
+ });
220
+
221
+ const Resizable = ({ width }: { width: string }) => <Box css={baseStyles} style={{ width }} />;
222
+ ```
223
+
224
+ ## More examples
225
+
226
+ ### Migrating from `styled`
227
+
228
+ #### Before
229
+
230
+ ```tsx
231
+ import { styled } from '@compiled/react';
232
+
233
+ type Status = 'success' | 'warning' | 'danger';
234
+
235
+ type Props = {
236
+ width: string;
237
+ status: Status;
238
+ isSelected: boolean;
239
+ };
240
+
241
+ const textColors: Record<Status, string> = {
242
+ success: token('color.text.success'),
243
+ warning: token('color.text.warning'),
244
+ danger: token('color.text.danger'),
245
+ };
246
+
247
+ const Component = styled.div<Props>(
248
+ {
249
+ padding: token('space.100'),
250
+ width: (props) => props.width,
251
+ },
252
+ ({ status }: { status: Status }) => ({
253
+ color: textColors[status],
254
+ }),
255
+ ({ isSelected }) => (isSelected ? { border: `1px solid ${token('color.border.selected')}` } : {}),
256
+ );
257
+ ```
258
+
259
+ #### After
260
+
261
+ We apply a combination of the patterns above:
262
+
263
+ - The padding is a [static style](#static-styles) so uses `css()` that is always applied.
264
+ - The selected border is a [toggled style](#toggled-styles) so uses `css()` that is conditionally
265
+ applied.
266
+ - The status color is a [variant style](#variant-styles) so is written using `cssMap()` that is
267
+ dynamically accessed.
268
+ - The width is a [completely dynamic style](#dynamic-styles) so uses the `style` prop.
269
+
270
+ ```tsx
271
+ import type { ReactNode } from 'react';
272
+ import { css, cssMap } from '@compiled/react';
273
+
274
+ type Status = 'success' | 'warning' | 'danger';
275
+
276
+ type Props = {
277
+ width: string;
278
+ status: Status;
279
+ isSelected: boolean;
280
+ // Props that were implicitly added now need to be made explicit
281
+ children: ReactNode;
282
+ };
283
+
284
+ const baseStyles = css({
285
+ padding: token('space.100'),
286
+ });
287
+
288
+ const selectedStyles = css({
289
+ border: `1px solid ${token('color.border.selected')}`,
290
+ });
291
+
292
+ const statusStyles = cssMap({
293
+ success: {
294
+ color: token('color.text.success'),
295
+ },
296
+ warning: {
297
+ color: token('color.text.warning'),
298
+ },
299
+ danger: {
300
+ color: token('color.text.danger'),
301
+ },
302
+ });
303
+
304
+ const Component = ({ width, status, isSelected, children }: Props) => (
305
+ <div css={[baseStyles, isSelected && selectedStyles, statusStyles[status]]} style={{ width }}>
306
+ {children}
307
+ </div>
308
+ );
309
+ ```
@@ -1,3 +1,7 @@
1
+ ---
2
+ order: 1
3
+ ---
4
+
1
5
  The UI Styling Standard is a set of principles to guide styling UI code across Atlassian's frontend
2
6
  codebases and ecosystem.
3
7
 
@@ -1,5 +1,9 @@
1
- You must use the recommended config to follow the styling standards at Atlassian,
2
- which will ensure compatability and performance with the design system.
1
+ ---
2
+ order: 2
3
+ ---
4
+
5
+ You must use the recommended config to follow the styling standards at Atlassian, which will ensure
6
+ compatability and performance with the design system.
3
7
 
4
8
  ```diff
5
9
  module.exports = {
@@ -9,9 +13,9 @@ module.exports = {
9
13
  };
10
14
  ```
11
15
 
12
- We don't recommended maintaining your own configuration.
13
- If you do not use our config you will need to specify individual rules and configuration.
14
- Add the plugin to your `.eslintrc.js` file and enable individual rules as you're ready to adopt.
16
+ We don't recommended maintaining your own configuration. If you do not use our config you will need
17
+ to specify individual rules and configuration. Add the plugin to your `.eslintrc.js` file and enable
18
+ individual rules as you're ready to adopt.
15
19
 
16
20
  ```diff
17
21
  module.exports = {
@@ -1,38 +1,46 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports.rule = exports.default = void 0;
7
- var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
8
- var _createRule = require("../utils/create-rule");
9
- var schema = [{
10
- type: 'object',
11
- properties: {
12
- importSources: {
13
- type: 'array',
14
- items: {
15
- type: 'string'
16
- },
17
- uniqueItems: true
18
- }
19
- }
20
- }];
21
- var getCssSelector = function getCssSelector(node) {
22
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
23
- return node.key.value;
9
+ var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
10
+ var _postcssSelectorParser = _interopRequireWildcard(require("postcss-selector-parser"));
11
+ var _walkStyleProperties = require("@atlaskit/eslint-utils/walk-style-properties");
12
+ var _schema = require("@atlaskit/eslint-utils/schema");
13
+ var _createRuleWithTypedConfig = require("../utils/create-rule-with-typed-config");
14
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
15
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
16
+ var getCssSelector = function getCssSelector(key) {
17
+ if (key.type === 'Literal' && typeof key.value === 'string') {
18
+ return key.value;
24
19
  }
25
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
26
- return node.key.name;
20
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
21
+ return key.name;
27
22
  }
28
- if (node.key.type === 'TemplateLiteral') {
29
- return node.key.quasis.map(function (quasi) {
23
+ if (key.type === 'TemplateLiteral') {
24
+ return key.quasis.map(function (quasi) {
30
25
  return quasi.value.raw;
31
26
  }).join(' ');
32
27
  }
33
28
  return null;
34
29
  };
35
- var rule = exports.rule = (0, _createRule.createLintRule)({
30
+ function isAllowedSelector(selector) {
31
+ var _selector$nodes = (0, _toArray2.default)(selector.nodes),
32
+ head = _selector$nodes[0],
33
+ tail = _selector$nodes.slice(1);
34
+
35
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
36
+ var isHeadAllowed = (0, _postcssSelectorParser.isNesting)(head) || (0, _postcssSelectorParser.isPseudo)(head);
37
+
38
+ // All remaining nodes must be pseudo selectors.
39
+ var isTailAllowed = tail.every(_postcssSelectorParser.isPseudo);
40
+ return isHeadAllowed && isTailAllowed;
41
+ }
42
+ var cssSelectorProcessor = (0, _postcssSelectorParser.default)();
43
+ var rule = exports.rule = (0, _createRuleWithTypedConfig.createLintRuleWithTypedConfig)({
36
44
  meta: {
37
45
  name: 'no-nested-selectors',
38
46
  docs: {
@@ -44,37 +52,48 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
44
52
  'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
45
53
  },
46
54
  type: 'problem',
47
- schema: schema
55
+ schema: {
56
+ type: 'object',
57
+ properties: {
58
+ importSources: _schema.importSources
59
+ }
60
+ }
48
61
  },
49
- create: function create(context) {
50
- var importSources = (0, _isSupportedImport.getImportSources)(context);
62
+ create: function create(context, _ref) {
63
+ var importSources = _ref.importSources;
51
64
  return {
52
- 'CallExpression Property': function CallExpressionProperty(node) {
65
+ CallExpression: function CallExpression(node) {
53
66
  var _context$getScope = context.getScope(),
54
67
  references = _context$getScope.references;
55
- var ancestors = context.getAncestors();
56
- if (ancestors.every(function (ancestor) {
57
- return ancestor.type !== 'CallExpression' || !(ancestor !== null && ancestor !== void 0 && ancestor.callee) || !((0, _isSupportedImport.isCss)(ancestor.callee, references, importSources) || (0, _isSupportedImport.isStyled)(ancestor.callee, references, importSources) || (0, _isSupportedImport.isXcss)(ancestor.callee, references, importSources));
58
- })) {
59
- return;
60
- }
61
- if (node.value.type !== 'ObjectExpression') {
62
- // If the value is a CSS object, safe to assume we're at a CSS selector
63
- return;
64
- }
65
- var cssSelector = getCssSelector(node);
66
- if (cssSelector === null) {
67
- return;
68
- }
69
- var tokens = cssSelector.split(/[, ]+/);
70
- if (!cssSelector.includes('@') && !tokens.every(function (token) {
71
- return token.length === 0 || token.startsWith('&') || token.startsWith(':') || token.includes('+') || token.includes('~') || token.endsWith('&');
72
- })) {
73
- context.report({
74
- messageId: 'no-nested-selectors',
75
- node: node.key
76
- });
77
- }
68
+ (0, _walkStyleProperties.walkStyleProperties)(node, references, importSources, function (_ref2) {
69
+ var key = _ref2.key,
70
+ value = _ref2.value;
71
+ if (value.type !== 'ObjectExpression') {
72
+ // If the value is a CSS object, safe to assume we're at a CSS selector
73
+ return;
74
+ }
75
+ var selectorText = getCssSelector(key);
76
+ if (selectorText === null) {
77
+ return;
78
+ }
79
+
80
+ // Ignore at-rules
81
+ if (selectorText.includes('@')) {
82
+ return;
83
+ }
84
+ try {
85
+ var selectorList = cssSelectorProcessor.astSync(selectorText);
86
+ if (!selectorList.nodes.every(isAllowedSelector)) {
87
+ context.report({
88
+ messageId: 'no-nested-selectors',
89
+ node: key
90
+ });
91
+ }
92
+ } catch (_) {
93
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
94
+ // we can ignore it here.
95
+ }
96
+ });
78
97
  }
79
98
  };
80
99
  }
@@ -1,30 +1,31 @@
1
- import { isStyled, isCss, isXcss, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
2
- import { createLintRule } from '../utils/create-rule';
3
- const schema = [{
4
- type: 'object',
5
- properties: {
6
- importSources: {
7
- type: 'array',
8
- items: {
9
- type: 'string'
10
- },
11
- uniqueItems: true
12
- }
13
- }
14
- }];
15
- const getCssSelector = node => {
16
- if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
17
- return node.key.value;
1
+ import cssSelectorParser, { isNesting, isPseudo } from 'postcss-selector-parser';
2
+ import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
3
+ import { importSources } from '@atlaskit/eslint-utils/schema';
4
+ import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
5
+ const getCssSelector = key => {
6
+ if (key.type === 'Literal' && typeof key.value === 'string') {
7
+ return key.value;
18
8
  }
19
- if (node.key.type === 'Identifier' && typeof node.key.name === 'string') {
20
- return node.key.name;
9
+ if (key.type === 'Identifier' && typeof key.name === 'string') {
10
+ return key.name;
21
11
  }
22
- if (node.key.type === 'TemplateLiteral') {
23
- return node.key.quasis.map(quasi => quasi.value.raw).join(' ');
12
+ if (key.type === 'TemplateLiteral') {
13
+ return key.quasis.map(quasi => quasi.value.raw).join(' ');
24
14
  }
25
15
  return null;
26
16
  };
27
- export const rule = createLintRule({
17
+ function isAllowedSelector(selector) {
18
+ const [head, ...tail] = selector.nodes;
19
+
20
+ // The first node must be a nesting selector (`&`) or pseudo-selector.
21
+ const isHeadAllowed = isNesting(head) || isPseudo(head);
22
+
23
+ // All remaining nodes must be pseudo selectors.
24
+ const isTailAllowed = tail.every(isPseudo);
25
+ return isHeadAllowed && isTailAllowed;
26
+ }
27
+ const cssSelectorProcessor = cssSelectorParser();
28
+ export const rule = createLintRuleWithTypedConfig({
28
29
  meta: {
29
30
  name: 'no-nested-selectors',
30
31
  docs: {
@@ -36,34 +37,51 @@ export const rule = createLintRule({
36
37
  'no-nested-selectors': 'Please avoid setting styles for child elements or elements that require context from other elements.'
37
38
  },
38
39
  type: 'problem',
39
- schema
40
+ schema: {
41
+ type: 'object',
42
+ properties: {
43
+ importSources
44
+ }
45
+ }
40
46
  },
41
- create(context) {
42
- const importSources = getImportSources(context);
47
+ create(context, {
48
+ importSources
49
+ }) {
43
50
  return {
44
- 'CallExpression Property': node => {
51
+ CallExpression(node) {
45
52
  const {
46
53
  references
47
54
  } = context.getScope();
48
- const ancestors = context.getAncestors();
49
- if (ancestors.every(ancestor => ancestor.type !== 'CallExpression' || !(ancestor !== null && ancestor !== void 0 && ancestor.callee) || !(isCss(ancestor.callee, references, importSources) || isStyled(ancestor.callee, references, importSources) || isXcss(ancestor.callee, references, importSources)))) {
50
- return;
51
- }
52
- if (node.value.type !== 'ObjectExpression') {
53
- // If the value is a CSS object, safe to assume we're at a CSS selector
54
- return;
55
- }
56
- const cssSelector = getCssSelector(node);
57
- if (cssSelector === null) {
58
- return;
59
- }
60
- const tokens = cssSelector.split(/[, ]+/);
61
- if (!cssSelector.includes('@') && !tokens.every(token => token.length === 0 || token.startsWith('&') || token.startsWith(':') || token.includes('+') || token.includes('~') || token.endsWith('&'))) {
62
- context.report({
63
- messageId: 'no-nested-selectors',
64
- node: node.key
65
- });
66
- }
55
+ walkStyleProperties(node, references, importSources, ({
56
+ key,
57
+ value
58
+ }) => {
59
+ if (value.type !== 'ObjectExpression') {
60
+ // If the value is a CSS object, safe to assume we're at a CSS selector
61
+ return;
62
+ }
63
+ const selectorText = getCssSelector(key);
64
+ if (selectorText === null) {
65
+ return;
66
+ }
67
+
68
+ // Ignore at-rules
69
+ if (selectorText.includes('@')) {
70
+ return;
71
+ }
72
+ try {
73
+ const selectorList = cssSelectorProcessor.astSync(selectorText);
74
+ if (!selectorList.nodes.every(isAllowedSelector)) {
75
+ context.report({
76
+ messageId: 'no-nested-selectors',
77
+ node: key
78
+ });
79
+ }
80
+ } catch (_) {
81
+ // If it is not parsable then `no-unsafe-selectors` will give it an error,
82
+ // we can ignore it here.
83
+ }
84
+ });
67
85
  }
68
86
  };
69
87
  }