@atlaskit/eslint-plugin-ui-styling-standard 0.8.4 → 0.9.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 +16 -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-array-arguments/usage.mdx +45 -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-array-arguments/index.js +92 -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-array-arguments/index.js +68 -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-array-arguments/index.js +86 -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-array-arguments/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-array-arguments/index.d.ts +2 -0
- package/package.json +4 -4
- 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-array-arguments/README.md +43 -0
- package/src/rules/no-array-arguments/__tests__/rule.test.tsx +116 -0
- package/src/rules/no-array-arguments/index.tsx +87 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-ui-styling-standard
|
|
2
2
|
|
|
3
|
+
## 0.9.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#90977](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/90977) [`becda03f375e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/becda03f375e) - Create eslint rule to enforce style prop usage by disallowing passing static css values
|
|
8
|
+
|
|
9
|
+
## 0.9.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#91840](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/91840) [`997a3d2d0ca0`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/997a3d2d0ca0) - Add `no-array-arguments` rule which prevents passing array arguments to style declaration functions.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies
|
|
18
|
+
|
|
3
19
|
## 0.8.4
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -14,7 +14,9 @@ Not intended for use outside the Atlassian frontend repository.
|
|
|
14
14
|
| Rule | Description | Recommended | Fixable | Suggestions |
|
|
15
15
|
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
|
|
16
16
|
| <a href="./src/rules/convert-props-syntax/README.md">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
|
|
17
|
+
| <a href="./src/rules/enforce-style-prop/README.md">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
|
|
17
18
|
| <a href="./src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
|
|
19
|
+
| <a href="./src/rules/no-array-arguments/README.md">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
|
|
18
20
|
| <a href="./src/rules/no-classname-prop/README.md">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
|
|
19
21
|
| <a href="./src/rules/no-container-queries/README.md">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
|
|
20
22
|
| <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# enforce-style-prop
|
|
2
|
+
|
|
3
|
+
This rule enforces passing dynamic CSS values to the `style` prop in JSX components.
|
|
4
|
+
|
|
5
|
+
We only want `props.style` to be used for dynamic values that cannot be easily determined at build time, e.g. `<div style={{ width: props.width }}>`. No statics values such as numbers, strings or any other statically analysable values should be passed to the `style` attribute. Instead, static CSS values should be passed into the `css` prop in JSX components.
|
|
6
|
+
|
|
7
|
+
## Examples
|
|
8
|
+
|
|
9
|
+
### Incorrect
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
function Component() {
|
|
13
|
+
return (
|
|
14
|
+
<div
|
|
15
|
+
style={{
|
|
16
|
+
margin: 0,
|
|
17
|
+
color: 'red',
|
|
18
|
+
}}
|
|
19
|
+
/>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Correct
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
function Component(props) {
|
|
28
|
+
return (
|
|
29
|
+
<div
|
|
30
|
+
style={{
|
|
31
|
+
width: props.width,
|
|
32
|
+
'--my-nested-width': props.width,
|
|
33
|
+
}}
|
|
34
|
+
css={css({ margin: 0, color: 'red' })}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
@@ -12,7 +12,9 @@ but more are planned to support the standard over time.
|
|
|
12
12
|
| Rule | Description | Recommended | Fixable | Suggestions |
|
|
13
13
|
| --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
|
|
14
14
|
| <a href="convert-props-syntax/usage">convert-props-syntax</a> | Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries. | Yes | Yes | |
|
|
15
|
+
| <a href="enforce-style-prop/usage">enforce-style-prop</a> | Disallows usage of static styles in the `style` attribute in components | Yes | | |
|
|
15
16
|
| <a href="local-cx-xcss/usage">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
|
|
17
|
+
| <a href="no-array-arguments/usage">no-array-arguments</a> | Prevents usage of array arguments to style declaration functions | Yes | Yes | |
|
|
16
18
|
| <a href="no-classname-prop/usage">no-classname-prop</a> | Disallows usage of the `className` prop in JSX | Yes | | |
|
|
17
19
|
| <a href="no-container-queries/usage">no-container-queries</a> | Prevents usage of @container query within css styling | Yes | | |
|
|
18
20
|
| <a href="no-important-styles/usage">no-important-styles</a> | Disallows important style declarations | Yes | | |
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# no-array-arguments
|
|
2
|
+
|
|
3
|
+
This rule prevents usage of array arguments to style declaration functions.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
### Incorrect
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { css } from '@compiled/react';
|
|
11
|
+
const styles = css([{ width: 100 }, { height: 100 }]);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
import { styled } from '@compiled/react';
|
|
16
|
+
const styles = css([{ width: 100 }, ...items]);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Correct
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { css } from '@compiled/react';
|
|
23
|
+
const styles = css({ width: 100 }, { height: 100 });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { css } from '@compiled/react';
|
|
28
|
+
const styles = css({ width: 100 }, ...items);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Options
|
|
32
|
+
|
|
33
|
+
### importSources
|
|
34
|
+
|
|
35
|
+
By default, this rule will check `css` usages from:
|
|
36
|
+
|
|
37
|
+
- `@atlaskit/css`
|
|
38
|
+
- `@atlaskit/primitives`
|
|
39
|
+
- `@compiled/react`
|
|
40
|
+
- `@emotion/react`
|
|
41
|
+
- `@emotion/core`
|
|
42
|
+
- `@emotion/styled`
|
|
43
|
+
- `styled-components`
|
|
44
|
+
|
|
45
|
+
To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
|
|
@@ -6,14 +6,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
9
|
-
* @codegen <<SignedSource::
|
|
9
|
+
* @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
|
|
10
10
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
11
11
|
*/
|
|
12
12
|
var _default = exports.default = {
|
|
13
13
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
14
14
|
rules: {
|
|
15
15
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
16
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
16
17
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
17
19
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
18
20
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
21
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
@@ -6,14 +6,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
9
|
-
* @codegen <<SignedSource::
|
|
9
|
+
* @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
|
|
10
10
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
11
11
|
*/
|
|
12
12
|
var _default = exports.default = {
|
|
13
13
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
14
14
|
rules: {
|
|
15
15
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
16
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
16
17
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
18
|
+
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
17
19
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
18
20
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
19
21
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.rule = exports.default = void 0;
|
|
7
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
8
|
+
var _createRule = require("../utils/create-rule");
|
|
9
|
+
var findVariable = function findVariable(context, name) {
|
|
10
|
+
return context.getScope().variables.find(function (varb) {
|
|
11
|
+
return varb.name === name;
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
var isParameter = function isParameter(context, variableName) {
|
|
15
|
+
var _findVariable;
|
|
16
|
+
var definitions = (_findVariable = findVariable(context, variableName)) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
|
|
17
|
+
if (!definitions || !definitions.length) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
var result = definitions[0];
|
|
21
|
+
return result.type === 'Parameter';
|
|
22
|
+
};
|
|
23
|
+
var checkIdentifier = function checkIdentifier(context, identifier) {
|
|
24
|
+
var _parent;
|
|
25
|
+
var val = identifier.name;
|
|
26
|
+
var variable = findVariable(context, val);
|
|
27
|
+
if (!variable) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
var variableDefinition = variable.defs[0].node;
|
|
31
|
+
|
|
32
|
+
// If identifier is a RestElement, report warning
|
|
33
|
+
if (((_parent = variable.identifiers[0].parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'RestElement') {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Checking if identifier has been destructured from props within function
|
|
38
|
+
if (variableDefinition.type === 'VariableDeclarator') {
|
|
39
|
+
var _variableDefinition$i, _variableDefinition$i2, _variableDefinition$i3;
|
|
40
|
+
// If it's a let variable, ignore
|
|
41
|
+
if (variableDefinition.parent.kind === 'let') {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
// If it's initialised by a call expression, ignore
|
|
45
|
+
if (((_variableDefinition$i = variableDefinition.init) === null || _variableDefinition$i === void 0 ? void 0 : _variableDefinition$i.type) === 'CallExpression') {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
// Destructured from member expression
|
|
49
|
+
// e.g. const width = props.width
|
|
50
|
+
if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
|
|
51
|
+
var memberIdentifierName = variableDefinition.init.object.name;
|
|
52
|
+
return isParameter(context, memberIdentifierName);
|
|
53
|
+
}
|
|
54
|
+
// Destructured from object pattern
|
|
55
|
+
// e.g. const { width } = props
|
|
56
|
+
var propName = (_variableDefinition$i3 = variableDefinition.init) === null || _variableDefinition$i3 === void 0 ? void 0 : _variableDefinition$i3.name;
|
|
57
|
+
return isParameter(context, propName);
|
|
58
|
+
}
|
|
59
|
+
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
60
|
+
return isParameter(context, val);
|
|
61
|
+
};
|
|
62
|
+
function isPropertyValueAllowed(property, context) {
|
|
63
|
+
if (property.type === 'SpreadElement') {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
// property.type === 'Property', moving on to check if value is static
|
|
67
|
+
if (property.value.type === 'Literal') {
|
|
68
|
+
return false;
|
|
69
|
+
} else if (property.value.type === 'Identifier') {
|
|
70
|
+
return checkIdentifier(context, property.value);
|
|
71
|
+
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier' && property.value.callee.name === 'token') {
|
|
72
|
+
return false;
|
|
73
|
+
} else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
|
|
74
|
+
var _findVariable2;
|
|
75
|
+
var val = property.value.object.name;
|
|
76
|
+
var definitions = (_findVariable2 = findVariable(context, val)) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
|
|
77
|
+
if (!definitions || !definitions.length) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
var variable = definitions[0];
|
|
81
|
+
if (!variable) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
var varType = variable.type;
|
|
85
|
+
if (varType === 'Variable') {
|
|
86
|
+
if (!variable.node.init) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
var varSource = variable.node.init.type;
|
|
90
|
+
// Variables initialised by a call expression or object expression are ok
|
|
91
|
+
if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
// Check if member expression matches with function argument
|
|
95
|
+
} else if (varType !== 'Parameter') {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
var rule = exports.rule = (0, _createRule.createLintRule)({
|
|
102
|
+
meta: {
|
|
103
|
+
name: 'enforce-style-prop',
|
|
104
|
+
docs: {
|
|
105
|
+
description: 'Disallows usage of static styles in the `style` attribute in components',
|
|
106
|
+
recommended: true,
|
|
107
|
+
severity: 'warn'
|
|
108
|
+
},
|
|
109
|
+
messages: {
|
|
110
|
+
'enforce-style-prop': 'Avoid adding static values to the style prop. Use the css prop for static styles instead.'
|
|
111
|
+
},
|
|
112
|
+
type: 'problem'
|
|
113
|
+
},
|
|
114
|
+
create: function create(context) {
|
|
115
|
+
return {
|
|
116
|
+
'JSXAttribute[name.name="style"] > JSXExpressionContainer': function JSXAttributeNameNameStyleJSXExpressionContainer(node) {
|
|
117
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXExpressionContainer')) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// we've reached an attribute named style
|
|
121
|
+
|
|
122
|
+
if (node.expression.type === 'ObjectExpression') {
|
|
123
|
+
node.expression.properties.forEach(function (value) {
|
|
124
|
+
if (!isPropertyValueAllowed(value, context)) {
|
|
125
|
+
context.report({
|
|
126
|
+
node: value,
|
|
127
|
+
messageId: 'enforce-style-prop'
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
context.report({
|
|
133
|
+
node: node,
|
|
134
|
+
messageId: 'enforce-style-prop'
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
var _default = exports.default = rule;
|
|
@@ -6,19 +6,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
});
|
|
7
7
|
exports.default = void 0;
|
|
8
8
|
var _convertPropsSyntax = _interopRequireDefault(require("./convert-props-syntax"));
|
|
9
|
+
var _enforceStyleProp = _interopRequireDefault(require("./enforce-style-prop"));
|
|
9
10
|
var _localCxXcss = _interopRequireDefault(require("./local-cx-xcss"));
|
|
11
|
+
var _noArrayArguments = _interopRequireDefault(require("./no-array-arguments"));
|
|
10
12
|
var _noClassnameProp = _interopRequireDefault(require("./no-classname-prop"));
|
|
11
13
|
var _noContainerQueries = _interopRequireDefault(require("./no-container-queries"));
|
|
12
14
|
var _noImportantStyles = _interopRequireDefault(require("./no-important-styles"));
|
|
13
15
|
var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
|
|
14
16
|
/**
|
|
15
17
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
16
|
-
* @codegen <<SignedSource::
|
|
18
|
+
* @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
|
|
17
19
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
18
20
|
*/
|
|
19
21
|
var _default = exports.default = {
|
|
20
22
|
'convert-props-syntax': _convertPropsSyntax.default,
|
|
23
|
+
'enforce-style-prop': _enforceStyleProp.default,
|
|
21
24
|
'local-cx-xcss': _localCxXcss.default,
|
|
25
|
+
'no-array-arguments': _noArrayArguments.default,
|
|
22
26
|
'no-classname-prop': _noClassnameProp.default,
|
|
23
27
|
'no-container-queries': _noContainerQueries.default,
|
|
24
28
|
'no-important-styles': _noImportantStyles.default,
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.rule = exports.default = void 0;
|
|
7
|
+
var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
|
|
8
|
+
var _createRule = require("../utils/create-rule");
|
|
9
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
10
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
11
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
12
|
+
var schema = [{
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
importSources: {
|
|
16
|
+
type: 'array',
|
|
17
|
+
items: {
|
|
18
|
+
type: 'string'
|
|
19
|
+
},
|
|
20
|
+
uniqueItems: true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}];
|
|
24
|
+
var rule = exports.rule = (0, _createRule.createLintRule)({
|
|
25
|
+
meta: {
|
|
26
|
+
name: 'no-array-arguments',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Prevents usage of array arguments to style declaration functions',
|
|
29
|
+
recommended: true,
|
|
30
|
+
severity: 'warn'
|
|
31
|
+
},
|
|
32
|
+
messages: {
|
|
33
|
+
'no-array-arguments': "Don't pass multiple value in an array. Pass them as multiple arguments instead."
|
|
34
|
+
},
|
|
35
|
+
type: 'problem',
|
|
36
|
+
fixable: 'code',
|
|
37
|
+
schema: schema
|
|
38
|
+
},
|
|
39
|
+
create: function create(context) {
|
|
40
|
+
var importSources = (0, _isSupportedImport.getImportSources)(context);
|
|
41
|
+
return {
|
|
42
|
+
CallExpression: function CallExpression(node) {
|
|
43
|
+
var _context$sourceCode$g = context.sourceCode.getScope(node),
|
|
44
|
+
references = _context$sourceCode$g.references;
|
|
45
|
+
if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
var _iterator = _createForOfIteratorHelper(node.arguments),
|
|
49
|
+
_step;
|
|
50
|
+
try {
|
|
51
|
+
var _loop = function _loop() {
|
|
52
|
+
var argument = _step.value;
|
|
53
|
+
if (argument.type === 'ArrayExpression') {
|
|
54
|
+
context.report({
|
|
55
|
+
node: argument,
|
|
56
|
+
messageId: 'no-array-arguments',
|
|
57
|
+
fix: function fix(fixer) {
|
|
58
|
+
if (argument.elements.length === 0) {
|
|
59
|
+
/**
|
|
60
|
+
* Don't autofix if it's an empty array.
|
|
61
|
+
*
|
|
62
|
+
* I couldn't figure out how to get rid of the comma afterwards,
|
|
63
|
+
* so the autofix was producing syntax problems.
|
|
64
|
+
*
|
|
65
|
+
* This is an edge case that shouldn't actually be happening,
|
|
66
|
+
* but if it does it's not hard to just delete the empty array...
|
|
67
|
+
*/
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
var items = argument.elements.filter(function (value) {
|
|
71
|
+
return value !== null;
|
|
72
|
+
}).map(function (element) {
|
|
73
|
+
return context.sourceCode.getText(element);
|
|
74
|
+
});
|
|
75
|
+
return fixer.replaceText(argument, items.join(', '));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
81
|
+
_loop();
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
_iterator.e(err);
|
|
85
|
+
} finally {
|
|
86
|
+
_iterator.f();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
var _default = exports.default = rule;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
7
7
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
8
8
|
rules: {
|
|
9
9
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
10
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
10
11
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
12
|
+
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
11
13
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
12
14
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
13
15
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::f2d8f76f2f41101bd777223bb368ede0>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
7
7
|
plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
|
|
8
8
|
rules: {
|
|
9
9
|
'@atlaskit/ui-styling-standard/convert-props-syntax': 'error',
|
|
10
|
+
'@atlaskit/ui-styling-standard/enforce-style-prop': 'warn',
|
|
10
11
|
'@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
|
|
12
|
+
'@atlaskit/ui-styling-standard/no-array-arguments': 'warn',
|
|
11
13
|
'@atlaskit/ui-styling-standard/no-classname-prop': 'warn',
|
|
12
14
|
'@atlaskit/ui-styling-standard/no-container-queries': 'warn',
|
|
13
15
|
'@atlaskit/ui-styling-standard/no-important-styles': 'warn',
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import { createLintRule } from '../utils/create-rule';
|
|
3
|
+
const findVariable = (context, name) => {
|
|
4
|
+
return context.getScope().variables.find(varb => varb.name === name);
|
|
5
|
+
};
|
|
6
|
+
const isParameter = (context, variableName) => {
|
|
7
|
+
var _findVariable;
|
|
8
|
+
const definitions = (_findVariable = findVariable(context, variableName)) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
|
|
9
|
+
if (!definitions || !definitions.length) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const result = definitions[0];
|
|
13
|
+
return result.type === 'Parameter';
|
|
14
|
+
};
|
|
15
|
+
const checkIdentifier = (context, identifier) => {
|
|
16
|
+
var _parent;
|
|
17
|
+
const val = identifier.name;
|
|
18
|
+
const variable = findVariable(context, val);
|
|
19
|
+
if (!variable) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const variableDefinition = variable.defs[0].node;
|
|
23
|
+
|
|
24
|
+
// If identifier is a RestElement, report warning
|
|
25
|
+
if (((_parent = variable.identifiers[0].parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'RestElement') {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Checking if identifier has been destructured from props within function
|
|
30
|
+
if (variableDefinition.type === 'VariableDeclarator') {
|
|
31
|
+
var _variableDefinition$i, _variableDefinition$i2, _variableDefinition$i3;
|
|
32
|
+
// If it's a let variable, ignore
|
|
33
|
+
if (variableDefinition.parent.kind === 'let') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
// If it's initialised by a call expression, ignore
|
|
37
|
+
if (((_variableDefinition$i = variableDefinition.init) === null || _variableDefinition$i === void 0 ? void 0 : _variableDefinition$i.type) === 'CallExpression') {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
// Destructured from member expression
|
|
41
|
+
// e.g. const width = props.width
|
|
42
|
+
if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
|
|
43
|
+
const memberIdentifierName = variableDefinition.init.object.name;
|
|
44
|
+
return isParameter(context, memberIdentifierName);
|
|
45
|
+
}
|
|
46
|
+
// Destructured from object pattern
|
|
47
|
+
// e.g. const { width } = props
|
|
48
|
+
const propName = (_variableDefinition$i3 = variableDefinition.init) === null || _variableDefinition$i3 === void 0 ? void 0 : _variableDefinition$i3.name;
|
|
49
|
+
return isParameter(context, propName);
|
|
50
|
+
}
|
|
51
|
+
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
52
|
+
return isParameter(context, val);
|
|
53
|
+
};
|
|
54
|
+
function isPropertyValueAllowed(property, context) {
|
|
55
|
+
if (property.type === 'SpreadElement') {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
// property.type === 'Property', moving on to check if value is static
|
|
59
|
+
if (property.value.type === 'Literal') {
|
|
60
|
+
return false;
|
|
61
|
+
} else if (property.value.type === 'Identifier') {
|
|
62
|
+
return checkIdentifier(context, property.value);
|
|
63
|
+
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier' && property.value.callee.name === 'token') {
|
|
64
|
+
return false;
|
|
65
|
+
} else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
|
|
66
|
+
var _findVariable2;
|
|
67
|
+
const val = property.value.object.name;
|
|
68
|
+
const definitions = (_findVariable2 = findVariable(context, val)) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
|
|
69
|
+
if (!definitions || !definitions.length) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const variable = definitions[0];
|
|
73
|
+
if (!variable) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const varType = variable.type;
|
|
77
|
+
if (varType === 'Variable') {
|
|
78
|
+
if (!variable.node.init) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const varSource = variable.node.init.type;
|
|
82
|
+
// Variables initialised by a call expression or object expression are ok
|
|
83
|
+
if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
// Check if member expression matches with function argument
|
|
87
|
+
} else if (varType !== 'Parameter') {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
export const rule = createLintRule({
|
|
94
|
+
meta: {
|
|
95
|
+
name: 'enforce-style-prop',
|
|
96
|
+
docs: {
|
|
97
|
+
description: 'Disallows usage of static styles in the `style` attribute in components',
|
|
98
|
+
recommended: true,
|
|
99
|
+
severity: 'warn'
|
|
100
|
+
},
|
|
101
|
+
messages: {
|
|
102
|
+
'enforce-style-prop': 'Avoid adding static values to the style prop. Use the css prop for static styles instead.'
|
|
103
|
+
},
|
|
104
|
+
type: 'problem'
|
|
105
|
+
},
|
|
106
|
+
create(context) {
|
|
107
|
+
return {
|
|
108
|
+
'JSXAttribute[name.name="style"] > JSXExpressionContainer': node => {
|
|
109
|
+
if (!isNodeOfType(node, 'JSXExpressionContainer')) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
// we've reached an attribute named style
|
|
113
|
+
|
|
114
|
+
if (node.expression.type === 'ObjectExpression') {
|
|
115
|
+
node.expression.properties.forEach(value => {
|
|
116
|
+
if (!isPropertyValueAllowed(value, context)) {
|
|
117
|
+
context.report({
|
|
118
|
+
node: value,
|
|
119
|
+
messageId: 'enforce-style-prop'
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
} else {
|
|
124
|
+
context.report({
|
|
125
|
+
node: node,
|
|
126
|
+
messageId: 'enforce-style-prop'
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
export default rule;
|
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
-
* @codegen <<SignedSource::
|
|
3
|
+
* @codegen <<SignedSource::0611efa9bbe46ece121a804fa5891dd6>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
|
|
5
5
|
*/
|
|
6
6
|
import convertPropsSyntax from './convert-props-syntax';
|
|
7
|
+
import enforceStyleProp from './enforce-style-prop';
|
|
7
8
|
import localCxXcss from './local-cx-xcss';
|
|
9
|
+
import noArrayArguments from './no-array-arguments';
|
|
8
10
|
import noClassnameProp from './no-classname-prop';
|
|
9
11
|
import noContainerQueries from './no-container-queries';
|
|
10
12
|
import noImportantStyles from './no-important-styles';
|
|
11
13
|
import noNestedSelectors from './no-nested-selectors';
|
|
12
14
|
export default {
|
|
13
15
|
'convert-props-syntax': convertPropsSyntax,
|
|
16
|
+
'enforce-style-prop': enforceStyleProp,
|
|
14
17
|
'local-cx-xcss': localCxXcss,
|
|
18
|
+
'no-array-arguments': noArrayArguments,
|
|
15
19
|
'no-classname-prop': noClassnameProp,
|
|
16
20
|
'no-container-queries': noContainerQueries,
|
|
17
21
|
'no-important-styles': noImportantStyles,
|