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
78 lines • 3.72 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(".");
exports.BUILT_INS = ["Promise", "Symbol", "typeIs", "opcall"];
exports.replacements = new Map([["undefined", "nil"], ["typeOf", "typeof"]]);
function compileIdentifier(state, node, isDefinition = false) {
let name = node.getText();
const replacement = exports.replacements.get(name);
if (replacement) {
return replacement;
}
_1.checkReserved(name, node);
if (exports.BUILT_INS.indexOf(name) !== -1) {
state.usesTSLibrary = true;
name = `TS.${name}`;
}
const definitions = isDefinition ? [node] : node.getDefinitions().map(def => def.getNode());
for (const definition of definitions) {
// I have no idea why, but getDefinitionNodes() cannot replace this
if (definition.getSourceFile() === node.getSourceFile()) {
let parent = definition;
do {
if (ts.TypeGuards.isVariableStatement(parent)) {
if (parent.hasExportKeyword()) {
const declarationKind = parent.getDeclarationKind();
if (declarationKind === ts.VariableDeclarationKind.Let) {
return state.getExportContextName(parent) + "." + name;
}
else if (declarationKind === ts.VariableDeclarationKind.Const) {
const idContext = node.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);
const defContext = parent.getFirstAncestorByKind(ts.SyntaxKind.ModuleDeclaration);
if (idContext && defContext && idContext !== defContext) {
state.pushHoistStack(`local ${name} = ${state.getNameForContext(defContext)}.${name}`);
}
}
}
break;
}
else if (ts.TypeGuards.isNamespaceDeclaration(parent)) {
// If within a namespace, scope it. If it is a namespace, don't
if (parent !== definition.getParent()) {
const parentName = state.namespaceStack.get(parent.getName());
if (parentName) {
return parentName + "." + name;
}
}
break;
}
else if (parent.getKind() === ts.SyntaxKind.OpenParenToken) {
parent = parent.getParent();
if (!ts.TypeGuards.isArrowFunction(parent)) {
break;
}
}
else if (!ts.TypeGuards.isVariableDeclarationList(parent) &&
!ts.TypeGuards.isIdentifier(parent) &&
!ts.TypeGuards.isBindingElement(parent) &&
!ts.TypeGuards.isArrayBindingPattern(parent) &&
!ts.TypeGuards.isVariableDeclaration(parent) &&
!ts.TypeGuards.isObjectBindingPattern(parent)) {
break;
}
parent = parent.getParent();
} while (parent);
}
}
return state.getAlias(name);
}
exports.compileIdentifier = compileIdentifier;
//# sourceMappingURL=identifier.js.map