Skip to content

Commit 5d41a2e

Browse files
committed
feat: Support for private fields
1 parent 49b1c4f commit 5d41a2e

File tree

10 files changed

+646
-478
lines changed

10 files changed

+646
-478
lines changed

scripts/rebuild_specs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ts = require('typescript');
99
const app = new TypeDoc.Application();
1010
app.bootstrap({
1111
mode: TypeDoc.SourceFileMode.Modules,
12-
target: ts.ScriptTarget.ES5,
12+
target: ts.ScriptTarget.ES2016,
1313
module: ts.ModuleKind.CommonJS,
1414
experimentalDecorators: true,
1515
jsx: ts.JsxEmit.React,

src/lib/converter/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ export class Context {
408408
}
409409
}
410410

411-
function isNamedNode(node: ts.Node): node is ts.Node & { name: ts.Identifier | ts.ComputedPropertyName } {
411+
function isNamedNode(node: ts.Node): node is ts.Node & { name: ts.Identifier | ts.PrivateIdentifier | ts.ComputedPropertyName } {
412412
return node['name'] && (
413-
ts.isIdentifier(node['name']) ||
413+
ts.isIdentifierOrPrivateIdentifier(node['name']) ||
414414
ts.isComputedPropertyName(node['name'])
415415
);
416416
}

src/lib/converter/nodes/class.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ export class ClassConverter extends ConverterNodeComponent<ts.ClassDeclaration>
3939
context.withScope(reflection, node.typeParameters, () => {
4040
if (node.members) {
4141
node.members.forEach((member) => {
42-
const modifiers = ts.getCombinedModifierFlags(member);
43-
const privateMember = (modifiers & ts.ModifierFlags.Private) > 0;
44-
const protectedMember = (modifiers & ts.ModifierFlags.Protected) > 0;
45-
const exclude = (context.converter.excludePrivate && privateMember)
46-
|| (context.converter.excludeProtected && protectedMember);
47-
48-
if (!exclude) {
49-
this.owner.convertNode(context, member);
42+
const child = this.owner.convertNode(context, member);
43+
// class Foo { #foo = 1 }
44+
if (child && member.name && ts.isPrivateIdentifier(member.name)) {
45+
child.flags.setFlag(ReflectionFlag.Private, true);
5046
}
5147
});
5248
}

src/lib/converter/nodes/constructor.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ export class ConstructorConverter extends ConverterNodeComponent<ts.ConstructorD
6868
return;
6969
}
7070

71-
const privateParameter = modifiers & ts.ModifierFlags.Private;
72-
if (privateParameter && context.converter.excludePrivate) {
73-
return;
74-
}
75-
76-
const protectedParameter = modifiers & ts.ModifierFlags.Protected;
77-
if (protectedParameter && context.converter.excludeProtected) {
78-
return;
79-
}
80-
8171
const property = createDeclaration(context, parameter, ReflectionKind.Property);
8272
if (!property) {
8373
return;

src/lib/converter/plugins/CommentPlugin.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,18 @@ export class CommentPlugin extends ConverterComponent {
246246
info.reflection.comment = comment;
247247
}
248248

249-
const stripInternal = this.application.options.getCompilerOptions().stripInternal;
249+
const stripInternal = !!this.application.options.getCompilerOptions().stripInternal;
250+
const excludePrivate = this.application.options.getValue('excludePrivate');
251+
const excludeProtected = this.application.options.getValue('excludeProtected');
250252

251253
const project = context.project;
252254
const reflections = Object.values(project.reflections);
253255

254256
// remove signatures
255-
const hidden = reflections.filter(reflection => CommentPlugin.isHidden(reflection, stripInternal));
257+
// TODO: This doesn't really belong here. Removing comments due to @hidden yes, but private/protected no.
258+
// it needs to be here for now because users can use @public/@private/@protected to override visibility.
259+
// the converter should probably have a post resolve step in which it handles the excludePrivate/protected options.
260+
const hidden = reflections.filter(reflection => CommentPlugin.isHidden(reflection, stripInternal, excludePrivate, excludeProtected));
256261
hidden.forEach(reflection => project.removeReflection(reflection, true));
257262

258263
// remove functions with empty signatures after their signatures have been removed
@@ -378,9 +383,22 @@ export class CommentPlugin extends ConverterComponent {
378383
*
379384
* @param reflection Reflection to check if hidden
380385
*/
381-
private static isHidden(reflection: Reflection, stripInternal: boolean | undefined) {
386+
private static isHidden(
387+
reflection: Reflection,
388+
stripInternal: boolean,
389+
excludePrivate: boolean,
390+
excludeProtected: boolean
391+
) {
382392
const comment = reflection.comment;
383393

394+
if (reflection.flags.hasFlag(ReflectionFlag.Private) && excludePrivate) {
395+
return true;
396+
}
397+
398+
if (reflection.flags.hasFlag(ReflectionFlag.Protected) && excludeProtected) {
399+
return true;
400+
}
401+
384402
if (!comment) {
385403
return false;
386404
}

src/test/converter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Converter', function() {
1313
app.bootstrap({
1414
mode: 'modules',
1515
logger: 'none',
16-
target: ScriptTarget.ES5,
16+
target: ScriptTarget.ES2016,
1717
module: ModuleKind.CommonJS,
1818
experimentalDecorators: true,
1919
jsx: JsxEmit.React,

src/test/converter/class/class.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,8 @@ export class ComputedNames {
125125
['literal2'] = true;
126126
y = false;
127127
}
128+
129+
export class Ts38PrivateFields {
130+
/** Docs */
131+
#foo = 1;
132+
}

0 commit comments

Comments
 (0)