Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load custom transformers from tsconfig #552

Merged
merged 21 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
143 changes: 52 additions & 91 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion 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
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
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.3.1"
},
"devDependencies": {
"@types/glob": "^5.0.35",
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/resolve": "0.0.8",
"fengari": "^0.1.2",
"glob": "^7.1.2",
"jest": "^24.5.0",
Expand All @@ -54,6 +56,6 @@
"rimraf": "^2.6.3",
"ts-jest": "^24.0.0",
"ts-node": "^7.0.0",
"tslint": "^5.10.0"
"tslint": "^5.16.0"
}
}
58 changes: 49 additions & 9 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface ParsedCommandLine extends ts.ParsedCommandLine {
interface CommandLineOptionBase {
name: string;
aliases?: string[];
describe: string;
description: string;
isTSConfigOnly?: boolean;
}

interface CommandLineOptionOfEnum extends CommandLineOptionBase {
Expand All @@ -22,36 +23,52 @@ interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
type: "boolean";
}

type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean;
interface CommandLineOptionOfListType extends CommandLineOptionBase {
isTSConfigOnly: true;
type: "list";
}

type CommandLineOption =
| CommandLineOptionOfEnum
| CommandLineOptionOfBoolean
| CommandLineOptionOfListType;

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",
},
{
name: "tsTransformers",
description: "Custom TypeScript transformers.",
isTSConfigOnly: true,
type: "list",
},
];

export const version = `Version ${require("../package.json").version}`;
Expand All @@ -72,13 +89,15 @@ export function getHelpString(): string {

result += "Options:\n";
for (const option of optionDeclarations) {
if (option.isTSConfigOnly) continue;

const aliasStrings = (option.aliases || []).map(a => "-" + a);
const optionString = aliasStrings.concat(["--" + option.name]).join("|");

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 Down Expand Up @@ -165,14 +184,24 @@ interface CommandLineArgument extends ReadValueResult {
}

function readCommandLineArgument(option: CommandLineOption, value: any): CommandLineArgument {
if (option.isTSConfigOnly) {
return {
value: undefined,
error: diagnostics.optionCanOnlyBeSpecifiedInTsconfigJsonFile(option.name),
increment: 0,
};
}

if (option.type === "boolean") {
if (value === "true" || value === "false") {
value = value === "true";
} else {
// 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),
value: undefined,
Expand Down Expand Up @@ -222,6 +251,17 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {

return { value: normalizedValue };
}

case "list": {
if (!Array.isArray(value)) {
return {
value: undefined,
error: diagnostics.compilerOptionRequiresAValueOfType(option.name, "Array"),
};
}

return { value };
}
}
}

Expand Down