Skip to content

Commit f32c34d

Browse files
Added detection of destructured object parameters
1 parent 3c4aff0 commit f32c34d

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

typescript/src/core/processors/function-declaration.processor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class FunctionDeclarationProcessor extends Processor {
101101
*/
102102
export class FunctionParameterProcessor extends Processor {
103103
public executionCondition: ExecutionCondition = new ExecutionCondition(
104-
[AST_NODE_TYPES.Identifier], // TODO: add other parameter patterns
104+
[AST_NODE_TYPES.Identifier, AST_NODE_TYPES.ObjectPattern], // TODO: add other parameter patterns
105105
({localContexts}) =>
106106
!!localContexts.parentContexts && localContexts.parentContexts.has(CoreContextKeys.FUNCTION_TYPE)
107107
);
@@ -119,12 +119,13 @@ export class FunctionParameterProcessor extends Processor {
119119
const funcTypeParam = functionType.parameters[paramIndex];
120120

121121
// TODO: handle function overloads: funcTypeParam must always be defined!
122-
if (funcTypeParam && node.type === AST_NODE_TYPES.Identifier) {
122+
if (funcTypeParam) {
123+
const paramName = node.type === AST_NODE_TYPES.Identifier ? funcTypeParam.name : "";
123124
return singleEntryConceptMap(
124125
LCEParameterDeclaration.conceptId,
125126
new LCEParameterDeclaration(
126127
funcTypeParam.index,
127-
funcTypeParam.name,
128+
paramName,
128129
funcTypeParam.type,
129130
"optional" in node && !!node.optional,
130131
getAndDeleteChildConcepts(IdentifierTraverser.DECORATORS_PROP, LCEDecorator.conceptId, childConcepts),

typescript/test/core/integration/function-declarations.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ describe("function declarations test", () => {
255255
}
256256
});
257257

258+
test("function with single destructured parameter of referenced interface type", async () => {
259+
const decl = funDecls.get('"./src/main.ts".fDestructuredParam');
260+
expect(decl).toBeDefined();
261+
if (decl) {
262+
expect(decl.coordinates.fileName).toBe(mainModule.path);
263+
expect(decl.fqn.globalFqn).toBe(resolveGlobalFqn(projectRootPath, '"./src/main.ts".fDestructuredParam'));
264+
expect(decl.functionName).toBe("fDestructuredParam");
265+
266+
expectPrimitiveType(decl.returnType, "void");
267+
268+
expect(decl.parameters).toHaveLength(1);
269+
expectFunctionParameter(decl.parameters, 0, "", false);
270+
expectDeclaredType(decl.parameters[0].type, resolveGlobalFqn(projectRootPath, '"./src/main.ts".CustomInterface'));
271+
272+
expect(decl.async).toBe(false);
273+
274+
expect(decl.typeParameters).toHaveLength(0);
275+
}
276+
277+
expectDependency(
278+
projectRootPath,
279+
dependencies,
280+
'"./src/main.ts".fDestructuredParam',
281+
resolveGlobalFqn(projectRootPath, '"./src/main.ts".CustomInterface'),
282+
1,
283+
);
284+
});
285+
258286
test("function with single parameter of referenced class type", async () => {
259287
const decl = funDecls.get('"./src/main.ts".fParamRef');
260288
expect(decl).toBeDefined();

typescript/test/core/integration/sample-projects/function-declarations/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function fBodyRef() {
5353

5454
function fParam(p1: number) {}
5555
function fMultiParam(p1: number, p2: string, p3?: string) {}
56+
function fDestructuredParam({x, y}: CustomInterface): void {}
5657
// function fParamDefault(p1: string = "") {} // TODO: enable this once default parameters are supported
5758
// function fSpread(...p1: number[]) {} // TODO: enable this once spread parameters are supported
5859
function fParamRef(p1: CustomClass) {}

0 commit comments

Comments
 (0)