typescript-to-lua
Version:
A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!
158 lines • 7.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.methodExtensionKinds = exports.IterableExtensionKind = exports.ExtensionKind = void 0;
exports.getExtensionKindForType = getExtensionKindForType;
exports.getExtensionKindForNode = getExtensionKindForNode;
exports.getExtensionKindForSymbol = getExtensionKindForSymbol;
exports.isLuaIterable = isLuaIterable;
exports.getIterableExtensionTypeForType = getIterableExtensionTypeForType;
exports.getIterableExtensionKindForNode = getIterableExtensionKindForNode;
exports.getNaryCallExtensionArgs = getNaryCallExtensionArgs;
exports.getUnaryCallExtensionArg = getUnaryCallExtensionArg;
exports.getBinaryCallExtensionArgs = getBinaryCallExtensionArgs;
const ts = require("typescript");
const diagnostics_1 = require("./diagnostics");
var ExtensionKind;
(function (ExtensionKind) {
ExtensionKind["MultiFunction"] = "MultiFunction";
ExtensionKind["RangeFunction"] = "RangeFunction";
ExtensionKind["VarargConstant"] = "VarargConstant";
ExtensionKind["AdditionOperatorType"] = "Addition";
ExtensionKind["AdditionOperatorMethodType"] = "AdditionMethod";
ExtensionKind["SubtractionOperatorType"] = "Subtraction";
ExtensionKind["SubtractionOperatorMethodType"] = "SubtractionMethod";
ExtensionKind["MultiplicationOperatorType"] = "Multiplication";
ExtensionKind["MultiplicationOperatorMethodType"] = "MultiplicationMethod";
ExtensionKind["DivisionOperatorType"] = "Division";
ExtensionKind["DivisionOperatorMethodType"] = "DivisionMethod";
ExtensionKind["ModuloOperatorType"] = "Modulo";
ExtensionKind["ModuloOperatorMethodType"] = "ModuloMethod";
ExtensionKind["PowerOperatorType"] = "Power";
ExtensionKind["PowerOperatorMethodType"] = "PowerMethod";
ExtensionKind["FloorDivisionOperatorType"] = "FloorDivision";
ExtensionKind["FloorDivisionOperatorMethodType"] = "FloorDivisionMethod";
ExtensionKind["BitwiseAndOperatorType"] = "BitwiseAnd";
ExtensionKind["BitwiseAndOperatorMethodType"] = "BitwiseAndMethod";
ExtensionKind["BitwiseOrOperatorType"] = "BitwiseOr";
ExtensionKind["BitwiseOrOperatorMethodType"] = "BitwiseOrMethod";
ExtensionKind["BitwiseExclusiveOrOperatorType"] = "BitwiseExclusiveOr";
ExtensionKind["BitwiseExclusiveOrOperatorMethodType"] = "BitwiseExclusiveOrMethod";
ExtensionKind["BitwiseLeftShiftOperatorType"] = "BitwiseLeftShift";
ExtensionKind["BitwiseLeftShiftOperatorMethodType"] = "BitwiseLeftShiftMethod";
ExtensionKind["BitwiseRightShiftOperatorType"] = "BitwiseRightShift";
ExtensionKind["BitwiseRightShiftOperatorMethodType"] = "BitwiseRightShiftMethod";
ExtensionKind["ConcatOperatorType"] = "Concat";
ExtensionKind["ConcatOperatorMethodType"] = "ConcatMethod";
ExtensionKind["LessThanOperatorType"] = "LessThan";
ExtensionKind["LessThanOperatorMethodType"] = "LessThanMethod";
ExtensionKind["GreaterThanOperatorType"] = "GreaterThan";
ExtensionKind["GreaterThanOperatorMethodType"] = "GreaterThanMethod";
ExtensionKind["NegationOperatorType"] = "Negation";
ExtensionKind["NegationOperatorMethodType"] = "NegationMethod";
ExtensionKind["BitwiseNotOperatorType"] = "BitwiseNot";
ExtensionKind["BitwiseNotOperatorMethodType"] = "BitwiseNotMethod";
ExtensionKind["LengthOperatorType"] = "Length";
ExtensionKind["LengthOperatorMethodType"] = "LengthMethod";
ExtensionKind["TableNewType"] = "TableNew";
ExtensionKind["TableDeleteType"] = "TableDelete";
ExtensionKind["TableDeleteMethodType"] = "TableDeleteMethod";
ExtensionKind["TableGetType"] = "TableGet";
ExtensionKind["TableGetMethodType"] = "TableGetMethod";
ExtensionKind["TableHasType"] = "TableHas";
ExtensionKind["TableHasMethodType"] = "TableHasMethod";
ExtensionKind["TableSetType"] = "TableSet";
ExtensionKind["TableSetMethodType"] = "TableSetMethod";
ExtensionKind["TableAddKeyType"] = "TableAddKey";
ExtensionKind["TableAddKeyMethodType"] = "TableAddKeyMethod";
ExtensionKind["TableIsEmptyType"] = "TableIsEmpty";
ExtensionKind["TableIsEmptyMethodType"] = "TableIsEmptyMethod";
})(ExtensionKind || (exports.ExtensionKind = ExtensionKind = {}));
const extensionValues = new Set(Object.values(ExtensionKind));
function getExtensionKindForType(context, type) {
const value = getPropertyValue(context, type, "__tstlExtension");
if (value && extensionValues.has(value)) {
return value;
}
}
const excludedTypeFlags = ((1 << 18) - 1) | // All flags from Any...Never
ts.TypeFlags.Index |
ts.TypeFlags.NonPrimitive;
function getPropertyValue(context, type, propertyName) {
if (type.flags & excludedTypeFlags)
return;
const property = type.getProperty(propertyName);
if (!property)
return undefined;
const propertyType = context.checker.getTypeOfSymbolAtLocation(property, context.sourceFile);
if (propertyType.isStringLiteral())
return propertyType.value;
}
function getExtensionKindForNode(context, node) {
const originalNode = ts.getOriginalNode(node);
let type = context.checker.getTypeAtLocation(originalNode);
if (ts.isOptionalChain(originalNode)) {
type = context.checker.getNonNullableType(type);
}
return getExtensionKindForType(context, type);
}
function getExtensionKindForSymbol(context, symbol) {
const type = context.checker.getTypeOfSymbolAtLocation(symbol, context.sourceFile);
return getExtensionKindForType(context, type);
}
var IterableExtensionKind;
(function (IterableExtensionKind) {
IterableExtensionKind["Iterable"] = "Iterable";
IterableExtensionKind["Pairs"] = "Pairs";
IterableExtensionKind["PairsKey"] = "PairsKey";
})(IterableExtensionKind || (exports.IterableExtensionKind = IterableExtensionKind = {}));
function isLuaIterable(context, type) {
return getPropertyValue(context, type, "__tstlIterable") !== undefined;
}
function getIterableExtensionTypeForType(context, type) {
const value = getPropertyValue(context, type, "__tstlIterable");
if (value && value in IterableExtensionKind) {
return value;
}
}
function getIterableExtensionKindForNode(context, node) {
const type = context.checker.getTypeAtLocation(node);
return getIterableExtensionTypeForType(context, type);
}
exports.methodExtensionKinds = new Set(Object.values(ExtensionKind).filter(key => key.endsWith("Method")));
function getNaryCallExtensionArgs(context, node, kind, numArgs) {
let expressions;
if (node.arguments.some(ts.isSpreadElement)) {
context.diagnostics.push((0, diagnostics_1.invalidSpreadInCallExtension)(node));
return undefined;
}
if (exports.methodExtensionKinds.has(kind)) {
if (!(ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression))) {
context.diagnostics.push((0, diagnostics_1.invalidMethodCallExtensionUse)(node));
return undefined;
}
if (node.arguments.length < numArgs - 1) {
// assumed to be TS error
return undefined;
}
expressions = [node.expression.expression, ...node.arguments];
}
else {
if (node.arguments.length < numArgs) {
// assumed to be TS error
return undefined;
}
expressions = node.arguments;
}
return expressions;
}
function getUnaryCallExtensionArg(context, node, kind) {
var _a;
return (_a = getNaryCallExtensionArgs(context, node, kind, 1)) === null || _a === void 0 ? void 0 : _a[0];
}
function getBinaryCallExtensionArgs(context, node, kind) {
const expressions = getNaryCallExtensionArgs(context, node, kind, 2);
if (expressions === undefined)
return undefined;
return [expressions[0], expressions[1]];
}
//# sourceMappingURL=language-extensions.js.map