Skip to content

Commit 0be1311

Browse files
committed
Changed getCustomDecorators return from collection to map
1 parent 86ed142 commit 0be1311

4 files changed

Lines changed: 21 additions & 40 deletions

File tree

src/Decorator.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,6 @@ export class Decorator {
1212
}
1313
}
1414

15-
export class DecoratorCollection {
16-
private decorators: Map<DecoratorKind, Decorator>;
17-
18-
constructor(rawCommentLines: string[]) {
19-
this.decorators = new Map<DecoratorKind, Decorator>();
20-
rawCommentLines.forEach(raw => {
21-
const dec = new Decorator(raw);
22-
this.decorators.set(dec.kind, dec);
23-
});
24-
}
25-
26-
public hasDecorator(kind: DecoratorKind): boolean {
27-
return this.decorators.has(kind);
28-
}
29-
30-
public getDecorator(kind: DecoratorKind): Decorator {
31-
return this.decorators.get(kind);
32-
}
33-
}
34-
3515
export enum DecoratorKind {
3616
Extension = "Extension",
3717
MetaExtension = "MetaExtension",

src/TSHelper.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import { DecoratorCollection, DecoratorKind } from "./Decorator";
2+
import { Decorator, DecoratorKind } from "./Decorator";
33

44
export class TSHelper {
55

@@ -34,7 +34,7 @@ export class TSHelper {
3434
if (clause.token === ts.SyntaxKind.ExtendsKeyword) {
3535
const superType = checker.getTypeAtLocation(clause.types[0]);
3636
const decorators = this.getCustomDecorators(superType, checker);
37-
if (!decorators.hasDecorator(DecoratorKind.PureAbstract)) {
37+
if (!decorators.has(DecoratorKind.PureAbstract)) {
3838
return superType;
3939
}
4040
}
@@ -75,23 +75,28 @@ export class TSHelper {
7575
const type = checker.getTypeAtLocation(node.expression);
7676

7777
return this.getCustomDecorators(type, checker)
78-
.hasDecorator(DecoratorKind.TupleReturn);
78+
.has(DecoratorKind.TupleReturn);
7979
} else {
8080
return false;
8181
}
8282
}
8383

84-
public static getCustomDecorators(type: ts.Type, checker: ts.TypeChecker): DecoratorCollection {
84+
public static getCustomDecorators(type: ts.Type, checker: ts.TypeChecker): Map<DecoratorKind, Decorator> {
8585
if (type.symbol) {
8686
const comments = type.symbol.getDocumentationComment(checker);
8787
const decorators =
8888
comments.filter(comment => comment.kind === "text")
8989
.map(comment => comment.text.trim().split("\n"))
9090
.reduce((a, b) => a.concat(b), [])
9191
.filter(comment => comment[0] === "!");
92-
return new DecoratorCollection(decorators);
92+
const decMap = new Map<DecoratorKind, Decorator>();
93+
decorators.forEach(decStr => {
94+
const dec = new Decorator(decStr);
95+
decMap.set(dec.kind, dec);
96+
});
97+
return decMap;
9398
}
94-
return new DecoratorCollection([]);
99+
return new Map<DecoratorKind, Decorator>();
95100
}
96101

97102
// Search up until finding a node satisfying the callback

src/Transpiler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export abstract class LuaTranspiler {
346346
public transpileNamespace(node: ts.ModuleDeclaration): string {
347347
const decorators = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(node), this.checker);
348348
// If phantom namespace just transpile the body as normal
349-
if (decorators.hasDecorator(DecoratorKind.Phantom) && node.body) {
349+
if (decorators.has(DecoratorKind.Phantom) && node.body) {
350350
return this.transpileNode(node.body);
351351
}
352352

@@ -379,7 +379,7 @@ export abstract class LuaTranspiler {
379379

380380
const type = this.checker.getTypeAtLocation(node);
381381
const membersOnly = tsHelper.getCustomDecorators(type, this.checker)
382-
.hasDecorator(DecoratorKind.CompileMembersOnly);
382+
.has(DecoratorKind.CompileMembersOnly);
383383

384384
if (!membersOnly) {
385385
const name = this.transpileIdentifier(node.name);
@@ -690,7 +690,7 @@ export abstract class LuaTranspiler {
690690
this.checker.getTypeAtLocation(declaration),
691691
this.checker
692692
);
693-
isTupleReturn = decorators.hasDecorator(DecoratorKind.TupleReturn);
693+
isTupleReturn = decorators.has(DecoratorKind.TupleReturn);
694694
}
695695
if (isTupleReturn && ts.isArrayLiteralExpression(node.expression)) {
696696
return "return " + node.expression.elements.map(elem => this.transpileExpression(elem)).join(",");
@@ -1045,8 +1045,8 @@ export abstract class LuaTranspiler {
10451045

10461046
this.checkForLuaLibType(type);
10471047

1048-
if (classDecorators.hasDecorator(DecoratorKind.CustomConstructor)) {
1049-
const customDecorator = classDecorators.getDecorator(DecoratorKind.CustomConstructor);
1048+
if (classDecorators.has(DecoratorKind.CustomConstructor)) {
1049+
const customDecorator = classDecorators.get(DecoratorKind.CustomConstructor);
10501050
if (!customDecorator.args[0]) {
10511051
throw new TranspileError("!CustomConstructor requires one argument", node);
10521052
}
@@ -1268,7 +1268,7 @@ export abstract class LuaTranspiler {
12681268

12691269
const decorators = tsHelper.getCustomDecorators(type, this.checker);
12701270
// Do not output path for member only enums
1271-
if (decorators.hasDecorator(DecoratorKind.CompileMembersOnly)) {
1271+
if (decorators.has(DecoratorKind.CompileMembersOnly)) {
12721272
return property;
12731273
}
12741274

@@ -1561,9 +1561,9 @@ export abstract class LuaTranspiler {
15611561
const decorators = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(node), this.checker);
15621562

15631563
// Find out if this class is extension of existing class
1564-
const isExtension = decorators.hasDecorator(DecoratorKind.Extension);
1564+
const isExtension = decorators.has(DecoratorKind.Extension);
15651565

1566-
const isMetaExtension = decorators.hasDecorator(DecoratorKind.MetaExtension);
1566+
const isMetaExtension = decorators.has(DecoratorKind.MetaExtension);
15671567

15681568
if (isExtension && isMetaExtension) {
15691569
throw new TranspileError(
@@ -1607,7 +1607,7 @@ export abstract class LuaTranspiler {
16071607
}
16081608

16091609
if (isExtension) {
1610-
const extensionNameArg = decorators.getDecorator(DecoratorKind.Extension).args[0];
1610+
const extensionNameArg = decorators.get(DecoratorKind.Extension).args[0];
16111611
if (extensionNameArg) {
16121612
className = extensionNameArg;
16131613
} else if (extendsType) {
@@ -1658,7 +1658,7 @@ export abstract class LuaTranspiler {
16581658
let noClassOr = false;
16591659
if (extendsType) {
16601660
const decorators = tsHelper.getCustomDecorators(extendsType, this.checker);
1661-
noClassOr = decorators.hasDecorator(DecoratorKind.NoClassOr);
1661+
noClassOr = decorators.has(DecoratorKind.NoClassOr);
16621662
}
16631663

16641664
let result = "";

test/unit/decoratorMetaExtension.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ export class DecoratorMetaExtension {
2323
return debug.getregistry()["_LOADED"].test();
2424
`
2525
);
26-
console.log(lua);
27-
console.log("\n");
28-
console.log("\n");
29-
console.log("\n");
3026
const result = util.executeLua(lua);
3127
// Assert
3228
Expect(result).toBe(5);

0 commit comments

Comments
 (0)