Skip to content

Commit

Permalink
Merge pull request #317 from timocov/fix315-binding-patterns
Browse files Browse the repository at this point in the history
Added support for variable declarations with binding patterns
  • Loading branch information
timocov committed Apr 17, 2024
2 parents 487adc9 + 900f971 commit 665afab
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/bundle-generator.ts
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
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
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
@@ -0,0 +1,4 @@
export const [
FOO = '321',
BAR = 1337,
];
@@ -0,0 +1,5 @@
import { TestCaseConfig } from '../test-case-config';

const config: TestCaseConfig = {};

export = config;
@@ -0,0 +1 @@
require('../run-test-case').runTestCase(__dirname);
@@ -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;
@@ -0,0 +1,4 @@
export declare const {
FOO = 123,
BAR = 42,
};
@@ -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 {};

0 comments on commit 665afab

Please sign in to comment.