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

Added support for variable declarations with binding patterns #317

Merged
merged 1 commit into from
Apr 17, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/bundle-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,11 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
continue;
}

// it seems that the compiler doesn't produce anything else (e.g. binding elements) in declaration files
// but it is still possible to write such code manually
// this feels like quite rare case so no support for now
warnLog(`Unhandled variable identifier type detected (${
ts.SyntaxKind[variableDeclaration.name.kind]
}). Please report this issue to https://github.com/timocov/dts-bundle-generator`);
for (const element of variableDeclaration.name.elements) {
if (!ts.isOmittedExpression(element) && ts.isIdentifier(element.name)) {
collisionsResolver.addTopLevelIdentifier(element.name);
}
}
}
} else if (isNodeNamedDeclaration(statement)) {
const statementName = getNodeName(statement);
Expand Down Expand Up @@ -894,6 +893,10 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:

if (ts.isVariableStatement(node)) {
return node.declarationList.declarations.some((declaration: ts.VariableDeclaration) => {
if (ts.isObjectBindingPattern(declaration.name) || ts.isArrayBindingPattern(declaration.name)) {
return declaration.name.elements.some(isNodeUsed);
}

return isNodeUsed(declaration);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const namedDeclarationKinds = [
ts.SyntaxKind.NamespaceExport,
ts.SyntaxKind.NamespaceImport,
ts.SyntaxKind.ExportSpecifier,
ts.SyntaxKind.BindingElement,
];

export type NodeName = ts.DeclarationName | ts.DefaultKeyword | ts.QualifiedName | ts.PropertyAccessExpression;
export type NodeName = ts.DeclarationName | ts.DefaultKeyword | ts.QualifiedName | ts.PropertyAccessExpression | ts.BindingPattern;

export function isNodeNamedDeclaration(node: ts.Node): node is ts.NamedDeclaration {
return namedDeclarationKinds.indexOf(node.kind) !== -1;
Expand Down
12 changes: 9 additions & 3 deletions src/types-usage-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,15 @@ export class TypesUsageEvaluator {
if (isNodeNamedDeclaration(node)) {
const nodeName = getNodeName(node);
if (nodeName !== undefined) {
const childSymbol = this.getSymbol(nodeName);
if (childSymbol !== null) {
this.computeUsagesRecursively(node, childSymbol);
if (ts.isObjectBindingPattern(nodeName) || ts.isArrayBindingPattern(nodeName)) {
for (const element of nodeName.elements) {
this.computeUsageForNode(element);
}
} else {
const childSymbol = this.getSymbol(nodeName);
if (childSymbol !== null) {
this.computeUsagesRecursively(node, childSymbol);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const [
FOO = '321',
BAR = 1337,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};

export = config;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../run-test-case').runTestCase(__dirname);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BAR as ARR_BAR, FOO as ARR_FOO } from './array-const';
import { BAR as OBJ_BAR, FOO as OBJ_FOO } from './obj-const';

export type BarType = typeof ARR_BAR | typeof OBJ_BAR;
export type FooType = typeof ARR_FOO | typeof OBJ_FOO;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const {
FOO = 123,
BAR = 42,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const [FOO = "321", BAR = 1337,];
declare const { FOO$1 = 123, BAR$1 = 42, };
export type BarType = typeof BAR | typeof BAR$1;
export type FooType = typeof FOO | typeof FOO$1;

export {};