Socket
Socket
Sign inDemoInstall

typescript-to-lua

Package Overview
Dependencies
Maintainers
2
Versions
157
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-to-lua - npm Package Compare versions

Comparing version 0.7.0 to 0.8.0

dist/Decorator.js

15

CHANGELOG.md
# Changelog
## 0.7.0
* Lualib runtime library is now compiled from TypeScript using the transpiler when building!
* Split up runtime library definition into individual files.
* Added multiple inclusion modes using the tsconfig option `lubLibImport`, options are:
* `require` : Requires the entire library if lualib features are used.
* `always` : Always require the runtime library.
* `inline` : Inline the library code for used features in the file.
* `none` : Do not include the runtime library
* Added support for assigning expressions (`+=`, `&=`, `++`, etc) in other expressions (i.e. `lastIndex = i++` or `return a += b`) by transpiling them as immediately called anonymous functions.
* Unreachable code (after returns) is no longer transpiled, preventing a Lua syntax error.
* Fixed issue with destructing statements in Lua 5.1
* Fixed issue with escaped characters in strings.
* Fixed bug regarding changing an exported variable after its export.
## 0.6.0

@@ -4,0 +19,0 @@ * Reworked part of the class system to solve some issues.

42

dist/CommandLineParser.js

@@ -10,3 +10,3 @@ "use strict";

exports.CLIError = CLIError;
const optionDeclarations = {
exports.optionDeclarations = {
luaLibImport: {

@@ -32,2 +32,17 @@ choices: ["inline", "require", "none"],

/**
* Removes defaults from the arguments.
* Returns a tuple where [0] is a copy of the options without defaults and [1] is the extracted defaults.
*/
function getYargOptionsWithoutDefaults(options) {
// options is a deep object, Object.assign or {...options} still keeps the referece
const copy = JSON.parse(JSON.stringify(options));
const optionDefaults = { _: null, $0: null };
for (const optionName in copy) {
const section = copy[optionName];
optionDefaults[optionName] = section.default;
delete section.default;
}
return [copy, optionDefaults];
}
/**
* Pares the supplied arguments.

@@ -37,2 +52,4 @@ * The result will include arguments supplied via CLI and arguments from tsconfig.

function parseCommandLine(args) {
// Get a copy of the options without defaults to prevent defaults overriding project config
const [tstlOptions, tstlDefaults] = getYargOptionsWithoutDefaults(exports.optionDeclarations);
const parsedArgs = yargs

@@ -46,3 +63,3 @@ .usage("Syntax: tstl [options] [files...]\n\n" +

.wrap(yargs.terminalWidth())
.options(optionDeclarations)
.options(tstlOptions)
.fail((msg, err) => {

@@ -67,2 +84,4 @@ throw new CLIError(msg);

addTSTLOptions(commandLine);
// Add TSTL defaults last
addTSTLOptions(commandLine, tstlDefaults);
// Run diagnostics again to check for errors in tsconfig

@@ -88,3 +107,3 @@ runDiagnostics(commandLine);

// dont override, this will prioritize CLI over tsconfig.
if (optionDeclarations[arg] && (!commandLine.options[arg] || forceOverride)) {
if (exports.optionDeclarations[arg] && (!commandLine.options[arg] || forceOverride)) {
commandLine.options[arg] = additionalArgs[arg];

@@ -103,5 +122,5 @@ }

const optionNames = [];
for (const key of Object.keys(optionDeclarations)) {
for (const key of Object.keys(exports.optionDeclarations)) {
optionNames.push(key);
const alias = optionDeclarations[key].alias;
const alias = exports.optionDeclarations[key].alias;
if (alias) {

@@ -137,10 +156,11 @@ if (typeof alias === "string") {

}
let configPath;
/* istanbul ignore else: Testing else part is not really possible via automated tests */
if (path.isAbsolute(commandLine.options.project)) {
configPath = commandLine.options.project;
let configPath = commandLine.options.project;
// If the project path is wrapped in double quotes, remove them
if (/^".*"$/.test(configPath)) {
configPath = configPath.substring(1, configPath.length - 1);
}
else {
/* istanbul ignore if: Testing else part is not really possible via automated tests */
if (!path.isAbsolute(configPath)) {
// TODO check if commandLine.options.project can even contain non absolute paths
configPath = path.join(process.cwd(), commandLine.options.project);
configPath = path.join(process.cwd(), configPath);
}

@@ -147,0 +167,0 @@ if (fs.statSync(configPath).isDirectory()) {

@@ -14,27 +14,65 @@ "use strict";

const commandLine = CommandLineParser_1.parseCommandLine(argv);
compileFilesWithOptions(commandLine.fileNames, commandLine.options);
/* istanbul ignore if: tested in test/compiler/watchmode.spec with subproccess */
if (commandLine.options.watch) {
watchWithOptions(commandLine.fileNames, commandLine.options);
}
else {
compileFilesWithOptions(commandLine.fileNames, commandLine.options);
}
}
exports.compile = compile;
/* istanbul ignore next: tested in test/compiler/watchmode.spec with subproccess */
function watchWithOptions(fileNames, options) {
let host;
let config = false;
if (options.project) {
config = true;
host = ts.createWatchCompilerHost(options.project, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
}
else {
host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram);
}
host.afterProgramCreate = program => {
const status = emitFilesAndReportErrors(program.getProgram());
const errorDiagnostic = {
category: undefined,
code: 6194,
file: undefined,
length: 0,
messageText: "Found 0 errors. Watching for file changes.",
start: 0,
};
if (status !== 0) {
errorDiagnostic.messageText = "Found Errors. Watching for file changes.";
errorDiagnostic.code = 6193;
}
host.onWatchStatusChange(errorDiagnostic, host.getNewLine(), program.getCompilerOptions());
};
if (config) {
ts.createWatchProgram(host);
}
else {
ts.createWatchProgram(host);
}
}
exports.watchWithOptions = watchWithOptions;
function compileFilesWithOptions(fileNames, options) {
if (!options.luaTarget) {
options.luaTarget = Transpiler_1.LuaTarget.LuaJIT;
}
const program = ts.createProgram(fileNames, options);
emitFilesAndReportErrors(program);
}
exports.compileFilesWithOptions = compileFilesWithOptions;
function emitFilesAndReportErrors(program) {
const options = program.getCompilerOptions();
const checker = program.getTypeChecker();
// Get all diagnostics, ignore unsupported extension
const diagnostics = ts.getPreEmitDiagnostics(program).filter(diag => diag.code !== 6054);
diagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
diagnostics.forEach(reportDiagnostic);
// If there are errors dont emit
if (diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).length > 0) {
if (!options.watch) {
process.exit(1);
}
else {
console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`);
return 1;
}
});
// If there are errors dont emit
if (diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error).length > 0) {
console.log("Stopping compilation process because of errors.");
process.exit(1);
}

@@ -90,10 +128,8 @@ program.getSourceFiles().forEach(sourceFile => {

}
return 0;
}
exports.compileFilesWithOptions = compileFilesWithOptions;
function createTranspiler(checker, options, sourceFile) {
let luaTargetTranspiler;
switch (options.luaTarget) {
case Transpiler_1.LuaTarget.LuaJIT:
luaTargetTranspiler = new Transpiler_JIT_1.LuaTranspilerJIT(checker, options, sourceFile);
break;
const target = options.luaTarget ? options.luaTarget.toLowerCase() : "";
switch (target) {
case Transpiler_1.LuaTarget.Lua51:

@@ -109,4 +145,4 @@ luaTargetTranspiler = new Transpiler_51_1.LuaTranspiler51(checker, options, sourceFile);

default:
// should not happen
throw Error("No luaTarget Specified please ensure a target is set!");
luaTargetTranspiler = new Transpiler_JIT_1.LuaTranspilerJIT(checker, options, sourceFile);
break;
}

@@ -116,2 +152,12 @@ return luaTargetTranspiler;

exports.createTranspiler = createTranspiler;
function reportDiagnostic(diagnostic) {
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(`${diagnostic.code}: ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
}
else {
console.log(`${diagnostic.code}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`);
}
}
//# sourceMappingURL=Compiler.js.map

@@ -0,0 +0,0 @@ "use strict";

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Errors_1 = require("../Errors");
const Transpiler_51_1 = require("./Transpiler.51");

@@ -27,2 +28,4 @@ const ts = require("typescript");

return `bit32.bnot(${operand})`;
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operator, node);
}

@@ -45,2 +48,4 @@ }

return `bit32.arshift(${lhs},${rhs})`;
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operatorToken.kind, node);
}

@@ -52,4 +57,8 @@ }

}
/** @override */
transpileSpreadElement(node) {
return "table.unpack(" + this.transpileExpression(node.expression) + ")";
}
}
exports.LuaTranspiler52 = LuaTranspiler52;
//# sourceMappingURL=Transpiler.52.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Transpiler_1 = require("../Transpiler");
const Errors_1 = require("../Errors");
const Transpiler_52_1 = require("./Transpiler.52");

@@ -12,2 +12,4 @@ const ts = require("typescript");

return `~${operand}`;
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operator, node);
}

@@ -29,3 +31,5 @@ }

case ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
throw new Transpiler_1.TranspileError("Bitwise operator >>> not supported in Lua 5.3", node);
throw Errors_1.TSTLErrors.UnsupportedForTarget("Bitwise >>> operator", this.options.luaTarget, node);
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operatorToken.kind, node);
}

@@ -32,0 +36,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Errors_1 = require("../Errors");
const Transpiler_52_1 = require("./Transpiler.52");

@@ -11,2 +12,4 @@ const ts = require("typescript");

return `bit.bnot(${operand})`;
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operator, node);
}

@@ -29,2 +32,4 @@ }

return `bit.arshift(${lhs},${rhs})`;
default:
throw Errors_1.TSTLErrors.UnsupportedKind("bitwise operator", node.operatorToken.kind, node);
}

@@ -31,0 +36,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const Decorator_1 = require("./Decorator");
class TSHelper {

@@ -28,7 +29,8 @@ // Reverse lookup of enum key by value

static getExtendedType(node, checker) {
if (node.heritageClauses) {
if (node && node.heritageClauses) {
for (const clause of node.heritageClauses) {
if (clause.token === ts.SyntaxKind.ExtendsKeyword) {
const superType = checker.getTypeAtLocation(clause.types[0]);
if (!this.isPureAbstractClass(superType, checker)) {
const decorators = this.getCustomDecorators(superType, checker);
if (!decorators.has(Decorator_1.DecoratorKind.PureAbstract)) {
return superType;

@@ -63,27 +65,7 @@ }

}
static isCompileMembersOnlyEnum(type, checker) {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Enum) !== 0)
&& type.symbol.getDocumentationComment(checker)[0] !== undefined
&& this.hasCustomDecorator(type, checker, "!CompileMembersOnly");
}
static isPureAbstractClass(type, checker) {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Class) !== 0)
&& this.hasCustomDecorator(type, checker, "!PureAbstract");
}
static isExtensionClass(type, checker) {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Class) !== 0)
&& this.hasCustomDecorator(type, checker, "!Extension");
}
static isPhantom(type, checker) {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Namespace) !== 0)
&& this.hasCustomDecorator(type, checker, "!Phantom");
}
static isTupleReturnCall(node, checker) {
if (ts.isCallExpression(node)) {
const type = checker.getTypeAtLocation(node.expression);
return this.isTupleReturnFunction(type, checker);
return this.getCustomDecorators(type, checker)
.has(Decorator_1.DecoratorKind.TupleReturn);
}

@@ -94,17 +76,17 @@ else {

}
static isTupleReturnFunction(type, checker) {
return type.symbol
&& ((type.symbol.flags & ts.SymbolFlags.Function) !== 0
|| (type.symbol.flags & ts.SymbolFlags.Method) !== 0)
&& this.hasCustomDecorator(type, checker, "!TupleReturn");
}
static hasCustomDecorator(type, checker, decorator) {
static getCustomDecorators(type, checker) {
if (type.symbol) {
const comments = type.symbol.getDocumentationComment(checker);
const decorators = comments.filter(comment => comment.kind === "text")
.map(comment => comment.text.trim())
.map(comment => comment.text.trim().split("\n"))
.reduce((a, b) => a.concat(b), [])
.filter(comment => comment[0] === "!");
return decorators.indexOf(decorator) > -1;
const decMap = new Map();
decorators.forEach(decStr => {
const dec = new Decorator_1.Decorator(decStr);
decMap.set(dec.kind, dec);
});
return decMap;
}
return false;
return new Map();
}

@@ -111,0 +93,0 @@ // Search up until finding a node satisfying the callback

{
"name": "typescript-to-lua",
"license": "MIT",
"version": "0.7.0",
"version": "0.8.0",
"repository": "https://github.com/Perryvw/TypescriptToLua",

@@ -45,3 +45,3 @@ "keywords": [

"typescript": "^2.9.2",
"yargs": "^11.1.0"
"yargs": "^12.0.1"
},

@@ -51,3 +51,3 @@ "devDependencies": {

"@types/node": "^9.6.23",
"@types/yargs": "^11.0.0",
"@types/yargs": "^11.1.1",
"alsatian": "^2.2.1",

@@ -54,0 +54,0 @@ "circular-json": "^0.5.5",

@@ -0,0 +0,0 @@ # TypescriptToLua

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc