|
1 | 1 | import * as ts from "typescript"; |
| 2 | +import * as path from "path"; |
2 | 3 | import { Decorator, DecoratorKind } from "./Decorator"; |
3 | 4 | import * as tstl from "./LuaAST"; |
| 5 | +import * as TSTLErrors from "./TSTLErrors"; |
| 6 | +import { EmitResolver } from "./LuaTransformer"; |
4 | 7 |
|
5 | 8 | export enum ContextType { |
6 | 9 | None, |
@@ -57,6 +60,47 @@ export function isAssignmentPattern(node: ts.Node): node is ts.AssignmentPattern |
57 | 60 | return ts.isObjectLiteralExpression(node) || ts.isArrayLiteralExpression(node); |
58 | 61 | } |
59 | 62 |
|
| 63 | +export function getExportable(exportSpecifiers: ts.NamedExports, resolver: EmitResolver): ts.ExportSpecifier[] { |
| 64 | + return exportSpecifiers.elements.filter(exportSpecifier => resolver.isValueAliasDeclaration(exportSpecifier)); |
| 65 | +} |
| 66 | + |
| 67 | +export function isDefaultExportSpecifier(node: ts.ExportSpecifier): boolean { |
| 68 | + return ( |
| 69 | + (node.name !== undefined && node.name.originalKeywordKind === ts.SyntaxKind.DefaultKeyword) || |
| 70 | + (node.propertyName !== undefined && node.propertyName.originalKeywordKind === ts.SyntaxKind.DefaultKeyword) |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export function hasDefaultExportModifier(modifiers?: ts.NodeArray<ts.Modifier>): boolean { |
| 75 | + return modifiers ? modifiers.some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword) : false; |
| 76 | +} |
| 77 | + |
| 78 | +export function shouldResolveModulePath(moduleSpecifier: ts.Expression, checker: ts.TypeChecker): boolean { |
| 79 | + const moduleOwnerSymbol = checker.getSymbolAtLocation(moduleSpecifier); |
| 80 | + if (moduleOwnerSymbol) { |
| 81 | + const decorators = new Map<DecoratorKind, Decorator>(); |
| 82 | + collectCustomDecorators(moduleOwnerSymbol, checker, decorators); |
| 83 | + if (decorators.has(DecoratorKind.NoResolution)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + return true; |
| 88 | +} |
| 89 | + |
| 90 | +export function shouldBeImported( |
| 91 | + importNode: ts.ImportClause | ts.ImportSpecifier, |
| 92 | + checker: ts.TypeChecker, |
| 93 | + resolver: EmitResolver |
| 94 | +): boolean { |
| 95 | + const decorators = getCustomDecorators(checker.getTypeAtLocation(importNode), checker); |
| 96 | + |
| 97 | + return ( |
| 98 | + resolver.isReferencedAliasDeclaration(importNode) && |
| 99 | + !decorators.has(DecoratorKind.Extension) && |
| 100 | + !decorators.has(DecoratorKind.MetaExtension) |
| 101 | + ); |
| 102 | +} |
| 103 | + |
60 | 104 | export function isFileModule(sourceFile: ts.SourceFile): boolean { |
61 | 105 | return sourceFile.statements.some(isStatementExported); |
62 | 106 | } |
@@ -968,3 +1012,55 @@ export function isSimpleExpression(expression: tstl.Expression): boolean { |
968 | 1012 | } |
969 | 1013 | return true; |
970 | 1014 | } |
| 1015 | + |
| 1016 | +export function getAbsoluteImportPath( |
| 1017 | + relativePath: string, |
| 1018 | + directoryPath: string, |
| 1019 | + options: ts.CompilerOptions |
| 1020 | +): string { |
| 1021 | + if (relativePath.charAt(0) !== "." && options.baseUrl) { |
| 1022 | + return path.resolve(options.baseUrl, relativePath); |
| 1023 | + } |
| 1024 | + |
| 1025 | + return path.resolve(directoryPath, relativePath); |
| 1026 | +} |
| 1027 | + |
| 1028 | +export function getImportPath( |
| 1029 | + fileName: string, |
| 1030 | + relativePath: string, |
| 1031 | + node: ts.Node, |
| 1032 | + options: ts.CompilerOptions |
| 1033 | +): string { |
| 1034 | + const rootDir = options.rootDir ? path.resolve(options.rootDir) : path.resolve("."); |
| 1035 | + |
| 1036 | + const absoluteImportPath = path.format( |
| 1037 | + path.parse(getAbsoluteImportPath(relativePath, path.dirname(fileName), options)) |
| 1038 | + ); |
| 1039 | + const absoluteRootDirPath = path.format(path.parse(rootDir)); |
| 1040 | + if (absoluteImportPath.includes(absoluteRootDirPath)) { |
| 1041 | + return formatPathToLuaPath(absoluteImportPath.replace(absoluteRootDirPath, "").slice(1)); |
| 1042 | + } else { |
| 1043 | + throw TSTLErrors.UnresolvableRequirePath( |
| 1044 | + node, |
| 1045 | + `Cannot create require path. Module does not exist within --rootDir`, |
| 1046 | + relativePath |
| 1047 | + ); |
| 1048 | + } |
| 1049 | +} |
| 1050 | + |
| 1051 | +export function getExportPath(fileName: string, options: ts.CompilerOptions): string { |
| 1052 | + const rootDir = options.rootDir ? path.resolve(options.rootDir) : path.resolve("."); |
| 1053 | + |
| 1054 | + const absolutePath = path.resolve(fileName.replace(/.ts$/, "")); |
| 1055 | + const absoluteRootDirPath = path.format(path.parse(rootDir)); |
| 1056 | + return formatPathToLuaPath(absolutePath.replace(absoluteRootDirPath, "").slice(1)); |
| 1057 | +} |
| 1058 | + |
| 1059 | +export function formatPathToLuaPath(filePath: string): string { |
| 1060 | + filePath = filePath.replace(/\.json$/, ""); |
| 1061 | + if (process.platform === "win32") { |
| 1062 | + // Windows can use backslashes |
| 1063 | + filePath = filePath.replace(/\.\\/g, "").replace(/\\/g, "."); |
| 1064 | + } |
| 1065 | + return filePath.replace(/\.\//g, "").replace(/\//g, "."); |
| 1066 | +} |
0 commit comments