UNPKG

typescript-to-lua

Version:

A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!

156 lines 7.08 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.createDefaultExportStringLiteral = void 0; exports.hasDefaultExportModifier = hasDefaultExportModifier; exports.hasExportModifier = hasExportModifier; exports.shouldBeExported = shouldBeExported; exports.getExportedSymbolDeclaration = getExportedSymbolDeclaration; exports.getSymbolFromIdentifier = getSymbolFromIdentifier; exports.getIdentifierExportScope = getIdentifierExportScope; exports.getSymbolExportScope = getSymbolExportScope; exports.getExportedSymbolsFromScope = getExportedSymbolsFromScope; exports.getDependenciesOfSymbol = getDependenciesOfSymbol; exports.isSymbolExported = isSymbolExported; exports.isSymbolExportedFromScope = isSymbolExportedFromScope; exports.addExportToIdentifier = addExportToIdentifier; exports.createExportedIdentifier = createExportedIdentifier; exports.createDefaultExportExpression = createDefaultExportExpression; const ts = __importStar(require("typescript")); const lua = __importStar(require("../../LuaAST")); const namespace_1 = require("../visitors/namespace"); const lua_ast_1 = require("./lua-ast"); const symbols_1 = require("./symbols"); const typescript_1 = require("./typescript"); function hasDefaultExportModifier(node) { var _a; return (ts.canHaveModifiers(node) && ((_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword)) === true); } function hasExportModifier(node) { var _a; return (ts.canHaveModifiers(node) && ((_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword)) === true); } function shouldBeExported(node) { if (hasExportModifier(node)) { // Don't export if we're inside a namespace (module declaration) return ts.findAncestor(node, ts.isModuleDeclaration) === undefined; } return false; } const createDefaultExportStringLiteral = (original) => lua.createStringLiteral("default", original); exports.createDefaultExportStringLiteral = createDefaultExportStringLiteral; function getExportedSymbolDeclaration(symbol) { const declarations = symbol.getDeclarations(); if (declarations) { return declarations.find(d => (ts.getCombinedModifierFlags(d) & ts.ModifierFlags.Export) !== 0); } } function getSymbolFromIdentifier(context, identifier) { if (identifier.symbolId !== undefined) { const symbolInfo = (0, symbols_1.getSymbolInfo)(context, identifier.symbolId); if (symbolInfo !== undefined) { return symbolInfo.symbol; } } } function getIdentifierExportScope(context, identifier) { const symbol = getSymbolFromIdentifier(context, identifier); if (!symbol) { return undefined; } return getSymbolExportScope(context, symbol); } function isGlobalAugmentation(module) { return (module.flags & ts.NodeFlags.GlobalAugmentation) !== 0; } function getSymbolExportScope(context, symbol) { const exportedDeclaration = getExportedSymbolDeclaration(symbol); if (!exportedDeclaration) { return undefined; } const scope = (0, typescript_1.findFirstNodeAbove)(exportedDeclaration, (n) => ts.isSourceFile(n) || ts.isModuleDeclaration(n)); if (!scope) { return undefined; } if (ts.isModuleDeclaration(scope) && isGlobalAugmentation(scope)) { return undefined; } if (!isSymbolExportedFromScope(context, symbol, scope)) { return undefined; } return scope; } function getExportedSymbolsFromScope(context, scope) { const scopeSymbol = context.checker.getSymbolAtLocation(ts.isSourceFile(scope) ? scope : scope.name); if ((scopeSymbol === null || scopeSymbol === void 0 ? void 0 : scopeSymbol.exports) === undefined) { return []; } // ts.Iterator is not a ES6-compatible iterator, because TypeScript targets ES5 const it = { [Symbol.iterator]: () => scopeSymbol.exports.values() }; return [...it]; } function getDependenciesOfSymbol(context, originalSymbol) { return getExportedSymbolsFromScope(context, context.sourceFile).filter(exportSymbol => { var _a; return (_a = exportSymbol.declarations) === null || _a === void 0 ? void 0 : _a.filter(ts.isExportSpecifier).map(context.checker.getExportSpecifierLocalTargetSymbol).includes(originalSymbol); }); } function isSymbolExported(context, symbol) { return (getExportedSymbolDeclaration(symbol) !== undefined || // Symbol may have been exported separately (e.g. 'const foo = "bar"; export { foo }') isSymbolExportedFromScope(context, symbol, context.sourceFile)); } function isSymbolExportedFromScope(context, symbol, scope) { return getExportedSymbolsFromScope(context, scope).includes(symbol); } function addExportToIdentifier(context, identifier) { const exportScope = getIdentifierExportScope(context, identifier); return exportScope ? createExportedIdentifier(context, identifier, exportScope) : identifier; } function createExportedIdentifier(context, identifier, exportScope) { if (!identifier.exportable) { return identifier; } const exportTable = exportScope && ts.isModuleDeclaration(exportScope) ? (0, namespace_1.createModuleLocalName)(context, exportScope) : (0, lua_ast_1.createExportsIdentifier)(); return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text)); } function createDefaultExportExpression(node) { return lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), (0, exports.createDefaultExportStringLiteral)(node), node); } //# sourceMappingURL=export.js.map