Skip to content

Commit 51f7cb7

Browse files
committed
fix: Ensure valid syntax when adding parameters
1 parent c43aa6d commit 51f7cb7

File tree

6 files changed

+95
-2
lines changed

6 files changed

+95
-2
lines changed

src/autofix/solutions/amdImports.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,15 @@ export function addDependencies(
156156
newParametersValue += ")";
157157
}
158158

159-
const insertAfterParameterDeclaration = parameters?.[insertAfterIndex];
159+
const insertAfterParameterDeclaration = parameters[insertAfterIndex];
160+
161+
const existingParameterLeft = insertAfterIndex > -1 && parameters.length > 0;
162+
const existingParameterRight = insertAfterIndex === -1 && parameters.length > 0;
163+
164+
let value = existingParameterLeft ? ", " + newParametersValue : newParametersValue;
165+
value += existingParameterRight ? ", " : "";
166+
160167
const start = insertAfterParameterDeclaration?.getEnd() ?? parameterSyntax.syntaxList.getStart();
161-
const value = parameters.length ? ", " + newParametersValue : newParametersValue;
162168
changeSet.push({
163169
action: ChangeAction.INSERT,
164170
start,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sap.ui.define([], function(Input, Button, ComboBox) {
2+
"use strict";
3+
4+
new sap.m.Button();
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sap.ui.define([], function(Input) {
2+
"use strict";
3+
4+
new sap.m.Button();
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sap.ui.define([], function(Button) {
2+
"use strict";
3+
4+
new sap.m.Button();
5+
});

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,78 @@ Generated by [AVA](https://avajs.dev).
580580
});␊
581581
`
582582

583+
## General: GlobalsExistingDefineWithExistingUnusedParamsMultiple.js
584+
585+
> Snapshot 1
586+
587+
[
588+
{
589+
coverageInfo: [],
590+
errorCount: 0,
591+
fatalErrorCount: 0,
592+
filePath: 'GlobalsExistingDefineWithExistingUnusedParamsMultiple.js',
593+
messages: [],
594+
warningCount: 0,
595+
},
596+
]
597+
598+
> AutofixResult: /GlobalsExistingDefineWithExistingUnusedParamsMultiple.js
599+
600+
`sap.ui.define(["sap/m/Button"], function(MButton, Input, Button, ComboBox) {␊
601+
"use strict";␊
602+
603+
new MButton();␊
604+
});␊
605+
`
606+
607+
## General: GlobalsExistingDefineWithExistingUnusedParamsSingle.js
608+
609+
> Snapshot 1
610+
611+
[
612+
{
613+
coverageInfo: [],
614+
errorCount: 0,
615+
fatalErrorCount: 0,
616+
filePath: 'GlobalsExistingDefineWithExistingUnusedParamsSingle.js',
617+
messages: [],
618+
warningCount: 0,
619+
},
620+
]
621+
622+
> AutofixResult: /GlobalsExistingDefineWithExistingUnusedParamsSingle.js
623+
624+
`sap.ui.define(["sap/m/Button"], function(Button, Input) {␊
625+
"use strict";␊
626+
627+
new Button();␊
628+
});␊
629+
`
630+
631+
## General: GlobalsExistingDefineWithExistingUnusedParamsSingleMatching.js
632+
633+
> Snapshot 1
634+
635+
[
636+
{
637+
coverageInfo: [],
638+
errorCount: 0,
639+
fatalErrorCount: 0,
640+
filePath: 'GlobalsExistingDefineWithExistingUnusedParamsSingleMatching.js',
641+
messages: [],
642+
warningCount: 0,
643+
},
644+
]
645+
646+
> AutofixResult: /GlobalsExistingDefineWithExistingUnusedParamsSingleMatching.js
647+
648+
`sap.ui.define(["sap/m/Button"], function(MButton, Button) {␊
649+
"use strict";␊
650+
651+
new MButton();␊
652+
});␊
653+
`
654+
583655
## General: GlobalsExistingDefineWithTrailingCommas.js
584656

585657
> Snapshot 1
174 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)