Skip to content

Commit

Permalink
feat: Support for private fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Mar 15, 2020
1 parent 49b1c4f commit 5d41a2e
Show file tree
Hide file tree
Showing 10 changed files with 646 additions and 478 deletions.
2 changes: 1 addition & 1 deletion scripts/rebuild_specs.js
Expand Up @@ -9,7 +9,7 @@ const ts = require('typescript');
const app = new TypeDoc.Application();
app.bootstrap({
mode: TypeDoc.SourceFileMode.Modules,
target: ts.ScriptTarget.ES5,
target: ts.ScriptTarget.ES2016,
module: ts.ModuleKind.CommonJS,
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/context.ts
Expand Up @@ -408,9 +408,9 @@ export class Context {
}
}

function isNamedNode(node: ts.Node): node is ts.Node & { name: ts.Identifier | ts.ComputedPropertyName } {
function isNamedNode(node: ts.Node): node is ts.Node & { name: ts.Identifier | ts.PrivateIdentifier | ts.ComputedPropertyName } {
return node['name'] && (
ts.isIdentifier(node['name']) ||
ts.isIdentifierOrPrivateIdentifier(node['name']) ||
ts.isComputedPropertyName(node['name'])
);
}
12 changes: 4 additions & 8 deletions src/lib/converter/nodes/class.ts
Expand Up @@ -39,14 +39,10 @@ export class ClassConverter extends ConverterNodeComponent<ts.ClassDeclaration>
context.withScope(reflection, node.typeParameters, () => {
if (node.members) {
node.members.forEach((member) => {
const modifiers = ts.getCombinedModifierFlags(member);
const privateMember = (modifiers & ts.ModifierFlags.Private) > 0;
const protectedMember = (modifiers & ts.ModifierFlags.Protected) > 0;
const exclude = (context.converter.excludePrivate && privateMember)
|| (context.converter.excludeProtected && protectedMember);

if (!exclude) {
this.owner.convertNode(context, member);
const child = this.owner.convertNode(context, member);
// class Foo { #foo = 1 }
if (child && member.name && ts.isPrivateIdentifier(member.name)) {
child.flags.setFlag(ReflectionFlag.Private, true);
}
});
}
Expand Down
10 changes: 0 additions & 10 deletions src/lib/converter/nodes/constructor.ts
Expand Up @@ -68,16 +68,6 @@ export class ConstructorConverter extends ConverterNodeComponent<ts.ConstructorD
return;
}

const privateParameter = modifiers & ts.ModifierFlags.Private;
if (privateParameter && context.converter.excludePrivate) {
return;
}

const protectedParameter = modifiers & ts.ModifierFlags.Protected;
if (protectedParameter && context.converter.excludeProtected) {
return;
}

const property = createDeclaration(context, parameter, ReflectionKind.Property);
if (!property) {
return;
Expand Down
24 changes: 21 additions & 3 deletions src/lib/converter/plugins/CommentPlugin.ts
Expand Up @@ -246,13 +246,18 @@ export class CommentPlugin extends ConverterComponent {
info.reflection.comment = comment;
}

const stripInternal = this.application.options.getCompilerOptions().stripInternal;
const stripInternal = !!this.application.options.getCompilerOptions().stripInternal;
const excludePrivate = this.application.options.getValue('excludePrivate');
const excludeProtected = this.application.options.getValue('excludeProtected');

const project = context.project;
const reflections = Object.values(project.reflections);

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

// remove functions with empty signatures after their signatures have been removed
Expand Down Expand Up @@ -378,9 +383,22 @@ export class CommentPlugin extends ConverterComponent {
*
* @param reflection Reflection to check if hidden
*/
private static isHidden(reflection: Reflection, stripInternal: boolean | undefined) {
private static isHidden(
reflection: Reflection,
stripInternal: boolean,
excludePrivate: boolean,
excludeProtected: boolean
) {
const comment = reflection.comment;

if (reflection.flags.hasFlag(ReflectionFlag.Private) && excludePrivate) {
return true;
}

if (reflection.flags.hasFlag(ReflectionFlag.Protected) && excludeProtected) {
return true;
}

if (!comment) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/converter.test.ts
Expand Up @@ -13,7 +13,7 @@ describe('Converter', function() {
app.bootstrap({
mode: 'modules',
logger: 'none',
target: ScriptTarget.ES5,
target: ScriptTarget.ES2016,
module: ModuleKind.CommonJS,
experimentalDecorators: true,
jsx: JsxEmit.React,
Expand Down
5 changes: 5 additions & 0 deletions src/test/converter/class/class.ts
Expand Up @@ -125,3 +125,8 @@ export class ComputedNames {
['literal2'] = true;
y = false;
}

export class Ts38PrivateFields {
/** Docs */
#foo = 1;
}

0 comments on commit 5d41a2e

Please sign in to comment.