Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cbf05eb
Load custom transformers from tsconfig
ark120202 May 2, 2019
1c68f73
Rename diagnostics imported namespace to diagnosticFactories
ark120202 May 3, 2019
1d4f2b0
Tests
ark120202 May 5, 2019
1d72e24
Add diagnostic when transformer could not be resolved
ark120202 May 5, 2019
de282fe
Rename "transform" to "name"
ark120202 May 5, 2019
f5209db
Validate name and when transformer options
ark120202 May 5, 2019
2f46cfc
Merge remote-tracking branch 'upstream/master' into load-custom-trans…
ark120202 May 5, 2019
8e2135a
Resolve ts-node from module location instead of base dir
ark120202 May 6, 2019
a353f22
Make .js files have higher priority during transformer resolution
ark120202 May 6, 2019
d6750e1
Rename variable
ark120202 May 10, 2019
cc144bd
Add ts-node as an optional peer dependency
ark120202 May 13, 2019
f04ac39
Merge remote-tracking branch 'upstream/master' into load-custom-trans…
ark120202 May 19, 2019
6da44e9
Add compatibility with ttypescript and support more transformer types
ark120202 May 25, 2019
a4c9c9a
Merge remote-tracking branch 'upstream/master' into load-custom-trans…
ark120202 May 25, 2019
a1a68e1
Remove old option tests
ark120202 May 25, 2019
1a14df9
Use original source file node in getEmitResolver
ark120202 May 25, 2019
c6edc48
Merge remote-tracking branch 'upstream/master' into load-custom-trans…
ark120202 May 27, 2019
ccc28e3
Move transformer loading code to a separate file
ark120202 Jun 3, 2019
2b1a6f0
Check for "transform" property existence instead of "name" non-existence
ark120202 Jun 3, 2019
5f10ec5
Merge remote-tracking branch 'upstream/master' into load-custom-trans…
ark120202 Jun 3, 2019
e0cf202
Remove ts-node optional peer dependency
ark120202 Jun 3, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
"node": ">=8.5.0"
},
"dependencies": {
"resolve": "^1.10.1",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid runtime deps if possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's necessary in this case. Module resolution algorithm is complex and TS implementation isn't exposed and limited to types only. It's quite small, has only one sub-dependency and pretty much standard for customized module resolution, so IMO it's okay to add it. We'll need it for module support as well.

"source-map": "^0.7.3",
"typescript": "^3.4.5"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.12",
"@types/node": "^11.13.0",
"@types/resolve": "0.0.8",
"fengari": "^0.1.4",
"jest": "^24.8.0",
"jest-circus": "^24.8.0",
Expand Down
35 changes: 20 additions & 15 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
import * as diagnostics from "./diagnostics";
import * as diagnosticFactories from "./diagnostics";

export interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
Expand All @@ -10,7 +10,7 @@ export interface ParsedCommandLine extends ts.ParsedCommandLine {
interface CommandLineOptionBase {
name: string;
aliases?: string[];
describe: string;
description: string;
}

interface CommandLineOptionOfEnum extends CommandLineOptionBase {
Expand All @@ -23,33 +23,34 @@ interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
}

type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean;

const optionDeclarations: CommandLineOption[] = [
{
name: "luaLibImport",
describe: "Specifies how js standard features missing in lua are imported.",
description: "Specifies how js standard features missing in lua are imported.",
type: "enum",
choices: Object.values(LuaLibImportKind),
},
{
name: "luaTarget",
aliases: ["lt"],
describe: "Specify Lua target version.",
description: "Specify Lua target version.",
type: "enum",
choices: Object.values(LuaTarget),
},
{
name: "noHeader",
describe: "Specify if a header will be added to compiled files.",
description: "Specify if a header will be added to compiled files.",
type: "boolean",
},
{
name: "noHoisting",
describe: "Disables hoisting.",
description: "Disables hoisting.",
type: "boolean",
},
{
name: "sourceMapTraceback",
describe: "Applies the source map to show source TS files and lines in error tracebacks.",
description: "Applies the source map to show source TS files and lines in error tracebacks.",
type: "boolean",
},
];
Expand Down Expand Up @@ -78,7 +79,7 @@ export function getHelpString(): string {
const valuesHint = option.type === "enum" ? option.choices.join("|") : option.type;
const spacing = " ".repeat(Math.max(1, 45 - optionString.length - valuesHint.length));

result += `\n ${optionString} <${valuesHint}>${spacing}${option.describe}\n`;
result += `\n ${optionString} <${valuesHint}>${spacing}${option.description}\n`;
}

return result;
Expand All @@ -97,13 +98,15 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine):

if (parsedConfigFile.raw.tstl) {
if (hasRootLevelOptions) {
parsedConfigFile.errors.push(diagnostics.tstlOptionsAreMovingToTheTstlObject(parsedConfigFile.raw.tstl));
parsedConfigFile.errors.push(
diagnosticFactories.tstlOptionsAreMovingToTheTstlObject(parsedConfigFile.raw.tstl)
);
}

for (const key in parsedConfigFile.raw.tstl) {
const option = optionDeclarations.find(option => option.name === key);
if (!option) {
parsedConfigFile.errors.push(diagnostics.unknownCompilerOption(key));
parsedConfigFile.errors.push(diagnosticFactories.unknownCompilerOption(key));
continue;
}

Expand Down Expand Up @@ -167,9 +170,11 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
// Set boolean arguments without supplied value to true
return { value: true, increment: 0 };
}
} else if (value === undefined) {
}

if (value === undefined) {
return {
error: diagnostics.compilerOptionExpectsAnArgument(option.name),
error: diagnosticFactories.compilerOptionExpectsAnArgument(option.name),
value: undefined,
increment: 0,
};
Expand All @@ -191,7 +196,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
if (typeof value !== "boolean") {
return {
value: undefined,
error: diagnostics.compilerOptionRequiresAValueOfType(option.name, "boolean"),
error: diagnosticFactories.compilerOptionRequiresAValueOfType(option.name, "boolean"),
};
}

Expand All @@ -202,7 +207,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
if (typeof value !== "string") {
return {
value: undefined,
error: diagnostics.compilerOptionRequiresAValueOfType(option.name, "string"),
error: diagnosticFactories.compilerOptionRequiresAValueOfType(option.name, "string"),
};
}

Expand All @@ -211,7 +216,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
const optionChoices = option.choices.join(", ");
return {
value: undefined,
error: diagnostics.argumentForOptionMustBe(`--${option.name}`, optionChoices),
error: diagnosticFactories.argumentForOptionMustBe(`--${option.name}`, optionChoices),
};
}

Expand Down
23 changes: 21 additions & 2 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import * as ts from "typescript";

export interface CompilerOptions extends ts.CompilerOptions {
type KnownKeys<T> = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends {
[_ in keyof T]: infer U
}
? U
: never;

type OmitIndexSignature<T extends Record<any, any>> = Pick<T, KnownKeys<T>>;

export interface TransformerImport {
transform: string;
import?: string;
after?: boolean;
afterDeclarations?: boolean;
type?: "program" | "config" | "checker" | "raw" | "compilerOptions";
[option: string]: any;
}

export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
noHeader?: boolean;
luaTarget?: LuaTarget;
luaLibImport?: LuaLibImportKind;
noHoisting?: boolean;
sourceMapTraceback?: boolean;
}
plugins?: Array<ts.PluginImport | TransformerImport>;
[option: string]: ts.CompilerOptions[string] | Array<ts.PluginImport | TransformerImport>;
};

export enum LuaLibImportKind {
None = "none",
Expand Down
7 changes: 6 additions & 1 deletion src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export class LuaTransformer {
this.setupState();

this.currentSourceFile = node;
this.resolver = this.checker.getEmitResolver(node);

// Use `getParseTreeNode` to get original SourceFile node, before it was substituted by custom transformers.
// It's required because otherwise `getEmitResolver` won't use cached diagnostics, produced in `emitWorker`
// and would try to re-analyze the file, which would fail because of replaced nodes.
const originalSourceFile = ts.getParseTreeNode(node, ts.isSourceFile) || node;
this.resolver = this.checker.getEmitResolver(originalSourceFile);

let statements: tstl.Statement[] = [];
if (node.flags & ts.NodeFlags.JsonFile) {
Expand Down
Loading