@atlaskit/eslint-plugin-ui-styling-standard 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-important-styles/usage.mdx +29 -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 +4 -2
  8. package/dist/cjs/rules/no-important-styles/index.js +75 -0
  9. package/dist/cjs/rules/utils/is-supported-import.js +72 -20
  10. package/dist/es2019/presets/all.codegen.js +2 -1
  11. package/dist/es2019/presets/recommended.codegen.js +2 -1
  12. package/dist/es2019/rules/index.codegen.js +4 -2
  13. package/dist/es2019/rules/no-important-styles/index.js +69 -0
  14. package/dist/es2019/rules/utils/is-supported-import.js +69 -18
  15. package/dist/esm/presets/all.codegen.js +2 -1
  16. package/dist/esm/presets/recommended.codegen.js +2 -1
  17. package/dist/esm/rules/index.codegen.js +4 -2
  18. package/dist/esm/rules/no-important-styles/index.js +68 -0
  19. package/dist/esm/rules/utils/is-supported-import.js +72 -18
  20. package/dist/types/index.codegen.d.ts +2 -0
  21. package/dist/types/presets/all.codegen.d.ts +2 -1
  22. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  23. package/dist/types/rules/index.codegen.d.ts +1 -0
  24. package/dist/types/rules/no-important-styles/index.d.ts +2 -0
  25. package/dist/types/rules/utils/is-supported-import.d.ts +17 -5
  26. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  27. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  28. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  29. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  30. package/dist/types-ts4.5/rules/no-important-styles/index.d.ts +2 -0
  31. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +17 -5
  32. package/package.json +5 -3
  33. package/src/presets/all.codegen.tsx +2 -1
  34. package/src/presets/recommended.codegen.tsx +2 -1
  35. package/src/rules/index.codegen.tsx +3 -1
  36. package/src/rules/no-important-styles/README.md +27 -0
  37. package/src/rules/no-important-styles/__tests__/rule.test.tsx +140 -0
  38. package/src/rules/no-important-styles/index.tsx +88 -0
  39. package/src/rules/utils/is-supported-import.tsx +99 -20
@@ -0,0 +1,88 @@
1
+ import estraverse from 'estraverse';
2
+
3
+ import { createLintRule } from '../utils/create-rule';
4
+ import {
5
+ isCss,
6
+ getImportSources,
7
+ isStyled,
8
+ isKeyframes,
9
+ isCssMap,
10
+ isXcss,
11
+ } from '../utils/is-supported-import';
12
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
13
+
14
+ const schema: JSONSchema4 = [
15
+ {
16
+ type: 'object',
17
+ properties: {
18
+ importSources: {
19
+ type: 'array',
20
+ items: { type: 'string' },
21
+ uniqueItems: true,
22
+ },
23
+ },
24
+ },
25
+ ];
26
+
27
+ export const rule = createLintRule({
28
+ meta: {
29
+ name: 'no-important-styles',
30
+ docs: {
31
+ description: 'Disallows important style declarations',
32
+ recommended: true,
33
+ severity: 'warn',
34
+ },
35
+ messages: {
36
+ 'no-important-styles':
37
+ 'Important style declarations are disallowed. Refactor so the `!important` flag is not needed.',
38
+ },
39
+ type: 'problem',
40
+ schema,
41
+ },
42
+ create(context) {
43
+ const importSources = getImportSources(context);
44
+
45
+ return {
46
+ CallExpression(node) {
47
+ const { references } = context.getScope();
48
+
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
+ }
61
+
62
+ if (node.value.type !== 'Literal') {
63
+ return;
64
+ }
65
+
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
+ });
82
+ }
83
+ },
84
+ };
85
+ },
86
+ });
87
+
88
+ export default rule;
@@ -10,6 +10,7 @@ export const CSS_IN_JS_IMPORTS = {
10
10
  compiled: '@compiled/react',
11
11
  emotionReact: '@emotion/react',
12
12
  emotionCore: '@emotion/core',
13
+ emotionStyled: '@emotion/styled',
13
14
  styledComponents: 'styled-components',
14
15
  atlaskitCss: '@atlaskit/css',
15
16
  atlaskitPrimitives: '@atlaskit/primitives',
@@ -25,19 +26,31 @@ export type SupportedNameChecker = (
25
26
  importSources: ImportSource[],
26
27
  ) => boolean;
27
28
 
28
- // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
29
- export const DEFAULT_IMPORT_SOURCES: ImportSource[] = [
30
- CSS_IN_JS_IMPORTS.compiled,
31
- CSS_IN_JS_IMPORTS.atlaskitCss,
32
- ];
29
+ /**
30
+ * By default all known import sources are checked against.
31
+ */
32
+ export const DEFAULT_IMPORT_SOURCES: ImportSource[] =
33
+ Object.values(CSS_IN_JS_IMPORTS);
34
+
35
+ const getIdentifierNode = (node: Callee) => {
36
+ let identifierNode = node.type === 'Identifier' ? node : undefined;
37
+ if (!identifierNode) {
38
+ // Handles styled.div`` case
39
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
40
+ identifierNode = node.object;
41
+ }
42
+
43
+ // Handles styled(Component)`` case
44
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
45
+ identifierNode = node.callee;
46
+ }
47
+ }
48
+ return identifierNode;
49
+ };
33
50
 
34
51
  /**
35
52
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
36
- * The importSources option is used to define additional libraries for which an ESLint rule
37
- * should apply to.
38
- *
39
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
40
- * of what importSources is configured to by the user.
53
+ * The importSources option is used to override which libraries an ESLint rule applies to.
41
54
  *
42
55
  * @param context The rule context.
43
56
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -50,7 +63,7 @@ export const getImportSources = (context: Rule.RuleContext): ImportSource[] => {
50
63
  }
51
64
 
52
65
  if (options[0].importSources && Array.isArray(options[0].importSources)) {
53
- return [...DEFAULT_IMPORT_SOURCES, ...options[0].importSources];
66
+ return options[0].importSources;
54
67
  }
55
68
 
56
69
  return DEFAULT_IMPORT_SOURCES;
@@ -58,6 +71,7 @@ export const getImportSources = (context: Rule.RuleContext): ImportSource[] => {
58
71
 
59
72
  const isSupportedImportWrapper = (
60
73
  functionName: string,
74
+ defaultFromImportSources: ImportSource[] = [],
61
75
  ): SupportedNameChecker => {
62
76
  const checkDefinitionHasImport = (
63
77
  def: Definition,
@@ -74,13 +88,28 @@ const isSupportedImportWrapper = (
74
88
  return false;
75
89
  }
76
90
 
91
+ // Matches the imported name from a named import
92
+ // import { functionName, functioName as otherName } from 'import-source';
93
+ const isNamedImport =
94
+ def.node.type === 'ImportSpecifier' &&
95
+ def.node.imported.name === functionName;
96
+
97
+ // Must explicitly match the local name from a default import
98
+ // import functionName from 'import-source';
99
+ const isDefaultImportMatchingLocal =
100
+ def.node.type === 'ImportDefaultSpecifier' &&
101
+ def.node.local.name === functionName;
102
+
103
+ // Can match any local name from a default import
104
+ // import anything from 'import-source'
105
+ const isKnownDefaultImport =
106
+ def.node.type === 'ImportDefaultSpecifier' &&
107
+ defaultFromImportSources.includes(
108
+ def.parent.source.value as ImportSource,
109
+ );
110
+
77
111
  return (
78
- // import { functionName } from 'import-source';
79
- (def.node.type === 'ImportSpecifier' &&
80
- def.node.imported.name === functionName) ||
81
- // import functionName from 'import-source';
82
- (def.node.type === 'ImportDefaultSpecifier' &&
83
- def.node.local.name === functionName)
112
+ isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport
84
113
  );
85
114
  };
86
115
 
@@ -105,11 +134,13 @@ const isSupportedImportWrapper = (
105
134
  referencesInScope: Reference[],
106
135
  importSources: ImportSource[],
107
136
  ): boolean => {
137
+ const identifierNode = getIdentifierNode(nodeToCheck);
138
+
108
139
  return (
109
- nodeToCheck.type === 'Identifier' &&
140
+ identifierNode?.type === 'Identifier' &&
110
141
  referencesInScope.some(
111
142
  (reference) =>
112
- reference.identifier === nodeToCheck &&
143
+ reference.identifier === identifierNode &&
113
144
  reference.resolved?.defs.some((def) =>
114
145
  checkDefinitionHasImport(def, importSources),
115
146
  ),
@@ -127,4 +158,52 @@ export const isCss = isSupportedImportWrapper('css');
127
158
  export const isCxFunction = isSupportedImportWrapper('cx');
128
159
  export const isCssMap = isSupportedImportWrapper('cssMap');
129
160
  export const isKeyframes = isSupportedImportWrapper('keyframes');
130
- export const isStyled = isSupportedImportWrapper('styled');
161
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
162
+ export const isStyled = isSupportedImportWrapper('styled', [
163
+ 'styled-components',
164
+ '@emotion/styled',
165
+ ]);
166
+ export const isXcss = isSupportedImportWrapper('xcss');
167
+
168
+ export const isImportedFrom =
169
+ (moduleName: string, exactMatch = true) =>
170
+ (
171
+ nodeToCheck: Callee,
172
+ referencesInScope: Reference[],
173
+ importSources: ImportSource[],
174
+ ): boolean => {
175
+ if (!importSources.includes(moduleName)) {
176
+ // Don't go through the trouble of checking the import sources does not include this
177
+ // We'll assume this is skipped elsewhere.
178
+ return false;
179
+ }
180
+
181
+ const identifierNode = getIdentifierNode(nodeToCheck);
182
+
183
+ return (
184
+ identifierNode?.type === 'Identifier' &&
185
+ referencesInScope.some(
186
+ (reference) =>
187
+ reference.identifier === identifierNode &&
188
+ reference.resolved?.defs.some((def) => {
189
+ return (
190
+ def.type === 'ImportBinding' &&
191
+ (def.parent?.source.value === moduleName ||
192
+ (!exactMatch &&
193
+ String(def.parent?.source.value)?.startsWith(moduleName)))
194
+ );
195
+ }),
196
+ )
197
+ );
198
+ };
199
+
200
+ /**
201
+ * Determine if this node is specifically from a `'styled-components'` import.
202
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
203
+ * we need to handle them differently in a few scenarios.
204
+ *
205
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
206
+ */
207
+ export const isStyledComponents = isImportedFrom('styled-components');
208
+ export const isCompiled = isImportedFrom('@compiled/', false);
209
+ export const isEmotion = isImportedFrom('@emotion/', false);