Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow setType on binding patterns #1380

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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