Skip to content

Commit c7a4712

Browse files
committed
feat: WIP delete improvements and bug fixes
1 parent 131b918 commit c7a4712

File tree

2 files changed

+63
-40
lines changed

2 files changed

+63
-40
lines changed

src/utils/file-modification-calculator.test.ts

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('File modification calculator tests', () => {
8484
})
8585
modifiedResource.attachResourceInfo(generateResourceInfo('resource1'))
8686

87-
const calculator = new FileModificationCalculator(project.resourceConfigs, project.sourceMaps.getSourceMap(defaultPath).file, project.sourceMaps);
87+
const calculator = new FileModificationCalculator(project);
8888
const result = await calculator.calculate([{
8989
modification: ModificationType.DELETE,
9090
resource: modifiedResource,
@@ -97,7 +97,6 @@ describe('File modification calculator tests', () => {
9797
' "default": "latest"\n' +
9898
' }\n' +
9999
' }\n' +
100-
' \n' +
101100
']')
102101
console.log(result)
103102
console.log(result.diff)
@@ -136,15 +135,55 @@ describe('File modification calculator tests', () => {
136135
resource: modifiedResource,
137136
}])
138137

139-
// expect(result.newFile).to.eq('[\n' +
140-
// ' {\n' +
141-
// ' "type": "project",\n' +
142-
// ' "plugins": {\n' +
143-
// ' "default": "latest"\n' +
144-
// ' }\n' +
145-
// ' }\n' +
146-
// ' \n' +
147-
// ']')
138+
expect(result.newFile).to.eq('[\n' +
139+
' {\n' +
140+
' "type": "project",\n' +
141+
' "plugins": {\n' +
142+
' "default": "latest"\n' +
143+
' }\n' +
144+
' }\n' +
145+
']',)
146+
console.log(result)
147+
console.log(result.diff)
148+
})
149+
150+
it('Can delete a resource from an existing config 3 (with proper commas)', async () => {
151+
const existingFile =
152+
`[
153+
{ "type": "resource2", "param2": ["a", "b", "c"] }, { "type": "resource1", "param2": ["a", "b", "c"] }, {
154+
"type": "project",
155+
"plugins": {
156+
"default": "latest"
157+
}
158+
}
159+
]`
160+
generateTestFile(existingFile);
161+
162+
const project = await CodifyParser.parse(defaultPath)
163+
project.resourceConfigs.forEach((r) => {
164+
r.attachResourceInfo(generateResourceInfo(r.type))
165+
});
166+
167+
const modifiedResource = new ResourceConfig({
168+
type: 'resource1',
169+
parameter1: 'abc'
170+
})
171+
modifiedResource.attachResourceInfo(generateResourceInfo('resource1'))
172+
173+
const calculator = new FileModificationCalculator(project);
174+
const result = await calculator.calculate([{
175+
modification: ModificationType.DELETE,
176+
resource: modifiedResource,
177+
}])
178+
179+
expect(result.newFile).to.eq('[\n' +
180+
' { "type": "resource2", "param2": ["a", "b", "c"] }, {\n' +
181+
' "type": "project",\n' +
182+
' "plugins": {\n' +
183+
' "default": "latest"\n' +
184+
' }\n' +
185+
' }\n' +
186+
']')
148187
console.log(result)
149188
console.log(result.diff)
150189
})

src/utils/file-modification-calculator.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ export class FileModificationCalculator {
6363

6464
// Reverse the traversal order so we edit from the back. This way the line numbers won't be messed up with new edits.
6565
for (const existing of this.existingConfigs.reverse()) {
66-
// Skip past the project config; This also has the effect of casting the rest of this to resource config.
67-
if (!this.isResourceConfig(existing)) {
68-
continue;
69-
}
70-
7166
const duplicateIndex = modifications.findIndex((modified) => existing.isSameOnSystem(modified.resource))
7267

7368
// The resource was not modified in any way. Skip.
@@ -83,10 +78,11 @@ export class FileModificationCalculator {
8378
const isLast = sourceIndex === this.totalConfigLength - 1;
8479
const isFirst = sourceIndex === 0;
8580

81+
// We try to start deleting from the previous element to the next element if possible. This covers any spaces as well.
8682
const value = !isFirst ? this.sourceMap.lookup(`/${sourceIndex - 1}`)?.valueEnd : this.sourceMap.lookup(duplicateSourceKey)?.value;
8783
const valueEnd = !isLast ? this.sourceMap.lookup(`/${sourceIndex + 1}`)?.value : this.sourceMap.lookup(duplicateSourceKey)?.valueEnd;
8884

89-
newFile = this.remove(newFile, value!, valueEnd!);
85+
newFile = this.remove(newFile, value!, valueEnd!, isFirst, isLast);
9086
continue;
9187
}
9288

@@ -166,30 +162,18 @@ export class FileModificationCalculator {
166162
file: string,
167163
value: SourceLocation,
168164
valueEnd: SourceLocation,
165+
isFirst: boolean,
166+
isLast: boolean,
169167
): string {
170-
let result = this.r(file, value.position, valueEnd.position)
171-
172-
// let commaIndex = - 1;
173-
// for (let counter = value.position; counter > 0; counter--) {
174-
// if (result[counter] === ',') {
175-
// commaIndex = counter;
176-
// break;
177-
// }
178-
// }
179-
//
180-
// // Not able to find comma behind (this was the first element). We want to delete the comma behind then.
181-
// if (commaIndex === -1) {
182-
// for (let counter = value.position; counter < file.length - 1; counter++) {
183-
// if (result[counter] === ',') {
184-
// commaIndex = counter;
185-
// break;
186-
// }
187-
// }
188-
// }
189-
//
190-
// if (commaIndex !== -1) {
191-
// result = this.splice(result, commaIndex, 1)
192-
// }
168+
// Start one later so we leave the previous trailing comma alone
169+
const start = isFirst || isLast ? value.position : value.position + 1;
170+
171+
let result = this.r(file, start, valueEnd.position)
172+
173+
// If there's no gap between the remaining elements, we add a space.
174+
if (!isFirst && !/\s/.test(result[start])) {
175+
result = this.splice(result, start, 0, ' ');
176+
}
193177

194178
return result;
195179
}

0 commit comments

Comments
 (0)