Skip to content

Commit 06b0c90

Browse files
committed
Fixed strict issues everywhere except transformer
1 parent 683e43a commit 06b0c90

File tree

10 files changed

+230
-150
lines changed

10 files changed

+230
-150
lines changed

src/CommandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function parseTsConfigString(
228228
}
229229

230230
function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CLIParseResult {
231-
const result = {};
231+
const result: { [key: string]: string | boolean } = {};
232232
for (let i = 0; i < args.length; i++) {
233233
if (args[i].startsWith("--")) {
234234
const argumentName = args[i].substr(2);
@@ -245,15 +245,15 @@ function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CL
245245
}
246246
} else if (args[i].startsWith("-")) {
247247
const argument = args[i].substr(1);
248-
let argumentName: string;
248+
let argumentName: string | undefined;
249249
for (const key in optionDeclarations) {
250250
if (optionDeclarations[key].aliases && optionDeclarations[key].aliases.indexOf(argument) >= 0) {
251251
argumentName = key;
252252
break;
253253
}
254254
}
255255

256-
if (argumentName) {
256+
if (argumentName !== undefined) {
257257
const argumentResult = getArgumentValue(argumentName, i, args);
258258
if (argumentResult.isValid === true) {
259259
result[argumentName] = argumentResult.result;

src/Compiler.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ export function compile(argv: string[]): void {
3333

3434
/* istanbul ignore next: tested in test/compiler/watchmode.spec with subproccess */
3535
export function watchWithOptions(fileNames: string[], options: CompilerOptions): void {
36-
let host: ts.WatchCompilerHost<ts.SemanticDiagnosticsBuilderProgram>;
37-
let config = false;
38-
if (options.project) {
39-
config = true;
40-
host = ts.createWatchCompilerHost(options.project, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
41-
} else {
42-
host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
43-
}
36+
const host = options.project !== undefined
37+
? ts.createWatchCompilerHost(options.project, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram)
38+
: ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
4439

4540
let fullRecompile = true;
4641
host.afterProgramCreate = program => {
@@ -71,7 +66,7 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions):
7166
}
7267

7368
const errorDiagnostic: ts.Diagnostic = {
74-
category: undefined,
69+
category: ts.DiagnosticCategory.Error,
7570
code: 6194,
7671
file: undefined,
7772
length: 0,
@@ -82,10 +77,13 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions):
8277
errorDiagnostic.messageText = "Found Errors. Watching for file changes.";
8378
errorDiagnostic.code = 6193;
8479
}
85-
host.onWatchStatusChange(errorDiagnostic, host.getNewLine(), program.getCompilerOptions());
80+
81+
if (host.onWatchStatusChange) {
82+
host.onWatchStatusChange(errorDiagnostic, host.getNewLine(), program.getCompilerOptions());
83+
}
8684
};
8785

88-
if (config) {
86+
if (options.project !== undefined) {
8987
ts.createWatchProgram(
9088
host as ts.WatchCompilerHostOfConfigFile<ts.SemanticDiagnosticsBuilderProgram>
9189
);
@@ -118,8 +116,8 @@ export function createStringCompilerProgram(
118116
): ts.Program {
119117
const compilerHost = {
120118
directoryExists: () => true,
121-
fileExists: (fileName): boolean => true,
122-
getCanonicalFileName: fileName => fileName,
119+
fileExists: () => true,
120+
getCanonicalFileName: (fileName: string) => fileName,
123121
getCurrentDirectory: () => "",
124122
getDefaultLibFileName: ts.getDefaultLibFileName,
125123
getDirectories: () => [],
@@ -156,7 +154,7 @@ export function createStringCompilerProgram(
156154

157155
useCaseSensitiveFileNames: () => false,
158156
// Don't write output
159-
writeFile: (name, text, writeByteOrderMark) => undefined,
157+
writeFile: () => undefined,
160158
};
161159
const filePaths = typeof input === "string" ? [filePath] : Object.keys(input);
162160
return ts.createProgram(filePaths, options, compilerHost);
@@ -182,5 +180,10 @@ export function transpileString(
182180

183181
const transpiler = new LuaTranspiler(program);
184182

185-
return transpiler.transpileSourceFile(program.getSourceFile(filePath));
183+
const sourceFile = program.getSourceFile(filePath);
184+
if (sourceFile !== undefined) {
185+
return transpiler.transpileSourceFile(sourceFile);
186+
} else {
187+
throw new Error(`Could not find file ${filePath} in created program.`);
188+
}
186189
}

src/Decorator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class Decorator {
33
return this.getDecoratorKind(decoratorKindString) !== undefined;
44
}
55

6-
public static getDecoratorKind(decoratorKindString: string): DecoratorKind {
6+
public static getDecoratorKind(decoratorKindString: string): DecoratorKind | undefined {
77
switch (decoratorKindString.toLowerCase()) {
88
case "extension":
99
return DecoratorKind.Extension;
@@ -38,7 +38,12 @@ export class Decorator {
3838
public args: string[];
3939

4040
constructor(name: string, args: string[]) {
41-
this.kind = Decorator.getDecoratorKind(name);
41+
const kind = Decorator.getDecoratorKind(name);
42+
if (kind === undefined) {
43+
throw new Error(`Failed to parse decorator '${name}'`);
44+
}
45+
46+
this.kind = kind;
4247
this.args = args;
4348
}
4449
}

src/LuaAST.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export interface Node extends TextRange {
109109
}
110110

111111
export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node, parent?: Node): Node {
112+
if (tsOriginal === undefined) {
113+
return {kind, parent};
114+
}
115+
112116
const sourcePosition = getSourcePosition(tsOriginal);
113117
if (sourcePosition) {
114118
return {kind, parent, line: sourcePosition.line, column: sourcePosition.column};
@@ -172,14 +176,14 @@ export function getOriginalPos(node: Node): TextRange {
172176

173177
export interface Block extends Node {
174178
kind: SyntaxKind.Block;
175-
statements?: Statement[];
179+
statements: Statement[];
176180
}
177181

178182
export function isBlock(node: Node): node is Block {
179183
return node.kind === SyntaxKind.Block;
180184
}
181185

182-
export function createBlock(statements?: Statement[], tsOriginal?: ts.Node, parent?: Node): Block {
186+
export function createBlock(statements: Statement[], tsOriginal?: ts.Node, parent?: Node): Block {
183187
const block = createNode(SyntaxKind.Block, tsOriginal, parent) as Block;
184188
setParent(statements, block);
185189
block.statements = statements;
@@ -192,14 +196,14 @@ export interface Statement extends Node {
192196

193197
export interface DoStatement extends Statement {
194198
kind: SyntaxKind.DoStatement;
195-
statements?: Statement[];
199+
statements: Statement[];
196200
}
197201

198202
export function isDoStatement(node: Node): node is DoStatement {
199203
return node.kind === SyntaxKind.DoStatement;
200204
}
201205

202-
export function createDoStatement(statements?: Statement[], tsOriginal?: ts.Node, parent?: Node): DoStatement {
206+
export function createDoStatement(statements: Statement[], tsOriginal?: ts.Node, parent?: Node): DoStatement {
203207
const statement = createNode(SyntaxKind.DoStatement, tsOriginal, parent) as DoStatement;
204208
setParent(statements, statement);
205209
statement.statements = statements;
@@ -257,7 +261,7 @@ export function isAssignmentStatement(node: Node): node is AssignmentStatement {
257261

258262
export function createAssignmentStatement(
259263
left: IdentifierOrTableIndexExpression | IdentifierOrTableIndexExpression[],
260-
right: Expression | Expression[],
264+
right?: Expression | Expression[],
261265
tsOriginal?: ts.Node,
262266
parent?: Node
263267
): AssignmentStatement
@@ -273,7 +277,7 @@ export function createAssignmentStatement(
273277
if (Array.isArray(right)) {
274278
statement.right = right;
275279
} else {
276-
statement.right = [right];
280+
statement.right = right ? [right] : [];
277281
}
278282
return statement;
279283
}
@@ -878,7 +882,7 @@ export function isFunctionDefinition(statement: VariableDeclarationStatement | A
878882
: statement is FunctionDefinition
879883
{
880884
return statement.left.length === 1
881-
&& statement.right
885+
&& statement.right !== undefined
882886
&& statement.right.length === 1
883887
&& isFunctionExpression(statement.right[0]);
884888
}
@@ -888,7 +892,7 @@ export type InlineFunctionExpression = FunctionExpression & {
888892
};
889893

890894
export function isInlineFunctionExpression(expression: FunctionExpression) : expression is InlineFunctionExpression {
891-
return expression.body.statements
895+
return expression.body.statements !== undefined
892896
&& expression.body.statements.length === 1
893897
&& isReturnStatement(expression.body.statements[0])
894898
&& (expression.flags & FunctionExpressionFlags.Inline) !== 0;

src/LuaLib.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ export class LuaLib {
6666
function load(feature: LuaLibFeature): void {
6767
if (!loadedFeatures.has(feature)) {
6868
loadedFeatures.add(feature);
69-
if (luaLibDependencies[feature]) {
70-
luaLibDependencies[feature].forEach(load);
69+
const dependencies = luaLibDependencies[feature];
70+
if (dependencies) {
71+
dependencies.forEach(load);
7172
}
7273
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
7374
result += fs.readFileSync(featureFile).toString() + "\n";

src/LuaPrinter.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ export class LuaPrinter {
4343
private options: CompilerOptions;
4444
private currentIndent: string;
4545

46-
private sourceFile: string;
46+
private sourceFile = "";
4747

4848
public constructor(options: CompilerOptions) {
4949
this.options = options;
5050
this.currentIndent = "";
5151
}
5252

53-
public print(block: tstl.Block, luaLibFeatures?: Set<LuaLibFeature>, sourceFile?: string): [string, string] {
53+
public print(block: tstl.Block, luaLibFeatures?: Set<LuaLibFeature>, sourceFile = ""): [string, string] {
5454
// Add traceback lualib if sourcemap traceback option is enabled
5555
if (this.options.sourceMapTraceback) {
5656
if (luaLibFeatures === undefined) {
@@ -113,7 +113,7 @@ export class LuaPrinter {
113113
private printImplementation(
114114
block: tstl.Block,
115115
luaLibFeatures?: Set<LuaLibFeature>,
116-
sourceFile?: string): SourceNode {
116+
sourceFile = ""): SourceNode {
117117

118118
let header = "";
119119

@@ -162,13 +162,15 @@ export class LuaPrinter {
162162
private createSourceNode(node: tstl.Node, chunks: SourceChunk | SourceChunk[]): SourceNode {
163163
const originalPos = tstl.getOriginalPos(node);
164164

165-
return originalPos !== undefined
165+
return originalPos !== undefined && originalPos.line !== undefined && originalPos.column !== undefined
166166
? new SourceNode(originalPos.line + 1, originalPos.column, this.sourceFile, chunks)
167-
: new SourceNode(undefined, undefined, this.sourceFile, chunks);
167+
// tslint:disable-next-line:no-null-keyword
168+
: new SourceNode(null, null, this.sourceFile, chunks);
168169
}
169170

170171
private concatNodes(...chunks: SourceChunk[]): SourceNode {
171-
return new SourceNode(undefined, undefined, this.sourceFile, chunks);
172+
// tslint:disable-next-line:no-null-keyword
173+
return new SourceNode(null, null, this.sourceFile, chunks);
172174
}
173175

174176
private printBlock(block: tstl.Block): SourceNode {
@@ -515,7 +517,7 @@ export class LuaPrinter {
515517
chunks.push(" ");
516518
const returnNode: SourceChunk[] = [
517519
"return ",
518-
...this.joinChunks(", ", returnStatement.expressions.map(e => this.printExpression(e))),
520+
...this.joinChunks(", ", returnStatement.expressions!.map(e => this.printExpression(e))),
519521
];
520522
chunks.push(this.createSourceNode(returnStatement, returnNode));
521523
chunks.push(" end");
@@ -616,19 +618,26 @@ export class LuaPrinter {
616618

617619
private printCallExpression(expression: tstl.CallExpression): SourceNode {
618620
const chunks = [];
619-
const parameterChunks = this.joinChunks(", ", expression.params.map(e => this.printExpression(e)));
620621

621-
chunks.push(this.printExpression(expression.expression), "(", ...parameterChunks, ")");
622+
const parameterChunks = expression.params !== undefined
623+
? expression.params.map(e => this.printExpression(e))
624+
: [];
625+
626+
chunks.push(this.printExpression(expression.expression), "(", ...this.joinChunks(", ", parameterChunks), ")");
622627

623628
return this.concatNodes(...chunks);
624629
}
625630

626631
private printMethodCallExpression(expression: tstl.MethodCallExpression): SourceNode {
627632
const prefix = this.printExpression(expression.prefixExpression);
628-
const parameterChunks = this.joinChunks(", ", expression.params.map(e => this.printExpression(e)));
633+
634+
const parameterChunks = expression.params !== undefined
635+
? expression.params.map(e => this.printExpression(e))
636+
: [];
637+
629638
const name = this.printIdentifier(expression.name);
630639

631-
return this.concatNodes(prefix, ":", name, "(", ...parameterChunks, ")");
640+
return this.concatNodes(prefix, ":", name, "(", ...this.joinChunks(", ", parameterChunks), ")");
632641
}
633642

634643
private printIdentifier(expression: tstl.Identifier): SourceNode {

0 commit comments

Comments
 (0)