typescript-to-lua
Advanced tools
Comparing version 1.14.0 to 1.15.0
@@ -41,6 +41,41 @@ "use strict"; | ||
function parseConfigFileWithSystem(configFileName, commandLineOptions, system = ts.sys) { | ||
const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(configFileName, system.readFile), system, path.dirname(configFileName), commandLineOptions, configFileName); | ||
var _a; | ||
const configRootDir = path.dirname(configFileName); | ||
const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(configFileName, system.readFile), system, configRootDir, commandLineOptions, configFileName); | ||
const cycleCache = new Set(); | ||
const extendedTstlOptions = getExtendedTstlOptions(configFileName, configRootDir, cycleCache, system); | ||
parsedConfigFile.raw.tstl = Object.assign(extendedTstlOptions, (_a = parsedConfigFile.raw.tstl) !== null && _a !== void 0 ? _a : {}); | ||
return (0, parse_1.updateParsedConfigFile)(parsedConfigFile); | ||
} | ||
exports.parseConfigFileWithSystem = parseConfigFileWithSystem; | ||
function getExtendedTstlOptions(configFilePath, configRootDir, cycleCache, system) { | ||
const absolutePath = path.isAbsolute(configFilePath) ? configFilePath : path.resolve(configRootDir, configFilePath); | ||
const newConfigRoot = path.dirname(absolutePath); | ||
if (cycleCache.has(absolutePath)) { | ||
return {}; | ||
} | ||
cycleCache.add(absolutePath); | ||
const fileContent = system.readFile(absolutePath); | ||
const options = {}; | ||
if (fileContent) { | ||
const { config: parsedConfig } = ts.parseConfigFileTextToJson(configFilePath, fileContent); | ||
if (!parsedConfig) { | ||
return {}; | ||
} | ||
if (parsedConfig.extends) { | ||
if (Array.isArray(parsedConfig.extends)) { | ||
for (const extendedConfigFile of parsedConfig.extends) { | ||
Object.assign(options, getExtendedTstlOptions(extendedConfigFile, newConfigRoot, cycleCache, system)); | ||
} | ||
} | ||
else { | ||
Object.assign(options, getExtendedTstlOptions(parsedConfig.extends, newConfigRoot, cycleCache, system)); | ||
} | ||
} | ||
if (parsedConfig.tstl) { | ||
Object.assign(options, parsedConfig.tstl); | ||
} | ||
} | ||
return options; | ||
} | ||
function createConfigFileUpdater(optionsToExtend) { | ||
@@ -47,0 +82,0 @@ const configFileMap = new WeakMap(); |
@@ -35,27 +35,28 @@ import * as ts from "typescript"; | ||
TableIndexExpression = 30, | ||
AdditionOperator = 31, | ||
SubtractionOperator = 32, | ||
MultiplicationOperator = 33, | ||
DivisionOperator = 34, | ||
FloorDivisionOperator = 35, | ||
ModuloOperator = 36, | ||
PowerOperator = 37, | ||
NegationOperator = 38, | ||
ConcatOperator = 39, | ||
LengthOperator = 40, | ||
EqualityOperator = 41, | ||
InequalityOperator = 42, | ||
LessThanOperator = 43, | ||
LessEqualOperator = 44, | ||
GreaterThanOperator = 45, | ||
GreaterEqualOperator = 46, | ||
AndOperator = 47, | ||
OrOperator = 48, | ||
NotOperator = 49, | ||
BitwiseAndOperator = 50, | ||
BitwiseOrOperator = 51, | ||
BitwiseExclusiveOrOperator = 52, | ||
BitwiseRightShiftOperator = 53, | ||
BitwiseLeftShiftOperator = 54, | ||
BitwiseNotOperator = 55 | ||
ParenthesizedExpression = 31, | ||
AdditionOperator = 32, | ||
SubtractionOperator = 33, | ||
MultiplicationOperator = 34, | ||
DivisionOperator = 35, | ||
FloorDivisionOperator = 36, | ||
ModuloOperator = 37, | ||
PowerOperator = 38, | ||
NegationOperator = 39, | ||
ConcatOperator = 40, | ||
LengthOperator = 41, | ||
EqualityOperator = 42, | ||
InequalityOperator = 43, | ||
LessThanOperator = 44, | ||
LessEqualOperator = 45, | ||
GreaterThanOperator = 46, | ||
GreaterEqualOperator = 47, | ||
AndOperator = 48, | ||
OrOperator = 49, | ||
NotOperator = 50, | ||
BitwiseAndOperator = 51, | ||
BitwiseOrOperator = 52, | ||
BitwiseExclusiveOrOperator = 53, | ||
BitwiseRightShiftOperator = 54, | ||
BitwiseLeftShiftOperator = 55, | ||
BitwiseNotOperator = 56 | ||
} | ||
@@ -318,1 +319,6 @@ export type UnaryBitwiseOperator = SyntaxKind.BitwiseNotOperator; | ||
export declare function isInlineFunctionExpression(expression: FunctionExpression): expression is InlineFunctionExpression; | ||
export type ParenthesizedExpression = Expression & { | ||
expression: Expression; | ||
}; | ||
export declare function isParenthesizedExpression(node: Node): node is ParenthesizedExpression; | ||
export declare function createParenthesizedExpression(expression: Expression, tsOriginal?: ts.Node): ParenthesizedExpression; |
@@ -6,3 +6,3 @@ "use strict"; | ||
exports.isStringLiteral = exports.createNumericLiteral = exports.isNumericLiteral = exports.createArgLiteral = exports.isArgLiteral = exports.createDotsLiteral = exports.isDotsLiteral = exports.createBooleanLiteral = exports.isBooleanLiteral = exports.createNilLiteral = exports.isNilLiteral = exports.createExpressionStatement = exports.isExpressionStatement = exports.createBreakStatement = exports.isBreakStatement = exports.createReturnStatement = exports.isReturnStatement = exports.createLabelStatement = exports.isLabelStatement = exports.createGotoStatement = exports.isGotoStatement = exports.createForInStatement = exports.isForInStatement = exports.createForStatement = exports.isForStatement = exports.createRepeatStatement = exports.isRepeatStatement = exports.createWhileStatement = exports.isWhileStatement = exports.isIterationStatement = exports.createIfStatement = exports.isIfStatement = exports.createAssignmentStatement = exports.isAssignmentStatement = exports.createVariableDeclarationStatement = exports.isVariableDeclarationStatement = exports.createDoStatement = exports.isDoStatement = exports.createBlock = exports.isBlock = exports.createFile = exports.isFile = exports.setNodeFlags = exports.getOriginalPos = exports.setNodeOriginal = exports.setNodePosition = exports.cloneNode = exports.createNode = exports.NodeFlags = exports.SyntaxKind = void 0; | ||
exports.isInlineFunctionExpression = exports.isFunctionDefinition = exports.isAssignmentLeftHandSideExpression = exports.createTableIndexExpression = exports.isTableIndexExpression = exports.createAnonymousIdentifier = exports.cloneIdentifier = exports.createIdentifier = exports.isIdentifier = exports.createMethodCallExpression = exports.isMethodCallExpression = exports.createCallExpression = exports.isCallExpression = exports.createBinaryExpression = exports.isBinaryExpression = exports.createUnaryExpression = exports.isUnaryExpression = exports.createTableExpression = exports.isTableExpression = exports.createTableFieldExpression = exports.isTableFieldExpression = exports.createFunctionExpression = exports.isFunctionExpression = exports.isLiteral = exports.createStringLiteral = void 0; | ||
exports.createParenthesizedExpression = exports.isParenthesizedExpression = exports.isInlineFunctionExpression = exports.isFunctionDefinition = exports.isAssignmentLeftHandSideExpression = exports.createTableIndexExpression = exports.isTableIndexExpression = exports.createAnonymousIdentifier = exports.cloneIdentifier = exports.createIdentifier = exports.isIdentifier = exports.createMethodCallExpression = exports.isMethodCallExpression = exports.createCallExpression = exports.isCallExpression = exports.createBinaryExpression = exports.isBinaryExpression = exports.createUnaryExpression = exports.isUnaryExpression = exports.createTableExpression = exports.isTableExpression = exports.createTableFieldExpression = exports.isTableFieldExpression = exports.createFunctionExpression = exports.isFunctionExpression = exports.isLiteral = exports.createStringLiteral = void 0; | ||
// We can elide a lot of nodes especially tokens and keywords | ||
@@ -47,36 +47,37 @@ // because we don't create the AST from text | ||
SyntaxKind[SyntaxKind["TableIndexExpression"] = 30] = "TableIndexExpression"; | ||
SyntaxKind[SyntaxKind["ParenthesizedExpression"] = 31] = "ParenthesizedExpression"; | ||
// Operators | ||
// Arithmetic | ||
SyntaxKind[SyntaxKind["AdditionOperator"] = 31] = "AdditionOperator"; | ||
SyntaxKind[SyntaxKind["SubtractionOperator"] = 32] = "SubtractionOperator"; | ||
SyntaxKind[SyntaxKind["MultiplicationOperator"] = 33] = "MultiplicationOperator"; | ||
SyntaxKind[SyntaxKind["DivisionOperator"] = 34] = "DivisionOperator"; | ||
SyntaxKind[SyntaxKind["FloorDivisionOperator"] = 35] = "FloorDivisionOperator"; | ||
SyntaxKind[SyntaxKind["ModuloOperator"] = 36] = "ModuloOperator"; | ||
SyntaxKind[SyntaxKind["PowerOperator"] = 37] = "PowerOperator"; | ||
SyntaxKind[SyntaxKind["NegationOperator"] = 38] = "NegationOperator"; | ||
SyntaxKind[SyntaxKind["AdditionOperator"] = 32] = "AdditionOperator"; | ||
SyntaxKind[SyntaxKind["SubtractionOperator"] = 33] = "SubtractionOperator"; | ||
SyntaxKind[SyntaxKind["MultiplicationOperator"] = 34] = "MultiplicationOperator"; | ||
SyntaxKind[SyntaxKind["DivisionOperator"] = 35] = "DivisionOperator"; | ||
SyntaxKind[SyntaxKind["FloorDivisionOperator"] = 36] = "FloorDivisionOperator"; | ||
SyntaxKind[SyntaxKind["ModuloOperator"] = 37] = "ModuloOperator"; | ||
SyntaxKind[SyntaxKind["PowerOperator"] = 38] = "PowerOperator"; | ||
SyntaxKind[SyntaxKind["NegationOperator"] = 39] = "NegationOperator"; | ||
// Concat | ||
SyntaxKind[SyntaxKind["ConcatOperator"] = 39] = "ConcatOperator"; | ||
SyntaxKind[SyntaxKind["ConcatOperator"] = 40] = "ConcatOperator"; | ||
// Length | ||
SyntaxKind[SyntaxKind["LengthOperator"] = 40] = "LengthOperator"; | ||
SyntaxKind[SyntaxKind["LengthOperator"] = 41] = "LengthOperator"; | ||
// Relational Ops | ||
SyntaxKind[SyntaxKind["EqualityOperator"] = 41] = "EqualityOperator"; | ||
SyntaxKind[SyntaxKind["InequalityOperator"] = 42] = "InequalityOperator"; | ||
SyntaxKind[SyntaxKind["LessThanOperator"] = 43] = "LessThanOperator"; | ||
SyntaxKind[SyntaxKind["LessEqualOperator"] = 44] = "LessEqualOperator"; | ||
SyntaxKind[SyntaxKind["EqualityOperator"] = 42] = "EqualityOperator"; | ||
SyntaxKind[SyntaxKind["InequalityOperator"] = 43] = "InequalityOperator"; | ||
SyntaxKind[SyntaxKind["LessThanOperator"] = 44] = "LessThanOperator"; | ||
SyntaxKind[SyntaxKind["LessEqualOperator"] = 45] = "LessEqualOperator"; | ||
// Syntax Sugar `x > y` <=> `not (y <= x)` | ||
// but we should probably use them to make the output code more readable | ||
SyntaxKind[SyntaxKind["GreaterThanOperator"] = 45] = "GreaterThanOperator"; | ||
SyntaxKind[SyntaxKind["GreaterEqualOperator"] = 46] = "GreaterEqualOperator"; | ||
SyntaxKind[SyntaxKind["GreaterThanOperator"] = 46] = "GreaterThanOperator"; | ||
SyntaxKind[SyntaxKind["GreaterEqualOperator"] = 47] = "GreaterEqualOperator"; | ||
// Logical | ||
SyntaxKind[SyntaxKind["AndOperator"] = 47] = "AndOperator"; | ||
SyntaxKind[SyntaxKind["OrOperator"] = 48] = "OrOperator"; | ||
SyntaxKind[SyntaxKind["NotOperator"] = 49] = "NotOperator"; | ||
SyntaxKind[SyntaxKind["AndOperator"] = 48] = "AndOperator"; | ||
SyntaxKind[SyntaxKind["OrOperator"] = 49] = "OrOperator"; | ||
SyntaxKind[SyntaxKind["NotOperator"] = 50] = "NotOperator"; | ||
// Bitwise | ||
SyntaxKind[SyntaxKind["BitwiseAndOperator"] = 50] = "BitwiseAndOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseOrOperator"] = 51] = "BitwiseOrOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseExclusiveOrOperator"] = 52] = "BitwiseExclusiveOrOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseRightShiftOperator"] = 53] = "BitwiseRightShiftOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseLeftShiftOperator"] = 54] = "BitwiseLeftShiftOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseNotOperator"] = 55] = "BitwiseNotOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseAndOperator"] = 51] = "BitwiseAndOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseOrOperator"] = 52] = "BitwiseOrOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseExclusiveOrOperator"] = 53] = "BitwiseExclusiveOrOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseRightShiftOperator"] = 54] = "BitwiseRightShiftOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseLeftShiftOperator"] = 55] = "BitwiseLeftShiftOperator"; | ||
SyntaxKind[SyntaxKind["BitwiseNotOperator"] = 56] = "BitwiseNotOperator"; | ||
})(SyntaxKind = exports.SyntaxKind || (exports.SyntaxKind = {})); | ||
@@ -505,2 +506,12 @@ var NodeFlags; | ||
exports.isInlineFunctionExpression = isInlineFunctionExpression; | ||
function isParenthesizedExpression(node) { | ||
return node.kind === SyntaxKind.ParenthesizedExpression; | ||
} | ||
exports.isParenthesizedExpression = isParenthesizedExpression; | ||
function createParenthesizedExpression(expression, tsOriginal) { | ||
const parenthesizedExpression = createNode(SyntaxKind.ParenthesizedExpression, tsOriginal); | ||
parenthesizedExpression.expression = expression; | ||
return parenthesizedExpression; | ||
} | ||
exports.createParenthesizedExpression = createParenthesizedExpression; | ||
//# sourceMappingURL=LuaAST.js.map |
@@ -78,2 +78,3 @@ import { SourceNode } from "source-map"; | ||
printTableIndexExpression(expression: lua.TableIndexExpression): SourceNode; | ||
printParenthesizedExpression(expression: lua.ParenthesizedExpression): SourceNode; | ||
printOperator(kind: lua.Operator): SourceNode; | ||
@@ -80,0 +81,0 @@ protected joinChunksWithComma(chunks: SourceChunk[]): SourceChunk[]; |
@@ -433,2 +433,4 @@ "use strict"; | ||
return this.printTableIndexExpression(expression); | ||
case lua.SyntaxKind.ParenthesizedExpression: | ||
return this.printParenthesizedExpression(expression); | ||
default: | ||
@@ -586,2 +588,5 @@ throw new Error(`Tried to print unknown statement kind: ${lua.SyntaxKind[expression.kind]}`); | ||
} | ||
printParenthesizedExpression(expression) { | ||
return this.createSourceNode(expression, ["(", this.printExpression(expression.expression), ")"]); | ||
} | ||
printOperator(kind) { | ||
@@ -588,0 +593,0 @@ return new source_map_1.SourceNode(null, null, this.relativeSourcePath, LuaPrinter.operatorMap[kind]); |
@@ -106,3 +106,3 @@ "use strict"; | ||
const exportTable = exportScope && ts.isModuleDeclaration(exportScope) | ||
? (0, namespace_1.createModuleLocalNameIdentifier)(context, exportScope) | ||
? (0, namespace_1.createModuleLocalName)(context, exportScope) | ||
: (0, lua_ast_1.createExportsIdentifier)(); | ||
@@ -109,0 +109,0 @@ return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text)); |
@@ -56,10 +56,10 @@ "use strict"; | ||
} | ||
// When selecting the first element, we can shortcut | ||
if (ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0") { | ||
return { expression: table }; | ||
const canOmitSelect = ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0"; | ||
if (canOmitSelect) { | ||
// wrapping in parenthesis ensures only the first return value is used | ||
// https://www.lua.org/manual/5.1/manual.html#2.5 | ||
return { expression: lua.createParenthesizedExpression(table) }; | ||
} | ||
else { | ||
const selectIdentifier = lua.createIdentifier("select"); | ||
return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) }; | ||
} | ||
const selectIdentifier = lua.createIdentifier("select"); | ||
return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) }; | ||
} | ||
@@ -66,0 +66,0 @@ if (thisValueCapture) { |
@@ -21,6 +21,10 @@ "use strict"; | ||
function transformParameterDefaultValueDeclaration(context, parameterName, value, tsOriginal) { | ||
const parameterValue = value ? context.transformExpression(value) : undefined; | ||
const assignment = lua.createAssignmentStatement(parameterName, parameterValue); | ||
const { precedingStatements: statements, result: parameterValue } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(value)); | ||
if (!lua.isNilLiteral(parameterValue)) { | ||
statements.push(lua.createAssignmentStatement(parameterName, parameterValue)); | ||
} | ||
if (statements.length === 0) | ||
return undefined; | ||
const nilCondition = lua.createBinaryExpression(parameterName, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator); | ||
const ifBlock = lua.createBlock([assignment]); | ||
const ifBlock = lua.createBlock(statements, tsOriginal); | ||
return lua.createIfStatement(nilCondition, ifBlock, undefined, tsOriginal); | ||
@@ -90,3 +94,5 @@ } | ||
// Default binding parameter | ||
headerStatements.push(transformParameterDefaultValueDeclaration(context, identifier, declaration.initializer)); | ||
const initializer = transformParameterDefaultValueDeclaration(context, identifier, declaration.initializer); | ||
if (initializer) | ||
headerStatements.push(initializer); | ||
} | ||
@@ -100,3 +106,5 @@ // Binding pattern | ||
// Default parameter | ||
headerStatements.push(transformParameterDefaultValueDeclaration(context, (0, identifier_1.transformIdentifier)(context, declaration.name), declaration.initializer)); | ||
const initializer = transformParameterDefaultValueDeclaration(context, (0, identifier_1.transformIdentifier)(context, declaration.name), declaration.initializer); | ||
if (initializer) | ||
headerStatements.push(initializer); | ||
} | ||
@@ -103,0 +111,0 @@ } |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { FunctionVisitor, TransformationContext } from "../context"; | ||
export declare function createModuleLocalName(context: TransformationContext, module: ts.ModuleDeclaration): lua.Expression; | ||
export declare function createModuleLocalNameIdentifier(context: TransformationContext, declaration: ts.ModuleDeclaration): lua.Identifier; | ||
export declare const transformModuleDeclaration: FunctionVisitor<ts.ModuleDeclaration>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transformModuleDeclaration = exports.createModuleLocalNameIdentifier = void 0; | ||
exports.transformModuleDeclaration = exports.createModuleLocalNameIdentifier = exports.createModuleLocalName = void 0; | ||
const ts = require("typescript"); | ||
@@ -12,2 +12,11 @@ const lua = require("../../LuaAST"); | ||
const identifier_1 = require("./identifier"); | ||
function createModuleLocalName(context, module) { | ||
if (!ts.isSourceFile(module.parent) && ts.isModuleDeclaration(module.parent)) { | ||
const parentDeclaration = createModuleLocalName(context, module.parent); | ||
const name = createModuleLocalNameIdentifier(context, module); | ||
return lua.createTableIndexExpression(parentDeclaration, lua.createStringLiteral(name.text), module.name); | ||
} | ||
return createModuleLocalNameIdentifier(context, module); | ||
} | ||
exports.createModuleLocalName = createModuleLocalName; | ||
function createModuleLocalNameIdentifier(context, declaration) { | ||
@@ -18,3 +27,2 @@ const moduleSymbol = context.checker.getSymbolAtLocation(declaration.name); | ||
} | ||
// TODO: Should synthetic name nodes be escaped as well? | ||
return (0, identifier_1.transformIdentifier)(context, declaration.name); | ||
@@ -21,0 +29,0 @@ } |
{ | ||
"name": "typescript-to-lua", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!", | ||
@@ -66,3 +66,3 @@ "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua", | ||
"javascript-stringify": "^2.0.1", | ||
"jest": "^28.1.3", | ||
"jest": "^29.5.0", | ||
"jest-circus": "^29.5.0", | ||
@@ -72,3 +72,3 @@ "lua-types": "^2.13.0", | ||
"prettier": "^2.8.4", | ||
"ts-jest": "^28.0.8", | ||
"ts-jest": "^29.1.0", | ||
"ts-node": "^10.9.1", | ||
@@ -75,0 +75,0 @@ "typescript": "^5.0.2" |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1043186
14922