An open API service indexing awesome lists of open source software.

https://github.com/halliwood/ts2lua

Change typescript to lua.
https://github.com/halliwood/ts2lua

lua typescript

Last synced: 3 months ago
JSON representation

Change typescript to lua.

Awesome Lists containing this project

README

          

# ts2lua
ts2lua是一个将TypeScript代码转换为lua代码的工具。在使用之前,建议您先阅读[lua-objects](https://github.com/dmccuskey/lua-objects "lua-objects的Github"),它是一个很不错的lua面向对象解决方案,支持多重继承、getter/setter等,ts2lua按照lua-objects的规范来处理TypeScript类。

## 安装
```
$ npm i ts2lua
```

## 用法
转换TypeScript语句

```JavaScript
const ts2lua = require('ts2lua');
const tsCode = 'let a = "Hello World!";';
const luaCode = ts2lua.translate(tsCode);
console.log(luaCode);
```

TypeScript
```TypeScript
import ts2lua = require('ts2lua');

let testCode = `
let num = 123;
if(num > 100) {
console.log(num + ' is bigger than 100!');
} else {
console.log(num + ' is smaller than 100!');
}
`
console.log(ts2lua.translate(testCode));
```

批量转换TypeScript文件

```JavaScript
const ts2lua = require('ts2lua');
const inputPath = 'ts_file_root';
conse outputPath = 'lua_file_root';
ts2lua.translateFiles(inputPath, outputPath);
// 指定生成lua文件后缀名
ts2lua.translateFiles(inputPath, outputPath, { ext: '.lua.txt' });
```

## 批量转换接口的说明
使用`translateFiles`可以批量转换TypeScript代码。先来看看index.d.ts的声明:

```TypeScript
/**
* Translate typescript files from the given input path and write lua files into the given output path.
* @param inputPath input path which contains typescript files to translate.
* @param outputPath output path where to write lua files into.
* @param option translate option
*/
export declare function translateFiles(inputPath : string, outputPath : string, option ?: TranslateOption): void;
```

其中,必选参数`inputPath`和`outputPath`分别表示TypeScript文件目录和生成的lua文件目录。可选参数`option`表示转换选项,当前支持如下选项:

```TypeScript
export interface TranslateOption {
/**生成lua代码文件后缀名,默认为'.lua' */
ext?: string,
/**lua代码风格,默认适配xlua */
style?: 'xlua' | null,
/**是否在生成的lua代码中,增加ts2lua认为有必要人工处理的提示,默认为true */
addTip?: boolean,
/**函数名替换配置json文件路径,默认为lib\\func.json */
funcReplConfJson?: string,
/**正则表达式替换配置txt文件路径,默认为lib\\regex.txt */
regexReplConfTxt?: string,
/**对于没有替换配置的正则表达式,是否尝试简单翻译成lua,默认false。如果为true,则将正则表达式翻译为字符串,将转义符翻译成%。 */
translateRegex?: boolean,
/**输出未识别的正则表达式的文件路径,默认不输出 */
traceUnknowRegex?: string
}
```

*可选字段`ext`表示生成lua文件后缀,比如可以指定为`.lua.txt`。
*可选字段`style`表示生成lua代码的风格,默认是xlua风格,ts2lua会按照xlua的一些规范生成对应的lua代码,详细见下方说明。如果不使用xlua风格,请设置`style`为`null`,如下所示:

```JavaScript
ts2lua.translateFiles('in', 'out', { style: null });
```

* 可选字段`addTip`默认为true,当ts2lua遇到无法确定转换结果100%效果一致时,将在代码中插入必要的提示。比如数组下标访问、正则表达式处理等。
* 可选字段`funcReplConfJson`表示用于配置函数名转换规则的json文件的存放路径。ts2lua将根据该文件的映射关系对指定的函数名进行翻译,你可以直接修改默认配置`lib\\func.json`。比如,`replace`函数将默认翻译为`gsub`。
* 可选字段`regexReplConfTxt`表示用于配置正则表达式转换规则的txt文件的存放路径。ts2lua将根据该文件的映射关系对指定的正则表达式进行翻译,你可以直接修改默认配置`lib\\regex.txt`。
* 可选字段`translateRegex`若为`true`,则对于正则表达式转换规则json文件中没有配置的正则表达式,ts2lua将简单的进行处理:将正则表达式翻译为字符串,将转义符翻译成%。比如`/\w+/g`将翻译成`'%w+'`。该字段默认为`false`,即原样输出(对lua来说,通常会有语法错误)。
* 可选字段`traceUnknowRegex`表示未识别正则表达式的输出路径。若指定了该值,则对于正则表达式转换规则json文件中没有配置的正则表达式,ts2lua将记录到一个文件中,方便集中处理后加入到转换配置中。
* 可选字段`ignoreNoUsedExp`若为`true`,则ts2lua会忽略代码块(`BlockStatement`)中没有实际用途的表达式(包括多余的成员表达式`MemberExpression`和标识`Identifier`访问)。该字段默认为`true`。如下述代码中多余的语句将被忽略:

TypeScript
```TypeScript
doStr() {
let idx = 2;
this.myArr; // 这句是多余的MemberExpression
idx; // 这句是多余的Identifier
console.log('idx == ' + idx);
}
```

lua
```lua
function doStr()
local idx = 2
print('idx == ' .. idx)
end
```

## 关于变量名、函数名不符合lua规范的处理
如果变量名、函数名为lua关键字,则自动添加`tsvar_`的前缀。如果包含`$`等lua不支持的字符,则自动将`$`替换为`tsvar_`。

## 关于单个ts文件中存在多个类定义的处理
TypeScript允许在单个ts文件中定义多个类,lua其实也可以这么写。但是为了避免循环引用的问题,最好的做法是将每个“类”定义在单独的文件里。ts2lua采用了这一策略。比如,在`module/something/Thing.ts`中定义了类`ThingB`,ts2lua会将`ThingB`生成到`module/something/Thing/ThingB.lua`中。

## 关于数组下标访问的处理
由于lua的下标从1开始,所以对于类似`arr[i]`这种会转化为`arr[i+1]`,而对于`arr[idx]`这种则不会进行+1处理,ts2lua会自动添加注释提醒您人工确认转换结果是否正确。
比如,下述代码将转换为

TypeScript
```TypeScript
doStr() {
if(this.subValue > 10) {
console.log('subValue is bigger than 10: ' + this.subValue + ', yes!');
} else {
console.log('subValue is smaller than 10: ' + this.subValue + ', no!');
}
for(let i = 0, len = this.myArr.length; i < len; i++) {
console.log(this.myArr[i]);
}
let idx = 2;
console.log('this.myArr[2] == ' + this.myArr[idx]);
}
```

lua
```lua
function doStr()
if self.subValue>10 then
print('subValue is bigger than 10: '..self.subValue..', yes!')

else
print('subValue is smaller than 10: '..self.subValue..', no!')

end

local i=0
local len=#self.myArr
repeat
print(self.myArr[i+1])
i=i+1
until not(i