Skip to content

Commit 8aafab2

Browse files
committed
Simplify some code
1 parent 0c18503 commit 8aafab2

3 files changed

Lines changed: 20 additions & 38 deletions

File tree

src/transformation/transformers/return.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
3-
import { FunctionVisitor, TransformationContext } from "../context";
3+
import { FunctionVisitor } from "../context";
44
import { isInTupleReturnFunction, isTupleReturnCall } from "../utils/annotations";
55
import { validateAssignment } from "../utils/assignment-validation";
66
import { createUnpackCall, wrapInTable } from "../utils/lua-ast";
77
import { ScopeType, walkScopesUp } from "../utils/scope";
8-
import { findFirstNodeAbove, isArrayType } from "../utils/typescript";
9-
10-
function getContainingFunctionReturnType(context: TransformationContext, node: ts.Node): ts.Type | undefined {
11-
const declaration = findFirstNodeAbove(node, ts.isFunctionLike);
12-
if (declaration) {
13-
const signature = context.checker.getSignatureFromDeclaration(declaration);
14-
if (signature) {
15-
return context.checker.getReturnTypeOfSignature(signature);
16-
}
17-
}
18-
}
8+
import { isArrayType } from "../utils/typescript";
199

2010
export const transformReturnStatement: FunctionVisitor<ts.ReturnStatement> = (statement, context) => {
2111
// Bubble up explicit return flag and check if we're inside a try/catch block
@@ -34,7 +24,7 @@ export const transformReturnStatement: FunctionVisitor<ts.ReturnStatement> = (st
3424

3525
if (statement.expression) {
3626
const expressionType = context.checker.getTypeAtLocation(statement.expression);
37-
const returnType = getContainingFunctionReturnType(context, statement);
27+
const returnType = context.checker.getContextualType(statement.expression);
3828
if (returnType) {
3929
validateAssignment(context, statement, expressionType, returnType);
4030
}

src/transformation/utils/annotations/collect.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as ts from "typescript";
22
import { flatMap } from "../../../utils";
33
import { TransformationContext } from "../../context";
44

5-
export type AnnotationName = string & { _annotationNameBrand: any };
65
export enum AnnotationKind {
76
Extension = "Extension",
87
MetaExtension = "MetaExtension",
@@ -20,21 +19,15 @@ export enum AnnotationKind {
2019
ForRange = "ForRange",
2120
}
2221

23-
function getAnnotationKindByName(name: AnnotationName): AnnotationKind;
24-
function getAnnotationKindByName(name: string): AnnotationKind | undefined;
25-
function getAnnotationKindByName(name: string): AnnotationKind | undefined {
26-
return Object.values(AnnotationKind).find(k => k.toLowerCase() === name.toLowerCase());
22+
export interface Annotation {
23+
kind: AnnotationKind;
24+
args: string[];
2725
}
2826

29-
function isValidAnnotationName(name: string): name is AnnotationName {
30-
return getAnnotationKindByName(name) !== undefined;
31-
}
32-
33-
export class Annotation {
34-
public kind: AnnotationKind;
35-
36-
constructor(name: AnnotationName, public args: string[]) {
37-
this.kind = getAnnotationKindByName(name);
27+
function createAnnotation(name: string, args: string[]): Annotation | undefined {
28+
const annotationKind = Object.values(AnnotationKind).find(k => k.toLowerCase() === name.toLowerCase());
29+
if (annotationKind !== undefined) {
30+
return { kind: annotationKind, args };
3831
}
3932
}
4033

@@ -55,8 +48,8 @@ function collectAnnotations(
5548

5649
for (const line of oldStyleAnnotations) {
5750
const [name, ...args] = line.slice(1).split(" ");
58-
if (isValidAnnotationName(name)) {
59-
const annotation = new Annotation(name, args);
51+
const annotation = createAnnotation(name, args);
52+
if (annotation) {
6053
annotationsMap.set(annotation.kind, annotation);
6154
console.warn(`[Deprecated] Annotations with ! are being deprecated, use '@${annotation.kind}' instead`);
6255
} else {
@@ -65,8 +58,8 @@ function collectAnnotations(
6558
}
6659

6760
for (const tag of source.getJsDocTags()) {
68-
if (isValidAnnotationName(tag.name)) {
69-
const annotation = new Annotation(tag.name, tag.text ? tag.text.split(" ") : []);
61+
const annotation = createAnnotation(tag.name, tag.text ? tag.text.split(" ") : []);
62+
if (annotation) {
7063
annotationsMap.set(annotation.kind, annotation);
7164
}
7265
}
@@ -92,8 +85,8 @@ export function getNodeAnnotations(node: ts.Node): AnnotationsMap {
9285

9386
for (const tag of ts.getJSDocTags(node)) {
9487
const tagName = tag.tagName.text;
95-
if (isValidAnnotationName(tagName)) {
96-
const annotation = new Annotation(tagName, tag.comment ? tag.comment.split(" ") : []);
88+
const annotation = createAnnotation(tagName, tag.comment ? tag.comment.split(" ") : []);
89+
if (annotation) {
9790
annotationsMap.set(annotation.kind, annotation);
9891
}
9992
}
@@ -110,9 +103,9 @@ export function getFileAnnotations(sourceFile: ts.SourceFile): AnnotationsMap {
110103
if (jsDoc) {
111104
for (const tag of flatMap(jsDoc, x => x.tags || [])) {
112105
const tagName = tag.tagName.text;
113-
if (isValidAnnotationName(tagName)) {
114-
const dec = new Annotation(tagName, tag.comment ? tag.comment.split(" ") : []);
115-
annotationsMap.set(dec.kind, dec);
106+
const annotation = createAnnotation(tagName, tag.comment ? tag.comment.split(" ") : []);
107+
if (annotation) {
108+
annotationsMap.set(annotation.kind, annotation);
116109
}
117110
}
118111
}

src/transformation/utils/lualib.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import { LuaLibFeature } from "../../LuaLib";
44
import { getOrUpdate } from "../../utils";
55
import { TransformationContext } from "../context";
66

7-
const luaLibFeatures = new WeakMap<TransformationContext, Set<LuaLibFeature>>();
8-
97
export { LuaLibFeature };
108

9+
const luaLibFeatures = new WeakMap<TransformationContext, Set<LuaLibFeature>>();
1110
export function getUsedLuaLibFeatures(context: TransformationContext): Set<LuaLibFeature> {
1211
return getOrUpdate(luaLibFeatures, context, () => new Set());
1312
}

0 commit comments

Comments
 (0)