@atlaskit/eslint-plugin-ui-styling-standard 0.4.1 → 0.5.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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -0
- package/constellation/index/overview.mdx +1 -0
- package/constellation/no-important-styles/usage.mdx +29 -0
- package/dist/cjs/presets/all.codegen.js +2 -1
- package/dist/cjs/presets/recommended.codegen.js +2 -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 +2 -1
- package/dist/es2019/presets/recommended.codegen.js +2 -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 +2 -1
- package/dist/esm/presets/recommended.codegen.js +2 -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 +2 -0
- package/dist/types/presets/all.codegen.d.ts +2 -1
- package/dist/types/presets/recommended.codegen.d.ts +2 -1
- 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 +2 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
- 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 +6 -4
- package/src/presets/all.codegen.tsx +2 -1
- package/src/presets/recommended.codegen.tsx +2 -1
- package/src/rules/__tests__/utils/_tester.tsx +1 -1
- 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
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { tester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
tester.run('no-important-styles', rule, {
|
|
5
|
+
valid: [
|
|
6
|
+
`
|
|
7
|
+
import { css } from '@compiled/react';
|
|
8
|
+
|
|
9
|
+
const styles = css({
|
|
10
|
+
color: 'red'
|
|
11
|
+
});
|
|
12
|
+
`,
|
|
13
|
+
{
|
|
14
|
+
// Custom import sources (subtractive)
|
|
15
|
+
code: `
|
|
16
|
+
import { css } from '@compiled/react';
|
|
17
|
+
|
|
18
|
+
const styles = css({
|
|
19
|
+
color: 'red !important'
|
|
20
|
+
});
|
|
21
|
+
`,
|
|
22
|
+
options: [
|
|
23
|
+
{
|
|
24
|
+
importSources: [],
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
invalid: [
|
|
30
|
+
{
|
|
31
|
+
// Basic test case
|
|
32
|
+
code: `
|
|
33
|
+
import { css } from '@compiled/react';
|
|
34
|
+
|
|
35
|
+
const styles = css({
|
|
36
|
+
color: 'red !important'
|
|
37
|
+
});
|
|
38
|
+
`,
|
|
39
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
// Whitespace handling
|
|
43
|
+
code: `
|
|
44
|
+
import { css } from '@compiled/react';
|
|
45
|
+
|
|
46
|
+
const styles = css({
|
|
47
|
+
color: 'red !important '
|
|
48
|
+
});
|
|
49
|
+
`,
|
|
50
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
// Nesting
|
|
54
|
+
code: `
|
|
55
|
+
import { css } from '@compiled/react';
|
|
56
|
+
|
|
57
|
+
const styles = css({
|
|
58
|
+
div: {
|
|
59
|
+
padding: '8px',
|
|
60
|
+
span: {
|
|
61
|
+
padding: '8px',
|
|
62
|
+
color: 'blue!important',
|
|
63
|
+
margin: '8px'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
`,
|
|
68
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
// styled API
|
|
72
|
+
code: `
|
|
73
|
+
import { styled } from '@compiled/react';
|
|
74
|
+
|
|
75
|
+
const Component = styled.div({
|
|
76
|
+
div: {
|
|
77
|
+
padding: '8px',
|
|
78
|
+
span: {
|
|
79
|
+
padding: '8px',
|
|
80
|
+
color: 'blue!important',
|
|
81
|
+
margin: '8px'
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
`,
|
|
86
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
// keyframes API
|
|
90
|
+
code: `
|
|
91
|
+
import { keyframes } from '@compiled/react';
|
|
92
|
+
|
|
93
|
+
const fadeOut = keyframes({
|
|
94
|
+
from: {
|
|
95
|
+
opacity: '1 !important',
|
|
96
|
+
},
|
|
97
|
+
to: {
|
|
98
|
+
opacity: 0,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
`,
|
|
102
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
// cssMap API
|
|
106
|
+
code: `
|
|
107
|
+
import { cssMap } from '@compiled/react';
|
|
108
|
+
|
|
109
|
+
const borderStyleMap = cssMap({
|
|
110
|
+
none: { borderStyle: 'none !important' },
|
|
111
|
+
solid: { borderStyle: 'solid' },
|
|
112
|
+
});
|
|
113
|
+
`,
|
|
114
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
// xcss API
|
|
118
|
+
code: `
|
|
119
|
+
import { xcss } from '@atlaskit/primitives';
|
|
120
|
+
|
|
121
|
+
const styles = xcss({
|
|
122
|
+
display: 'block !important'
|
|
123
|
+
});
|
|
124
|
+
`,
|
|
125
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
// Custom import sources (additive)
|
|
129
|
+
code: `
|
|
130
|
+
import { css } from 'custom-library';
|
|
131
|
+
|
|
132
|
+
const styles = css({
|
|
133
|
+
color: 'red !important'
|
|
134
|
+
});
|
|
135
|
+
`,
|
|
136
|
+
errors: [{ messageId: 'no-important-styles' }],
|
|
137
|
+
options: [{ importSources: ['custom-library'] }],
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
});
|
|
@@ -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
|
-
|
|
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);
|