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

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.
@@ -124,5 +124,21 @@ tester.run('no-dynamic-styles', rule, {
124
124
  `,
125
125
  errors: [{ messageId: 'no-dynamic-styles' }],
126
126
  },
127
+ {
128
+ name: 'dynamic styles when using styled and overriding another component',
129
+ code: `
130
+ import { styled } from '@compiled/react';
131
+ import { BaseComponent } from './base-component';
132
+
133
+ const Component = styled(BaseComponent)({
134
+ width: props => props.width,
135
+ height: function(props) { return props.height; }
136
+ });
137
+ `,
138
+ errors: [
139
+ { messageId: 'no-dynamic-styles' },
140
+ { messageId: 'no-dynamic-styles' },
141
+ ],
142
+ },
127
143
  ],
128
144
  });
@@ -5,7 +5,7 @@ import {
5
5
  isKeyframes,
6
6
  isCssMap,
7
7
  isXcss,
8
- SupportedNameChecker,
8
+ type SupportedNameChecker,
9
9
  } from '@atlaskit/eslint-utils/is-supported-import';
10
10
  import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
11
11
  import { createNoExportedRule } from '@atlaskit/eslint-utils/create-no-exported-rule';
@@ -11,7 +11,7 @@ tester.run('no-important-styles', rule, {
11
11
  });
12
12
  `,
13
13
  {
14
- // Custom import sources (subtractive)
14
+ name: 'custom import sources (subtractive)',
15
15
  code: `
16
16
  import { css } from '@compiled/react';
17
17
 
@@ -28,7 +28,7 @@ tester.run('no-important-styles', rule, {
28
28
  ],
29
29
  invalid: [
30
30
  {
31
- // Basic test case
31
+ name: 'basic test case',
32
32
  code: `
33
33
  import { css } from '@compiled/react';
34
34
 
@@ -39,7 +39,7 @@ tester.run('no-important-styles', rule, {
39
39
  errors: [{ messageId: 'no-important-styles' }],
40
40
  },
41
41
  {
42
- // Whitespace handling
42
+ name: 'whitespace handling',
43
43
  code: `
44
44
  import { css } from '@compiled/react';
45
45
 
@@ -50,7 +50,7 @@ tester.run('no-important-styles', rule, {
50
50
  errors: [{ messageId: 'no-important-styles' }],
51
51
  },
52
52
  {
53
- // Nesting
53
+ name: 'nesting',
54
54
  code: `
55
55
  import { css } from '@compiled/react';
56
56
 
@@ -68,7 +68,7 @@ tester.run('no-important-styles', rule, {
68
68
  errors: [{ messageId: 'no-important-styles' }],
69
69
  },
70
70
  {
71
- // styled API
71
+ name: 'styled API',
72
72
  code: `
73
73
  import { styled } from '@compiled/react';
74
74
 
@@ -86,7 +86,7 @@ tester.run('no-important-styles', rule, {
86
86
  errors: [{ messageId: 'no-important-styles' }],
87
87
  },
88
88
  {
89
- // keyframes API
89
+ name: 'keyframes API',
90
90
  code: `
91
91
  import { keyframes } from '@compiled/react';
92
92
 
@@ -102,7 +102,7 @@ tester.run('no-important-styles', rule, {
102
102
  errors: [{ messageId: 'no-important-styles' }],
103
103
  },
104
104
  {
105
- // cssMap API
105
+ name: 'cssMap API',
106
106
  code: `
107
107
  import { cssMap } from '@compiled/react';
108
108
 
@@ -114,7 +114,7 @@ tester.run('no-important-styles', rule, {
114
114
  errors: [{ messageId: 'no-important-styles' }],
115
115
  },
116
116
  {
117
- // xcss API
117
+ name: 'xcss API',
118
118
  code: `
119
119
  import { xcss } from '@atlaskit/primitives';
120
120
 
@@ -125,7 +125,7 @@ tester.run('no-important-styles', rule, {
125
125
  errors: [{ messageId: 'no-important-styles' }],
126
126
  },
127
127
  {
128
- // Custom import sources (additive)
128
+ name: 'custom import sources (additive)',
129
129
  code: `
130
130
  import { css } from 'custom-library';
131
131
 
@@ -136,5 +136,18 @@ tester.run('no-important-styles', rule, {
136
136
  errors: [{ messageId: 'no-important-styles' }],
137
137
  options: [{ importSources: ['custom-library'] }],
138
138
  },
139
+ {
140
+ name: 'template string',
141
+ code: `
142
+ import { css } from '@compiled/react';
143
+
144
+ const color = 'red';
145
+
146
+ const styles = css({
147
+ color: \`\${color}!important\`
148
+ });
149
+ `,
150
+ errors: [{ messageId: 'no-important-styles' }],
151
+ },
139
152
  ],
140
153
  });
@@ -1,13 +1,11 @@
1
- import estraverse from 'estraverse';
1
+ import esquery from 'esquery';
2
+
3
+ import type { Property } from 'estree';
2
4
 
3
5
  import { createLintRule } from '../utils/create-rule';
4
6
  import {
5
- isCss,
6
7
  getImportSources,
7
- isStyled,
8
- isKeyframes,
9
- isCssMap,
10
- isXcss,
8
+ hasStyleObjectArguments,
11
9
  } from '@atlaskit/eslint-utils/is-supported-import';
12
10
  import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
13
11
 
@@ -46,39 +44,22 @@ export const rule = createLintRule({
46
44
  CallExpression(node) {
47
45
  const { references } = context.getScope();
48
46
 
49
- if (
50
- isCss(node.callee, references, importSources) ||
51
- isStyled(node.callee, references, importSources) ||
52
- isKeyframes(node.callee, references, importSources) ||
53
- isCssMap(node.callee, references, importSources) ||
54
- isXcss(node.callee, references, importSources)
55
- ) {
56
- estraverse.traverse(node, {
57
- enter(node) {
58
- if (node.type !== 'Property') {
59
- return;
60
- }
47
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
48
+ return;
49
+ }
61
50
 
62
- if (node.value.type !== 'Literal') {
63
- return;
64
- }
51
+ const matches = esquery(node, 'Property');
52
+ for (const match of matches) {
53
+ if (match.type !== 'Property') {
54
+ return;
55
+ }
65
56
 
66
- /**
67
- * There can be whitespace between the `!` and the `important` keyword.
68
- * There can also be whitespace after the `important` keyword.
69
- */
70
- if (node.value.value?.toString().match(/!\s*important\s*$/gm)) {
71
- context.report({
72
- node: node.value,
73
- messageId: 'no-important-styles',
74
- });
75
- }
76
- },
77
- /**
78
- * This is needed to handle unknown node types. Otherwise an error is thrown.
79
- */
80
- fallback: 'iteration',
81
- });
57
+ if (isImportant(match.value)) {
58
+ context.report({
59
+ node: match.value,
60
+ messageId: 'no-important-styles',
61
+ });
62
+ }
82
63
  }
83
64
  },
84
65
  };
@@ -86,3 +67,17 @@ export const rule = createLintRule({
86
67
  });
87
68
 
88
69
  export default rule;
70
+
71
+ const importantRegex = /!\s*important\s*$/;
72
+ function isImportant(node: Property['value']): boolean {
73
+ if (node.type === 'Literal') {
74
+ return typeof node.value === 'string' && importantRegex.test(node.value);
75
+ }
76
+
77
+ if (node.type === 'TemplateLiteral') {
78
+ const joinedRaw = node.quasis.map((element) => element.value.raw).join('');
79
+ return importantRegex.test(joinedRaw);
80
+ }
81
+
82
+ return false;
83
+ }
@@ -7,7 +7,7 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
7
7
  import { findVariable } from '@atlaskit/eslint-utils/find-variable';
8
8
  import type { Rule } from 'eslint';
9
9
  import {
10
- AllowList,
10
+ type AllowList,
11
11
  getAllowedDynamicKeys,
12
12
  getAllowedFunctionCalls,
13
13
  isAllowListedVariable,
@@ -1,4 +1,4 @@
1
- This rule prevents the usage of styles that are difficult/impossible to statically anaylze.
1
+ This rule prevents the usage of styles that are difficult/impossible to statically analyze.
2
2
 
3
3
  ## Examples
4
4
 
package/tsconfig.app.json CHANGED
@@ -33,6 +33,9 @@
33
33
  "outDir": "../../../tsDist/@atlaskit__eslint-plugin-ui-styling-standard/app"
34
34
  },
35
35
  "references": [
36
+ {
37
+ "path": "../../../build/formatting/tsconfig.app.json"
38
+ },
36
39
  {
37
40
  "path": "../eslint-utils/tsconfig.app.json"
38
41
  },