@atlaskit/eslint-plugin-ui-styling-standard 0.9.1 → 0.11.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 +16 -0
- package/README.md +2 -0
- package/constellation/index/overview.mdx +2 -0
- package/constellation/no-dynamic-styles/usage.mdx +128 -0
- package/constellation/no-unsafe-values/usage.mdx +222 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/index.codegen.js +6 -2
- package/dist/cjs/rules/no-dynamic-styles/index.js +69 -0
- package/dist/cjs/rules/no-unsafe-values/index.js +85 -0
- package/dist/cjs/rules/no-unsafe-values/messages.js +24 -0
- package/dist/cjs/rules/no-unsafe-values/utils.js +437 -0
- package/dist/es2019/presets/all.codegen.js +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/index.codegen.js +6 -2
- package/dist/es2019/rules/no-dynamic-styles/index.js +51 -0
- package/dist/es2019/rules/no-unsafe-values/index.js +80 -0
- package/dist/es2019/rules/no-unsafe-values/messages.js +72 -0
- package/dist/es2019/rules/no-unsafe-values/utils.js +350 -0
- package/dist/esm/presets/all.codegen.js +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/index.codegen.js +6 -2
- package/dist/esm/rules/no-dynamic-styles/index.js +62 -0
- package/dist/esm/rules/no-unsafe-values/index.js +79 -0
- package/dist/esm/rules/no-unsafe-values/messages.js +17 -0
- package/dist/esm/rules/no-unsafe-values/utils.js +430 -0
- package/dist/types/index.codegen.d.ts +4 -0
- package/dist/types/presets/all.codegen.d.ts +3 -1
- package/dist/types/presets/recommended.codegen.d.ts +3 -1
- package/dist/types/rules/index.codegen.d.ts +2 -0
- package/dist/types/rules/no-dynamic-styles/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types/rules/no-unsafe-values/utils.d.ts +28 -0
- package/dist/types-ts4.5/index.codegen.d.ts +4 -0
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -1
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +3 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-dynamic-styles/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/messages.d.ts +13 -0
- package/dist/types-ts4.5/rules/no-unsafe-values/utils.d.ts +28 -0
- package/package.json +5 -4
- package/src/presets/all.codegen.tsx +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/index.codegen.tsx +5 -1
- package/src/rules/no-dynamic-styles/README.md +126 -0
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +128 -0
- package/src/rules/no-dynamic-styles/index.tsx +63 -0
- package/src/rules/no-unsafe-values/README.md +220 -0
- package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +27 -0
- package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +37 -0
- package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +229 -0
- package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +267 -0
- package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +52 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +106 -0
- package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +44 -0
- package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +48 -0
- package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +75 -0
- package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +121 -0
- package/src/rules/no-unsafe-values/index.tsx +96 -0
- package/src/rules/no-unsafe-values/messages.tsx +86 -0
- package/src/rules/no-unsafe-values/utils.tsx +399 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'no-unsafe-values',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [],
|
|
10
|
+
invalid: [
|
|
11
|
+
{
|
|
12
|
+
name: 'object spread',
|
|
13
|
+
code: `
|
|
14
|
+
import { css } from '@compiled/react';
|
|
15
|
+
|
|
16
|
+
const buttonStyles = {
|
|
17
|
+
color: 'blue',
|
|
18
|
+
};
|
|
19
|
+
const styles = css({
|
|
20
|
+
...buttonStyles,
|
|
21
|
+
});
|
|
22
|
+
`,
|
|
23
|
+
errors: [{ messageId: 'no-spread-elements' }],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'nested object spread',
|
|
27
|
+
code: `
|
|
28
|
+
import { css } from '@compiled/react';
|
|
29
|
+
|
|
30
|
+
const buttonStyles = {
|
|
31
|
+
color: 'blue',
|
|
32
|
+
};
|
|
33
|
+
const styles = css({
|
|
34
|
+
button: {
|
|
35
|
+
p: {
|
|
36
|
+
img: {
|
|
37
|
+
color: 'pink',
|
|
38
|
+
...buttonStyles,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
`,
|
|
44
|
+
errors: [{ messageId: 'no-spread-elements' }],
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'no-unsafe-values',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [
|
|
10
|
+
{
|
|
11
|
+
name: 'literal values',
|
|
12
|
+
code: `
|
|
13
|
+
import { css } from '@compiled/react';
|
|
14
|
+
|
|
15
|
+
const styles = css({
|
|
16
|
+
height: '5px',
|
|
17
|
+
opacity: 1
|
|
18
|
+
});
|
|
19
|
+
`,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: 'variables referencing literal values',
|
|
23
|
+
code: `
|
|
24
|
+
import { css } from '@compiled/react';
|
|
25
|
+
|
|
26
|
+
const height = '5px';
|
|
27
|
+
const opacity = 1;
|
|
28
|
+
const styles = css({
|
|
29
|
+
height,
|
|
30
|
+
opacity
|
|
31
|
+
});
|
|
32
|
+
`,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'template strings interpolating safe values',
|
|
36
|
+
code: `
|
|
37
|
+
import { css } from '@compiled/react';
|
|
38
|
+
|
|
39
|
+
const height = 5;
|
|
40
|
+
const paddingBlock = 10;
|
|
41
|
+
const paddingInline = 10;
|
|
42
|
+
|
|
43
|
+
const styles = css({
|
|
44
|
+
height: \`\${height}px\`,
|
|
45
|
+
padding: \`\${paddingBlock}px \${paddingInline}px\`
|
|
46
|
+
});
|
|
47
|
+
`,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'base component',
|
|
51
|
+
code: `
|
|
52
|
+
import { styled } from '@compiled/react';
|
|
53
|
+
import { BaseComponent } from './base-component';
|
|
54
|
+
|
|
55
|
+
styled(BaseComponent)({});
|
|
56
|
+
`,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: 'binary operations are allowed',
|
|
60
|
+
code: `
|
|
61
|
+
import { css } from '@compiled/react';
|
|
62
|
+
|
|
63
|
+
const ITEM_HEIGHT = 40;
|
|
64
|
+
|
|
65
|
+
const styles = css({
|
|
66
|
+
height: ITEM_HEIGHT,
|
|
67
|
+
width: ITEM_HEIGHT * 2,
|
|
68
|
+
marginBottom: ITEM_HEIGHT + 123,
|
|
69
|
+
});
|
|
70
|
+
`,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
invalid: [],
|
|
74
|
+
},
|
|
75
|
+
);
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { typescriptEslintTester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
typescriptEslintTester.run(
|
|
5
|
+
'no-unsafe-values',
|
|
6
|
+
// @ts-expect-error
|
|
7
|
+
rule,
|
|
8
|
+
{
|
|
9
|
+
valid: [
|
|
10
|
+
{
|
|
11
|
+
name: 'identifier resolves to string literal',
|
|
12
|
+
code: `
|
|
13
|
+
import { css } from '@compiled/react';
|
|
14
|
+
|
|
15
|
+
const margin = '5px';
|
|
16
|
+
const styles = css({
|
|
17
|
+
margin
|
|
18
|
+
})
|
|
19
|
+
`,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: 'identifier resolves to a template string with allowed values as interpolations',
|
|
23
|
+
code: `
|
|
24
|
+
import styled from '@emotion/styled';
|
|
25
|
+
|
|
26
|
+
import { N30A } from '@atlaskit/theme/colors';
|
|
27
|
+
import { token } from '@atlaskit/tokens';
|
|
28
|
+
|
|
29
|
+
const borderStyle: string = \`2px solid \${token('color.border', N30A)}\`;
|
|
30
|
+
|
|
31
|
+
export const HorizontalDivider = styled.hr({
|
|
32
|
+
borderBottom: borderStyle,
|
|
33
|
+
});
|
|
34
|
+
`,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
invalid: [
|
|
38
|
+
{
|
|
39
|
+
name: 'undefined is blocked',
|
|
40
|
+
code: `
|
|
41
|
+
import { css } from '@compiled/react';
|
|
42
|
+
|
|
43
|
+
const styles = css({
|
|
44
|
+
margin: undefined
|
|
45
|
+
})
|
|
46
|
+
`,
|
|
47
|
+
errors: [{ messageId: 'no-variables' }],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'undefined is still blocked even if it has been shadowed',
|
|
51
|
+
code: `
|
|
52
|
+
import { css } from '@compiled/react';
|
|
53
|
+
import { someBlockedValue as undefined } from 'my-package';
|
|
54
|
+
|
|
55
|
+
const styles = css({
|
|
56
|
+
margin: undefined
|
|
57
|
+
})
|
|
58
|
+
`,
|
|
59
|
+
errors: [{ messageId: 'no-variables' }],
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'anon function in variable as value',
|
|
63
|
+
code: `
|
|
64
|
+
import { css } from '@compiled/react';
|
|
65
|
+
|
|
66
|
+
const myFunction = function() {
|
|
67
|
+
return '5px';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const styles = css({
|
|
71
|
+
height: myFunction,
|
|
72
|
+
});
|
|
73
|
+
`,
|
|
74
|
+
errors: [{ messageId: 'no-variables' }],
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'arrow function in variable as value',
|
|
78
|
+
code: `
|
|
79
|
+
import { css } from '@compiled/react';
|
|
80
|
+
|
|
81
|
+
const myFunction = () => '5px';
|
|
82
|
+
|
|
83
|
+
const styles = css({
|
|
84
|
+
height: myFunction,
|
|
85
|
+
});
|
|
86
|
+
`,
|
|
87
|
+
errors: [{ messageId: 'no-variables' }],
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'function in variable as value',
|
|
91
|
+
code: `
|
|
92
|
+
import { css } from '@compiled/react';
|
|
93
|
+
|
|
94
|
+
function myFunction() {
|
|
95
|
+
return '5px';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const styles = css({
|
|
99
|
+
height: myFunction,
|
|
100
|
+
});
|
|
101
|
+
`,
|
|
102
|
+
errors: [{ messageId: 'no-variables' }],
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: 'variable in array argument',
|
|
106
|
+
code: `
|
|
107
|
+
import { css } from '@compiled/react';
|
|
108
|
+
|
|
109
|
+
function myFunction() {
|
|
110
|
+
return '5px';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const styles = css([
|
|
114
|
+
{ height: myFunction },
|
|
115
|
+
]);
|
|
116
|
+
`,
|
|
117
|
+
errors: [{ messageId: 'no-variables' }],
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
);
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createLintRule } from '../utils/create-rule';
|
|
2
|
+
import {
|
|
3
|
+
isCss,
|
|
4
|
+
getImportSources,
|
|
5
|
+
isStyled,
|
|
6
|
+
isKeyframes,
|
|
7
|
+
isCssMap,
|
|
8
|
+
isXcss,
|
|
9
|
+
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
10
|
+
import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
11
|
+
import { Linter } from './utils';
|
|
12
|
+
import { messages } from './messages';
|
|
13
|
+
|
|
14
|
+
const schema: JSONSchema4 = [
|
|
15
|
+
{
|
|
16
|
+
type: 'object',
|
|
17
|
+
additionalProperties: false,
|
|
18
|
+
properties: {
|
|
19
|
+
importSources: {
|
|
20
|
+
type: 'array',
|
|
21
|
+
items: { type: 'string' },
|
|
22
|
+
uniqueItems: true,
|
|
23
|
+
},
|
|
24
|
+
allowedFunctionCalls: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
items: {
|
|
27
|
+
type: 'array',
|
|
28
|
+
minItems: 2,
|
|
29
|
+
maxItems: 2,
|
|
30
|
+
items: [{ type: 'string' }, { type: 'string' }],
|
|
31
|
+
},
|
|
32
|
+
uniqueItems: true,
|
|
33
|
+
},
|
|
34
|
+
allowedDynamicKeys: {
|
|
35
|
+
type: 'array',
|
|
36
|
+
items: {
|
|
37
|
+
type: 'array',
|
|
38
|
+
minItems: 2,
|
|
39
|
+
maxItems: 2,
|
|
40
|
+
items: [{ type: 'string' }, { type: 'string' }],
|
|
41
|
+
},
|
|
42
|
+
uniqueItems: true,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
export const rule = createLintRule({
|
|
49
|
+
meta: {
|
|
50
|
+
name: 'no-unsafe-values',
|
|
51
|
+
docs: {
|
|
52
|
+
description:
|
|
53
|
+
'Disallows styles that are difficult/impossible to statically anaylze.',
|
|
54
|
+
recommended: true,
|
|
55
|
+
severity: 'warn',
|
|
56
|
+
},
|
|
57
|
+
messages,
|
|
58
|
+
type: 'problem',
|
|
59
|
+
schema,
|
|
60
|
+
},
|
|
61
|
+
create(context) {
|
|
62
|
+
const importSources = getImportSources(context);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
CallExpression(node) {
|
|
66
|
+
const { references } = context.sourceCode.getScope(node);
|
|
67
|
+
|
|
68
|
+
if (
|
|
69
|
+
!isCss(node.callee, references, importSources) &&
|
|
70
|
+
!isStyled(node.callee, references, importSources) &&
|
|
71
|
+
!isKeyframes(node.callee, references, importSources) &&
|
|
72
|
+
!isCssMap(node.callee, references, importSources) &&
|
|
73
|
+
!isXcss(node.callee, references, importSources)
|
|
74
|
+
) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (
|
|
79
|
+
isStyled(node.callee, references, importSources) &&
|
|
80
|
+
(node.parent.type === 'CallExpression' ||
|
|
81
|
+
node.parent.type === 'TaggedTemplateExpression')
|
|
82
|
+
) {
|
|
83
|
+
/**
|
|
84
|
+
* If it is of the form styled(Base)({}) we don't want to lint the inner function arguments.
|
|
85
|
+
*/
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const linter = new Linter(context, node);
|
|
90
|
+
linter.run();
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export default rule;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import outdent from 'outdent';
|
|
2
|
+
|
|
3
|
+
const whereToGetHelp =
|
|
4
|
+
"If you think this should be allowed, or if you're running into issues, reach out in #help-ui-styling-standard";
|
|
5
|
+
|
|
6
|
+
export const messages = {
|
|
7
|
+
'no-array-values': outdent`
|
|
8
|
+
Arrays are not allowed in style objects. Support is inconsistent between styling libraries.
|
|
9
|
+
|
|
10
|
+
Compose styles with the css prop instead.
|
|
11
|
+
|
|
12
|
+
${whereToGetHelp}
|
|
13
|
+
`,
|
|
14
|
+
|
|
15
|
+
'no-conditional-values': outdent`
|
|
16
|
+
Conditional expressions are not allowed in style objects.
|
|
17
|
+
|
|
18
|
+
Conditionally apply styles with the css prop instead.
|
|
19
|
+
|
|
20
|
+
${whereToGetHelp}
|
|
21
|
+
`,
|
|
22
|
+
|
|
23
|
+
'no-dynamic-keys': outdent`
|
|
24
|
+
Dynamic keys are not allowed in style objects.
|
|
25
|
+
|
|
26
|
+
${whereToGetHelp}
|
|
27
|
+
`,
|
|
28
|
+
|
|
29
|
+
'no-function-calls': outdent`
|
|
30
|
+
Function calls are not allowed in style objects.
|
|
31
|
+
|
|
32
|
+
${whereToGetHelp}
|
|
33
|
+
`,
|
|
34
|
+
|
|
35
|
+
'no-identifier-arguments': outdent`
|
|
36
|
+
Variable arguments are not allowed.
|
|
37
|
+
|
|
38
|
+
Inline the object value, or compose styles with the css prop instead.
|
|
39
|
+
|
|
40
|
+
${whereToGetHelp}
|
|
41
|
+
`,
|
|
42
|
+
|
|
43
|
+
'no-object-access': outdent`
|
|
44
|
+
Accessing object values is not allowed in style objects.
|
|
45
|
+
|
|
46
|
+
If you are accessing the component's props, use the style prop to apply dynamic values instead.
|
|
47
|
+
|
|
48
|
+
${whereToGetHelp}
|
|
49
|
+
`,
|
|
50
|
+
|
|
51
|
+
'no-object-references': outdent`
|
|
52
|
+
Referencing objects is not allowed in style objects.
|
|
53
|
+
|
|
54
|
+
Inline the object value instead.
|
|
55
|
+
|
|
56
|
+
${whereToGetHelp}
|
|
57
|
+
`,
|
|
58
|
+
|
|
59
|
+
'no-spread-elements': outdent`
|
|
60
|
+
Spread elements are not allowed in style objects.
|
|
61
|
+
|
|
62
|
+
Compose styles with the css prop instead.
|
|
63
|
+
|
|
64
|
+
${whereToGetHelp}
|
|
65
|
+
`,
|
|
66
|
+
|
|
67
|
+
// Fallback case if a more specific message is not applicable
|
|
68
|
+
'no-unsafe-values': outdent`
|
|
69
|
+
Unsafe values are not allowed in style objects.
|
|
70
|
+
|
|
71
|
+
${whereToGetHelp}
|
|
72
|
+
`,
|
|
73
|
+
|
|
74
|
+
'no-variables': outdent`
|
|
75
|
+
Variables are not allowed in style objects, unless they:
|
|
76
|
+
|
|
77
|
+
- Have a simple, static value.
|
|
78
|
+
- Are defined in the same file.
|
|
79
|
+
|
|
80
|
+
Use the style prop to apply dynamic values instead.
|
|
81
|
+
|
|
82
|
+
${whereToGetHelp}
|
|
83
|
+
`,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export type MessageId = keyof typeof messages;
|