Skip to content

Commit 8626036

Browse files
ark120202Perryvw
authored andcommitted
Small improvements (#656)
* Remove duplication in decorator name finding * Make TableExpression.fields not optional * Remove unsound cast * Remove use of escapedText and __String * Remove getIdentifierText * Remove function assignment validation in type assertion * Make LuaLib.loadFeatures a regular function * Remove TODOs for old TS upgrades * Use globalThis instead of _G * Remove TS prefix from 4-underscore-prefixed identifiers * Revert "Remove function assignment validation in type assertion" This reverts commit f17da36. * Add line break before constructor * Replace __TSTL_ with 4 underscores * Remove outdated `Building & Tests` group from readme
1 parent d4f4f52 commit 8626036

File tree

13 files changed

+189
-270
lines changed

13 files changed

+189
-270
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ The real power of this transpiler is usage together with good declarations for t
6565
- [Defold Game Engine Scripting](https://github.com/dasannikov/DefoldTypeScript/blob/master/defold.d.ts)
6666
- [LÖVE 2D Game Development](https://github.com/hazzard993/love-typescript-definitions)
6767

68-
## Building & Tests
69-
70-
`npm run build` to build the project.
71-
72-
`npm run test` to run tests.
73-
74-
`npm run test-threaded` runs test in parallel, faster but less detailed output.
75-
76-
`npm run coverage` or `npm run coverage-html` to generate a coverage report.
77-
7868
## Sublime Text integration
7969

8070
This compiler works great in combination with the [Sublime Text Typescript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin) (available through the package manager as `TypeScript`).

build_lualib.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
44
import * as tstl from "./src";
5-
import { LuaLib } from "./src/LuaLib";
5+
import { loadLuaLibFeatures } from "./src/LuaLib";
66

77
const configFileName = path.resolve(__dirname, "src/lualib/tsconfig.json");
88
const { emitResult, diagnostics } = tstl.transpileProject(configFileName);
@@ -16,8 +16,4 @@ if (fs.existsSync(bundlePath)) {
1616
fs.unlinkSync(bundlePath);
1717
}
1818

19-
const emitHost = {
20-
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
21-
writeFile: fs.writeFileSync,
22-
};
23-
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature), emitHost));
19+
fs.writeFileSync(bundlePath, loadLuaLibFeatures(Object.values(tstl.LuaLibFeature), ts.sys));

src/Decorator.ts

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,20 @@ export class Decorator {
44
}
55

66
public static getDecoratorKind(decoratorKindString: string): DecoratorKind | undefined {
7-
switch (decoratorKindString.toLowerCase()) {
8-
case "extension":
9-
return DecoratorKind.Extension;
10-
case "metaextension":
11-
return DecoratorKind.MetaExtension;
12-
case "customconstructor":
13-
return DecoratorKind.CustomConstructor;
14-
case "compilemembersonly":
15-
return DecoratorKind.CompileMembersOnly;
16-
case "noresolution":
17-
return DecoratorKind.NoResolution;
18-
case "pureabstract":
19-
return DecoratorKind.PureAbstract;
20-
case "phantom":
21-
return DecoratorKind.Phantom;
22-
case "tuplereturn":
23-
return DecoratorKind.TupleReturn;
24-
case "luaiterator":
25-
return DecoratorKind.LuaIterator;
26-
case "luatable":
27-
return DecoratorKind.LuaTable;
28-
case "noself":
29-
return DecoratorKind.NoSelf;
30-
case "noselfinfile":
31-
return DecoratorKind.NoSelfInFile;
32-
case "vararg":
33-
return DecoratorKind.Vararg;
34-
case "forrange":
35-
return DecoratorKind.ForRange;
36-
}
37-
38-
return undefined;
7+
return Object.values(DecoratorKind).find(
8+
decoratorKind => decoratorKind.toLowerCase() === decoratorKindString.toLowerCase()
9+
);
3910
}
4011

4112
public kind: DecoratorKind;
42-
public args: string[];
4313

44-
constructor(name: string, args: string[]) {
14+
constructor(name: string, public args: string[]) {
4515
const kind = Decorator.getDecoratorKind(name);
4616
if (kind === undefined) {
4717
throw new Error(`Failed to parse decorator '${name}'`);
4818
}
4919

5020
this.kind = kind;
51-
this.args = args;
5221
}
5322
}
5423

src/LuaAST.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ export function isStringLiteral(node: Node): node is StringLiteral {
614614
return node.kind === SyntaxKind.StringLiteral;
615615
}
616616

617-
export function createStringLiteral(value: string | ts.__String, tsOriginal?: ts.Node, parent?: Node): StringLiteral {
617+
export function createStringLiteral(value: string, tsOriginal?: ts.Node, parent?: Node): StringLiteral {
618618
const expression = createNode(SyntaxKind.StringLiteral, tsOriginal, parent) as StringLiteral;
619619
expression.value = value as string;
620620
return expression;
@@ -687,15 +687,15 @@ export function createTableFieldExpression(
687687

688688
export interface TableExpression extends Expression {
689689
kind: SyntaxKind.TableExpression;
690-
fields?: TableFieldExpression[];
690+
fields: TableFieldExpression[];
691691
}
692692

693693
export function isTableExpression(node: Node): node is TableExpression {
694694
return node.kind === SyntaxKind.TableExpression;
695695
}
696696

697697
export function createTableExpression(
698-
fields?: TableFieldExpression[],
698+
fields: TableFieldExpression[] = [],
699699
tsOriginal?: ts.Node,
700700
parent?: Node
701701
): TableExpression {
@@ -839,14 +839,14 @@ export function isIdentifier(node: Node): node is Identifier {
839839
}
840840

841841
export function createIdentifier(
842-
text: string | ts.__String,
842+
text: string,
843843
tsOriginal?: ts.Node,
844844
symbolId?: SymbolId,
845845
originalName?: string,
846846
parent?: Node
847847
): Identifier {
848848
const expression = createNode(SyntaxKind.Identifier, tsOriginal, parent) as Identifier;
849-
expression.text = text as string;
849+
expression.text = text;
850850
expression.symbolId = symbolId;
851851
expression.originalName = originalName;
852852
return expression;

src/LuaLib.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,32 @@ const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
7272
SymbolRegistry: [LuaLibFeature.Symbol],
7373
};
7474

75-
export class LuaLib {
76-
public static loadFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
77-
let result = "";
75+
export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
76+
let result = "";
7877

79-
const loadedFeatures = new Set<LuaLibFeature>();
78+
const loadedFeatures = new Set<LuaLibFeature>();
8079

81-
function load(feature: LuaLibFeature): void {
82-
if (!loadedFeatures.has(feature)) {
83-
loadedFeatures.add(feature);
84-
const dependencies = luaLibDependencies[feature];
85-
if (dependencies) {
86-
dependencies.forEach(load);
87-
}
88-
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
89-
const luaLibFeature = emitHost.readFile(featureFile);
90-
if (luaLibFeature !== undefined) {
91-
result += luaLibFeature.toString() + "\n";
92-
} else {
93-
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
94-
}
95-
}
80+
function load(feature: LuaLibFeature): void {
81+
if (loadedFeatures.has(feature)) return;
82+
loadedFeatures.add(feature);
83+
84+
const dependencies = luaLibDependencies[feature];
85+
if (dependencies) {
86+
dependencies.forEach(load);
9687
}
9788

98-
for (const feature of features) {
99-
load(feature);
89+
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
90+
const luaLibFeature = emitHost.readFile(featureFile);
91+
if (luaLibFeature !== undefined) {
92+
result += luaLibFeature + "\n";
93+
} else {
94+
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
10095
}
101-
return result;
10296
}
97+
98+
for (const feature of features) {
99+
load(feature);
100+
}
101+
102+
return result;
103103
}

src/LuaPrinter.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
33
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
44
import * as tstl from "./LuaAST";
55
import { luaKeywords } from "./LuaKeywords";
6-
import { LuaLib, LuaLibFeature } from "./LuaLib";
7-
import * as tsHelper from "./TSHelper";
6+
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
87
import { EmitHost } from "./Transpile";
8+
import * as tsHelper from "./TSHelper";
99

1010
type SourceChunk = string | SourceNode;
1111

@@ -124,7 +124,7 @@ export class LuaPrinter {
124124
// Inline lualib features
125125
else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
126126
header += "-- Lua Library inline imports\n";
127-
header += LuaLib.loadFeatures(luaLibFeatures, this.emitHost);
127+
header += loadLuaLibFeatures(luaLibFeatures, this.emitHost);
128128
}
129129
}
130130

@@ -563,17 +563,7 @@ export class LuaPrinter {
563563
}
564564

565565
public printTableExpression(expression: tstl.TableExpression): SourceNode {
566-
const chunks: SourceChunk[] = [];
567-
568-
chunks.push("{");
569-
570-
if (expression.fields) {
571-
chunks.push(...this.printExpressionList(expression.fields));
572-
}
573-
574-
chunks.push("}");
575-
576-
return this.createSourceNode(expression, chunks);
566+
return this.createSourceNode(expression, ["{", ...this.printExpressionList(expression.fields), "}"]);
577567
}
578568

579569
public printUnaryExpression(expression: tstl.UnaryExpression): SourceNode {

0 commit comments

Comments
 (0)