UNPKG

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

223 lines 7.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; 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 path_1 = __importDefault(require("path")); const ts = __importStar(require("ts-morph")); const ProjectError_1 = require("./errors/ProjectError"); const luaIdentifierRegex = /^[A-Za-z_][A-Za-z0-9_]*$/; function isValidLuaIdentifier(id) { return luaIdentifierRegex.test(id); } exports.isValidLuaIdentifier = isValidLuaIdentifier; function safeLuaIndex(parent, child) { if (isValidLuaIdentifier(child)) { return `${parent}.${child}`; } else { return `${parent}["${child}"]`; } } exports.safeLuaIndex = safeLuaIndex; function isCompiledIdentifier(s) { return isValidLuaIdentifier(s); } exports.isCompiledIdentifier = isCompiledIdentifier; function joinIndentedLines(lines, numTabs = 0) { if (lines.length > 0) { if (numTabs > 0) { const sep = "\t".repeat(numTabs); return lines.join("").replace(/.+/g, a => sep + a); } else { return lines.join(""); } } else { return ""; } } exports.joinIndentedLines = joinIndentedLines; function stripExtensions(fileName) { const ext = path_1.default.extname(fileName); if (ext.length > 0) { return stripExtensions(path_1.default.basename(fileName, ext)); } else { return fileName; } } exports.stripExtensions = stripExtensions; const scriptContextCache = new Map(); function clearContextCache() { scriptContextCache.clear(); } exports.clearContextCache = clearContextCache; var ScriptType; (function (ScriptType) { ScriptType[ScriptType["Server"] = 0] = "Server"; ScriptType[ScriptType["Client"] = 1] = "Client"; ScriptType[ScriptType["Module"] = 2] = "Module"; })(ScriptType = exports.ScriptType || (exports.ScriptType = {})); function getScriptType(file) { const filePath = file.getFilePath(); const ext = path_1.default.extname(filePath); if (ext !== ".ts" && ext !== ".tsx") { throw new ProjectError_1.ProjectError(`Unexpected extension type: ${ext}`, ProjectError_1.ProjectErrorType.UnexpectedExtensionType); } const subext = path_1.default.extname(path_1.default.basename(filePath, ext)); if (subext === ".server") { return ScriptType.Server; } else if (subext === ".client") { return ScriptType.Client; } else { return ScriptType.Module; } } exports.getScriptType = getScriptType; var ScriptContext; (function (ScriptContext) { ScriptContext[ScriptContext["None"] = 0] = "None"; ScriptContext[ScriptContext["Client"] = 1] = "Client"; ScriptContext[ScriptContext["Server"] = 2] = "Server"; ScriptContext[ScriptContext["Both"] = 3] = "Both"; })(ScriptContext = exports.ScriptContext || (exports.ScriptContext = {})); function getScriptContext(file, seen = new Set()) { const filePath = file.getFilePath(); if (scriptContextCache.has(filePath)) { return scriptContextCache.get(filePath); } // prevent infinite recursion if (seen.has(filePath)) { return ScriptContext.None; } seen.add(filePath); const scriptType = getScriptType(file); if (scriptType === ScriptType.Server) { return ScriptContext.Server; } else if (scriptType === ScriptType.Client) { return ScriptContext.Client; } else { let isServer = false; let isClient = false; for (const referencingFile of file.getReferencingSourceFiles()) { const referenceContext = getScriptContext(referencingFile, seen); if (referenceContext === ScriptContext.Server) { isServer = true; } else if (referenceContext === ScriptContext.Client) { isClient = true; } else if (referenceContext === ScriptContext.Both) { isServer = true; isClient = true; } } if (isServer && isClient) { return ScriptContext.Both; } else if (isServer) { return ScriptContext.Server; } else if (isClient) { return ScriptContext.Client; } else { return ScriptContext.None; } } } exports.getScriptContext = getScriptContext; function red(text) { return `\x1b[31m${text}\x1b[0m`; } exports.red = red; function yellow(text) { return `\x1b[33m${text}\x1b[0m`; } exports.yellow = yellow; function bold(text) { return `\x1b[1m${text}\x1b[0m`; } exports.bold = bold; function suggest(text) { return `...\t${yellow(text)}`; } exports.suggest = suggest; function isIdentifierWhoseDefinitionMatchesNode(node, potentialDefinition) { if (ts.TypeGuards.isIdentifier(node)) { for (const def of node.getDefinitions()) { if (def.getNode() === potentialDefinition) { return true; } } } return false; } exports.isIdentifierWhoseDefinitionMatchesNode = isIdentifierWhoseDefinitionMatchesNode; function skipNodesDownwards(exp, dontSkipParenthesis) { if (exp) { while ((!dontSkipParenthesis && ts.TypeGuards.isParenthesizedExpression(exp)) || ts.TypeGuards.isNonNullExpression(exp)) { exp = exp.getExpression(); } return exp; } } exports.skipNodesDownwards = skipNodesDownwards; function skipNodesUpwards(exp, dontSkipParenthesis) { if (exp) { while ((!dontSkipParenthesis && ts.TypeGuards.isParenthesizedExpression(exp)) || ts.TypeGuards.isNonNullExpression(exp)) { exp = exp.getParent(); } return exp; } } exports.skipNodesUpwards = skipNodesUpwards; function makeSetStatement(varToSet, value) { if (varToSet === "return") { return `return ${value}`; } else { return `${varToSet} = ${value}`; } } exports.makeSetStatement = makeSetStatement; function transformPathToLua(rootPath, outPath, filePath) { const relativeToRoot = path_1.default.dirname(path_1.default.relative(rootPath, filePath)); let name = path_1.default.basename(filePath, path_1.default.extname(filePath)); const exts = new Array(); while (true) { const ext = path_1.default.extname(name); if (ext.length > 0) { exts.unshift(ext); name = path_1.default.basename(name, ext); } else { break; } } if (exts[exts.length - 1] === ".d") { exts.pop(); } if (name === "index") { name = "init"; } const luaName = name + exts.join("") + ".lua"; return path_1.default.join(outPath, relativeToRoot, luaName); } exports.transformPathToLua = transformPathToLua; //# sourceMappingURL=utility.js.map