Skip to content

Commit

Permalink
fix(@angular-devkit/core): allow property remove with workspace API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clydin authored and filipesilva committed Aug 31, 2020
1 parent 055d9c3 commit d4bdf2e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
@@ -0,0 +1,13 @@
{
"version": 1,
// Comment
"schematics": {
"@angular/schematics:component": {
"prefix": "abc"
}
},
"x-foo": {
"is": ["good", "great", "awesome"]
},
"x-bar": 5,
}
@@ -0,0 +1,8 @@
{
"version": 1,
// Comment
"x-foo": {
"is": ["good", "great", "awesome"]
},
"x-bar": 5,
}
20 changes: 18 additions & 2 deletions packages/angular_devkit/core/src/workspace/json/utilities.ts
Expand Up @@ -207,6 +207,12 @@ function create(
return value;
},
set(target: {}, p: PropertyKey, value: unknown): boolean {
if (value === undefined) {
// setting to undefined is equivalent to a delete
// tslint:disable-next-line: no-non-null-assertion
return this.deleteProperty!(target, p);
}

if (typeof p === 'symbol' || Reflect.has(target, p)) {
return Reflect.set(target, p, value);
} else if (excluded.has(p) || (included && !included.has(p))) {
Expand Down Expand Up @@ -251,13 +257,23 @@ function create(
if (cacheEntry.node) {
alteredNodes.add(cacheEntry.node);
}
reporter(propertyPath, cacheEntry.parent, cacheEntry.node, oldValue, undefined);
if (cacheEntry.parent.kind === 'keyvalue') {
// Remove the entire key/value pair from this JSON object
reporter(propertyPath, ast, cacheEntry.node, oldValue, undefined);
} else {
reporter(propertyPath, cacheEntry.parent, cacheEntry.node, oldValue, undefined);
}
} else {
const { node, parent } = findNode(ast, p);
if (node) {
cache.set(propertyPath, { node, parent, value: undefined });
alteredNodes.add(node);
reporter(propertyPath, parent, node, node && node.value, undefined);
if (parent.kind === 'keyvalue') {
// Remove the entire key/value pair from this JSON object
reporter(propertyPath, ast, node, node && node.value, undefined);
} else {
reporter(propertyPath, parent, node, node && node.value, undefined);
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions packages/angular_devkit/core/src/workspace/json/writer_spec.ts
Expand Up @@ -592,4 +592,35 @@ describe('writeJsonWorkpaceFile', () => {

await writeJsonWorkspace(workspace, host, 'ObjectReplace3');
});

it('removes a property when property value is set to undefined', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

workspace.extensions['x-baz'] = undefined;

await writeJsonWorkspace(workspace, host, 'ObjectRemove');
});

it('removes a property when using delete operator', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

delete workspace.extensions['x-baz'];

await writeJsonWorkspace(workspace, host, 'ObjectRemove');
});

it('removes multiple properties when using delete operator', async () => {
const host = createTestCaseHost(basicFile);

const workspace = await readJsonWorkspace('', host);

delete workspace.extensions['x-baz'];
delete workspace.extensions.schematics;

await writeJsonWorkspace(workspace, host, 'ObjectRemoveMultiple');
});
});

0 comments on commit d4bdf2e

Please sign in to comment.