Skip to content

Commit

Permalink
fix: Fix removing class elements on object literal expressions.
Browse files Browse the repository at this point in the history
Previously this would incorrectly throw.
  • Loading branch information
dsherret committed Apr 6, 2019
1 parent 580e27b commit 4e1464e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/compiler/ast/class/ClassElement.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { removeClassMember } from "../../../manipulation";
import * as errors from "../../../errors";
import { removeClassMember, removeCommaSeparatedChild } from "../../../manipulation";
import { ts } from "../../../typescript";
import { TypeGuards } from "../../../utils";
import { Node } from "../common";

export class ClassElement<T extends ts.ClassElement = ts.ClassElement> extends Node<T> {
/**
* Removes the class member.
*/
remove() {
removeClassMember(this);
const parent = this.getParent();
if (TypeGuards.isClassDeclaration(parent) || TypeGuards.isClassExpression(parent))
removeClassMember(this);
else if (TypeGuards.isObjectLiteralExpression(parent))
removeCommaSeparatedChild(this);
else
errors.throwNotImplementedForSyntaxKindError(parent.getKind());
}
}
2 changes: 1 addition & 1 deletion src/manipulation/manipulations/removal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function removeCommaSeparatedChild(child: Node) {

removeChildren({
children: childrenToRemove,
removePrecedingSpaces: !isRemovingFirstChild,
removePrecedingSpaces: !isRemovingFirstChild || syntaxList.getChildren().length === childrenToRemove.length && childrenToRemove[0].isFirstNodeOnLine(),
removeFollowingSpaces: isRemovingFirstChild,
removePrecedingNewLines: !isRemovingFirstChild,
removeFollowingNewLines: isRemovingFirstChild
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ describe(nameof(ObjectLiteralExpression), () => {
});
});

describe("removing elements", () => {
function doTest(text: string, index: number, expectedText: string) {
const { sourceFile, objectLiteralExpression } = getObjectLiteralExpression(text);
objectLiteralExpression.getProperties()[index].remove();
expect(sourceFile.getFullText()).to.equal(expectedText);
}

it("should remove a property assignment when on a newline", () => {
doTest("const t = {\n prop1: 5\n};", 0, "const t = {\n};");
});

it("should remove a property assignment when on the same line", () => {
doTest("const t = { prop1: 5 };", 0, "const t = { };");
});

it("should remove a method between properties", () => {
doTest("const t = {\n prop1: 5,\n myMethod() {},\n prop2: 6\n};", 1,
"const t = {\n prop1: 5,\n prop2: 6\n};");
});
});

/* Property Assignments */

describe(nameof<ObjectLiteralExpression>(e => e.insertPropertyAssignments), () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe(nameof(PropertyAssignment), () => {

describe(nameof<PropertyAssignment>(p => p.remove), () => {
function doTest(code: string, propertyToRemove: string, expectedCode: string) {
const { sourceFile, descendant } = getInfoFromTextWithDescendant<ObjectLiteralExpression>(code,
const { descendant } = getInfoFromTextWithDescendant<ObjectLiteralExpression>(code,
SyntaxKind.ObjectLiteralExpression);

descendant.getPropertyOrThrow(propertyToRemove).remove();
Expand Down

0 comments on commit 4e1464e

Please sign in to comment.