Skip to content

Commit 1ddfcd6

Browse files
committed
Fix spread style attributes in ESLint rules
1 parent b4577a4 commit 1ddfcd6

5 files changed

Lines changed: 11 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
// To prevent (for example) <div style={{ animate: 'width 2s' }}>
4545
const property = styleAttribute.value.expression.properties.find(
4646
(prop) =>
47+
prop.key != null &&
4748
forbiddenProperties.some((forbidProp) =>
4849
prop.key.name.includes(forbidProp),
4950
),

eslint-plugin/lib/rules/prefer-shorthand-css-notations.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ module.exports = {
9393
(attr) => attr.name?.name === "style",
9494
);
9595
if (styleAttribute?.value.expression?.properties) {
96-
const nodePropertyNames =
97-
styleAttribute.value.expression.properties.map(
98-
(property) => property.key.name,
99-
);
96+
const nodePropertyNames = styleAttribute.value.expression.properties
97+
.filter((property) => property.key != null)
98+
.map((property) => property.key.name);
10099

101100
for (const [shorthandProp, matchProperties] of Object.entries(
102101
shorthandProperties,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ruleTester.run("avoid-css-animations", rule, {
5757
`<div style="border: 2px solid red">My red element</div>`,
5858
// spread attributes should not throw an error (#49)
5959
"<input {...inputProps} className={styles.input} onChange={handleChange}/>",
60+
// spread style attributes should not throw an error (#100)
61+
"<input style={{ ...inputProps.style, color: 'red' }} onChange={handleChange}/>",
6062
],
6163

6264
invalid: [

eslint-plugin/tests/lib/rules/prefer-shorthand-css-notations.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ ruleTester.run("prefer-shorthand-css-notations", rule, {
7777
},
7878
// spread attributes should not throw an error (#49)
7979
"<input {...inputProps} className={styles.input} onChange={handleChange}/>",
80+
// spread style attributes should not throw an error (#100)
81+
"<input style={{ ...inputProps.style, color: 'red' }} onChange={handleChange}/>",
8082
],
8183
invalid: [
8284
{

test-project/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ __metadata:
2020
linkType: hard
2121

2222
"@creedengo/eslint-plugin@file:../eslint-plugin::locator=creedengo-javascript-test-project%40workspace%3A.":
23-
version: 2.2.0
24-
resolution: "@creedengo/eslint-plugin@file:../eslint-plugin#../eslint-plugin::hash=18baa3&locator=creedengo-javascript-test-project%40workspace%3A."
23+
version: 3.0.0
24+
resolution: "@creedengo/eslint-plugin@file:../eslint-plugin#../eslint-plugin::hash=2b5b53&locator=creedengo-javascript-test-project%40workspace%3A."
2525
peerDependencies:
2626
eslint: ^9.0.0
27-
checksum: 10c0/93b16bd19046035eefc37d51b52015310472a5f6ff711d8ffd70c7856c20c9bffea5f90efd7c1309d87d939362dae998fd283edfc9e0e62e1f6f0165ea66cce0
27+
checksum: 10c0/d5385c705fcc8dcaba9a394051338d462b2b22ce5aeac6bcf7b4a679b8a9959bf0bdcf7210d23dadb343e36f915652983c33729a11b74e8760434f7d8951b7f9
2828
languageName: node
2929
linkType: hard
3030

0 commit comments

Comments
 (0)