UNPKG

typescript-to-lua

Version:

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

123 lines 5.69 kB
"use strict"; 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 = require("typescript"); const lua = 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