diff --git a/packages/ts-morph/lib/ts-morph.d.ts b/packages/ts-morph/lib/ts-morph.d.ts index db463d483..6800cc79a 100644 --- a/packages/ts-morph/lib/ts-morph.d.ts +++ b/packages/ts-morph/lib/ts-morph.d.ts @@ -948,7 +948,7 @@ export type LocalTargetDeclarations = SourceFile | ClassDeclaration | InterfaceD * Declarations that can be exported from a module. * @remarks This may be missing some types. Please open an issue if this returns a type not listed here. */ -export type ExportedDeclarations = ClassDeclaration | InterfaceDeclaration | EnumDeclaration | FunctionDeclaration | VariableDeclaration | TypeAliasDeclaration | ModuleDeclaration | Expression | SourceFile; +export type ExportedDeclarations = BindingElement | ClassDeclaration | InterfaceDeclaration | EnumDeclaration | FunctionDeclaration | VariableDeclaration | TypeAliasDeclaration | ModuleDeclaration | Expression | SourceFile; export declare function AmbientableNode>(Base: T): Constructor & T; export interface AmbientableNode { diff --git a/packages/ts-morph/src/compiler/ast/aliases.ts b/packages/ts-morph/src/compiler/ast/aliases.ts index 44257c333..509fbdbd2 100644 --- a/packages/ts-morph/src/compiler/ast/aliases.ts +++ b/packages/ts-morph/src/compiler/ast/aliases.ts @@ -184,6 +184,7 @@ export type LocalTargetDeclarations = * @remarks This may be missing some types. Please open an issue if this returns a type not listed here. */ export type ExportedDeclarations = + | BindingElement | ClassDeclaration | InterfaceDeclaration | EnumDeclaration diff --git a/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts b/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts index e1910cf1f..1bed9c238 100644 --- a/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/base/moduledNodeTests.ts @@ -551,7 +551,7 @@ describe("ModuledNode", () => { if ( !Node.isClassDeclaration(node) && !Node.isInterfaceDeclaration(node) && !Node.isEnumDeclaration(node) && !Node.isFunctionDeclaration(node) && !Node.isVariableDeclaration(node) && !Node.isTypeAliasDeclaration(node) && !Node.isModuleDeclaration(node) - && !Node.isExpression(node) && !Node.isSourceFile(node) + && !Node.isExpression(node) && !Node.isSourceFile(node) && !Node.isBindingElement(node) ) { const assertNever: never = node; throw new Error(`Node was not an exported declaration: ${(node as Node).getKindName()}`); @@ -559,6 +559,33 @@ describe("ModuledNode", () => { } } + it("should get binding elements from exported destructuring declarations", () => { + const { sourceFile } = getInfoFromText( + `export const { foo, bar: baz, ...rest } = { foo: 1, bar: 2, other: 3 };\n` + + `export const [first, ...[second, third]] = [1, 2, 3];\n` + + `export const { nested: { value } } = { nested: { value: 4 } };`, + ); + + assertMapsEqual([ + ["foo", ["foo"]], + ["baz", ["bar: baz"]], + ["rest", ["...rest"]], + ["first", ["first"]], + ["second", ["second"]], + ["third", ["third"]], + ["value", ["value"]], + ], sourceFile.getExportedDeclarations()); + + const bindingElement = sourceFile.getDescendants().find(Node.isBindingElement)!; + const exportedDeclaration: ExportedDeclarations = bindingElement; + expect(exportedDeclaration.getKindName()).to.equal("BindingElement"); + + for (const declarations of sourceFile.getExportedDeclarations().values()) { + for (const declaration of declarations) + expect(Node.isBindingElement(declaration)).to.equal(true); + } + }); + it("should get from a file", () => { const project = new Project({ useInMemoryFileSystem: true }); const mainSourceFile = project.createSourceFile(