roblox-ts
Version:
<div align="center"><img width=25% src="https://i.imgur.com/yCjHmng.png"></div> <h1 align="center"><a href="https://roblox-ts.github.io/">roblox-ts</a></h1> <div align="center">A TypeScript-to-Lua Compiler for Roblox</div> <br> <div align="center"> <a hr
120 lines • 5.23 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("ts-morph"));
const _1 = require(".");
const CompilerError_1 = require("../errors/CompilerError");
const utility_1 = require("../utility");
function assignMembers(state, from, target) {
state.pushIdStack();
const i = state.getNewId();
const v = state.getNewId();
const str = state.indent + `for ${i}, ${v} in pairs(${from}) do ${target}[${i}] = ${v}; end;\n`;
state.popIdStack();
return str;
}
function compileObjectLiteralExpression(state, node) {
const properties = node.getProperties();
if (properties.length === 0) {
return "{}";
}
const lines = new Array();
let hasContext = false;
let id = "";
let line;
for (const prop of properties) {
const context = state.enterPrecedingStatementContext();
if (ts.TypeGuards.isPropertyAssignment(prop) || ts.TypeGuards.isShorthandPropertyAssignment(prop)) {
let lhs;
let n = 0;
let child = prop.getChildAtIndex(n);
while (ts.TypeGuards.isJSDoc(child)) {
child = prop.getChildAtIndex(++n);
}
child = utility_1.skipNodesDownwards(child);
if (ts.TypeGuards.isComputedPropertyName(child)) {
lhs = utility_1.skipNodesDownwards(child.getExpression());
}
else if (ts.TypeGuards.isIdentifier(child) ||
ts.TypeGuards.isStringLiteral(child) ||
ts.TypeGuards.isNumericLiteral(child)) {
lhs = child;
}
else {
throw new CompilerError_1.CompilerError(`Unexpected type of object index! (${child.getKindName()})`, child, CompilerError_1.CompilerErrorType.UnexpectedObjectIndex);
}
let rhs; // You may want to move this around
if (ts.TypeGuards.isShorthandPropertyAssignment(prop) && ts.TypeGuards.isIdentifier(child)) {
lhs = prop.getNameNode();
rhs = child;
}
else {
rhs = utility_1.skipNodesDownwards(prop.getInitializerOrThrow());
}
let lhsStr = _1.compileExpression(state, lhs);
state.enterPrecedingStatementContext();
const rhsStr = _1.compileExpression(state, rhs);
const rhsContext = state.exitPrecedingStatementContext();
if (rhsContext.length > 0) {
if (!ts.TypeGuards.isIdentifier(lhs) && !context.isPushed) {
lhsStr = state.pushPrecedingStatementToReuseableId(lhs, lhsStr, rhsContext);
}
context.push(...rhsContext);
context.isPushed = rhsContext.isPushed;
}
line = `${ts.TypeGuards.isIdentifier(lhs) ? lhsStr : `[${lhsStr}]`} = ${rhsStr};\n`;
}
else if (ts.TypeGuards.isMethodDeclaration(prop)) {
line = "";
}
else if (ts.TypeGuards.isSpreadAssignment(prop)) {
line = _1.compileExpression(state, utility_1.skipNodesDownwards(prop.getExpression()));
}
else {
throw new CompilerError_1.CompilerError(`Unexpected property type in object! Got ${prop.getKindName()}`, prop, CompilerError_1.CompilerErrorType.BadObjectPropertyType, true);
}
state.exitPrecedingStatementContext();
if (hasContext ||
context.length > 0 ||
ts.TypeGuards.isSpreadAssignment(prop) ||
ts.TypeGuards.isMethodDeclaration(prop)) {
if (!hasContext) {
id = state.pushToDeclarationOrNewId(node, "{}", declaration => declaration.isIdentifier);
}
if (ts.TypeGuards.isSpreadAssignment(prop)) {
line = assignMembers(state, line, id);
}
else if (ts.TypeGuards.isMethodDeclaration(prop)) {
line = state.indent + _1.compileMethodDeclaration(state, prop, id + ":").trimLeft();
}
else {
line = state.indent + id + (line.startsWith("[") ? "" : ".") + line;
}
if (hasContext) {
state.pushPrecedingStatements(node, ...context, line);
}
else {
state.pushPrecedingStatements(node, ...lines.map(current => state.indent + id + (current.startsWith("[") ? "" : ".") + current), ...context, line);
hasContext = true;
}
}
else {
lines.push(line);
}
}
if (id) {
state.getCurrentPrecedingStatementContext(node).isPushed = true;
return id;
}
else {
return "{\n" + lines.map(myLine => state.indent + utility_1.joinIndentedLines([myLine], 1)).join("") + state.indent + "}";
}
}
exports.compileObjectLiteralExpression = compileObjectLiteralExpression;
//# sourceMappingURL=object.js.map