Skip to content

Commit

Permalink
fix: allow setType on binding patterns (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Mar 16, 2023
1 parent dc1c1b4 commit d58f238
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -10081,7 +10081,8 @@ function TypedNode(Base) {
});
return this;
function getInsertPosWhenNoType(node) {
const identifier = node.getFirstChildByKindOrThrow(SyntaxKind.Identifier);
var _a;
let identifier = (_a = node.getFirstChildByKind(SyntaxKind.Identifier)) !== null && _a !== void 0 ? _a : node.getFirstChildIfKindOrThrow(SyntaxKind.ObjectBindingPattern, "A first child of the kind Identifier or ObjectBindingPattern was expected.");
const nextSibling = identifier.getNextSibling();
const insertAfterNode = isQuestionOrExclamation(nextSibling) ? nextSibling : identifier;
return insertAfterNode.getEnd();
Expand Down
8 changes: 7 additions & 1 deletion packages/ts-morph/src/compiler/ast/base/TypedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ export function TypedNode<T extends Constructor<TypedNodeExtensionType>>(Base: T
return this;

function getInsertPosWhenNoType(node: Node) {
const identifier = node.getFirstChildByKindOrThrow(SyntaxKind.Identifier);
let identifier = node.getFirstChildByKind(SyntaxKind.Identifier) ?? node.getFirstChildByKind(
SyntaxKind.ArrayBindingPattern,
) ?? node.getFirstChildIfKindOrThrow(
SyntaxKind.ObjectBindingPattern,
"A first child of the kind Identifier, ArrayBindingPattern, or ObjectBindingPattern was expected.",
);

const nextSibling = identifier.getNextSibling();
const insertAfterNode = isQuestionOrExclamation(nextSibling) ? nextSibling : identifier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ describe("TypedNode", () => {
doTest(`function Identifier(param, param2) {}`, "number", `function Identifier(param: number, param2) {}`);
});

it("should set with object binding pattern", () => {
doTest(`function Identifier({ foo }) {}`, "Record<string, any>", `function Identifier({ foo }: Record<string, any>) {}`);
});

it("should set with array binding pattern", () => {
doTest(`function Identifier([foo]) {}`, "number[]", `function Identifier([foo]: number[]) {}`);
});

it("should set when explicit", () => {
doTest(`function Identifier(param: string) {}`, "number", `function Identifier(param: number) {}`);
});
Expand Down

0 comments on commit d58f238

Please sign in to comment.