Skip to content

Commit 2c17cdd

Browse files
authored
Avoid triggering an exception (GCI12) (#71)
1 parent 92820d3 commit 2c17cdd

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

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)
21+
- [#71](https://github.com/green-code-initiative/creedengo-javascript/pull/71) Avoid triggering an exception (GCI12)
2122

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

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ module.exports = {
3434
schema: [],
3535
},
3636
create: function (context) {
37-
function isNodeUseStyleProperty(node) {
38-
return node?.object?.property?.name === "style";
39-
}
37+
const isNodeUseStyleProperty = (node) =>
38+
node?.object?.property?.name === "style";
4039

4140
return {
4241
AssignmentExpression(node) {
@@ -45,8 +44,10 @@ module.exports = {
4544
const domElementName = node.left.object.object.name;
4645
const currentRangestart = node.left.object.object.range[0];
4746

48-
/** We get the parent AST to check if there is more than one assignation on
49-
the style of the same domElement */
47+
/**
48+
* Store parent AST to check if there is more
49+
* than one assignation on the style of the same domElement
50+
*/
5051
const currentScopeASTBody =
5152
context.getScope().block.body.length != null
5253
? context.getScope().block.body
@@ -62,10 +63,11 @@ module.exports = {
6263

6364
// De-duplication, prevents multiple alerts for each line involved
6465
const isCurrentNodeTheFirstAssignation =
66+
filtered.length > 1 &&
6567
currentRangestart <=
66-
filtered[0].expression.left.object.object.range[0];
68+
filtered[0].expression.left.object.object.range[0];
6769

68-
if (filtered.length > 1 && isCurrentNodeTheFirstAssignation) {
70+
if (isCurrentNodeTheFirstAssignation) {
6971
context.report({
7072
node,
7173
messageId: "UseClassInstead",

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,34 @@ ruleTester.run("no-multiple-style-changes", rule, {
4141
code: 'element.style.height = "800px";',
4242
},
4343
{
44-
code: `element.style.height = "800px";
45-
element2.style.width = "800px";`,
44+
code: `
45+
element.style.height = "800px";
46+
element2.style.width = "800px";
47+
`,
4648
},
4749
{
48-
code: `element.style.height = "800px";
50+
code: `
51+
element.style.height = "800px";
4952
function a() { element.style.width = "800px"; }
5053
`,
5154
},
5255
],
5356

5457
invalid: [
5558
{
56-
code: `function a(element){
57-
element.style.height = "800px";
58-
element.style.width = "800px";
59-
}`,
59+
code: `
60+
function a(element){
61+
element.style.height = "800px";
62+
element.style.width = "800px";
63+
}
64+
`,
6065
errors: [expectedError],
6166
},
6267
{
63-
code: `element.style.height = "800px";
64-
element.style.width = "800px";`,
68+
code: `
69+
element.style.height = "800px";
70+
element.style.width = "800px";
71+
`,
6572
errors: [expectedError],
6673
},
6774
{

0 commit comments

Comments
 (0)