Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/dist
/coverage
/test/translation/transformation/characterEscapeSequence.ts

/src
*.md
5 changes: 2 additions & 3 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ const isCI = require("is-ci");

/** @type {import("prettier").Options} */
module.exports = {
printWidth: 100,
printWidth: 120,
tabWidth: 4,
trailingComma: "all",
proseWrap: "always",
trailingComma: "es5",
endOfLine: isCI ? "lf" : "auto",
overrides: [{ files: ["**/*.md", "**/*.yml", "**/.*.yml"], options: { tabWidth: 2 } }],
};
324 changes: 172 additions & 152 deletions CHANGELOG.md

Large diffs are not rendered by default.

40 changes: 23 additions & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# Contributing to TypeScriptToLua

1) [Project Overview](#project-overview)
2) [Running Tests](#running-tests)
3) [Testing Guidelines](#testing-guidelines)
4) [Coding Conventions](#coding-conventions)
1. [Project Overview](#project-overview)
2. [Running Tests](#running-tests)
3. [Testing Guidelines](#testing-guidelines)
4. [Coding Conventions](#coding-conventions)

## Project Overview

To get familiar with the project structure, here is a short overview of each directory and their function.

- `src/`
* Source code for the project, has the transpiler core files in its root.
* `src/lualib/`
- Source code for the project, has the transpiler core files in its root.
- `src/lualib/`
- Contains the TypeScript source for the lualib. This consists of implementations of standard TypeScript functions that are not present in Lua. These files are compiled to Lua using the transpiler. They are included in the Lua result when transpiling.
* `src/targets/`
- `src/targets/`
- Version-specific transpiler overrides for the different Lua targets. The main transpiler transpiles Lua 5.0, each target-specific transpiler extends the transpiler of the version before it, so the 5.3 inherits 5.2 which inherits 5.1 which inherits 5.0. LuaJIT is based on 5.2 so inherits from the 5.2 transpiler.
* *Compiler.ts* - Main entry point of the transpiler, this is what interfaces with the TypeScript compiler API.
* *LuaTransformer.ts* - Main transpiler code, transforms a TypeScript AST to a Lua AST.
* *LuaPrinter.ts* - Transforms a Lua AST to a string.
* *TSHelper.ts* - Helper methods used during the transpilation process.
- _Compiler.ts_ - Main entry point of the transpiler, this is what interfaces with the TypeScript compiler API.
- _LuaTransformer.ts_ - Main transpiler code, transforms a TypeScript AST to a Lua AST.
- _LuaPrinter.ts_ - Transforms a Lua AST to a string.
- _TSHelper.ts_ - Helper methods used during the transpilation process.
- `test/`
* This directory contains all testing code for the transpiler.
* `test/unit/`
- This directory contains all testing code for the transpiler.
- `test/unit/`
- Unit/Functional tests for the transpiler. Tests in here are grouped by functionality they are testing. Generally each of these tests uses the transpiler to transpile some TypeScript to Lua, then executes it using the Fengari Lua VM. Assertion is done on the result of the lua code.
* `test/translation/`
- `test/translation/`
- **[Obsolete]** Contains tests that only check the transpiled Lua String. We prefer adding unit/functional tests over translation tests. This directory will probably be removed at some point.

## Running Tests

The tests for this project can be executed using the standard `npm test`. This runs all tests.

Due to the time required to run all tests, it is impractical to run every test while developing part of the transpiler. To speed up the test run you can:
Expand All @@ -46,14 +49,17 @@ Due to the time required to run all tests, it is impractical to run every test w
```

## Testing Guidelines

When submitting a pull request with new functionality, we require some functional (transpile and execute Lua) to be added, to ensure the new functionality works as expected, and will continue to work that way.

Translation tests are discouraged as in most cases as we do not really care about the exact Lua output, as long as executing it results in the correct result (which is tested by functional tests).

## Coding Conventions

Most coding conventions are enforced by the TSLint and Prettier. You can check your code locally by running `npm run lint`. The CI build will fail if your code does not pass the linter. For better experience, you can install extensions for your code editor for [TSLint](https://palantir.github.io/tslint/usage/third-party-tools/) and [Prettier](https://prettier.io/docs/en/editors.html).

Some extra conventions worth mentioning:
* Do not abbreviate variable names. The exception here are inline lambda arguments, if it is obvious what the argument is you can abbreviate to the first letter, e.g: `statements.filter(s => ts.VariableStatement(s))`
* Readability of code is more important than the amount of space it takes. If extra line breaks make your code more readable, add them.
* Functional style is encouraged!

- Do not abbreviate variable names. The exception here are inline lambda arguments, if it is obvious what the argument is you can abbreviate to the first letter, e.g: `statements.filter(s => ts.VariableStatement(s))`
- Readability of code is more important than the amount of space it takes. If extra line breaks make your code more readable, add them.
- Functional style is encouraged!
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A generic TypeScript to Lua transpiler. Write your code in TypeScript and publis
Large projects written in lua can become hard to maintain and make it easy to make mistakes. Writing code in TypeScript instead improves maintainability, readability and robustness, with the added bonus of good IDE support. This project is useful in any environment where Lua code is accepted, with the powerful option of simply declaring any existing API using TypeScript declaration files.

## Documentation

More detailed documentation and info on writing declarations can be found [on the wiki](https://github.com/TypeScriptToLua/TypescriptToLua/wiki).

Changelog can be found in [CHANGELOG.md](https://github.com/TypeScriptToLua/TypescriptToLua/blob/master/CHANGELOG.md)
Expand All @@ -38,24 +39,28 @@ Changelog can be found in [CHANGELOG.md](https://github.com/TypeScriptToLua/Type
`tstl -p path/to/tsconfig.json --watch`

**Example tsconfig.json**

```json
{
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"strict": true
},
"tstl": {
"luaTarget": "JIT"
}
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"strict": true
},
"tstl": {
"luaTarget": "JIT"
}
}
```

## Contributing

All contributions are welcome, but please read our [contribution guidelines](https://github.com/TypeScriptToLua/TypescriptToLua/blob/master/CONTRIBUTING.md)!

## Declarations

The real power of this transpiler is usage together with good declarations for the Lua API provided. Some examples of Lua interface declarations can be found here:

- [Dota 2 Modding](https://github.com/ModDota/API/tree/master/declarations/server)
- [Defold Game Engine Scripting](https://github.com/dasannikov/DefoldTypeScript/blob/master/defold.d.ts)
- [LÖVE 2D Game Development](https://github.com/hazzard993/love-typescript-definitions)
Expand All @@ -71,17 +76,20 @@ The real power of this transpiler is usage together with good declarations for t
`npm run coverage` or `npm run coverage-html` to generate a coverage report.

## Sublime Text integration

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`).

You can simply open your typescript project assuming a valid tsconfig.json file is present. The default TypeScript plugin will provide all functionality of a regular TypeScript project.

### Setting up a custom build system
To add the option to build with the Lua transpiler instead of the regular typescript compiler, go to `Tools > Build System > New Build System...`. In the new sublime-build file that opens, enter the following (adjust path to tstl if not installed globally):

To add the option to build with the Lua transpiler instead of the regular typescript compiler, go to `Tools > Build System > New Build System...`. In the new sublime-build file that opens, enter the following (adjust path to tstl if not installed globally):

```
{
"cmd": ["tstl", "-p", "$file"],
"shell": true
}
```

Save this in your Sublime settings as a `TypeScriptToLua.sublime-build`. You can now select the TypeScriptToLua build system in `Tools > Build System` to build using the normal hotkey (`ctrl+B`), or if you have multiple TypeScript projects open, you can choose your compiler before building by pressing `ctrl+shift+B`.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts",
"test": "jest",
"lint": "npm run lint:tslint && npm run lint:prettier",
"lint:prettier": "prettier --check **/*.{js,ts,yml,json} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
"lint:prettier": "prettier --check **/*.{js,ts,yml,json,md} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
"lint:tslint": "tslint -p . && tslint -p test && tslint -p src/lualib",
"fix:prettier": "prettier --check --write **/*.{js,ts,yml,json}",
"fix:prettier": "prettier --check --write **/*.{js,ts,yml,json,md}",
"release-major": "npm version major",
"release-minor": "npm version minor",
"release-patch": "npm version patch",
Expand Down
15 changes: 5 additions & 10 deletions src/CommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine):

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

for (const key in parsedConfigFile.raw.tstl) {
Expand All @@ -122,10 +120,7 @@ export function parseCommandLine(args: string[]): ParsedCommandLine {
return updateParsedCommandLine(ts.parseCommandLine(args), args);
}

function updateParsedCommandLine(
parsedCommandLine: ts.ParsedCommandLine,
args: string[]
): ParsedCommandLine {
function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args: string[]): ParsedCommandLine {
for (let i = 0; i < args.length; i++) {
if (!args[i].startsWith("-")) continue;

Expand All @@ -143,10 +138,10 @@ function updateParsedCommandLine(
if (option) {
// Ignore errors caused by tstl specific compiler options
const tsInvalidCompilerOptionErrorCode = 5023;
parsedCommandLine.errors = parsedCommandLine.errors.filter(err => {
parsedCommandLine.errors = parsedCommandLine.errors.filter(error => {
return !(
err.code === tsInvalidCompilerOptionErrorCode &&
String(err.messageText).endsWith(`'${args[i]}'.`)
error.code === tsInvalidCompilerOptionErrorCode &&
String(error.messageText).endsWith(`'${args[i]}'.`)
);
});

Expand Down
10 changes: 2 additions & 8 deletions src/Emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export interface OutputFile {
}

let lualibContent: string;
export function emitTranspiledFiles(
options: CompilerOptions,
transpiledFiles: TranspiledFile[]
): OutputFile[] {
export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: TranspiledFile[]): OutputFile[] {
let { rootDir, outDir, outFile, luaLibImport } = options;

const configFileName = options.configFilePath as string | undefined;
Expand Down Expand Up @@ -60,10 +57,7 @@ export function emitTranspiledFiles(

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

let outPath = path.resolve(rootDir, "lualib_bundle.lua");
Expand Down
Loading