Skip to content

Commit d4bdf2e

Browse files
clydinfilipesilva
authored andcommitted
fix(@angular-devkit/core): allow property remove with workspace API
Removal changes were previously being improperly recorded with the wrong parent. A property can now be directly removed by setting the value to undefined or by using the delete operator.
1 parent 055d9c3 commit d4bdf2e

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
// Comment
4+
"schematics": {
5+
"@angular/schematics:component": {
6+
"prefix": "abc"
7+
}
8+
},
9+
"x-foo": {
10+
"is": ["good", "great", "awesome"]
11+
},
12+
"x-bar": 5,
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": 1,
3+
// Comment
4+
"x-foo": {
5+
"is": ["good", "great", "awesome"]
6+
},
7+
"x-bar": 5,
8+
}

packages/angular_devkit/core/src/workspace/json/utilities.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ function create(
207207
return value;
208208
},
209209
set(target: {}, p: PropertyKey, value: unknown): boolean {
210+
if (value === undefined) {
211+
// setting to undefined is equivalent to a delete
212+
// tslint:disable-next-line: no-non-null-assertion
213+
return this.deleteProperty!(target, p);
214+
}
215+
210216
if (typeof p === 'symbol' || Reflect.has(target, p)) {
211217
return Reflect.set(target, p, value);
212218
} else if (excluded.has(p) || (included && !included.has(p))) {
@@ -251,13 +257,23 @@ function create(
251257
if (cacheEntry.node) {
252258
alteredNodes.add(cacheEntry.node);
253259
}
254-
reporter(propertyPath, cacheEntry.parent, cacheEntry.node, oldValue, undefined);
260+
if (cacheEntry.parent.kind === 'keyvalue') {
261+
// Remove the entire key/value pair from this JSON object
262+
reporter(propertyPath, ast, cacheEntry.node, oldValue, undefined);
263+
} else {
264+
reporter(propertyPath, cacheEntry.parent, cacheEntry.node, oldValue, undefined);
265+
}
255266
} else {
256267
const { node, parent } = findNode(ast, p);
257268
if (node) {
258269
cache.set(propertyPath, { node, parent, value: undefined });
259270
alteredNodes.add(node);
260-
reporter(propertyPath, parent, node, node && node.value, undefined);
271+
if (parent.kind === 'keyvalue') {
272+
// Remove the entire key/value pair from this JSON object
273+
reporter(propertyPath, ast, node, node && node.value, undefined);
274+
} else {
275+
reporter(propertyPath, parent, node, node && node.value, undefined);
276+
}
261277
}
262278
}
263279

packages/angular_devkit/core/src/workspace/json/writer_spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,35 @@ describe('writeJsonWorkpaceFile', () => {
592592

593593
await writeJsonWorkspace(workspace, host, 'ObjectReplace3');
594594
});
595+
596+
it('removes a property when property value is set to undefined', async () => {
597+
const host = createTestCaseHost(basicFile);
598+
599+
const workspace = await readJsonWorkspace('', host);
600+
601+
workspace.extensions['x-baz'] = undefined;
602+
603+
await writeJsonWorkspace(workspace, host, 'ObjectRemove');
604+
});
605+
606+
it('removes a property when using delete operator', async () => {
607+
const host = createTestCaseHost(basicFile);
608+
609+
const workspace = await readJsonWorkspace('', host);
610+
611+
delete workspace.extensions['x-baz'];
612+
613+
await writeJsonWorkspace(workspace, host, 'ObjectRemove');
614+
});
615+
616+
it('removes multiple properties when using delete operator', async () => {
617+
const host = createTestCaseHost(basicFile);
618+
619+
const workspace = await readJsonWorkspace('', host);
620+
621+
delete workspace.extensions['x-baz'];
622+
delete workspace.extensions.schematics;
623+
624+
await writeJsonWorkspace(workspace, host, 'ObjectRemoveMultiple');
625+
});
595626
});

0 commit comments

Comments
 (0)