Skip to content

Commit 6d65a0c

Browse files
committed
refactor(autofix): Correctly remove trailing comma if property assignment is deleted
1 parent 9e9f18b commit 6d65a0c

File tree

6 files changed

+87
-8
lines changed

6 files changed

+87
-8
lines changed

src/linter/ui5Types/fix/PropertyAssignmentBaseFix.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default abstract class PropertyAssignmentBaseFix extends XmlEnabledFix {
1313
protected sourcePosition: PositionInfo | undefined;
1414
protected startPos: number | undefined;
1515
protected endPos: number | undefined;
16+
protected trailingCommaPos: number | undefined;
1617

1718
constructor() {
1819
super();
@@ -44,6 +45,22 @@ export default abstract class PropertyAssignmentBaseFix extends XmlEnabledFix {
4445
}
4546
this.startPos = node.getStart();
4647
this.endPos = node.getEnd();
48+
49+
if (ts.isObjectLiteralExpression(node.parent)) {
50+
const syntaxList = node.parent.getChildren().find((child) => child.kind === ts.SyntaxKind.SyntaxList);
51+
if (syntaxList?.kind === ts.SyntaxKind.SyntaxList) {
52+
// Check if the last child is a comma
53+
const children = syntaxList.getChildren();
54+
const propAssIdx = children.findIndex((child) => child === node);
55+
if (propAssIdx === -1) {
56+
throw new Error("Unexpected: Property assignment not found in syntax list");
57+
}
58+
const commaToken = children[propAssIdx + 1];
59+
if (commaToken?.kind === ts.SyntaxKind.CommaToken) {
60+
this.trailingCommaPos = commaToken.getEnd();
61+
}
62+
}
63+
}
4764
return true;
4865
}
4966

src/linter/ui5Types/fix/PropertyAssignmentFix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class PropertyAssignmentFix extends PropertyAssignmentBaseFix {
6262
return {
6363
action: ChangeAction.DELETE,
6464
start: this.startPos,
65-
end: this.endPos,
65+
end: this.trailingCommaPos ?? this.endPos,
6666
};
6767
}
6868
}

src/linter/ui5Types/fix/PropertyAssignmentGeneratorFix.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export default class PropertyAssignmentGeneratorFix<GeneratorContext extends obj
7373

7474
this.propertyName = node.name.getFullText();
7575
this.propertyInitializer = node.initializer.getFullText();
76-
7776
return true;
7877
}
7978

@@ -97,11 +96,19 @@ export default class PropertyAssignmentGeneratorFix<GeneratorContext extends obj
9796
if (value === undefined) {
9897
return;
9998
}
100-
return {
101-
action: ChangeAction.REPLACE,
102-
start: this.startPos,
103-
end: this.endPos,
104-
value,
105-
};
99+
if (value !== "") {
100+
return {
101+
action: ChangeAction.REPLACE,
102+
start: this.startPos,
103+
end: this.endPos,
104+
value,
105+
};
106+
} else {
107+
return {
108+
action: ChangeAction.DELETE,
109+
start: this.startPos,
110+
end: this.trailingCommaPos ?? this.endPos,
111+
};
112+
}
106113
}
107114
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sap.ui.define(["sap/m/Label", "sap/ui/layout/form/SimpleForm",], function(Label, SimpleForm) {
2+
const oLabel = new Label({
3+
text: "Form Label"
4+
})
5+
const oForm = new SimpleForm("my-form", {
6+
minWidth : 1000,
7+
editable : true,
8+
layout :"ResponsiveGridLayout",
9+
labelSpanL : 12,
10+
labelSpanM : 12,
11+
columnsL : 1,
12+
columnsM : 1,
13+
emptySpanL : 0,
14+
content : [
15+
oLabel
16+
]
17+
});
18+
});

test/lib/autofix/snapshots/autofix.fixtures.ts.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,43 @@ Generated by [AVA](https://avajs.dev).
164164
},
165165
]
166166

167+
## General: DeprecatedApi.js
168+
169+
> AutofixResult iteration #0: /DeprecatedApi.js
170+
171+
`sap.ui.define(["sap/m/Label", "sap/ui/layout/form/SimpleForm",], function(Label, SimpleForm) {␊
172+
const oLabel = new Label({␊
173+
text: "Form Label"␊
174+
})␊
175+
const oForm = new SimpleForm("my-form", {␊
176+
177+
editable : true,␊
178+
layout :"ResponsiveGridLayout",␊
179+
labelSpanL : 12,␊
180+
labelSpanM : 12,␊
181+
columnsL : 1,␊
182+
columnsM : 1,␊
183+
emptySpanL : 0,␊
184+
content : [␊
185+
oLabel␊
186+
]␊
187+
});␊
188+
});␊
189+
`
190+
191+
> LintResult: DeprecatedApi.js
192+
193+
[
194+
{
195+
coverageInfo: [],
196+
errorCount: 0,
197+
fatalErrorCount: 0,
198+
filePath: 'DeprecatedApi.js',
199+
messages: [],
200+
warningCount: 0,
201+
},
202+
]
203+
167204
## General: DeprecatedApi.view.xml
168205

169206
> AutofixResult iteration #0: /DeprecatedApi.view.xml
203 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)