Skip to content

Commit 962e05a

Browse files
committed
PR #21 feedbacks
1 parent f093858 commit 962e05a

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

eslint-plugin/docs/rules/avoid-css-animations.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66

77
## Rule Details
88

9-
This rule aims to limit the usage of all types of CSS animations which can be very costly in terms of CPU and memory.
10-
They must only be used when they are indispensable and should be limited to the CSS properties `opacity` and `transform` with it's associated functions : `translate`, `rotate` and `scale`.
11-
You can also inform the navigator of upcoming changes with the `will-change` instruction for more optimization.
9+
This rule aims to limit the usage of all types of CSS animations which can be very costly in terms of CPU and memory.
10+
They must only be used when they are indispensable and should be limited to the CSS properties `opacity` and `transform`
11+
with it's associated functions : `translate`, `rotate` and `scale`. You can also inform the navigator of upcoming
12+
changes with the `will-change` instruction for more optimization.
1213

1314
## Examples
15+
1416
Examples of **non compliant** code for this rule:
1517

1618
```js
17-
<div style={{borderWidth:1, borderStyle: 'solid', borderColor: '#000000', transform:'translate(20px)'}}/>
19+
<div style={{ border: "1px solid black", transition: "border 2s ease" }}/>
1820
```
1921

2022
Examples of **compliant** code for this rule:
2123

2224
```js
23-
<div style={{borderWidth:1, borderStyle: 'solid', borderColor: '#000000'}}/>
25+
<div style={{ border: "1px solid black" }}/>
2426
```
27+
28+
## Further details
29+
30+
This recommendation is made by the
31+
CNUMR: [Avoid JavaScript / CSS animations](https://github.com/cnumr/best-practices/blob/main/chapters/BP_039_en.md)

eslint-plugin/lib/rules/avoid-css-animations.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ module.exports = {
3030
},
3131
schema: [],
3232
},
33-
3433
create(context) {
34+
const forbiddenProperties = ["transition", "animation"];
3535
return {
3636
JSXOpeningElement(node) {
37-
if (node.attributes.find((attr) => attr.name.name === "style")) {
38-
const styleProp = node.attributes.find(
39-
(attr) => attr.name.name === "style",
40-
);
37+
const styleAttribute = node.attributes.find(
38+
(attribute) => attribute.name.name === "style",
39+
);
4140

42-
// To prevent (for example) <div style={{ transform: rotate(30deg) }}>
43-
const transformValue = styleProp.value.expression.properties.find(
41+
if (styleAttribute != null) {
42+
// To prevent (for example) <div style={{ animate: 'width 2s' }}>
43+
const property = styleAttribute.value.expression.properties.find(
4444
(prop) =>
45-
prop.key.name.includes("transition") ||
46-
prop.key.name.includes("animation"),
45+
forbiddenProperties.some((forbidProp) =>
46+
prop.key.name.includes(forbidProp),
47+
),
4748
);
48-
if (transformValue && transformValue.value) {
49+
if (property != null) {
4950
context.report({
50-
node: transformValue,
51+
node: property,
5152
messageId: "AvoidCSSAnimations",
5253
data: {
53-
attribute: transformValue.key.name,
54+
attribute: property.key.name,
5455
},
5556
});
5657
}

eslint-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@
5454
"eslint": ">=7"
5555
},
5656
"packageManager": "[email protected]"
57-
}
57+
}

eslint-plugin/tests/lib/rules/avoid-css-animations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ruleTester.run("avoid-css-animations", rule, {
4949
5050
export default MyComponent;
5151
`,
52+
`<div style={{ width: '100px', height: '100px' }}>Hello world</div>`,
5253
],
5354

5455
invalid: [

0 commit comments

Comments
 (0)