Skip to content

Commit a41928f

Browse files
authored
Avoid false-positive with nested objects in GCI12 (#73)
1 parent 16928a2 commit a41928f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- [#69](https://github.com/green-code-initiative/creedengo-javascript/pull/69) Only support string literals (GCI11)
2020
- [#70](https://github.com/green-code-initiative/creedengo-javascript/pull/70) Only support SQL queries within standard methods (GCI24)
2121
- [#71](https://github.com/green-code-initiative/creedengo-javascript/pull/71) Avoid triggering an exception (GCI12)
22+
- [#73](https://github.com/green-code-initiative/creedengo-javascript/pull/73) Avoid false-positive with nested objects (GCI12)
2223

2324
## [2.0.0] - 2025-01-22
2425

eslint-plugin/lib/rules/no-multiple-style-changes.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@ module.exports = {
3737
const isNodeUseStyleProperty = (node) =>
3838
node?.object?.property?.name === "style";
3939

40+
const getNodeFullName = (node) => {
41+
let names = [];
42+
do {
43+
names.unshift(node.name ?? node.property.name);
44+
node = node.object;
45+
} while (node);
46+
return names.join(".");
47+
};
48+
4049
return {
4150
AssignmentExpression(node) {
42-
// Are we checking an assignation on a style property
43-
if (isNodeUseStyleProperty(node.left)) {
44-
const domElementName = node.left.object.object.name;
51+
// Check if there is a literal assignation on a style property
52+
if (
53+
node.right.type === "Literal" &&
54+
isNodeUseStyleProperty(node.left)
55+
) {
56+
const domElementName = getNodeFullName(node.left.object.object);
4557
const currentRangestart = node.left.object.object.range[0];
4658

4759
/**
@@ -58,7 +70,8 @@ module.exports = {
5870
e.type === "ExpressionStatement" &&
5971
e.expression.type === "AssignmentExpression" &&
6072
isNodeUseStyleProperty(e.expression.left) &&
61-
e.expression.left.object.object.name === domElementName,
73+
getNodeFullName(e.expression.left.object.object) ===
74+
domElementName,
6275
);
6376

6477
// De-duplication, prevents multiple alerts for each line involved

eslint-plugin/tests/lib/rules/no-multiple-style-changes.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ const RuleTester = require("eslint").RuleTester;
2929
// Tests
3030
//------------------------------------------------------------------------------
3131

32-
const ruleTester = new RuleTester();
32+
const ruleTester = new RuleTester({
33+
parserOptions: {
34+
ecmaVersion: 2021,
35+
sourceType: "module",
36+
},
37+
});
3338
const expectedError = {
3439
messageId: "UseClassInstead",
3540
type: "AssignmentExpression",
@@ -52,6 +57,22 @@ ruleTester.run("no-multiple-style-changes", rule, {
5257
function a() { element.style.width = "800px"; }
5358
`,
5459
},
60+
{
61+
name: "should not report on different elements in same object",
62+
code: `
63+
var elements = { element1, element2 };
64+
elements.element1.style.height = "800px";
65+
elements.element2.style.height = "800px";
66+
`,
67+
},
68+
{
69+
code: `
70+
var offsetWidth = 5;
71+
var offsetLeft = 3;
72+
element.style.width = \`\${offsetWidth}px\`;
73+
element.style.left = \`\${offsetLeft}px\`;
74+
`,
75+
},
5576
],
5677

5778
invalid: [
@@ -71,6 +92,14 @@ ruleTester.run("no-multiple-style-changes", rule, {
7192
`,
7293
errors: [expectedError],
7394
},
95+
{
96+
code: `
97+
var elements = { element1 };
98+
elements.element1.style.height = "800px";
99+
elements.element1.style.height = "800px";
100+
`,
101+
errors: [expectedError],
102+
},
74103
{
75104
code: `
76105
function changeStyle()

0 commit comments

Comments
 (0)