Skip to content

Commit

Permalink
feat: Add detecting read-only properties (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unnvaldr committed Apr 20, 2020
1 parent 8c2b698 commit 91644a9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/lib/converter/factories/declaration.ts
Expand Up @@ -179,6 +179,7 @@ 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.Readonly, !!(modifiers & ts.ModifierFlags.Readonly));

if (
context.isInherit &&
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/nodes/variable.ts
Expand Up @@ -85,8 +85,8 @@ export class VariableConverter extends ConverterNodeComponent<ts.VariableDeclara
break;
case ReflectionKind.Property:
if (node.modifiers
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword )) {
variable.setFlag(ReflectionFlag.Abstract, true);
&& node.modifiers.some( m => m.kind === ts.SyntaxKind.AbstractKeyword)) {
variable.setFlag(ReflectionFlag.Abstract, true);
}
break;
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/models/reflections/abstract.ts
Expand Up @@ -86,7 +86,8 @@ export enum ReflectionFlag {
ConstructorProperty = 1024,
Abstract = 2048,
Const = 4096,
Let = 8192
Let = 8192,
Readonly = 16384
}

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

/**
Expand Down Expand Up @@ -201,6 +203,10 @@ export class ReflectionFlags extends Array<string> {
return this.hasFlag(ReflectionFlag.Let);
}

get isReadonly() {
return this.hasFlag(ReflectionFlag.Readonly);
}

setFlag(flag: ReflectionFlag, set: boolean) {
switch (flag) {
case ReflectionFlag.Private:
Expand Down
6 changes: 4 additions & 2 deletions src/test/converter/class/specs-with-lump-categories.json
Expand Up @@ -1629,7 +1629,8 @@
"kind": 1024,
"kindString": "Property",
"flags": {
"isConstructorProperty": true
"isConstructorProperty": true,
"isReadonly": true
},
"comment": {
"shortText": "Vector name\n"
Expand Down Expand Up @@ -1807,7 +1808,8 @@
"kind": 1024,
"kindString": "Property",
"flags": {
"isConstructorProperty": true
"isConstructorProperty": true,
"isReadonly": true
},
"comment": {
"shortText": "Vector name\n"
Expand Down
6 changes: 4 additions & 2 deletions src/test/converter/class/specs.json
Expand Up @@ -1625,7 +1625,8 @@
"kind": 1024,
"kindString": "Property",
"flags": {
"isConstructorProperty": true
"isConstructorProperty": true,
"isReadonly": true
},
"comment": {
"shortText": "Vector name\n"
Expand Down Expand Up @@ -1803,7 +1804,8 @@
"kind": 1024,
"kindString": "Property",
"flags": {
"isConstructorProperty": true
"isConstructorProperty": true,
"isReadonly": true
},
"comment": {
"shortText": "Vector name\n"
Expand Down
Expand Up @@ -237,7 +237,7 @@ <h3><span class="tsd-flag ts-flagPrivate">Private</span> p4</h3>
</section>
<section class="tsd-panel tsd-member tsd-kind-property tsd-parent-kind-class">
<a name="p5" class="tsd-anchor"></a>
<h3>p5</h3>
<h3><span class="tsd-flag ts-flagReadonly">Readonly</span> p5</h3>
<div class="tsd-signature tsd-kind-icon">p5<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
</aside>
Expand Down
Expand Up @@ -214,7 +214,7 @@ <h3>p3</h3>
</section>
<section class="tsd-panel tsd-member tsd-kind-property tsd-parent-kind-class tsd-is-inherited">
<a name="p5" class="tsd-anchor"></a>
<h3>p5</h3>
<h3><span class="tsd-flag ts-flagReadonly">Readonly</span> p5</h3>
<div class="tsd-signature tsd-kind-icon">p5<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div>
<aside class="tsd-sources">
<p>Inherited from <a href="_classes_.genericclass.html">GenericClass</a>.<a href="_classes_.genericclass.html#p5">p5</a></p>
Expand Down

0 comments on commit 91644a9

Please sign in to comment.