Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Readonly" flag for readonly properties #623

Closed
wants to merge 8 commits into from
2 changes: 2 additions & 0 deletions src/lib/converter/factories/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ function setupDeclaration(context: Context, reflection: DeclarationReflection, n
reflection.setFlag(ReflectionFlag.Protected, !!(modifiers & ts.ModifierFlags.Protected));
reflection.setFlag(ReflectionFlag.Public, !!(modifiers & ts.ModifierFlags.Public));
reflection.setFlag(ReflectionFlag.Optional, !!(node['questionToken']));
reflection.setFlag(ReflectionFlag.Abstract, !!(modifiers & ts.ModifierFlags.Abstract));
reflection.setFlag(ReflectionFlag.Readonly, !!(modifiers & ts.ModifierFlags.Readonly));

if (
context.isInherit &&
Expand Down
6 changes: 1 addition & 5 deletions src/lib/converter/nodes/class.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ts from 'typescript';
import * as _ts from '../../ts-internal';

import { Reflection, ReflectionFlag, ReflectionKind, DeclarationReflection } from '../../models/index';
import { Reflection, ReflectionKind, DeclarationReflection } from '../../models/index';
import { createDeclaration } from '../factories/index';
import { Context } from '../context';
import { Component, ConverterNodeComponent } from '../components';
Expand Down Expand Up @@ -29,10 +29,6 @@ export class ClassConverter extends ConverterNodeComponent<ts.ClassDeclaration>
reflection = <DeclarationReflection> context.scope;
} else {
reflection = createDeclaration(context, node, ReflectionKind.Class);
// set possible abstract flag here, where node is not the inherited parent
if (node.modifiers && node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
reflection.setFlag(ReflectionFlag.Abstract, true);
}
}

context.withScope(reflection, node.typeParameters, () => {
Expand Down
6 changes: 5 additions & 1 deletion src/lib/converter/nodes/enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';

import { Reflection, ReflectionKind } from '../../models/index';
import { Reflection, ReflectionKind, ReflectionFlag } from '../../models/index';
import { createDeclaration } from '../factories/index';
import { Context } from '../context';
import { Component, ConverterNodeComponent } from '../components';
Expand All @@ -26,6 +26,10 @@ export class EnumConverter extends ConverterNodeComponent<ts.EnumDeclaration> {
const enumeration = createDeclaration(context, node, ReflectionKind.Enum);

context.withScope(enumeration, () => {
if (!!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Const)) {
enumeration.setFlag(ReflectionFlag.Const, true);
}

if (node.members) {
for (let member of node.members) {
this.convertMember(context, member);
Expand Down
9 changes: 1 addition & 8 deletions src/lib/converter/nodes/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ts from 'typescript';

import { Reflection, ReflectionFlag, ReflectionKind } from '../../models/index';
import { Reflection, ReflectionKind } from '../../models/index';
import { createDeclaration, createSignature } from '../factories/index';
import { Context } from '../context';
import { Converter } from '../converter';
Expand Down Expand Up @@ -30,13 +30,6 @@ export class FunctionConverter extends ConverterNodeComponent<ts.FunctionDeclara
const hasBody = !!node.body;
const method = createDeclaration(context, <ts.Node> node, kind);

if (method // child inheriting will return null on createDeclaration
&& kind & ReflectionKind.Method
&& node.modifiers
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
method.setFlag(ReflectionFlag.Abstract, true);
}

context.withScope(method, () => {
if (!hasBody || !method.signatures) {
const signature = createSignature(context, <ts.SignatureDeclaration> node, method.name, ReflectionKind.CallSignature);
Expand Down
15 changes: 3 additions & 12 deletions src/lib/converter/nodes/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,15 @@ export class VariableConverter extends ConverterNodeComponent<ts.VariableDeclara
const kind = scope.kind & ReflectionKind.ClassOrInterface ? ReflectionKind.Property : ReflectionKind.Variable;
const variable = createDeclaration(context, node, kind, name);

switch (kind) {
case ReflectionKind.Variable:
context.withScope(variable, () => {
if (kind === ReflectionKind.Variable && node.parent) {
if (node.parent.flags & ts.NodeFlags.Const) {
variable.setFlag(ReflectionFlag.Const, true);
} else if (node.parent.flags & ts.NodeFlags.Let) {
variable.setFlag(ReflectionFlag.Let, true);
}
break;
case ReflectionKind.Property:
if (variable // child inheriting will return null on createDeclaration
&& node.modifiers
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
variable.setFlag(ReflectionFlag.Abstract, true);
}
break;
}
}

context.withScope(variable, () => {
if (node.initializer) {
switch (node.initializer.kind) {
case ts.SyntaxKind.ArrowFunction:
Expand Down
23 changes: 21 additions & 2 deletions src/lib/models/reflections/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export enum ReflectionFlag {
ConstructorProperty = 1024,
Abstract = 2048,
Const = 4096,
Let = 8192
Let = 8192,
Readonly = 16384
}

const relevantFlags: ReflectionFlag[] = [
Expand All @@ -92,7 +93,8 @@ const relevantFlags: ReflectionFlag[] = [
ReflectionFlag.Rest,
ReflectionFlag.Abstract,
ReflectionFlag.Let,
ReflectionFlag.Const
ReflectionFlag.Const,
ReflectionFlag.Readonly
];

export interface ReflectionFlags extends Array<string> {
Expand Down Expand Up @@ -149,9 +151,17 @@ export interface ReflectionFlags extends Array<string> {

isAbstract?: boolean;

/**
* Is this a const variable or enum?
*/
isConst?: boolean;

isLet?: boolean;

/**
* Is this a readonly property?
*/
isReadonly?: boolean;
}

export interface DefaultValueContainer extends Reflection {
Expand Down Expand Up @@ -422,9 +432,18 @@ export abstract class Reflection {
break;
case ReflectionFlag.Let:
this.flags.isLet = value;
if (value) {
this.setFlag(ReflectionFlag.Const, false);
}
break;
case ReflectionFlag.Const:
this.flags.isConst = value;
if (value) {
this.setFlag(ReflectionFlag.Let, false);
}
break;
case ReflectionFlag.Readonly:
this.flags.isReadonly = value;
break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/converter/class/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,22 @@ export abstract class TestAbstractClass {
abstract myAbstractProperty: string;

protected abstract myAbstractMethod(): void;

abstract get myAbstractAccessor(): number;
abstract set myAbstractAccessor(value: number);
}

export class TestAbstractClassImplementation extends TestAbstractClass {
myAbstractProperty: string;

protected myAbstractMethod(): void { }

get myAbstractAccessor(): number
{
return 1;
}
set myAbstractAccessor(value: number)
{

}
}
Loading