@atlaskit/eslint-plugin-ui-styling-standard 0.4.0 → 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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -0
- package/constellation/consistent-css-prop-usage/usage.mdx +2 -2
- package/constellation/index/overview.mdx +9 -11
- package/constellation/no-css-tagged-template-expression/usage.mdx +2 -2
- package/constellation/no-empty-styled-expression/usage.mdx +2 -2
- package/constellation/no-exported-css/usage.mdx +2 -2
- package/constellation/no-exported-keyframes/usage.mdx +2 -2
- package/constellation/no-important-styles/usage.mdx +29 -0
- package/constellation/no-invalid-css-map/usage.mdx +2 -2
- package/constellation/no-js-xcss/usage.mdx +1 -19
- package/constellation/no-keyframes-tagged-template-expression/usage.mdx +2 -2
- package/constellation/no-styled-tagged-template-expression/usage.mdx +2 -2
- package/dist/cjs/presets/all.codegen.js +5 -2
- package/dist/cjs/presets/recommended.codegen.js +5 -2
- package/dist/cjs/rules/external-rules.js +27 -1
- package/dist/cjs/rules/index.codegen.js +4 -2
- package/dist/cjs/rules/no-important-styles/index.js +75 -0
- package/dist/cjs/rules/utils/is-supported-import.js +72 -20
- package/dist/es2019/presets/all.codegen.js +5 -2
- package/dist/es2019/presets/recommended.codegen.js +5 -2
- package/dist/es2019/rules/external-rules.js +27 -1
- package/dist/es2019/rules/index.codegen.js +4 -2
- package/dist/es2019/rules/no-important-styles/index.js +69 -0
- package/dist/es2019/rules/utils/is-supported-import.js +69 -18
- package/dist/esm/presets/all.codegen.js +5 -2
- package/dist/esm/presets/recommended.codegen.js +5 -2
- package/dist/esm/rules/external-rules.js +27 -1
- package/dist/esm/rules/index.codegen.js +4 -2
- package/dist/esm/rules/no-important-styles/index.js +68 -0
- package/dist/esm/rules/utils/is-supported-import.js +72 -18
- package/dist/types/index.codegen.d.ts +8 -2
- package/dist/types/presets/all.codegen.d.ts +5 -2
- package/dist/types/presets/recommended.codegen.d.ts +5 -2
- package/dist/types/rules/index.codegen.d.ts +1 -0
- package/dist/types/rules/no-important-styles/index.d.ts +2 -0
- package/dist/types/rules/utils/is-supported-import.d.ts +17 -5
- package/dist/types-ts4.5/index.codegen.d.ts +8 -2
- package/dist/types-ts4.5/presets/all.codegen.d.ts +5 -2
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +5 -2
- package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
- package/dist/types-ts4.5/rules/no-important-styles/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +17 -5
- package/package.json +5 -3
- package/scripts/codegen.tsx +1 -1
- package/src/presets/all.codegen.tsx +14 -2
- package/src/presets/recommended.codegen.tsx +14 -2
- package/src/rules/external-rules.tsx +30 -0
- package/src/rules/index.codegen.tsx +3 -1
- package/src/rules/no-important-styles/README.md +27 -0
- package/src/rules/no-important-styles/__tests__/rule.test.tsx +140 -0
- package/src/rules/no-important-styles/index.tsx +88 -0
- package/src/rules/utils/is-supported-import.tsx +99 -20
|
@@ -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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
140
|
+
identifierNode?.type === 'Identifier' &&
|
|
110
141
|
referencesInScope.some(
|
|
111
142
|
(reference) =>
|
|
112
|
-
reference.identifier ===
|
|
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
|
-
|
|
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);
|