Skip to content

Commit

Permalink
feat: Add JsxSpreadAttribute.setExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Sep 22, 2018
1 parent 9436954 commit 532131f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/compiler/jsx/JsxSpreadAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { removeChildren } from "../../manipulation";
import { JsxSpreadAttributeStructure } from "../../structures";
import { WriterFunction } from "../../types";
import { ts } from "../../typescript";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { Node } from "../common";
Expand All @@ -13,6 +14,15 @@ export class JsxSpreadAttribute extends JsxSpreadAttributeBase<ts.JsxSpreadAttri
return this.getNodeFromCompilerNode(this.compilerNode.expression);
}

/**
* Sets the expression.
* @param textOrWriterFunction - Text to set the expression with.
*/
setExpression(textOrWriterFunction: string | WriterFunction) {
this.getExpression().replaceWithText(textOrWriterFunction);
return this;
}

/**
* Removes the JSX spread attribute.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/tests/compiler/jsx/jsxSpreadAttributeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ describe(nameof(JsxSpreadAttribute), () => {
});
});

describe(nameof<JsxSpreadAttribute>(n => n.setExpression), () => {
function doTest(text: string, expression: string, expected: string) {
const { descendant, sourceFile } = getInfo(text);
descendant.setExpression(expression);
expect(sourceFile.getFullText()).to.equal(expected);
}

it("should set", () => {
doTest(`var t = (<jsx {...test} />);`, "newExpr", `var t = (<jsx {...newExpr} />);`);
});
});

describe(nameof<JsxSpreadAttribute>(n => n.getStructure), () => {
function doTest(text: string, expectedStructure: MakeRequired<JsxSpreadAttributeStructure>) {
const { descendant } = getInfo(text);
Expand Down

0 comments on commit 532131f

Please sign in to comment.