Skip to content

Commit bbe655f

Browse files
JeanMecheatscott
authored andcommitted
refactor(compiler): include public constructor paramters to class properties. (#56315)
Public properties declared in the constructor are part of the public API and we should extract them. Fixes #56310 PR Close #56315
1 parent 7c0a7a0 commit bbe655f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/compiler-cli/src/ngtsc/docs/src/class_extractor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,22 @@ class ClassExtractor {
293293
);
294294
}
295295

296+
/** Check if the parameter is a constructor parameter with a public modifier */
297+
private isPublicConstructorParameterProperty(node: ts.Node): boolean {
298+
if (ts.isParameterPropertyDeclaration(node, node.parent) && node.modifiers) {
299+
return node.modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.PublicKeyword);
300+
}
301+
return false;
302+
}
303+
296304
/** Gets whether a member is a property. */
297305
private isProperty(member: ts.Node): member is PropertyLike {
298306
// Classes have declarations, interface have signatures
299-
return ts.isPropertyDeclaration(member) || ts.isPropertySignature(member);
307+
return (
308+
ts.isPropertyDeclaration(member) ||
309+
ts.isPropertySignature(member) ||
310+
this.isPublicConstructorParameterProperty(member)
311+
);
300312
}
301313

302314
/** Gets whether a member is a method. */

packages/compiler-cli/test/ngtsc/doc_extraction/class_doc_extraction_spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,5 +573,33 @@ runInEachFileSystem(() => {
573573
expect(ageSetterEntry.type).toBe('number');
574574
expect(ageSetterEntry.memberTags).toContain(MemberTags.Inherited);
575575
});
576+
577+
it('should extract public constructor parameters', () => {
578+
env.write(
579+
'index.ts',
580+
`
581+
export class MyClass {
582+
myProp: string;
583+
584+
constructor(public foo: string, private: bar: string, protected: baz: string) {}
585+
}`,
586+
);
587+
588+
const docs: DocEntry[] = env.driveDocsExtraction('index.ts');
589+
expect(docs.length).toBe(1);
590+
591+
const classEntry = docs[0] as ClassEntry;
592+
expect(classEntry.members.length).toBe(2);
593+
594+
const [myPropEntry, fooEntry] = classEntry.members as PropertyEntry[];
595+
596+
expect(myPropEntry.name).toBe('myProp');
597+
expect(myPropEntry.memberType).toBe(MemberType.Property);
598+
expect((myPropEntry as PropertyEntry).type).toBe('string');
599+
600+
expect(fooEntry.name).toBe('foo');
601+
expect(fooEntry.memberType).toBe(MemberType.Property);
602+
expect((fooEntry as PropertyEntry).type).toBe('string');
603+
});
576604
});
577605
});

0 commit comments

Comments
 (0)