Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Constructor<AmbientableNodeExtensionType>>(Base: T): Constructor<AmbientableNode> & T;

export interface AmbientableNode {
Expand Down
1 change: 1 addition & 0 deletions packages/ts-morph/src/compiler/ast/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,41 @@ 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()}`);
}
}
}

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(
Expand Down