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

New emit pipeline and API/CLI refactor #511

Merged
merged 45 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
19d47de
New emit pipeline and API/CLI refactor
ark120202 Apr 6, 2019
e87c8ee
Move high-level API to index file
ark120202 Apr 6, 2019
8b8eaca
Extract multi-file `transpileString` input to `transpileVirtualProgram`
ark120202 Apr 6, 2019
02699cd
Return an results from emitTranspiledFiles instead of a using a callback
ark120202 Apr 6, 2019
de5ec24
Refactor CommandLineParser and further CLI refactor
ark120202 Apr 14, 2019
503cc68
Merge remote-tracking branch 'upstream/master' into new-emit-pipeline
ark120202 Apr 14, 2019
e767750
Fix strict issues
ark120202 Apr 15, 2019
c2b8812
Fix rootDir and outDir defaults always being used
ark120202 Apr 15, 2019
ff9a233
Remove failing test
ark120202 Apr 15, 2019
5ca24e0
Remove command line parser tests that test standard TS parser features
ark120202 Apr 15, 2019
021d940
Use tsconfig source file during parse to get better diagnostics
ark120202 Apr 15, 2019
b99b6b9
Parse command line options more similar to typescript
ark120202 Apr 15, 2019
e738b58
Refactor commandLineParser tests
ark120202 Apr 16, 2019
68c767a
Rename transpileVirtualProgram to transpileVirtualProject
ark120202 Apr 16, 2019
000793d
Remove export from CLI
ark120202 Apr 16, 2019
b51b916
Add expect(received).toHaveDiagnostics() matcher
ark120202 Apr 16, 2019
2d02ce1
Parse command line options case-insensitively
ark120202 Apr 16, 2019
6c36fe0
Disallow unknown options in "tstl" config object
ark120202 Apr 16, 2019
64c0962
Rename compiler tests to CLI tests
ark120202 Apr 16, 2019
9cc5be7
Refactor CLI tests
ark120202 Apr 17, 2019
52b5c50
Add transpile tests
ark120202 Apr 20, 2019
28b5b52
Fix invalid behavior with relative outFile and outDir
ark120202 Apr 21, 2019
c211764
Rename getTranspileOutput to getTranspilationResult
ark120202 Apr 21, 2019
65e5183
Remove `options` argument from `getTranspilationResult`
ark120202 Apr 21, 2019
07c23dc
Rename getTranspilationResult to transpile
ark120202 Apr 21, 2019
2fc099e
Extract luaLibImport to a variable
ark120202 Apr 21, 2019
e072bb0
Add command line parsing integration tests
ark120202 Apr 21, 2019
4045b47
Make diagnostics use node's source file instead of transformed one
ark120202 Apr 21, 2019
1b62fd9
Cast getCompilerOptions calls to custom compiler options
ark120202 Apr 21, 2019
946f16a
Export TranspileError and all LuaTransformer exports from package index
ark120202 Apr 22, 2019
4c76457
Remove options argument from LuaTransformer
ark120202 Apr 22, 2019
6a595b1
Rename sourceFile variable
ark120202 Apr 22, 2019
e821d7f
Set printer default during destructuring
ark120202 Apr 22, 2019
4982c44
Rename
ark120202 Apr 22, 2019
280ed35
Use emitTranspiledFiles in transpileFiles and transpileProject
ark120202 Apr 27, 2019
55acfd7
Use transpileFiles in transpile tests runner
ark120202 Apr 27, 2019
7f8a198
Fix typos
ark120202 Apr 28, 2019
96e0895
Deprecate root-level options
ark120202 Apr 29, 2019
f8ef95f
Move TranspileError diagnostic to diagnostics.ts
ark120202 Apr 29, 2019
f0e82ce
Add source to custom diagnostics
ark120202 Apr 29, 2019
f78e0d0
Show custom diagnostics as `TSTL<code>` in CLI
ark120202 Apr 29, 2019
dc53f26
Fix quotes
ark120202 Apr 30, 2019
8378cdf
Always import tstl as a namespace in tests
ark120202 May 1, 2019
d5b8e65
Simplify tstl object formatting in deprecation warning
ark120202 May 1, 2019
e8b89cd
Add Lua AST to TranspiledFile interface
ark120202 May 2, 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
42 changes: 20 additions & 22 deletions build_lualib.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import * as fs from "fs";
import * as glob from "glob";
import { compile } from "./src/Compiler";
import { LuaLib as luaLib, LuaLibFeature } from "./src/LuaLib";
import * as path from "path";
import * as tstl from "./src";
import { LuaLib } from "./src/LuaLib";

const bundlePath = "./dist/lualib/lualib_bundle.lua";
const options: tstl.CompilerOptions = {
skipLibCheck: true,
types: [],
luaLibImport: tstl.LuaLibImportKind.None,
luaTarget: tstl.LuaTarget.Lua51,
noHeader: true,
outDir: path.join(__dirname, "./dist/lualib"),
rootDir: path.join(__dirname, "./src/lualib"),
};

compile([
"--skipLibCheck",
"--types",
"node",
"--luaLibImport",
"none",
"--luaTarget",
"5.1",
"--noHeader",
"--outDir",
"./dist/lualib",
"--rootDir",
"./src/lualib",
"--noHeader",
"true",
...glob.sync("./src/lualib/**/*.ts"),
]);
// TODO: Check diagnostics
Perryvw marked this conversation as resolved.
Show resolved Hide resolved
const { transpiledFiles } = tstl.transpileFiles(glob.sync("./src/lualib/**/*.ts"), options);
tstl.emitTranspiledFiles(options, transpiledFiles);

const bundlePath = path.join(__dirname, "./dist/lualib/lualib_bundle.lua");
if (fs.existsSync(bundlePath)) {
fs.unlinkSync(bundlePath);
}

const bundle = luaLib.loadFeatures(Object.keys(LuaLibFeature).map(lib => LuaLibFeature[lib]));
fs.writeFileSync(bundlePath, bundle);
fs.writeFileSync(
bundlePath,
LuaLib.loadFeatures(Object.keys(tstl.LuaLibFeature).map(lib => tstl.LuaLibFeature[lib])),
);
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ const isCI = require("is-ci");
/** @type {Partial<import("@jest/types").Config.DefaultOptions>} */
module.exports = {
testMatch: ["**/test/**/*.spec.ts"],
collectCoverageFrom: ["<rootDir>/src/**/*", "!<rootDir>/src/lualib/**/*"],
collectCoverageFrom: [
"<rootDir>/src/**/*",
"!<rootDir>/src/lualib/**/*",
// https://github.com/facebook/jest/issues/5274
"!<rootDir>/src/tstl.ts",
],
watchPathIgnorePatterns: ["/watch\\.ts$"],

testEnvironment: "node",
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"devDependencies": {
"@types/glob": "^5.0.35",
"@types/jest": "^24.0.11",
"@types/node": "^9.6.23",
"@types/node": "^11.13.0",
"fengari": "^0.1.2",
"glob": "^7.1.2",
"jest": "^24.5.0",
Expand Down
17 changes: 6 additions & 11 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ArgumentParseResult<T> =
{ isValid: true; result: T; increment?: number }
| { isValid: false, errorMessage: string };

interface ParsedCommandLine extends ts.ParsedCommandLine {
export interface ParsedCommandLine extends ts.ParsedCommandLine {
options: CompilerOptions;
}

Expand Down Expand Up @@ -161,19 +161,13 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
}

const configPath = options.project;
const parsedJsonConfig = parseTsConfigFile(configPath, options);

return parsedJsonConfig;
const configContent = fs.readFileSync(configPath, "utf8");
return parseConfigFileContent(configContent, configPath, options);
}
return { isValid: true, result: parsedCommandLine };
}

export function parseTsConfigFile(filePath: string, existingOptions?: ts.CompilerOptions): CLIParseResult {
ark120202 marked this conversation as resolved.
Show resolved Hide resolved
const configContents = fs.readFileSync(filePath).toString();
return parseTsConfigString(configContents, filePath, existingOptions);
}

export function parseTsConfigString(
export function parseConfigFileContent(
tsConfigString: string,
configPath: string,
existingOptions?: ts.CompilerOptions
Expand All @@ -183,7 +177,8 @@ export function parseTsConfigString(
configJson.config,
ts.sys,
path.dirname(configPath),
existingOptions
existingOptions,
configPath
);

for (const key in parsedJsonConfig.raw) {
Expand Down
186 changes: 0 additions & 186 deletions src/Compiler.ts

This file was deleted.

65 changes: 65 additions & 0 deletions src/Emit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import { TranspiledFile } from "./Transpile";

const trimExt = (filePath: string) =>
path.join(path.dirname(filePath), path.basename(filePath, path.extname(filePath)));

let lualibContent: string;
export function emitTranspiledFiles(
ark120202 marked this conversation as resolved.
Show resolved Hide resolved
options: CompilerOptions,
transpiledFiles: Map<string, TranspiledFile>,
writeFile = ts.sys.writeFile
): void {
const { rootDir, outDir, outFile, luaLibImport } = options;

for (const [fileName, { lua, sourceMap, declaration, declarationMap }] of transpiledFiles) {
let outPath = fileName;
if (outDir !== rootDir) {
const relativeSourcePath = path.resolve(fileName).replace(path.resolve(rootDir), "");
outPath = path.join(outDir, relativeSourcePath);
}

// change extension or rename to outFile
if (outFile) {
if (path.isAbsolute(outFile)) {
outPath = outFile;
} else {
// append to workingDir or outDir
outPath = path.resolve(options.outDir, outFile);
}
} else {
outPath = trimExt(outPath) + ".lua";
}

if (lua !== undefined) {
writeFile(outPath, lua);
}

if (sourceMap !== undefined && options.sourceMap) {
writeFile(outPath + ".map", sourceMap);
}

if (declaration !== undefined) {
writeFile(trimExt(outPath) + ".d.ts", declaration);
}

if (declarationMap !== undefined) {
writeFile(trimExt(outPath) + ".d.ts.map", declarationMap);
}
}

if (luaLibImport === LuaLibImportKind.Require || luaLibImport === LuaLibImportKind.Always) {
if (lualibContent === undefined) {
lualibContent = fs.readFileSync(
path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"),
"utf8"
);
}

const outPath = path.join(outDir, "lualib_bundle.lua");
writeFile(outPath, lualibContent);
}
}