@atlaskit/eslint-plugin-ui-styling-standard 0.9.0 → 0.10.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 +2 -0
- package/constellation/enforce-style-prop/usage.mdx +38 -0
- package/constellation/index/overview.mdx +2 -0
- package/constellation/no-dynamic-styles/usage.mdx +128 -0
- package/dist/cjs/presets/all.codegen.js +3 -1
- package/dist/cjs/presets/recommended.codegen.js +3 -1
- package/dist/cjs/rules/enforce-style-prop/index.js +141 -0
- package/dist/cjs/rules/index.codegen.js +5 -1
- package/dist/cjs/rules/no-dynamic-styles/index.js +69 -0
- package/dist/es2019/presets/all.codegen.js +3 -1
- package/dist/es2019/presets/recommended.codegen.js +3 -1
- package/dist/es2019/rules/enforce-style-prop/index.js +133 -0
- package/dist/es2019/rules/index.codegen.js +5 -1
- package/dist/es2019/rules/no-dynamic-styles/index.js +51 -0
- package/dist/esm/presets/all.codegen.js +3 -1
- package/dist/esm/presets/recommended.codegen.js +3 -1
- package/dist/esm/rules/enforce-style-prop/index.js +135 -0
- package/dist/esm/rules/index.codegen.js +5 -1
- package/dist/esm/rules/no-dynamic-styles/index.js +62 -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/enforce-style-prop/index.d.ts +3 -0
- 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-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/enforce-style-prop/index.d.ts +3 -0
- 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/package.json +3 -2
- package/src/presets/all.codegen.tsx +3 -1
- package/src/presets/recommended.codegen.tsx +3 -1
- package/src/rules/enforce-style-prop/README.md +36 -0
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +231 -0
- package/src/rules/enforce-style-prop/index.tsx +169 -0
- 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
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { tester } from '../../__tests__/utils/_tester';
|
|
2
|
+
import rule from '../index';
|
|
3
|
+
|
|
4
|
+
tester.run('no-dynamic-styles', rule, {
|
|
5
|
+
valid: [
|
|
6
|
+
{
|
|
7
|
+
name: 'arrow function expression outside of a styling context',
|
|
8
|
+
code: `
|
|
9
|
+
const arrowFunctionExpression = (props) => ({
|
|
10
|
+
width: props.width
|
|
11
|
+
});
|
|
12
|
+
`,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'function expression outside of a styling context',
|
|
16
|
+
code: `
|
|
17
|
+
const functionExpression = function(props) {
|
|
18
|
+
return {
|
|
19
|
+
height: props.height
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
`,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'function expressions as object values outside of a styling context',
|
|
26
|
+
code: `
|
|
27
|
+
const styles = {
|
|
28
|
+
width: props => props.width,
|
|
29
|
+
height: function(props) { return props.height }
|
|
30
|
+
};
|
|
31
|
+
`,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: "importSources doesn't include the styling library",
|
|
35
|
+
code: `
|
|
36
|
+
import { css } from '@compiled/react';
|
|
37
|
+
|
|
38
|
+
const styles = css(props => ({
|
|
39
|
+
width: props.width
|
|
40
|
+
}));
|
|
41
|
+
`,
|
|
42
|
+
options: [{ importSources: [] }],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
invalid: [
|
|
46
|
+
{
|
|
47
|
+
name: 'function as argument to css',
|
|
48
|
+
code: `
|
|
49
|
+
import { css } from '@compiled/react';
|
|
50
|
+
|
|
51
|
+
const styles = css(props => ({
|
|
52
|
+
width: props.width
|
|
53
|
+
}));
|
|
54
|
+
`,
|
|
55
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'function as argument to styled',
|
|
59
|
+
code: `
|
|
60
|
+
import { styled } from '@compiled/react';
|
|
61
|
+
|
|
62
|
+
const Component = styled.div(props => ({
|
|
63
|
+
width: props.width
|
|
64
|
+
}));
|
|
65
|
+
`,
|
|
66
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'multiple functions as arguments',
|
|
70
|
+
code: `
|
|
71
|
+
import { styled } from '@compiled/react';
|
|
72
|
+
|
|
73
|
+
const Component = styled.div(
|
|
74
|
+
props => ({
|
|
75
|
+
width: props.width
|
|
76
|
+
}),
|
|
77
|
+
{ color: 'red' },
|
|
78
|
+
props => ({
|
|
79
|
+
height: props.height
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
`,
|
|
83
|
+
errors: [
|
|
84
|
+
{ messageId: 'no-dynamic-styles' },
|
|
85
|
+
{ messageId: 'no-dynamic-styles' },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'functions as values',
|
|
90
|
+
code: `
|
|
91
|
+
import { styled } from '@compiled/react';
|
|
92
|
+
|
|
93
|
+
const Component = styled.div({
|
|
94
|
+
width: props => props.width,
|
|
95
|
+
height: function(props) { return props.height; }
|
|
96
|
+
});
|
|
97
|
+
`,
|
|
98
|
+
errors: [
|
|
99
|
+
{ messageId: 'no-dynamic-styles' },
|
|
100
|
+
{ messageId: 'no-dynamic-styles' },
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'importSources includes styling library not in built-in list',
|
|
105
|
+
code: `
|
|
106
|
+
import { css } from 'my-package';
|
|
107
|
+
|
|
108
|
+
const styles = css(props => ({
|
|
109
|
+
width: props.width
|
|
110
|
+
}));
|
|
111
|
+
`,
|
|
112
|
+
options: [{ importSources: ['my-package'] }],
|
|
113
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
// This fails type-checking anyway, but doesn't hurt to test
|
|
117
|
+
name: 'arrow function in template literal',
|
|
118
|
+
code: `
|
|
119
|
+
import { css } from '@compiled/react';
|
|
120
|
+
|
|
121
|
+
const styles = css({
|
|
122
|
+
height: \`\${(props) => props.height}px\`,
|
|
123
|
+
});
|
|
124
|
+
`,
|
|
125
|
+
errors: [{ messageId: 'no-dynamic-styles' }],
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import esquery from 'esquery';
|
|
2
|
+
import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
getImportSources,
|
|
6
|
+
hasStyleObjectArguments,
|
|
7
|
+
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
8
|
+
|
|
9
|
+
import { createLintRule } from '../utils/create-rule';
|
|
10
|
+
|
|
11
|
+
const schema: JSONSchema4 = [
|
|
12
|
+
{
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
importSources: {
|
|
16
|
+
type: 'array',
|
|
17
|
+
items: { type: 'string' },
|
|
18
|
+
uniqueItems: true,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const rule = createLintRule({
|
|
25
|
+
meta: {
|
|
26
|
+
name: 'no-dynamic-styles',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Disallows use of dynamic styles in CSS-in-JS calls',
|
|
29
|
+
recommended: true,
|
|
30
|
+
severity: 'warn',
|
|
31
|
+
},
|
|
32
|
+
schema,
|
|
33
|
+
messages: {
|
|
34
|
+
'no-dynamic-styles':
|
|
35
|
+
'Dynamic styles are not allowed. Instead use the style prop to apply styles derived from component props.',
|
|
36
|
+
},
|
|
37
|
+
type: 'problem',
|
|
38
|
+
},
|
|
39
|
+
create(context) {
|
|
40
|
+
const importSources = getImportSources(context);
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
CallExpression(node) {
|
|
44
|
+
const { references } = context.sourceCode.getScope(node);
|
|
45
|
+
|
|
46
|
+
if (!hasStyleObjectArguments(node.callee, references, importSources)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const matches = esquery(
|
|
51
|
+
node,
|
|
52
|
+
'ArrowFunctionExpression, FunctionExpression',
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
for (const match of matches) {
|
|
56
|
+
context.report({ node: match, messageId: 'no-dynamic-styles' });
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export default rule;
|