@atlaskit/eslint-plugin-ui-styling-standard 0.8.4 → 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 (42) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +2 -0
  3. package/constellation/enforce-style-prop/usage.mdx +38 -0
  4. package/constellation/index/overview.mdx +2 -0
  5. package/constellation/no-array-arguments/usage.mdx +45 -0
  6. package/dist/cjs/presets/all.codegen.js +3 -1
  7. package/dist/cjs/presets/recommended.codegen.js +3 -1
  8. package/dist/cjs/rules/enforce-style-prop/index.js +141 -0
  9. package/dist/cjs/rules/index.codegen.js +5 -1
  10. package/dist/cjs/rules/no-array-arguments/index.js +92 -0
  11. package/dist/es2019/presets/all.codegen.js +3 -1
  12. package/dist/es2019/presets/recommended.codegen.js +3 -1
  13. package/dist/es2019/rules/enforce-style-prop/index.js +133 -0
  14. package/dist/es2019/rules/index.codegen.js +5 -1
  15. package/dist/es2019/rules/no-array-arguments/index.js +68 -0
  16. package/dist/esm/presets/all.codegen.js +3 -1
  17. package/dist/esm/presets/recommended.codegen.js +3 -1
  18. package/dist/esm/rules/enforce-style-prop/index.js +135 -0
  19. package/dist/esm/rules/index.codegen.js +5 -1
  20. package/dist/esm/rules/no-array-arguments/index.js +86 -0
  21. package/dist/types/index.codegen.d.ts +4 -0
  22. package/dist/types/presets/all.codegen.d.ts +3 -1
  23. package/dist/types/presets/recommended.codegen.d.ts +3 -1
  24. package/dist/types/rules/enforce-style-prop/index.d.ts +3 -0
  25. package/dist/types/rules/index.codegen.d.ts +2 -0
  26. package/dist/types/rules/no-array-arguments/index.d.ts +2 -0
  27. package/dist/types-ts4.5/index.codegen.d.ts +4 -0
  28. package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
  29. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
  30. package/dist/types-ts4.5/rules/enforce-style-prop/index.d.ts +3 -0
  31. package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
  32. package/dist/types-ts4.5/rules/no-array-arguments/index.d.ts +2 -0
  33. package/package.json +4 -4
  34. package/src/presets/all.codegen.tsx +3 -1
  35. package/src/presets/recommended.codegen.tsx +3 -1
  36. package/src/rules/enforce-style-prop/README.md +36 -0
  37. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +231 -0
  38. package/src/rules/enforce-style-prop/index.tsx +169 -0
  39. package/src/rules/index.codegen.tsx +5 -1
  40. package/src/rules/no-array-arguments/README.md +43 -0
  41. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +116 -0
  42. package/src/rules/no-array-arguments/index.tsx +87 -0
@@ -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,10 +1,12 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::c50c5112f3633542ef17c291cd9d88a8>>
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';
9
+ import noArrayArguments from './no-array-arguments';
8
10
  import noClassnameProp from './no-classname-prop';
9
11
  import noContainerQueries from './no-container-queries';
10
12
  import noImportantStyles from './no-important-styles';
@@ -12,7 +14,9 @@ import noNestedSelectors from './no-nested-selectors';
12
14
 
13
15
  export default {
14
16
  'convert-props-syntax': convertPropsSyntax,
17
+ 'enforce-style-prop': enforceStyleProp,
15
18
  'local-cx-xcss': localCxXcss,
19
+ 'no-array-arguments': noArrayArguments,
16
20
  'no-classname-prop': noClassnameProp,
17
21
  'no-container-queries': noContainerQueries,
18
22
  'no-important-styles': noImportantStyles,
@@ -0,0 +1,43 @@
1
+ This rule prevents usage of array arguments to style declaration functions.
2
+
3
+ ## Examples
4
+
5
+ ### Incorrect
6
+
7
+ ```js
8
+ import { css } from '@compiled/react';
9
+ const styles = css([{ width: 100 }, { height: 100 }]);
10
+ ```
11
+
12
+ ```js
13
+ import { styled } from '@compiled/react';
14
+ const styles = css([{ width: 100 }, ...items]);
15
+ ```
16
+
17
+ ### Correct
18
+
19
+ ```js
20
+ import { css } from '@compiled/react';
21
+ const styles = css({ width: 100 }, { height: 100 });
22
+ ```
23
+
24
+ ```js
25
+ import { css } from '@compiled/react';
26
+ const styles = css({ width: 100 }, ...items);
27
+ ```
28
+
29
+ ## Options
30
+
31
+ ### importSources
32
+
33
+ By default, this rule will check `css` usages from:
34
+
35
+ - `@atlaskit/css`
36
+ - `@atlaskit/primitives`
37
+ - `@compiled/react`
38
+ - `@emotion/react`
39
+ - `@emotion/core`
40
+ - `@emotion/styled`
41
+ - `styled-components`
42
+
43
+ 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,116 @@
1
+ import { typescriptEslintTester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ typescriptEslintTester.run(
5
+ 'no-nested-selectors',
6
+ // @ts-expect-error
7
+ rule,
8
+ {
9
+ valid: [],
10
+ invalid: [
11
+ {
12
+ name: 'single array argument',
13
+ code: `
14
+ import { css } from '@compiled/react';
15
+ const styles = css([
16
+ { width: 100 },
17
+ { height: 100 }
18
+ ]);
19
+ `,
20
+ output: `
21
+ import { css } from '@compiled/react';
22
+ const styles = css({ width: 100 }, { height: 100 });
23
+ `,
24
+ errors: [{ messageId: 'no-array-arguments' }],
25
+ },
26
+ {
27
+ name: 'multiple array arguments',
28
+ code: `
29
+ import { css } from '@compiled/react';
30
+ const styles = css(
31
+ [
32
+ { width: 100 },
33
+ { height: 100 }
34
+ ],
35
+ [
36
+ { color: 'red' }
37
+ ]
38
+ );
39
+ `,
40
+ output: `
41
+ import { css } from '@compiled/react';
42
+ const styles = css(
43
+ { width: 100 }, { height: 100 },
44
+ { color: 'red' }
45
+ );
46
+ `,
47
+ errors: [
48
+ { messageId: 'no-array-arguments' },
49
+ { messageId: 'no-array-arguments' },
50
+ ],
51
+ },
52
+ {
53
+ name: 'interspersed array arguments',
54
+ code: `
55
+ import { css } from '@compiled/react';
56
+ const styles = css(
57
+ { a: 1 },
58
+ [
59
+ { width: 100 },
60
+ { height: 100 }
61
+ ],
62
+ { b: 2 },
63
+ [
64
+ { color: 'red' },
65
+ { opacity: 0.5 },
66
+ ],
67
+ { c: 3 }
68
+ );
69
+ `,
70
+ output: `
71
+ import { css } from '@compiled/react';
72
+ const styles = css(
73
+ { a: 1 },
74
+ { width: 100 }, { height: 100 },
75
+ { b: 2 },
76
+ { color: 'red' }, { opacity: 0.5 },
77
+ { c: 3 }
78
+ );
79
+ `,
80
+ errors: [
81
+ { messageId: 'no-array-arguments' },
82
+ { messageId: 'no-array-arguments' },
83
+ ],
84
+ },
85
+ {
86
+ name: 'empty array argument',
87
+ code: `
88
+ import { css } from '@compiled/react';
89
+ const styles = css(
90
+ [],
91
+ { color: 'red' },
92
+ []
93
+ );
94
+ `,
95
+ errors: [
96
+ { messageId: 'no-array-arguments' },
97
+ { messageId: 'no-array-arguments' },
98
+ ],
99
+ },
100
+ {
101
+ name: 'spread in array',
102
+ code: `
103
+ import { css } from '@compiled/react';
104
+ const styles = css([
105
+ ...items
106
+ ]);
107
+ `,
108
+ output: `
109
+ import { css } from '@compiled/react';
110
+ const styles = css(...items);
111
+ `,
112
+ errors: [{ messageId: 'no-array-arguments' }],
113
+ },
114
+ ],
115
+ },
116
+ );
@@ -0,0 +1,87 @@
1
+ import type { Expression, SpreadElement } from 'estree';
2
+
3
+ import {
4
+ getImportSources,
5
+ hasStyleObjectArguments,
6
+ } from '@atlaskit/eslint-utils/is-supported-import';
7
+ import { createLintRule } from '../utils/create-rule';
8
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
9
+
10
+ const schema: JSONSchema4 = [
11
+ {
12
+ type: 'object',
13
+ properties: {
14
+ importSources: {
15
+ type: 'array',
16
+ items: { type: 'string' },
17
+ uniqueItems: true,
18
+ },
19
+ },
20
+ },
21
+ ];
22
+
23
+ export const rule = createLintRule({
24
+ meta: {
25
+ name: 'no-array-arguments',
26
+ docs: {
27
+ description:
28
+ 'Prevents usage of array arguments to style declaration functions',
29
+ recommended: true,
30
+ severity: 'warn',
31
+ },
32
+ messages: {
33
+ 'no-array-arguments':
34
+ "Don't pass multiple value in an array. Pass them as multiple arguments instead.",
35
+ },
36
+ type: 'problem',
37
+ fixable: 'code',
38
+ schema,
39
+ },
40
+ create(context) {
41
+ const importSources = getImportSources(context);
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
+ for (const argument of node.arguments) {
51
+ if (argument.type === 'ArrayExpression') {
52
+ context.report({
53
+ node: argument,
54
+ messageId: 'no-array-arguments',
55
+ fix(fixer) {
56
+ if (argument.elements.length === 0) {
57
+ /**
58
+ * Don't autofix if it's an empty array.
59
+ *
60
+ * I couldn't figure out how to get rid of the comma afterwards,
61
+ * so the autofix was producing syntax problems.
62
+ *
63
+ * This is an edge case that shouldn't actually be happening,
64
+ * but if it does it's not hard to just delete the empty array...
65
+ */
66
+ return null;
67
+ }
68
+
69
+ const items = argument.elements
70
+ .filter(
71
+ (
72
+ value: SpreadElement | Expression | null,
73
+ ): value is SpreadElement | Expression => value !== null,
74
+ )
75
+ .map((element) => context.sourceCode.getText(element));
76
+
77
+ return fixer.replaceText(argument, items.join(', '));
78
+ },
79
+ });
80
+ }
81
+ }
82
+ },
83
+ };
84
+ },
85
+ });
86
+
87
+ export default rule;