@atlaskit/eslint-plugin-ui-styling-standard 0.11.1 → 0.11.3

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 +12 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-imported-style-values/usage.mdx +146 -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-imported-style-values/index.js +145 -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-imported-style-values/index.js +125 -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-imported-style-values/index.js +138 -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-imported-style-values/index.d.ts +3 -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-imported-style-values/index.d.ts +3 -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/index.codegen.tsx +3 -1
  31. package/src/rules/no-imported-style-values/README.md +144 -0
  32. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +265 -0
  33. package/src/rules/no-imported-style-values/index.tsx +154 -0
@@ -0,0 +1,265 @@
1
+ import { typescriptEslintTester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ typescriptEslintTester.run(
5
+ 'no-imported-style-values',
6
+ // @ts-expect-error
7
+ rule,
8
+ {
9
+ valid: [
10
+ {
11
+ name: 'Basic valid test for css',
12
+ code: `
13
+ import { css } from '@compiled/react';
14
+ const sharedObject = { padding: 0 };
15
+ const styles = css({
16
+ ...sharedObject,
17
+ color: 'red',
18
+ });
19
+ `,
20
+ },
21
+ {
22
+ name: 'Valid emotion css call',
23
+ code: `
24
+ import { css } from '@emotion/react';
25
+ import { CURRENT_SURFACE_CSS_VAR, token } from '@atlaskit/tokens';
26
+ const styles = css({
27
+ backgroundColor: token('elevation.surface.overlay', N0),
28
+ borderRadius: token('border.radius', '3px'),
29
+ [CURRENT_SURFACE_CSS_VAR]: token('elevation.surface.overlay', N0),
30
+ });
31
+ `,
32
+ },
33
+ {
34
+ name: 'Valid styled component',
35
+ code: `
36
+ import { styled } from '@compiled/react';
37
+ import { token } from '@atlaskit/tokens';
38
+ const Component = styled.div({
39
+ borderRadius: token('border.radius', '3px')
40
+ })
41
+ `,
42
+ },
43
+ {
44
+ name: 'Variables defined in same file',
45
+ code: `
46
+ import { css } from '@compiled/react';
47
+ const color = 'red',
48
+ const containerWidth = 0;
49
+ const styles = css({
50
+ color,
51
+ width: containerWidth
52
+ });
53
+ `,
54
+ },
55
+ {
56
+ name: 'Importing token function',
57
+ code: `
58
+ import { css } from '@compiled/react';
59
+ import { token } from '@atlaskit/tokens';
60
+ const styles = css({
61
+ padding: token('space.150'),
62
+ });
63
+ `,
64
+ },
65
+ {
66
+ name: 'Valid cssMap call',
67
+ code: `
68
+ import { cssMap } from '@compiled/react';
69
+ import { token } from '@atlaskit/tokens';
70
+ const styles = cssMap({
71
+ root: {
72
+ width: '16rem',
73
+ },
74
+ blackBorder: {
75
+ border: '1px solid black',
76
+ borderRadius: token('border.radius.100', '3px'),
77
+ },
78
+ });
79
+ `,
80
+ },
81
+ {
82
+ name: 'Custom import sources (subtractive)',
83
+ code: `
84
+ import { css } from '@compiled/react';
85
+ import { color } from '../shared'
86
+ const styles = css({
87
+ color
88
+ });
89
+ `,
90
+ options: [
91
+ {
92
+ importSources: [],
93
+ },
94
+ ],
95
+ },
96
+ {
97
+ name: 'Custom allowed function',
98
+ code: `
99
+ import { css } from '@compiled/react';
100
+ import { getColor } from 'my-package'
101
+ const styles = css({
102
+ color: getColor()
103
+ });
104
+ `,
105
+ options: [
106
+ {
107
+ allowedFunctionCalls: [['my-package', 'getColor']],
108
+ },
109
+ ],
110
+ },
111
+ {
112
+ name: 'Custom allowed dynamic key',
113
+ code: `
114
+ import { css } from '@compiled/react';
115
+ import { colorKey } from 'my-package';
116
+ const styles = css({
117
+ [colorKey]: 'red',
118
+ });
119
+ `,
120
+ options: [
121
+ {
122
+ allowedDynamicKeys: [['my-package', 'colorKey']],
123
+ },
124
+ ],
125
+ },
126
+ ],
127
+ invalid: [
128
+ {
129
+ name: 'Importing spreadElement',
130
+ code: `
131
+ import { css } from '@compiled/react';
132
+ import { importedStyles } from '../shared';
133
+ const styles = css({
134
+ ...importedStyles,
135
+ color: 'red',
136
+ });
137
+ `,
138
+ errors: [{ messageId: 'no-imported-style-values' }],
139
+ },
140
+ {
141
+ name: 'Importing functions to use within css',
142
+ code: `
143
+ import { css } from '@compiled/react';
144
+ import { getColor } from '@mui/theme';
145
+ import { colorKey } from '../shared';
146
+ const styles = css({
147
+ [colorKey]: getColor('red'),
148
+ });
149
+ `,
150
+ errors: [
151
+ { messageId: 'no-imported-style-values' },
152
+ { messageId: 'no-imported-style-values' },
153
+ ],
154
+ },
155
+ {
156
+ name: 'Importing objects to use within css',
157
+ code: `
158
+ import { css } from '@compiled/react';
159
+ import { colors } from '@mui/theme';
160
+ const styles = css({
161
+ background: colors['red'],
162
+ });
163
+ `,
164
+ errors: [{ messageId: 'no-imported-style-values' }],
165
+ },
166
+ {
167
+ name: 'Importing variables to use within template literal',
168
+ code: `
169
+ import { css } from '@compiled/react';
170
+ import { HEIGHT } from '../shared';
171
+ const styles = css({
172
+ height: \`\${HEIGHT}px\`,
173
+ });
174
+ `,
175
+ errors: [{ messageId: 'no-imported-style-values' }],
176
+ },
177
+ {
178
+ name: 'Importing variables to within conditional expression',
179
+ code: `
180
+ import { css } from '@compiled/react';
181
+ import { ff } from '@atlaskit/ff';
182
+ import { HEIGHT } from '../shared';
183
+ const styles = css({
184
+ width: ff('…') ? \`\${HEIGHT}px\` : undefined,
185
+ });
186
+ `,
187
+ errors: [
188
+ { messageId: 'no-imported-style-values' },
189
+ { messageId: 'no-imported-style-values' },
190
+ ],
191
+ },
192
+ {
193
+ name: 'Invalid styled component',
194
+ code: `
195
+ import { styled } from '@compiled/react';
196
+ import { HEIGHT, importedStyles, getWidth } from '../shared';
197
+
198
+ const randomVar = 10;
199
+
200
+ const Component = styled.div({
201
+ ...importedStyles,
202
+ height: \`\${HEIGHT}px\`,
203
+ width: getWidth(10),
204
+ });
205
+ `,
206
+ errors: [
207
+ { messageId: 'no-imported-style-values' },
208
+ { messageId: 'no-imported-style-values' },
209
+ { messageId: 'no-imported-style-values' },
210
+ ],
211
+ },
212
+ {
213
+ name: 'Invalid cssMap call',
214
+ code: `
215
+ import { cssMap } from '@compiled/react';
216
+ import { textColor, bgColor } from '../shared';
217
+
218
+ const styles = cssMap({
219
+ text: { color: textColor },
220
+ bg: { background: bgColor },
221
+ });
222
+ `,
223
+ errors: [
224
+ { messageId: 'no-imported-style-values' },
225
+ { messageId: 'no-imported-style-values' },
226
+ ],
227
+ },
228
+ {
229
+ name: 'Invalid style composition in css prop',
230
+ code: `
231
+ import { css } from '@compiled/react';
232
+ import { buttonStyles } from '../shared';
233
+
234
+ const styles = css({
235
+ color: 'red',
236
+ });
237
+
238
+ export default () => <div css={[styles, buttonStyles]} />;
239
+ `,
240
+ errors: [{ messageId: 'no-imported-style-values' }],
241
+ },
242
+ {
243
+ name: 'Importing value within style prop',
244
+ code: `
245
+ import { importedWidth } from '../shared';
246
+
247
+ export default () => <div style={{ width: importedWidth }} />;
248
+ `,
249
+ errors: [{ messageId: 'no-imported-style-values' }],
250
+ },
251
+ {
252
+ name: 'Importing member expression',
253
+ code: `
254
+ import { hello } from 'package';
255
+ import { css } from '@compiled/react';
256
+
257
+ const styles = css({
258
+ color: hello.world,
259
+ });
260
+ `,
261
+ errors: [{ messageId: 'no-imported-style-values' }],
262
+ },
263
+ ],
264
+ },
265
+ );
@@ -0,0 +1,154 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import {
3
+ getImportSources,
4
+ hasStyleObjectArguments,
5
+ } from '@atlaskit/eslint-utils/is-supported-import';
6
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
7
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
8
+ import type { Rule } from 'eslint';
9
+ import {
10
+ AllowList,
11
+ getAllowedDynamicKeys,
12
+ getAllowedFunctionCalls,
13
+ isAllowListedVariable,
14
+ } from '@atlaskit/eslint-utils/allowed-function-calls';
15
+ import type { Identifier, Node } from 'eslint-codemod-utils';
16
+ import esquery from 'esquery';
17
+
18
+ const schema: JSONSchema4 = [
19
+ {
20
+ type: 'object',
21
+ properties: {
22
+ importSources: {
23
+ type: 'array',
24
+ items: { type: 'string' },
25
+ uniqueItems: true,
26
+ },
27
+ allowedFunctionCalls: {
28
+ type: 'array',
29
+ items: {
30
+ type: 'array',
31
+ minItems: 2,
32
+ maxItems: 2,
33
+ items: [{ type: 'string' }, { type: 'string' }],
34
+ },
35
+ uniqueItems: true,
36
+ },
37
+ allowedDynamicKeys: {
38
+ type: 'array',
39
+ items: {
40
+ type: 'array',
41
+ minItems: 2,
42
+ maxItems: 2,
43
+ items: [{ type: 'string' }, { type: 'string' }],
44
+ },
45
+ uniqueItems: true,
46
+ },
47
+ },
48
+ },
49
+ ];
50
+
51
+ const isIdentifierImported = (
52
+ identifier: Identifier,
53
+ context: Rule.RuleContext,
54
+ functionAllowList: AllowList,
55
+ keysAllowList: AllowList,
56
+ ): boolean => {
57
+ const variable = findVariable({
58
+ identifier: identifier,
59
+ sourceCode: context.sourceCode,
60
+ });
61
+ if (!variable) {
62
+ return false;
63
+ }
64
+
65
+ const isImported = variable.defs.some(
66
+ (definition) => definition.type === 'ImportBinding',
67
+ );
68
+
69
+ // check if imported variable is allowed
70
+ return (
71
+ isImported &&
72
+ !isAllowListedVariable({
73
+ allowList: functionAllowList,
74
+ variable,
75
+ }) &&
76
+ !isAllowListedVariable({ allowList: keysAllowList, variable })
77
+ );
78
+ };
79
+
80
+ const checkIdentifier = (
81
+ identifier: Identifier,
82
+ context: Rule.RuleContext,
83
+ functionAllowList: AllowList,
84
+ keysAllowList: AllowList,
85
+ ) => {
86
+ if (
87
+ isIdentifierImported(identifier, context, functionAllowList, keysAllowList)
88
+ ) {
89
+ context.report({
90
+ node: identifier,
91
+ messageId: 'no-imported-style-values',
92
+ });
93
+ }
94
+ };
95
+
96
+ export const rule = createLintRule({
97
+ meta: {
98
+ name: 'no-imported-style-values',
99
+ docs: {
100
+ description: 'Disallows imports of style values',
101
+ recommended: true,
102
+ severity: 'warn',
103
+ },
104
+ messages: {
105
+ 'no-imported-style-values':
106
+ 'Styles should not be imported to use in css, cssMap, styled, keyframes or xcss calls. All styles should be defined and used in the same file instead.',
107
+ },
108
+ type: 'problem',
109
+ schema,
110
+ },
111
+ create(context) {
112
+ const importSources = getImportSources(context);
113
+ const functionAllowList = getAllowedFunctionCalls(context.options);
114
+ const keysAllowList = getAllowedDynamicKeys(context.options);
115
+
116
+ const checkForIdentifiers = (node: Node) => {
117
+ const matches = esquery(node, 'Identifier');
118
+
119
+ for (const match of matches) {
120
+ if (match.type === 'Identifier') {
121
+ checkIdentifier(match, context, functionAllowList, keysAllowList);
122
+ }
123
+ }
124
+ };
125
+
126
+ return {
127
+ // Checking css/cssMap/keyframes/styled/xcss calls
128
+ CallExpression(node) {
129
+ const { references } = context.sourceCode.getScope(node.callee);
130
+
131
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
132
+ return;
133
+ }
134
+ node.arguments.forEach(checkForIdentifiers);
135
+ },
136
+ 'JSXAttribute[name.name="css"] Identifier': (node: Node) => {
137
+ if (node.type !== 'Identifier') {
138
+ return;
139
+ }
140
+
141
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
142
+ },
143
+ 'JSXAttribute[name.name="style"] Identifier': (node: Node) => {
144
+ if (node.type !== 'Identifier') {
145
+ return;
146
+ }
147
+
148
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
149
+ },
150
+ };
151
+ },
152
+ });
153
+
154
+ export default rule;