@atlaskit/eslint-plugin-ui-styling-standard 0.9.0 → 0.9.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.
Files changed (33) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -0
  3. package/constellation/enforce-style-prop/usage.mdx +38 -0
  4. package/constellation/index/overview.mdx +1 -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/enforce-style-prop/index.js +141 -0
  8. package/dist/cjs/rules/index.codegen.js +3 -1
  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/enforce-style-prop/index.js +133 -0
  12. package/dist/es2019/rules/index.codegen.js +3 -1
  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/enforce-style-prop/index.js +135 -0
  16. package/dist/esm/rules/index.codegen.js +3 -1
  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/enforce-style-prop/index.d.ts +3 -0
  21. package/dist/types/rules/index.codegen.d.ts +1 -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/enforce-style-prop/index.d.ts +3 -0
  26. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  27. package/package.json +2 -2
  28. package/src/presets/all.codegen.tsx +2 -1
  29. package/src/presets/recommended.codegen.tsx +2 -1
  30. package/src/rules/enforce-style-prop/README.md +36 -0
  31. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +231 -0
  32. package/src/rules/enforce-style-prop/index.tsx +169 -0
  33. package/src/rules/index.codegen.tsx +3 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.9.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#90977](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/90977) [`becda03f375e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/becda03f375e) - Create eslint rule to enforce style prop usage by disallowing passing static css values
8
+
3
9
  ## 0.9.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -14,6 +14,7 @@ Not intended for use outside the Atlassian frontend repository.
14
14
  | Rule | Description | Recommended | Fixable | Suggestions |
15
15
  | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
16
16
  | <a href="./src/rules/convert-props-syntax/README.md">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
17
+ | <a href="./src/rules/enforce-style-prop/README.md">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
17
18
  | <a href="./src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
18
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 | |
19
20
  | <a href="./src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
@@ -0,0 +1,38 @@
1
+ # enforce-style-prop
2
+
3
+ This rule enforces passing dynamic CSS values to the `style` prop in JSX components.
4
+
5
+ We only want `props.style` to be used for dynamic values that cannot be easily determined at build time, e.g. `<div style={{ width: props.width }}>`. No statics values such as numbers, strings or any other statically analysable values should be passed to the `style` attribute. Instead, static CSS values should be passed into the `css` prop in JSX components.
6
+
7
+ ## Examples
8
+
9
+ ### Incorrect
10
+
11
+ ```tsx
12
+ function Component() {
13
+ return (
14
+ <div
15
+ style={{
16
+ margin: 0,
17
+ color: 'red',
18
+ }}
19
+ />
20
+ );
21
+ }
22
+ ```
23
+
24
+ ### Correct
25
+
26
+ ```tsx
27
+ function Component(props) {
28
+ return (
29
+ <div
30
+ style={{
31
+ width: props.width,
32
+ '--my-nested-width': props.width,
33
+ }}
34
+ css={css({ margin: 0, color: 'red' })}
35
+ />
36
+ );
37
+ }
38
+ ```
@@ -12,6 +12,7 @@ but more are planned to support the standard over time.
12
12
  | Rule | Description | Recommended | Fixable | Suggestions |
13
13
  | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
14
14
  | <a href="convert-props-syntax/usage">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
15
+ | <a href="enforce-style-prop/usage">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
15
16
  | <a href="local-cx-xcss/usage">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
16
17
  | <a href="no-array-arguments/usage">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
17
18
  | <a href="no-classname-prop/usage">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
@@ -6,13 +6,14 @@ 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::d9829b8ad0e327987766d6901d0729b4>>
9
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
13
13
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
14
14
  rules: {
15
15
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
16
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
16
17
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
17
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -6,13 +6,14 @@ 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::d9829b8ad0e327987766d6901d0729b4>>
9
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
13
13
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
14
14
  rules: {
15
15
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
16
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
16
17
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
17
18
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.rule = exports.default = void 0;
7
+ var _eslintCodemodUtils = require("eslint-codemod-utils");
8
+ var _createRule = require("../utils/create-rule");
9
+ var findVariable = function findVariable(context, name) {
10
+ return context.getScope().variables.find(function (varb) {
11
+ return varb.name === name;
12
+ });
13
+ };
14
+ var isParameter = function isParameter(context, variableName) {
15
+ var _findVariable;
16
+ var definitions = (_findVariable = findVariable(context, variableName)) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
17
+ if (!definitions || !definitions.length) {
18
+ return false;
19
+ }
20
+ var result = definitions[0];
21
+ return result.type === 'Parameter';
22
+ };
23
+ var checkIdentifier = function checkIdentifier(context, identifier) {
24
+ var _parent;
25
+ var val = identifier.name;
26
+ var variable = findVariable(context, val);
27
+ if (!variable) {
28
+ return false;
29
+ }
30
+ var variableDefinition = variable.defs[0].node;
31
+
32
+ // If identifier is a RestElement, report warning
33
+ if (((_parent = variable.identifiers[0].parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'RestElement') {
34
+ return false;
35
+ }
36
+
37
+ // Checking if identifier has been destructured from props within function
38
+ if (variableDefinition.type === 'VariableDeclarator') {
39
+ var _variableDefinition$i, _variableDefinition$i2, _variableDefinition$i3;
40
+ // If it's a let variable, ignore
41
+ if (variableDefinition.parent.kind === 'let') {
42
+ return true;
43
+ }
44
+ // If it's initialised by a call expression, ignore
45
+ if (((_variableDefinition$i = variableDefinition.init) === null || _variableDefinition$i === void 0 ? void 0 : _variableDefinition$i.type) === 'CallExpression') {
46
+ return true;
47
+ }
48
+ // Destructured from member expression
49
+ // e.g. const width = props.width
50
+ if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
51
+ var memberIdentifierName = variableDefinition.init.object.name;
52
+ return isParameter(context, memberIdentifierName);
53
+ }
54
+ // Destructured from object pattern
55
+ // e.g. const { width } = props
56
+ var propName = (_variableDefinition$i3 = variableDefinition.init) === null || _variableDefinition$i3 === void 0 ? void 0 : _variableDefinition$i3.name;
57
+ return isParameter(context, propName);
58
+ }
59
+ // If identifier has not been deconstructed from parameter or is a RestElement, report warning
60
+ return isParameter(context, val);
61
+ };
62
+ function isPropertyValueAllowed(property, context) {
63
+ if (property.type === 'SpreadElement') {
64
+ return false;
65
+ }
66
+ // property.type === 'Property', moving on to check if value is static
67
+ if (property.value.type === 'Literal') {
68
+ return false;
69
+ } else if (property.value.type === 'Identifier') {
70
+ return checkIdentifier(context, property.value);
71
+ } else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier' && property.value.callee.name === 'token') {
72
+ return false;
73
+ } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
74
+ var _findVariable2;
75
+ var val = property.value.object.name;
76
+ var definitions = (_findVariable2 = findVariable(context, val)) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
77
+ if (!definitions || !definitions.length) {
78
+ return false;
79
+ }
80
+ var variable = definitions[0];
81
+ if (!variable) {
82
+ return false;
83
+ }
84
+ var varType = variable.type;
85
+ if (varType === 'Variable') {
86
+ if (!variable.node.init) {
87
+ return false;
88
+ }
89
+ var varSource = variable.node.init.type;
90
+ // Variables initialised by a call expression or object expression are ok
91
+ if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
92
+ return true;
93
+ }
94
+ // Check if member expression matches with function argument
95
+ } else if (varType !== 'Parameter') {
96
+ return false;
97
+ }
98
+ }
99
+ return true;
100
+ }
101
+ var rule = exports.rule = (0, _createRule.createLintRule)({
102
+ meta: {
103
+ name: 'enforce-style-prop',
104
+ docs: {
105
+ description: 'Disallows usage of static styles in the `style` attribute in components',
106
+ recommended: true,
107
+ severity: 'warn'
108
+ },
109
+ messages: {
110
+ 'enforce-style-prop': 'Avoid adding static values to the style prop. Use the css prop for static styles instead.'
111
+ },
112
+ type: 'problem'
113
+ },
114
+ create: function create(context) {
115
+ return {
116
+ 'JSXAttribute[name.name="style"] > JSXExpressionContainer': function JSXAttributeNameNameStyleJSXExpressionContainer(node) {
117
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXExpressionContainer')) {
118
+ return;
119
+ }
120
+ // we've reached an attribute named style
121
+
122
+ if (node.expression.type === 'ObjectExpression') {
123
+ node.expression.properties.forEach(function (value) {
124
+ if (!isPropertyValueAllowed(value, context)) {
125
+ context.report({
126
+ node: value,
127
+ messageId: 'enforce-style-prop'
128
+ });
129
+ }
130
+ });
131
+ } else {
132
+ context.report({
133
+ node: node,
134
+ messageId: 'enforce-style-prop'
135
+ });
136
+ }
137
+ }
138
+ };
139
+ }
140
+ });
141
+ var _default = exports.default = rule;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.default = void 0;
8
8
  var _convertPropsSyntax = _interopRequireDefault(require("./convert-props-syntax"));
9
+ var _enforceStyleProp = _interopRequireDefault(require("./enforce-style-prop"));
9
10
  var _localCxXcss = _interopRequireDefault(require("./local-cx-xcss"));
10
11
  var _noArrayArguments = _interopRequireDefault(require("./no-array-arguments"));
11
12
  var _noClassnameProp = _interopRequireDefault(require("./no-classname-prop"));
@@ -14,11 +15,12 @@ var _noImportantStyles = _interopRequireDefault(require("./no-important-styles")
14
15
  var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
15
16
  /**
16
17
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
17
- * @codegen <<SignedSource::f7d910c4a52a88b6aa58324ebe2edfc2>>
18
+ * @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
18
19
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
19
20
  */
20
21
  var _default = exports.default = {
21
22
  'convert-props-syntax': _convertPropsSyntax.default,
23
+ 'enforce-style-prop': _enforceStyleProp.default,
22
24
  'local-cx-xcss': _localCxXcss.default,
23
25
  'no-array-arguments': _noArrayArguments.default,
24
26
  'no-classname-prop': _noClassnameProp.default,
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -0,0 +1,133 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ const findVariable = (context, name) => {
4
+ return context.getScope().variables.find(varb => varb.name === name);
5
+ };
6
+ const isParameter = (context, variableName) => {
7
+ var _findVariable;
8
+ const definitions = (_findVariable = findVariable(context, variableName)) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
9
+ if (!definitions || !definitions.length) {
10
+ return false;
11
+ }
12
+ const result = definitions[0];
13
+ return result.type === 'Parameter';
14
+ };
15
+ const checkIdentifier = (context, identifier) => {
16
+ var _parent;
17
+ const val = identifier.name;
18
+ const variable = findVariable(context, val);
19
+ if (!variable) {
20
+ return false;
21
+ }
22
+ const variableDefinition = variable.defs[0].node;
23
+
24
+ // If identifier is a RestElement, report warning
25
+ if (((_parent = variable.identifiers[0].parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'RestElement') {
26
+ return false;
27
+ }
28
+
29
+ // Checking if identifier has been destructured from props within function
30
+ if (variableDefinition.type === 'VariableDeclarator') {
31
+ var _variableDefinition$i, _variableDefinition$i2, _variableDefinition$i3;
32
+ // If it's a let variable, ignore
33
+ if (variableDefinition.parent.kind === 'let') {
34
+ return true;
35
+ }
36
+ // If it's initialised by a call expression, ignore
37
+ if (((_variableDefinition$i = variableDefinition.init) === null || _variableDefinition$i === void 0 ? void 0 : _variableDefinition$i.type) === 'CallExpression') {
38
+ return true;
39
+ }
40
+ // Destructured from member expression
41
+ // e.g. const width = props.width
42
+ if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
43
+ const memberIdentifierName = variableDefinition.init.object.name;
44
+ return isParameter(context, memberIdentifierName);
45
+ }
46
+ // Destructured from object pattern
47
+ // e.g. const { width } = props
48
+ const propName = (_variableDefinition$i3 = variableDefinition.init) === null || _variableDefinition$i3 === void 0 ? void 0 : _variableDefinition$i3.name;
49
+ return isParameter(context, propName);
50
+ }
51
+ // If identifier has not been deconstructed from parameter or is a RestElement, report warning
52
+ return isParameter(context, val);
53
+ };
54
+ function isPropertyValueAllowed(property, context) {
55
+ if (property.type === 'SpreadElement') {
56
+ return false;
57
+ }
58
+ // property.type === 'Property', moving on to check if value is static
59
+ if (property.value.type === 'Literal') {
60
+ return false;
61
+ } else if (property.value.type === 'Identifier') {
62
+ return checkIdentifier(context, property.value);
63
+ } else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier' && property.value.callee.name === 'token') {
64
+ return false;
65
+ } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
66
+ var _findVariable2;
67
+ const val = property.value.object.name;
68
+ const definitions = (_findVariable2 = findVariable(context, val)) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
69
+ if (!definitions || !definitions.length) {
70
+ return false;
71
+ }
72
+ const variable = definitions[0];
73
+ if (!variable) {
74
+ return false;
75
+ }
76
+ const varType = variable.type;
77
+ if (varType === 'Variable') {
78
+ if (!variable.node.init) {
79
+ return false;
80
+ }
81
+ const varSource = variable.node.init.type;
82
+ // Variables initialised by a call expression or object expression are ok
83
+ if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
84
+ return true;
85
+ }
86
+ // Check if member expression matches with function argument
87
+ } else if (varType !== 'Parameter') {
88
+ return false;
89
+ }
90
+ }
91
+ return true;
92
+ }
93
+ export const rule = createLintRule({
94
+ meta: {
95
+ name: 'enforce-style-prop',
96
+ docs: {
97
+ description: 'Disallows usage of static styles in the `style` attribute in components',
98
+ recommended: true,
99
+ severity: 'warn'
100
+ },
101
+ messages: {
102
+ 'enforce-style-prop': 'Avoid adding static values to the style prop. Use the css prop for static styles instead.'
103
+ },
104
+ type: 'problem'
105
+ },
106
+ create(context) {
107
+ return {
108
+ 'JSXAttribute[name.name="style"] > JSXExpressionContainer': node => {
109
+ if (!isNodeOfType(node, 'JSXExpressionContainer')) {
110
+ return;
111
+ }
112
+ // we've reached an attribute named style
113
+
114
+ if (node.expression.type === 'ObjectExpression') {
115
+ node.expression.properties.forEach(value => {
116
+ if (!isPropertyValueAllowed(value, context)) {
117
+ context.report({
118
+ node: value,
119
+ messageId: 'enforce-style-prop'
120
+ });
121
+ }
122
+ });
123
+ } else {
124
+ context.report({
125
+ node: node,
126
+ messageId: 'enforce-style-prop'
127
+ });
128
+ }
129
+ }
130
+ };
131
+ }
132
+ });
133
+ export default rule;
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::f7d910c4a52a88b6aa58324ebe2edfc2>>
3
+ * @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
7
+ import enforceStyleProp from './enforce-style-prop';
7
8
  import localCxXcss from './local-cx-xcss';
8
9
  import noArrayArguments from './no-array-arguments';
9
10
  import noClassnameProp from './no-classname-prop';
@@ -12,6 +13,7 @@ import noImportantStyles from './no-important-styles';
12
13
  import noNestedSelectors from './no-nested-selectors';
13
14
  export default {
14
15
  'convert-props-syntax': convertPropsSyntax,
16
+ 'enforce-style-prop': enforceStyleProp,
15
17
  'local-cx-xcss': localCxXcss,
16
18
  'no-array-arguments': noArrayArguments,
17
19
  'no-classname-prop': noClassnameProp,
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -0,0 +1,135 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ var findVariable = function findVariable(context, name) {
4
+ return context.getScope().variables.find(function (varb) {
5
+ return varb.name === name;
6
+ });
7
+ };
8
+ var isParameter = function isParameter(context, variableName) {
9
+ var _findVariable;
10
+ var definitions = (_findVariable = findVariable(context, variableName)) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
11
+ if (!definitions || !definitions.length) {
12
+ return false;
13
+ }
14
+ var result = definitions[0];
15
+ return result.type === 'Parameter';
16
+ };
17
+ var checkIdentifier = function checkIdentifier(context, identifier) {
18
+ var _parent;
19
+ var val = identifier.name;
20
+ var variable = findVariable(context, val);
21
+ if (!variable) {
22
+ return false;
23
+ }
24
+ var variableDefinition = variable.defs[0].node;
25
+
26
+ // If identifier is a RestElement, report warning
27
+ if (((_parent = variable.identifiers[0].parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'RestElement') {
28
+ return false;
29
+ }
30
+
31
+ // Checking if identifier has been destructured from props within function
32
+ if (variableDefinition.type === 'VariableDeclarator') {
33
+ var _variableDefinition$i, _variableDefinition$i2, _variableDefinition$i3;
34
+ // If it's a let variable, ignore
35
+ if (variableDefinition.parent.kind === 'let') {
36
+ return true;
37
+ }
38
+ // If it's initialised by a call expression, ignore
39
+ if (((_variableDefinition$i = variableDefinition.init) === null || _variableDefinition$i === void 0 ? void 0 : _variableDefinition$i.type) === 'CallExpression') {
40
+ return true;
41
+ }
42
+ // Destructured from member expression
43
+ // e.g. const width = props.width
44
+ if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
45
+ var memberIdentifierName = variableDefinition.init.object.name;
46
+ return isParameter(context, memberIdentifierName);
47
+ }
48
+ // Destructured from object pattern
49
+ // e.g. const { width } = props
50
+ var propName = (_variableDefinition$i3 = variableDefinition.init) === null || _variableDefinition$i3 === void 0 ? void 0 : _variableDefinition$i3.name;
51
+ return isParameter(context, propName);
52
+ }
53
+ // If identifier has not been deconstructed from parameter or is a RestElement, report warning
54
+ return isParameter(context, val);
55
+ };
56
+ function isPropertyValueAllowed(property, context) {
57
+ if (property.type === 'SpreadElement') {
58
+ return false;
59
+ }
60
+ // property.type === 'Property', moving on to check if value is static
61
+ if (property.value.type === 'Literal') {
62
+ return false;
63
+ } else if (property.value.type === 'Identifier') {
64
+ return checkIdentifier(context, property.value);
65
+ } else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier' && property.value.callee.name === 'token') {
66
+ return false;
67
+ } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
68
+ var _findVariable2;
69
+ var val = property.value.object.name;
70
+ var definitions = (_findVariable2 = findVariable(context, val)) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
71
+ if (!definitions || !definitions.length) {
72
+ return false;
73
+ }
74
+ var variable = definitions[0];
75
+ if (!variable) {
76
+ return false;
77
+ }
78
+ var varType = variable.type;
79
+ if (varType === 'Variable') {
80
+ if (!variable.node.init) {
81
+ return false;
82
+ }
83
+ var varSource = variable.node.init.type;
84
+ // Variables initialised by a call expression or object expression are ok
85
+ if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
86
+ return true;
87
+ }
88
+ // Check if member expression matches with function argument
89
+ } else if (varType !== 'Parameter') {
90
+ return false;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+ export var rule = createLintRule({
96
+ meta: {
97
+ name: 'enforce-style-prop',
98
+ docs: {
99
+ description: 'Disallows usage of static styles in the `style` attribute in components',
100
+ recommended: true,
101
+ severity: 'warn'
102
+ },
103
+ messages: {
104
+ 'enforce-style-prop': 'Avoid adding static values to the style prop. Use the css prop for static styles instead.'
105
+ },
106
+ type: 'problem'
107
+ },
108
+ create: function create(context) {
109
+ return {
110
+ 'JSXAttribute[name.name="style"] > JSXExpressionContainer': function JSXAttributeNameNameStyleJSXExpressionContainer(node) {
111
+ if (!isNodeOfType(node, 'JSXExpressionContainer')) {
112
+ return;
113
+ }
114
+ // we've reached an attribute named style
115
+
116
+ if (node.expression.type === 'ObjectExpression') {
117
+ node.expression.properties.forEach(function (value) {
118
+ if (!isPropertyValueAllowed(value, context)) {
119
+ context.report({
120
+ node: value,
121
+ messageId: 'enforce-style-prop'
122
+ });
123
+ }
124
+ });
125
+ } else {
126
+ context.report({
127
+ node: node,
128
+ messageId: 'enforce-style-prop'
129
+ });
130
+ }
131
+ }
132
+ };
133
+ }
134
+ });
135
+ export default rule;
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::f7d910c4a52a88b6aa58324ebe2edfc2>>
3
+ * @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
7
+ import enforceStyleProp from './enforce-style-prop';
7
8
  import localCxXcss from './local-cx-xcss';
8
9
  import noArrayArguments from './no-array-arguments';
9
10
  import noClassnameProp from './no-classname-prop';
@@ -12,6 +13,7 @@ import noImportantStyles from './no-important-styles';
12
13
  import noNestedSelectors from './no-nested-selectors';
13
14
  export default {
14
15
  'convert-props-syntax': convertPropsSyntax,
16
+ 'enforce-style-prop': enforceStyleProp,
15
17
  'local-cx-xcss': localCxXcss,
16
18
  'no-array-arguments': noArrayArguments,
17
19
  'no-classname-prop': noClassnameProp,
@@ -4,6 +4,7 @@ export declare const configs: {
4
4
  plugins: string[];
5
5
  rules: {
6
6
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
7
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
7
8
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
8
9
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
9
10
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -36,6 +37,7 @@ export declare const configs: {
36
37
  plugins: string[];
37
38
  rules: {
38
39
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
40
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
39
41
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
40
42
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
41
43
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
7
7
  plugins: string[];
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
7
7
  plugins: string[];
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -1,5 +1,6 @@
1
1
  declare const _default: {
2
2
  'convert-props-syntax': import("eslint").Rule.RuleModule;
3
+ 'enforce-style-prop': import("eslint").Rule.RuleModule;
3
4
  'local-cx-xcss': import("eslint").Rule.RuleModule;
4
5
  'no-array-arguments': import("eslint").Rule.RuleModule;
5
6
  'no-classname-prop': import("eslint").Rule.RuleModule;
@@ -4,6 +4,7 @@ export declare const configs: {
4
4
  plugins: string[];
5
5
  rules: {
6
6
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
7
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
7
8
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
8
9
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
9
10
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -36,6 +37,7 @@ export declare const configs: {
36
37
  plugins: string[];
37
38
  rules: {
38
39
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
40
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
39
41
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
40
42
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
41
43
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
7
7
  plugins: string[];
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
7
7
  plugins: string[];
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/convert-props-syntax': string;
10
+ '@atlaskit/ui-styling-standard/enforce-style-prop': string;
10
11
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
11
12
  '@atlaskit/ui-styling-standard/no-array-arguments': string;
12
13
  '@atlaskit/ui-styling-standard/no-classname-prop': string;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -1,5 +1,6 @@
1
1
  declare const _default: {
2
2
  'convert-props-syntax': import("eslint").Rule.RuleModule;
3
+ 'enforce-style-prop': import("eslint").Rule.RuleModule;
3
4
  'local-cx-xcss': import("eslint").Rule.RuleModule;
4
5
  'no-array-arguments': import("eslint").Rule.RuleModule;
5
6
  'no-classname-prop': import("eslint").Rule.RuleModule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
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",
@@ -48,7 +48,7 @@
48
48
  },
49
49
  "devDependencies": {
50
50
  "@atlaskit/ds-lib": "^2.3.0",
51
- "@atlaskit/eslint-plugin-design-system": "^9.4.0",
51
+ "@atlaskit/eslint-plugin-design-system": "^9.5.0",
52
52
  "@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
53
53
  "@atlassian/codegen": "*",
54
54
  "@compiled/eslint-plugin": "^0.13.6",
@@ -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::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -11,6 +11,7 @@ export default {
11
11
  ],
12
12
  rules: {
13
13
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
14
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
14
15
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-classname-prop': '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::d9829b8ad0e327987766d6901d0729b4>>
3
+ * @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -11,6 +11,7 @@ export default {
11
11
  ],
12
12
  rules: {
13
13
  '@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
14
+ '@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
14
15
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
15
16
  '@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
16
17
  '@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
@@ -0,0 +1,36 @@
1
+ This rule enforces passing dynamic CSS values to the `style` prop in JSX components.
2
+
3
+ We only want `props.style` to be used for dynamic values that cannot be easily determined at build time, e.g. `<div style={{ width: props.width }}>`. No statics values such as numbers, strings or any other statically analysable values should be passed to the `style` attribute. Instead, static CSS values should be passed into the `css` prop in JSX components.
4
+
5
+ ## Examples
6
+
7
+ ### Incorrect
8
+
9
+ ```tsx
10
+ function Component() {
11
+ return (
12
+ <div
13
+ style={{
14
+ margin: 0,
15
+ color: 'red',
16
+ }}
17
+ />
18
+ );
19
+ }
20
+ ```
21
+
22
+ ### Correct
23
+
24
+ ```tsx
25
+ function Component(props) {
26
+ return (
27
+ <div
28
+ style={{
29
+ width: props.width,
30
+ '--my-nested-width': props.width,
31
+ }}
32
+ css={css({ margin: 0, color: 'red' })}
33
+ />
34
+ );
35
+ }
36
+ ```
@@ -0,0 +1,231 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('enforce-style-prop', rule, {
5
+ valid: [
6
+ {
7
+ name: 'No style attribute used',
8
+ code: `
9
+ <div css={styles} />
10
+ `,
11
+ },
12
+ {
13
+ name: 'Values from props are acceptable',
14
+ code: `
15
+ function Component(props) {
16
+ return <div
17
+ style={{
18
+ width: props.width,
19
+ '--my-nested-width': props.width,
20
+ }}
21
+ css={css({ margin: 0, color: 'red' })}
22
+ />
23
+ }
24
+ `,
25
+ },
26
+ {
27
+ name: 'Values from deconstructed props are acceptable',
28
+ code: `
29
+ function DeconstructionComponent1({ myWidth }) {
30
+ return <div style={{ width: myWidth }}>hello</div>;
31
+ }
32
+ `,
33
+ },
34
+ {
35
+ name: 'Multiple values from deconstructed props are acceptable',
36
+ code: `
37
+ function DeconstructionComponent2({ myWidth, height, myColor = '#fff' }) {
38
+ return <div style={{ width: myWidth, height, color: myColor }}>hello</div>;
39
+ }
40
+ `,
41
+ },
42
+ {
43
+ name: 'Skips linting @media query as it is not supported by the style prop anyways',
44
+ code: `
45
+ function Component() {
46
+ return <div
47
+ style={{
48
+ '@media …': { /* ... */ },
49
+ }}
50
+ />
51
+ }
52
+ `,
53
+ },
54
+ {
55
+ name: 'Skips linting selector as it is not supported by the style prop anyways',
56
+ code: `
57
+ function Component() {
58
+ return <div
59
+ style={{
60
+ 'button': { /* ... */ },
61
+ }}
62
+ />
63
+ }
64
+ `,
65
+ },
66
+ {
67
+ name: 'Static styles are outside of style attribute',
68
+ code: `
69
+ function Component(props) {
70
+ return (
71
+ <div
72
+ style={{
73
+ width: props.width,
74
+ }}
75
+ css={css({ margin: 0, color: 'red' })}
76
+ />
77
+ );
78
+ }
79
+ `,
80
+ },
81
+ {
82
+ name: 'Props destructured in function via object pattern',
83
+ code: `
84
+ function Component(props) {
85
+ const { width } = props;
86
+ return <div style={{ width: width }} />
87
+ }
88
+ `,
89
+ },
90
+ {
91
+ name: 'Props destructured in function via member expression',
92
+ code: `
93
+ function Component(props) {
94
+ const width = props.width;
95
+ return <div style={{ width: width }} />
96
+ }
97
+ `,
98
+ },
99
+ {
100
+ name: 'Ignore state variables',
101
+ code: `
102
+ const Component = () => {
103
+ const [state, setState] = useState();
104
+ return <div style={{ width: state.width }} />
105
+ };
106
+ `,
107
+ },
108
+ {
109
+ name: 'Ignore variables initialised by call expression',
110
+ code: `
111
+ const Component = () => {
112
+ const tableWidth = calcDefaultLayoutWidthByAppearance(
113
+ rendererAppearance,
114
+ tableNode,
115
+ );
116
+ return (
117
+ <div style={{ width: tableWidth }} />
118
+ )
119
+ }
120
+ `,
121
+ },
122
+ {
123
+ name: 'Property from object initialisation is ok',
124
+ code: `
125
+ function Component(props) {
126
+ const obj = { width: 0 };
127
+ return <div
128
+ style={{
129
+ width: obj.width
130
+ }}
131
+ />
132
+ }
133
+ `,
134
+ },
135
+ {
136
+ name: 'Let variables are ok',
137
+ code: `
138
+ function Component(props) {
139
+ let width = 0;
140
+ return <div
141
+ style={{
142
+ width
143
+ }}
144
+ />
145
+ }
146
+ `,
147
+ },
148
+ ],
149
+ invalid: [
150
+ {
151
+ name: 'Basic test for static css values',
152
+ code: `
153
+ function Component(props) {
154
+ return <div
155
+ style={{
156
+ margin: 0,
157
+ color: 'red',
158
+ }}
159
+ />
160
+ }
161
+ `,
162
+ errors: [
163
+ { messageId: 'enforce-style-prop' },
164
+ { messageId: 'enforce-style-prop' },
165
+ ],
166
+ },
167
+ {
168
+ name: 'No constants passed to style',
169
+ code: `
170
+ function Component(props) {
171
+ const color = 'red';
172
+ return <div
173
+ style={{
174
+ color, // should be static css
175
+ }}
176
+ />
177
+ }
178
+ `,
179
+ errors: [{ messageId: 'enforce-style-prop' }],
180
+ },
181
+ {
182
+ name: 'No spread operator within style',
183
+ code: `
184
+ import importedStyleObj from '../shared';
185
+ function Component(props) {
186
+ const color = 'red';
187
+ return <div
188
+ style={{
189
+ ...importedStyleObj,
190
+ ...props.style
191
+ }}
192
+ />
193
+ }
194
+ `,
195
+ errors: [
196
+ { messageId: 'enforce-style-prop' },
197
+ { messageId: 'enforce-style-prop' },
198
+ ],
199
+ },
200
+ {
201
+ name: 'No imported object passed to style',
202
+ code: `
203
+ import importedStyleObj from '../shared';
204
+ function AnotherComponent() {
205
+ return <div style={importedStyleObj} />;
206
+ }
207
+ `,
208
+ errors: [{ messageId: 'enforce-style-prop' }],
209
+ },
210
+ {
211
+ name: 'No rest prop',
212
+ code: `
213
+ function Component({ prop1, ...spreadElement }) {
214
+ return <div style={{ 'button': spreadElement }}>hello</div>
215
+ }
216
+ `,
217
+ errors: [{ messageId: 'enforce-style-prop' }],
218
+ },
219
+ {
220
+ name: 'No token calls',
221
+ code: `
222
+ import { token } from '@atlaskit/tokens';
223
+
224
+ function Component() {
225
+ return <div style={{ paddingBottom: token('space.150', '12px') }} />;
226
+ }
227
+ `,
228
+ errors: [{ messageId: 'enforce-style-prop' }],
229
+ },
230
+ ],
231
+ });
@@ -0,0 +1,169 @@
1
+ import { Identifier, isNodeOfType, Node } from 'eslint-codemod-utils';
2
+ import type { SpreadElement, Property } from 'estree-jsx';
3
+
4
+ import { createLintRule } from '../utils/create-rule';
5
+ import type { Rule, Scope } from 'eslint';
6
+
7
+ type ObjPropertyType = Property | SpreadElement;
8
+ type IdentifierWithParent = Scope.Reference['identifier'] &
9
+ Rule.NodeParentExtension;
10
+
11
+ const findVariable = (
12
+ context: Rule.RuleContext,
13
+ name: string,
14
+ ): Scope.Variable | undefined => {
15
+ return context.getScope().variables.find((varb) => varb.name === name);
16
+ };
17
+
18
+ const isParameter = (context: Rule.RuleContext, variableName: string) => {
19
+ const definitions = findVariable(context, variableName)?.defs;
20
+ if (!definitions || !definitions.length) {
21
+ return false;
22
+ }
23
+
24
+ const result = definitions[0];
25
+ return result.type === 'Parameter';
26
+ };
27
+
28
+ const checkIdentifier = (
29
+ context: Rule.RuleContext,
30
+ identifier: Identifier,
31
+ ): boolean => {
32
+ const val = identifier.name;
33
+ const variable = findVariable(context, val);
34
+
35
+ if (!variable) {
36
+ return false;
37
+ }
38
+
39
+ const variableDefinition = variable.defs[0].node;
40
+
41
+ // If identifier is a RestElement, report warning
42
+ if (
43
+ (variable.identifiers[0] as IdentifierWithParent).parent?.type ===
44
+ 'RestElement'
45
+ ) {
46
+ return false;
47
+ }
48
+
49
+ // Checking if identifier has been destructured from props within function
50
+ if (variableDefinition.type === 'VariableDeclarator') {
51
+ // If it's a let variable, ignore
52
+ if (variableDefinition.parent.kind === 'let') {
53
+ return true;
54
+ }
55
+ // If it's initialised by a call expression, ignore
56
+ if (variableDefinition.init?.type === 'CallExpression') {
57
+ return true;
58
+ }
59
+ // Destructured from member expression
60
+ // e.g. const width = props.width
61
+ if (variableDefinition.init?.type === 'MemberExpression') {
62
+ const memberIdentifierName = variableDefinition.init.object.name;
63
+ return isParameter(context, memberIdentifierName);
64
+ }
65
+ // Destructured from object pattern
66
+ // e.g. const { width } = props
67
+ const propName = variableDefinition.init?.name;
68
+ return isParameter(context, propName);
69
+ }
70
+ // If identifier has not been deconstructed from parameter or is a RestElement, report warning
71
+ return isParameter(context, val);
72
+ };
73
+
74
+ function isPropertyValueAllowed(
75
+ property: ObjPropertyType,
76
+ context: Rule.RuleContext,
77
+ ): boolean {
78
+ if (property.type === 'SpreadElement') {
79
+ return false;
80
+ }
81
+ // property.type === 'Property', moving on to check if value is static
82
+ if (property.value.type === 'Literal') {
83
+ return false;
84
+ } else if (property.value.type === 'Identifier') {
85
+ return checkIdentifier(context, property.value);
86
+ } else if (
87
+ property.value.type === 'CallExpression' &&
88
+ property.value.callee.type === 'Identifier' &&
89
+ property.value.callee.name === 'token'
90
+ ) {
91
+ return false;
92
+ } else if (
93
+ property.value.type === 'MemberExpression' &&
94
+ property.value.object.type === 'Identifier'
95
+ ) {
96
+ const val = property.value.object.name;
97
+ const definitions = findVariable(context, val)?.defs;
98
+ if (!definitions || !definitions.length) {
99
+ return false;
100
+ }
101
+ const variable = definitions[0];
102
+ if (!variable) {
103
+ return false;
104
+ }
105
+ const varType = variable.type;
106
+
107
+ if (varType === 'Variable') {
108
+ if (!variable.node.init) {
109
+ return false;
110
+ }
111
+ const varSource = variable.node.init.type;
112
+ // Variables initialised by a call expression or object expression are ok
113
+ if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
114
+ return true;
115
+ }
116
+ // Check if member expression matches with function argument
117
+ } else if (varType !== 'Parameter') {
118
+ return false;
119
+ }
120
+ }
121
+ return true;
122
+ }
123
+
124
+ export const rule = createLintRule({
125
+ meta: {
126
+ name: 'enforce-style-prop',
127
+ docs: {
128
+ description:
129
+ 'Disallows usage of static styles in the `style` attribute in components',
130
+ recommended: true,
131
+ severity: 'warn',
132
+ },
133
+ messages: {
134
+ 'enforce-style-prop':
135
+ 'Avoid adding static values to the style prop. Use the css prop for static styles instead.',
136
+ },
137
+ type: 'problem',
138
+ },
139
+ create(context) {
140
+ return {
141
+ 'JSXAttribute[name.name="style"] > JSXExpressionContainer': (
142
+ node: Node,
143
+ ) => {
144
+ if (!isNodeOfType(node, 'JSXExpressionContainer')) {
145
+ return;
146
+ }
147
+ // we've reached an attribute named style
148
+
149
+ if (node.expression.type === 'ObjectExpression') {
150
+ node.expression.properties.forEach((value) => {
151
+ if (!isPropertyValueAllowed(value, context)) {
152
+ context.report({
153
+ node: value,
154
+ messageId: 'enforce-style-prop',
155
+ });
156
+ }
157
+ });
158
+ } else {
159
+ context.report({
160
+ node: node,
161
+ messageId: 'enforce-style-prop',
162
+ });
163
+ }
164
+ },
165
+ };
166
+ },
167
+ });
168
+
169
+ export default rule;
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::f7d910c4a52a88b6aa58324ebe2edfc2>>
3
+ * @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
7
+ import enforceStyleProp from './enforce-style-prop';
7
8
  import localCxXcss from './local-cx-xcss';
8
9
  import noArrayArguments from './no-array-arguments';
9
10
  import noClassnameProp from './no-classname-prop';
@@ -13,6 +14,7 @@ import noNestedSelectors from './no-nested-selectors';
13
14
 
14
15
  export default {
15
16
  'convert-props-syntax': convertPropsSyntax,
17
+ 'enforce-style-prop': enforceStyleProp,
16
18
  'local-cx-xcss': localCxXcss,
17
19
  'no-array-arguments': noArrayArguments,
18
20
  'no-classname-prop': noClassnameProp,