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
104 lines (103 loc) • 4.27 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 Project_1 = require("../Project");
const RojoProject_1 = require("../RojoProject");
const typeUtilities_1 = require("../typeUtilities");
const utility_1 = require("../utility");
const { version: VERSION } = require("./../../package.json");
function getRuntimeLibraryStatement(state, node) {
if (state.runtimeOverride) {
return state.runtimeOverride.trim() + "\n";
}
let link;
if (state.projectInfo.type === Project_1.ProjectType.Package) {
link = `_G[script]`;
}
else if (state.projectInfo.type === Project_1.ProjectType.Game) {
const runtimeLibPath = [...state.projectInfo.runtimeLibPath];
const service = runtimeLibPath.shift();
if (!typeUtilities_1.isRbxService(service)) {
throw new CompilerError_1.CompilerError(`"${service}" is not a valid Roblox Service!`, node, CompilerError_1.CompilerErrorType.InvalidService);
}
const path = `game:GetService("${service}")` + runtimeLibPath.map(v => `:WaitForChild("${v}")`).join("");
link = `require(${path})`;
}
else if (state.projectInfo.type === Project_1.ProjectType.Bundle) {
const rbxPath = state.rojoProject.getRbxFromFile(utility_1.transformPathToLua(state.rootDirPath, state.outDirPath, node.getFilePath())).path;
if (!rbxPath) {
throw new CompilerError_1.CompilerError(`Bundle could not resolve runtime library location!`, node, CompilerError_1.CompilerErrorType.BadRojo);
}
const rbxRelative = RojoProject_1.RojoProject.relative(rbxPath, state.projectInfo.runtimeLibPath);
let start = "script";
while (rbxRelative[0] === "..") {
rbxRelative.shift();
start += ".Parent";
}
const path = [start, ...rbxRelative].join(".");
link = `require(${path})`;
}
return `local TS = ${link};\n`;
}
function compileSourceFile(state, node) {
console.profile(node.getBaseName());
state.scriptContext = utility_1.getScriptContext(node);
const scriptType = utility_1.getScriptType(node);
let result = _1.compileStatementedNode(state, node);
/* istanbul ignore else */
if (state.isModule) {
if (scriptType !== utility_1.ScriptType.Module) {
throw new CompilerError_1.CompilerError("Attempted to export in a non-ModuleScript!", node, CompilerError_1.CompilerErrorType.ExportInNonModuleScript);
}
let hasExportEquals = false;
for (const descendant of node.getDescendantsOfKind(ts.SyntaxKind.ExportAssignment)) {
/* istanbul ignore else */
if (descendant.isExportEquals()) {
hasExportEquals = true;
break;
}
}
if (hasExportEquals) {
result = state.indent + `local _exports;\n` + result;
}
else {
result = state.indent + `local _exports = {};\n` + result;
}
result += state.indent + "return _exports;\n";
}
else {
if (scriptType === utility_1.ScriptType.Module) {
result += state.indent + "return nil;\n";
}
}
/* istanbul ignore next */
if (state.usesTSLibrary) {
result = getRuntimeLibraryStatement(state, node) + result;
}
const CURRENT_TIME = new Date().toLocaleString("en-US", {
day: "numeric",
hour: "numeric",
hour12: true,
minute: "numeric",
month: "long",
timeZoneName: "long",
year: "numeric",
});
const GENERATED_HEADER = `-- Compiled with https://roblox-ts.github.io v${VERSION}
-- ${CURRENT_TIME}
`;
result = GENERATED_HEADER + result;
console.profileEnd();
return result;
}
exports.compileSourceFile = compileSourceFile;
//# sourceMappingURL=sourceFile.js.map