@atlaskit/eslint-plugin-ui-styling-standard 0.9.1 → 0.10.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 (33) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-dynamic-styles/usage.mdx +128 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +3 -1
  8. package/dist/cjs/rules/no-dynamic-styles/index.js +69 -0
  9. package/dist/es2019/presets/all.codegen.js +2 -1
  10. package/dist/es2019/presets/recommended.codegen.js +2 -1
  11. package/dist/es2019/rules/index.codegen.js +3 -1
  12. package/dist/es2019/rules/no-dynamic-styles/index.js +51 -0
  13. package/dist/esm/presets/all.codegen.js +2 -1
  14. package/dist/esm/presets/recommended.codegen.js +2 -1
  15. package/dist/esm/rules/index.codegen.js +3 -1
  16. package/dist/esm/rules/no-dynamic-styles/index.js +62 -0
  17. package/dist/types/index.codegen.d.ts +2 -0
  18. package/dist/types/presets/all.codegen.d.ts +2 -1
  19. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  20. package/dist/types/rules/index.codegen.d.ts +1 -0
  21. package/dist/types/rules/no-dynamic-styles/index.d.ts +2 -0
  22. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  23. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  24. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  25. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  26. package/dist/types-ts4.5/rules/no-dynamic-styles/index.d.ts +2 -0
  27. package/package.json +2 -1
  28. package/src/presets/all.codegen.tsx +2 -1
  29. package/src/presets/recommended.codegen.tsx +2 -1
  30. package/src/rules/index.codegen.tsx +3 -1
  31. package/src/rules/no-dynamic-styles/README.md +126 -0
  32. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +128 -0
  33. package/src/rules/no-dynamic-styles/index.tsx +63 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#91611](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91611) [`943056d22c36`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/943056d22c36) - Add `no-dynamic-styles` rule which blocks function expressions as values in style declarations.
8
+
3
9
  ## 0.9.1
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -19,6 +19,7 @@ Not intended for use outside the Atlassian frontend repository.
19
19
  | <a href="./src/rules/no-array-arguments/README.md">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
20
20
  | <a href="./src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
21
21
  | <a href="./src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
22
+ | <a href="./src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
22
23
  | <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
23
24
  | <a href="./src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
24
25
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
@@ -17,6 +17,7 @@ but more are planned to support the standard over time.
17
17
  | <a href="no-array-arguments/usage">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
18
18
  | <a href="no-classname-prop/usage">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
19
19
  | <a href="no-container-queries/usage">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
20
+ | <a href="no-dynamic-styles/usage">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
20
21
  | <a href="no-important-styles/usage">no-important-styles</a> | Disallows important style declarations | Yes | | |
21
22
  | <a href="no-nested-selectors/usage">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
22
23
  | <a href="consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
@@ -0,0 +1,128 @@
1
+ # no-dynamic-styles
2
+
3
+ This rule prevents the use of dynamic styles (function expressions) in style declarations.
4
+
5
+ This ensures that style declarations:
6
+
7
+ - are statically analyzable
8
+ - can be efficiently extracted into atomic CSS
9
+ - are more readable, deterministic, and thus easier to maintain
10
+
11
+ ## Examples
12
+
13
+ ### Incorrect
14
+
15
+ ```tsx
16
+ import { styled } from '@compiled/react';
17
+
18
+ const Component = styled.div<{ width: number }>((props) => ({
19
+ width: props.width,
20
+ }));
21
+ ```
22
+
23
+ ```tsx
24
+ import styled from 'styled-components';
25
+
26
+ const Component = styled.div<{ width: number }>({
27
+ width: (props) => props.width,
28
+ });
29
+ ```
30
+
31
+ ```tsx
32
+ import styled from 'styled-components';
33
+
34
+ const Container = styled.div<hasPadding>({
35
+ padding: ({ hasPadding }) =>
36
+ hasPadding ? token('space.100', '8px') : token('space.0', '0px'),
37
+ });
38
+ ```
39
+
40
+ ### Correct
41
+
42
+ **Flags**
43
+
44
+ If you have a style that is on/off depending on a prop,
45
+ use the `css` prop to conditionally apply it.
46
+
47
+ ```tsx
48
+ import { css } from '@compiled/react';
49
+
50
+ const baseStyles = css({ padding: token('space.0') });
51
+ const hasPaddingStyles = css({ padding: token('space.100') });
52
+
53
+ const Container = ({ hasPadding = false }: { hasPadding?: boolean }) => {
54
+ return <div css={[baseStyles, hasPadding && hasPaddingStyles]} />;
55
+ };
56
+ ```
57
+
58
+ **Variants**
59
+
60
+ If you have variants which are known ahead of time,
61
+ use `cssMap` to define them and the `css` prop to conditionally apply it.
62
+
63
+ ```tsx
64
+ import { css } from '@compiled/react';
65
+
66
+ const baseStyles = css({ width: 100 });
67
+ const stylesMap = cssMap({
68
+ primary: {
69
+ background: token('color.background.brand.bold'),
70
+ '&:hover': { background: token('color.background.brand.bold.hovered') },
71
+ },
72
+ error: {
73
+ background: token('color.background.warning.bold'),
74
+ '&:hover': { background: token('color.background.warning.bold.hovered') },
75
+ }
76
+ });
77
+
78
+ const Wrapper = ({ appearance = 'primary' }: { appearance?: 'primary' | 'error' }) => (
79
+ <div css={[baseStyles, stylesMap[appearance]} />
80
+ );
81
+ ```
82
+
83
+ **Dynamic values**
84
+
85
+ If you have styles which are truly dynamic,
86
+ use the `style` prop.
87
+
88
+ This approach should be used as a last resort.
89
+
90
+ ```tsx
91
+ import { css } from '@compiled/react';
92
+
93
+ const Component = ({ width }: { width: number }) => {
94
+ return <div style={{ width }} />;
95
+ };
96
+ ```
97
+
98
+ ```tsx
99
+ import { css } from '@compiled/react';
100
+
101
+ const containerStyles = css({
102
+ '::before': {
103
+ width: 'var(--ak-example-width)',
104
+ },
105
+ });
106
+
107
+ function Example({ width }: { width: number }) {
108
+ return (
109
+ <div css={containerStyles} style={{ '--ak-example-width': `${width}px` }} />
110
+ );
111
+ }
112
+ ```
113
+
114
+ ## Options
115
+
116
+ ### importSources
117
+
118
+ By default, this rule will check usages from:
119
+
120
+ - `@atlaskit/css`
121
+ - `@atlaskit/primitives`
122
+ - `@compiled/react`
123
+ - `@emotion/react`
124
+ - `@emotion/core`
125
+ - `@emotion/styled`
126
+ - `styled-components`
127
+
128
+ To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
9
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -18,6 +18,7 @@ var _default = exports.default = {
18
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
19
19
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
21
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
21
22
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
22
23
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
23
24
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
9
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -18,6 +18,7 @@ var _default = exports.default = {
18
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
19
19
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
20
20
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
21
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
21
22
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
22
23
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
23
24
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -11,11 +11,12 @@ var _localCxXcss = _interopRequireDefault(require("./local-cx-xcss"));
11
11
  var _noArrayArguments = _interopRequireDefault(require("./no-array-arguments"));
12
12
  var _noClassnameProp = _interopRequireDefault(require("./no-classname-prop"));
13
13
  var _noContainerQueries = _interopRequireDefault(require("./no-container-queries"));
14
+ var _noDynamicStyles = _interopRequireDefault(require("./no-dynamic-styles"));
14
15
  var _noImportantStyles = _interopRequireDefault(require("./no-important-styles"));
15
16
  var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
16
17
  /**
17
18
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
18
- * @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
19
+ * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
19
20
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
20
21
  */
21
22
  var _default = exports.default = {
@@ -25,6 +26,7 @@ var _default = exports.default = {
25
26
  'no-array-arguments': _noArrayArguments.default,
26
27
  'no-classname-prop': _noClassnameProp.default,
27
28
  'no-container-queries': _noContainerQueries.default,
29
+ 'no-dynamic-styles': _noDynamicStyles.default,
28
30
  'no-important-styles': _noImportantStyles.default,
29
31
  'no-nested-selectors': _noNestedSelectors.default
30
32
  };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.rule = exports.default = void 0;
8
+ var _esquery = _interopRequireDefault(require("esquery"));
9
+ var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
10
+ var _createRule = require("../utils/create-rule");
11
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
+ var schema = [{
15
+ type: 'object',
16
+ properties: {
17
+ importSources: {
18
+ type: 'array',
19
+ items: {
20
+ type: 'string'
21
+ },
22
+ uniqueItems: true
23
+ }
24
+ }
25
+ }];
26
+ var rule = exports.rule = (0, _createRule.createLintRule)({
27
+ meta: {
28
+ name: 'no-dynamic-styles',
29
+ docs: {
30
+ description: 'Disallows use of dynamic styles in CSS-in-JS calls',
31
+ recommended: true,
32
+ severity: 'warn'
33
+ },
34
+ schema: schema,
35
+ messages: {
36
+ 'no-dynamic-styles': 'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.'
37
+ },
38
+ type: 'problem'
39
+ },
40
+ create: function create(context) {
41
+ var importSources = (0, _isSupportedImport.getImportSources)(context);
42
+ return {
43
+ CallExpression: function CallExpression(node) {
44
+ var _context$sourceCode$g = context.sourceCode.getScope(node),
45
+ references = _context$sourceCode$g.references;
46
+ if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) {
47
+ return;
48
+ }
49
+ var matches = (0, _esquery.default)(node, 'ArrowFunctionExpression, FunctionExpression');
50
+ var _iterator = _createForOfIteratorHelper(matches),
51
+ _step;
52
+ try {
53
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
54
+ var match = _step.value;
55
+ context.report({
56
+ node: match,
57
+ messageId: 'no-dynamic-styles'
58
+ });
59
+ }
60
+ } catch (err) {
61
+ _iterator.e(err);
62
+ } finally {
63
+ _iterator.f();
64
+ }
65
+ }
66
+ };
67
+ }
68
+ });
69
+ var _default = exports.default = rule;
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -12,6 +12,7 @@ export default {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -12,6 +12,7 @@ export default {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -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::0611efa9bbe46ece121a804fa5891dd6>>
3
+ * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -9,6 +9,7 @@ import localCxXcss from './local-cx-xcss';
9
9
  import noArrayArguments from './no-array-arguments';
10
10
  import noClassnameProp from './no-classname-prop';
11
11
  import noContainerQueries from './no-container-queries';
12
+ import noDynamicStyles from './no-dynamic-styles';
12
13
  import noImportantStyles from './no-important-styles';
13
14
  import noNestedSelectors from './no-nested-selectors';
14
15
  export default {
@@ -18,6 +19,7 @@ export default {
18
19
  'no-array-arguments': noArrayArguments,
19
20
  'no-classname-prop': noClassnameProp,
20
21
  'no-container-queries': noContainerQueries,
22
+ 'no-dynamic-styles': noDynamicStyles,
21
23
  'no-important-styles': noImportantStyles,
22
24
  'no-nested-selectors': noNestedSelectors
23
25
  };
@@ -0,0 +1,51 @@
1
+ import esquery from 'esquery';
2
+ import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
3
+ import { createLintRule } from '../utils/create-rule';
4
+ const schema = [{
5
+ type: 'object',
6
+ properties: {
7
+ importSources: {
8
+ type: 'array',
9
+ items: {
10
+ type: 'string'
11
+ },
12
+ uniqueItems: true
13
+ }
14
+ }
15
+ }];
16
+ export const rule = createLintRule({
17
+ meta: {
18
+ name: 'no-dynamic-styles',
19
+ docs: {
20
+ description: 'Disallows use of dynamic styles in CSS-in-JS calls',
21
+ recommended: true,
22
+ severity: 'warn'
23
+ },
24
+ schema,
25
+ messages: {
26
+ 'no-dynamic-styles': 'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.'
27
+ },
28
+ type: 'problem'
29
+ },
30
+ create(context) {
31
+ const importSources = getImportSources(context);
32
+ return {
33
+ CallExpression(node) {
34
+ const {
35
+ references
36
+ } = context.sourceCode.getScope(node);
37
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
38
+ return;
39
+ }
40
+ const matches = esquery(node, 'ArrowFunctionExpression, FunctionExpression');
41
+ for (const match of matches) {
42
+ context.report({
43
+ node: match,
44
+ messageId: 'no-dynamic-styles'
45
+ });
46
+ }
47
+ }
48
+ };
49
+ }
50
+ });
51
+ export default rule;
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -12,6 +12,7 @@ export default {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -12,6 +12,7 @@ export default {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -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::0611efa9bbe46ece121a804fa5891dd6>>
3
+ * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -9,6 +9,7 @@ import localCxXcss from './local-cx-xcss';
9
9
  import noArrayArguments from './no-array-arguments';
10
10
  import noClassnameProp from './no-classname-prop';
11
11
  import noContainerQueries from './no-container-queries';
12
+ import noDynamicStyles from './no-dynamic-styles';
12
13
  import noImportantStyles from './no-important-styles';
13
14
  import noNestedSelectors from './no-nested-selectors';
14
15
  export default {
@@ -18,6 +19,7 @@ export default {
18
19
  'no-array-arguments': noArrayArguments,
19
20
  'no-classname-prop': noClassnameProp,
20
21
  'no-container-queries': noContainerQueries,
22
+ 'no-dynamic-styles': noDynamicStyles,
21
23
  'no-important-styles': noImportantStyles,
22
24
  'no-nested-selectors': noNestedSelectors
23
25
  };
@@ -0,0 +1,62 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
3
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
4
+ import esquery from 'esquery';
5
+ import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
6
+ import { createLintRule } from '../utils/create-rule';
7
+ var schema = [{
8
+ type: 'object',
9
+ properties: {
10
+ importSources: {
11
+ type: 'array',
12
+ items: {
13
+ type: 'string'
14
+ },
15
+ uniqueItems: true
16
+ }
17
+ }
18
+ }];
19
+ export var rule = createLintRule({
20
+ meta: {
21
+ name: 'no-dynamic-styles',
22
+ docs: {
23
+ description: 'Disallows use of dynamic styles in CSS-in-JS calls',
24
+ recommended: true,
25
+ severity: 'warn'
26
+ },
27
+ schema: schema,
28
+ messages: {
29
+ 'no-dynamic-styles': 'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.'
30
+ },
31
+ type: 'problem'
32
+ },
33
+ create: function create(context) {
34
+ var importSources = getImportSources(context);
35
+ return {
36
+ CallExpression: function CallExpression(node) {
37
+ var _context$sourceCode$g = context.sourceCode.getScope(node),
38
+ references = _context$sourceCode$g.references;
39
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
40
+ return;
41
+ }
42
+ var matches = esquery(node, 'ArrowFunctionExpression, FunctionExpression');
43
+ var _iterator = _createForOfIteratorHelper(matches),
44
+ _step;
45
+ try {
46
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
47
+ var match = _step.value;
48
+ context.report({
49
+ node: match,
50
+ messageId: 'no-dynamic-styles'
51
+ });
52
+ }
53
+ } catch (err) {
54
+ _iterator.e(err);
55
+ } finally {
56
+ _iterator.f();
57
+ }
58
+ }
59
+ };
60
+ }
61
+ });
62
+ export default rule;
@@ -9,6 +9,7 @@ export declare const configs: {
9
9
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
10
10
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
11
11
  '@atlaskit/ui-styling-standard/no-container-queries': string;
12
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
12
13
  '@atlaskit/ui-styling-standard/no-important-styles': string;
13
14
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
14
15
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -42,6 +43,7 @@ export declare const configs: {
42
43
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
43
44
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
44
45
  '@atlaskit/ui-styling-standard/no-container-queries': string;
46
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
45
47
  '@atlaskit/ui-styling-standard/no-important-styles': string;
46
48
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
47
49
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -12,6 +12,7 @@ declare const _default: {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': string;
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': string;
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -12,6 +12,7 @@ declare const _default: {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': string;
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': string;
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -5,6 +5,7 @@ declare const _default: {
5
5
  'no-array-arguments': import("eslint").Rule.RuleModule;
6
6
  'no-classname-prop': import("eslint").Rule.RuleModule;
7
7
  'no-container-queries': import("eslint").Rule.RuleModule;
8
+ 'no-dynamic-styles': import("eslint").Rule.RuleModule;
8
9
  'no-important-styles': import("eslint").Rule.RuleModule;
9
10
  'no-nested-selectors': import("eslint").Rule.RuleModule;
10
11
  };
@@ -0,0 +1,2 @@
1
+ export declare const rule: import("eslint").Rule.RuleModule;
2
+ export default rule;
@@ -9,6 +9,7 @@ export declare const configs: {
9
9
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
10
10
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
11
11
  '@atlaskit/ui-styling-standard/no-container-queries': string;
12
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
12
13
  '@atlaskit/ui-styling-standard/no-important-styles': string;
13
14
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
14
15
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -42,6 +43,7 @@ export declare const configs: {
42
43
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
43
44
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
44
45
  '@atlaskit/ui-styling-standard/no-container-queries': string;
46
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
45
47
  '@atlaskit/ui-styling-standard/no-important-styles': string;
46
48
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
47
49
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -12,6 +12,7 @@ declare const _default: {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': string;
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': string;
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
@@ -12,6 +12,7 @@ declare const _default: {
12
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
13
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
14
14
  '@atlaskit/ui-styling-standard/no-container-queries': string;
15
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': string;
15
16
  '@atlaskit/ui-styling-standard/no-important-styles': string;
16
17
  '@atlaskit/ui-styling-standard/no-nested-selectors': string;
17
18
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
@@ -5,6 +5,7 @@ declare const _default: {
5
5
  'no-array-arguments': import("eslint").Rule.RuleModule;
6
6
  'no-classname-prop': import("eslint").Rule.RuleModule;
7
7
  'no-container-queries': import("eslint").Rule.RuleModule;
8
+ 'no-dynamic-styles': import("eslint").Rule.RuleModule;
8
9
  'no-important-styles': import("eslint").Rule.RuleModule;
9
10
  'no-nested-selectors': import("eslint").Rule.RuleModule;
10
11
  };
@@ -0,0 +1,2 @@
1
+ export declare const rule: import("eslint").Rule.RuleModule;
2
+ export default rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
6
6
  "license": "Apache-2.0",
@@ -44,6 +44,7 @@
44
44
  "@typescript-eslint/utils": "^5.48.1",
45
45
  "eslint-codemod-utils": "^1.8.6",
46
46
  "eslint-scope": "^8.0.0",
47
+ "esquery": "^1.5.0",
47
48
  "estraverse": "^5.3.0"
48
49
  },
49
50
  "devDependencies": {
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -16,6 +16,7 @@ export default {
16
16
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
18
18
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
19
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
20
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
21
22
  '@atlaskit/design-system/consistent-css-prop-usage': [
@@ -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::f2d8f76f2f41101bd777223bb368ede0>>
3
+ * @codegen <<SignedSource::8859bbdd2c4bf68ed4a1780f0e776e47>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -16,6 +16,7 @@ export default {
16
16
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
18
18
  '@atlaskit/ui-styling-standard/no-container-queries': 'warn',
19
+ '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
20
21
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
21
22
  '@atlaskit/design-system/consistent-css-prop-usage': [
@@ -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::0611efa9bbe46ece121a804fa5891dd6>>
3
+ * @codegen <<SignedSource::12e455a3866637a2dc91ce8751babd54>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -9,6 +9,7 @@ import localCxXcss from './local-cx-xcss';
9
9
  import noArrayArguments from './no-array-arguments';
10
10
  import noClassnameProp from './no-classname-prop';
11
11
  import noContainerQueries from './no-container-queries';
12
+ import noDynamicStyles from './no-dynamic-styles';
12
13
  import noImportantStyles from './no-important-styles';
13
14
  import noNestedSelectors from './no-nested-selectors';
14
15
 
@@ -19,6 +20,7 @@ export default {
19
20
  'no-array-arguments': noArrayArguments,
20
21
  'no-classname-prop': noClassnameProp,
21
22
  'no-container-queries': noContainerQueries,
23
+ 'no-dynamic-styles': noDynamicStyles,
22
24
  'no-important-styles': noImportantStyles,
23
25
  'no-nested-selectors': noNestedSelectors,
24
26
  };
@@ -0,0 +1,126 @@
1
+ This rule prevents the use of dynamic styles (function expressions) in style declarations.
2
+
3
+ This ensures that style declarations:
4
+
5
+ - are statically analyzable
6
+ - can be efficiently extracted into atomic CSS
7
+ - are more readable, deterministic, and thus easier to maintain
8
+
9
+ ## Examples
10
+
11
+ ### Incorrect
12
+
13
+ ```tsx
14
+ import { styled } from '@compiled/react';
15
+
16
+ const Component = styled.div<{ width: number }>((props) => ({
17
+ width: props.width,
18
+ }));
19
+ ```
20
+
21
+ ```tsx
22
+ import styled from 'styled-components';
23
+
24
+ const Component = styled.div<{ width: number }>({
25
+ width: (props) => props.width,
26
+ });
27
+ ```
28
+
29
+ ```tsx
30
+ import styled from 'styled-components';
31
+
32
+ const Container = styled.div<hasPadding>({
33
+ padding: ({ hasPadding }) =>
34
+ hasPadding ? token('space.100', '8px') : token('space.0', '0px'),
35
+ });
36
+ ```
37
+
38
+ ### Correct
39
+
40
+ **Flags**
41
+
42
+ If you have a style that is on/off depending on a prop,
43
+ use the `css` prop to conditionally apply it.
44
+
45
+ ```tsx
46
+ import { css } from '@compiled/react';
47
+
48
+ const baseStyles = css({ padding: token('space.0') });
49
+ const hasPaddingStyles = css({ padding: token('space.100') });
50
+
51
+ const Container = ({ hasPadding = false }: { hasPadding?: boolean }) => {
52
+ return <div css={[baseStyles, hasPadding && hasPaddingStyles]} />;
53
+ };
54
+ ```
55
+
56
+ **Variants**
57
+
58
+ If you have variants which are known ahead of time,
59
+ use `cssMap` to define them and the `css` prop to conditionally apply it.
60
+
61
+ ```tsx
62
+ import { css } from '@compiled/react';
63
+
64
+ const baseStyles = css({ width: 100 });
65
+ const stylesMap = cssMap({
66
+ primary: {
67
+ background: token('color.background.brand.bold'),
68
+ '&:hover': { background: token('color.background.brand.bold.hovered') },
69
+ },
70
+ error: {
71
+ background: token('color.background.warning.bold'),
72
+ '&:hover': { background: token('color.background.warning.bold.hovered') },
73
+ }
74
+ });
75
+
76
+ const Wrapper = ({ appearance = 'primary' }: { appearance?: 'primary' | 'error' }) => (
77
+ <div css={[baseStyles, stylesMap[appearance]} />
78
+ );
79
+ ```
80
+
81
+ **Dynamic values**
82
+
83
+ If you have styles which are truly dynamic,
84
+ use the `style` prop.
85
+
86
+ This approach should be used as a last resort.
87
+
88
+ ```tsx
89
+ import { css } from '@compiled/react';
90
+
91
+ const Component = ({ width }: { width: number }) => {
92
+ return <div style={{ width }} />;
93
+ };
94
+ ```
95
+
96
+ ```tsx
97
+ import { css } from '@compiled/react';
98
+
99
+ const containerStyles = css({
100
+ '::before': {
101
+ width: 'var(--ak-example-width)',
102
+ },
103
+ });
104
+
105
+ function Example({ width }: { width: number }) {
106
+ return (
107
+ <div css={containerStyles} style={{ '--ak-example-width': `${width}px` }} />
108
+ );
109
+ }
110
+ ```
111
+
112
+ ## Options
113
+
114
+ ### importSources
115
+
116
+ By default, this rule will check usages from:
117
+
118
+ - `@atlaskit/css`
119
+ - `@atlaskit/primitives`
120
+ - `@compiled/react`
121
+ - `@emotion/react`
122
+ - `@emotion/core`
123
+ - `@emotion/styled`
124
+ - `styled-components`
125
+
126
+ To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
@@ -0,0 +1,128 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('no-dynamic-styles', rule, {
5
+ valid: [
6
+ {
7
+ name: 'arrow function expression outside of a styling context',
8
+ code: `
9
+ const arrowFunctionExpression = (props) => ({
10
+ width: props.width
11
+ });
12
+ `,
13
+ },
14
+ {
15
+ name: 'function expression outside of a styling context',
16
+ code: `
17
+ const functionExpression = function(props) {
18
+ return {
19
+ height: props.height
20
+ };
21
+ }
22
+ `,
23
+ },
24
+ {
25
+ name: 'function expressions as object values outside of a styling context',
26
+ code: `
27
+ const styles = {
28
+ width: props => props.width,
29
+ height: function(props) { return props.height }
30
+ };
31
+ `,
32
+ },
33
+ {
34
+ name: "importSources doesn't include the styling library",
35
+ code: `
36
+ import { css } from '@compiled/react';
37
+
38
+ const styles = css(props => ({
39
+ width: props.width
40
+ }));
41
+ `,
42
+ options: [{ importSources: [] }],
43
+ },
44
+ ],
45
+ invalid: [
46
+ {
47
+ name: 'function as argument to css',
48
+ code: `
49
+ import { css } from '@compiled/react';
50
+
51
+ const styles = css(props => ({
52
+ width: props.width
53
+ }));
54
+ `,
55
+ errors: [{ messageId: 'no-dynamic-styles' }],
56
+ },
57
+ {
58
+ name: 'function as argument to styled',
59
+ code: `
60
+ import { styled } from '@compiled/react';
61
+
62
+ const Component = styled.div(props => ({
63
+ width: props.width
64
+ }));
65
+ `,
66
+ errors: [{ messageId: 'no-dynamic-styles' }],
67
+ },
68
+ {
69
+ name: 'multiple functions as arguments',
70
+ code: `
71
+ import { styled } from '@compiled/react';
72
+
73
+ const Component = styled.div(
74
+ props => ({
75
+ width: props.width
76
+ }),
77
+ { color: 'red' },
78
+ props => ({
79
+ height: props.height
80
+ })
81
+ );
82
+ `,
83
+ errors: [
84
+ { messageId: 'no-dynamic-styles' },
85
+ { messageId: 'no-dynamic-styles' },
86
+ ],
87
+ },
88
+ {
89
+ name: 'functions as values',
90
+ code: `
91
+ import { styled } from '@compiled/react';
92
+
93
+ const Component = styled.div({
94
+ width: props => props.width,
95
+ height: function(props) { return props.height; }
96
+ });
97
+ `,
98
+ errors: [
99
+ { messageId: 'no-dynamic-styles' },
100
+ { messageId: 'no-dynamic-styles' },
101
+ ],
102
+ },
103
+ {
104
+ name: 'importSources includes styling library not in built-in list',
105
+ code: `
106
+ import { css } from 'my-package';
107
+
108
+ const styles = css(props => ({
109
+ width: props.width
110
+ }));
111
+ `,
112
+ options: [{ importSources: ['my-package'] }],
113
+ errors: [{ messageId: 'no-dynamic-styles' }],
114
+ },
115
+ {
116
+ // This fails type-checking anyway, but doesn't hurt to test
117
+ name: 'arrow function in template literal',
118
+ code: `
119
+ import { css } from '@compiled/react';
120
+
121
+ const styles = css({
122
+ height: \`\${(props) => props.height}px\`,
123
+ });
124
+ `,
125
+ errors: [{ messageId: 'no-dynamic-styles' }],
126
+ },
127
+ ],
128
+ });
@@ -0,0 +1,63 @@
1
+ import esquery from 'esquery';
2
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
3
+
4
+ import {
5
+ getImportSources,
6
+ hasStyleObjectArguments,
7
+ } from '@atlaskit/eslint-utils/is-supported-import';
8
+
9
+ import { createLintRule } from '../utils/create-rule';
10
+
11
+ const schema: JSONSchema4 = [
12
+ {
13
+ type: 'object',
14
+ properties: {
15
+ importSources: {
16
+ type: 'array',
17
+ items: { type: 'string' },
18
+ uniqueItems: true,
19
+ },
20
+ },
21
+ },
22
+ ];
23
+
24
+ export const rule = createLintRule({
25
+ meta: {
26
+ name: 'no-dynamic-styles',
27
+ docs: {
28
+ description: 'Disallows use of dynamic styles in CSS-in-JS calls',
29
+ recommended: true,
30
+ severity: 'warn',
31
+ },
32
+ schema,
33
+ messages: {
34
+ 'no-dynamic-styles':
35
+ 'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.',
36
+ },
37
+ type: 'problem',
38
+ },
39
+ create(context) {
40
+ const importSources = getImportSources(context);
41
+
42
+ return {
43
+ CallExpression(node) {
44
+ const { references } = context.sourceCode.getScope(node);
45
+
46
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
47
+ return;
48
+ }
49
+
50
+ const matches = esquery(
51
+ node,
52
+ 'ArrowFunctionExpression, FunctionExpression',
53
+ );
54
+
55
+ for (const match of matches) {
56
+ context.report({ node: match, messageId: 'no-dynamic-styles' });
57
+ }
58
+ },
59
+ };
60
+ },
61
+ });
62
+
63
+ export default rule;