typescript-to-lua
Advanced tools
Comparing version 1.16.2 to 1.16.3
@@ -14,7 +14,13 @@ "use strict"; | ||
} | ||
else if (ts.isBlock(s) && containsBreakOrReturn(s.getChildren())) { | ||
else if (ts.isBlock(s) && containsBreakOrReturn(s.statements)) { | ||
return true; | ||
} | ||
else if (s.kind === ts.SyntaxKind.SyntaxList && containsBreakOrReturn(s.getChildren())) { | ||
return true; | ||
else if (s.kind === ts.SyntaxKind.SyntaxList) { | ||
// We cannot use getChildren() because that breaks when using synthetic nodes from transformers | ||
// So get children the long way | ||
const children = []; | ||
ts.forEachChild(s, c => children.push(c)); | ||
if (containsBreakOrReturn(children)) { | ||
return true; | ||
} | ||
} | ||
@@ -21,0 +27,0 @@ } |
@@ -32,2 +32,3 @@ import * as ts from "typescript"; | ||
beforeEmit?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost, result: EmitFile[]) => ts.Diagnostic[] | void; | ||
moduleResolution?: (moduleIdentifier: string, requiringFile: string, options: CompilerOptions, emitHost: EmitHost) => string | undefined; | ||
} | ||
@@ -34,0 +35,0 @@ export declare function getPlugins(program: ts.Program): { |
import * as ts from "typescript"; | ||
import { EmitHost, ProcessedFile } from "./utils"; | ||
import { Plugin } from "./plugins"; | ||
interface ResolutionResult { | ||
@@ -7,3 +8,3 @@ resolvedFiles: ProcessedFile[]; | ||
} | ||
export declare function resolveDependencies(program: ts.Program, files: ProcessedFile[], emitHost: EmitHost): ResolutionResult; | ||
export declare function resolveDependencies(program: ts.Program, files: ProcessedFile[], emitHost: EmitHost, plugins: Plugin[]): ResolutionResult; | ||
export {}; |
@@ -22,6 +22,7 @@ "use strict"; | ||
class ResolutionContext { | ||
constructor(program, options, emitHost) { | ||
constructor(program, options, emitHost, plugins) { | ||
this.program = program; | ||
this.options = options; | ||
this.emitHost = emitHost; | ||
this.plugins = plugins; | ||
this.diagnostics = []; | ||
@@ -56,2 +57,3 @@ this.resolvedFiles = new Map(); | ||
resolveImport(file, required) { | ||
var _a; | ||
// Do no resolve lualib - always use the lualib of the application entry point, not the lualib from external packages | ||
@@ -68,3 +70,3 @@ if (required.requirePath === "lualib_bundle") { | ||
} | ||
const dependencyPath = this.resolveDependencyPath(file, required.requirePath); | ||
const dependencyPath = (_a = this.resolveDependencyPathsWithPlugins(file, required.requirePath)) !== null && _a !== void 0 ? _a : this.resolveDependencyPath(file, required.requirePath); | ||
if (!dependencyPath) | ||
@@ -83,2 +85,42 @@ return this.couldNotResolveImport(required, file); | ||
} | ||
resolveDependencyPathsWithPlugins(requiringFile, dependency) { | ||
const requiredFromLuaFile = requiringFile.fileName.endsWith(".lua"); | ||
for (const plugin of this.plugins) { | ||
if (plugin.moduleResolution != null) { | ||
const pluginResolvedPath = plugin.moduleResolution(dependency, requiringFile.fileName, this.options, this.emitHost); | ||
if (pluginResolvedPath !== undefined) { | ||
// If lua file is in node_module | ||
if (requiredFromLuaFile && isNodeModulesFile(requiringFile.fileName)) { | ||
// If requiring file is in lua module, try to resolve sibling in that file first | ||
const resolvedNodeModulesFile = this.resolveLuaDependencyPathFromNodeModules(requiringFile, pluginResolvedPath); | ||
if (resolvedNodeModulesFile) { | ||
if (this.options.tstlVerbose) { | ||
console.log(`Resolved file path for module ${dependency} to path ${pluginResolvedPath} using plugin.`); | ||
} | ||
return resolvedNodeModulesFile; | ||
} | ||
} | ||
const resolvedPath = this.formatPathToFile(pluginResolvedPath, requiringFile); | ||
const fileFromPath = this.getFileFromPath(resolvedPath); | ||
if (fileFromPath) { | ||
if (this.options.tstlVerbose) { | ||
console.log(`Resolved file path for module ${dependency} to path ${pluginResolvedPath} using plugin.`); | ||
} | ||
return fileFromPath; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
formatPathToFile(targetPath, required) { | ||
var _a; | ||
const isRelative = ["/", "./", "../"].some(p => targetPath.startsWith(p)); | ||
// // If the import is relative, always resolve it relative to the requiring file | ||
// // If the import is not relative, resolve it relative to options.baseUrl if it is set | ||
const fileDirectory = path.dirname(required.fileName); | ||
const relativeTo = isRelative ? fileDirectory : (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : fileDirectory; | ||
// // Check if file is a file in the project | ||
const resolvedPath = path.join(relativeTo, targetPath); | ||
return resolvedPath; | ||
} | ||
processDependency(dependencyPath) { | ||
@@ -109,3 +151,2 @@ if (this.processedDependencies.has(dependencyPath)) | ||
resolveDependencyPath(requiringFile, dependency) { | ||
var _a; | ||
const fileDirectory = path.dirname(requiringFile.fileName); | ||
@@ -123,9 +164,4 @@ if (this.options.tstlVerbose) { | ||
} | ||
// Check if the import is relative | ||
const isRelative = ["/", "./", "../"].some(p => dependency.startsWith(p)); | ||
// If the import is relative, always resolve it relative to the requiring file | ||
// If the import is not relative, resolve it relative to options.baseUrl if it is set | ||
const relativeTo = isRelative ? fileDirectory : (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : fileDirectory; | ||
// Check if file is a file in the project | ||
const resolvedPath = path.join(relativeTo, dependencyPath); | ||
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile); | ||
const fileFromPath = this.getFileFromPath(resolvedPath); | ||
@@ -244,5 +280,5 @@ if (fileFromPath) | ||
} | ||
function resolveDependencies(program, files, emitHost) { | ||
function resolveDependencies(program, files, emitHost, plugins) { | ||
const options = program.getCompilerOptions(); | ||
const resolutionContext = new ResolutionContext(program, options, emitHost); | ||
const resolutionContext = new ResolutionContext(program, options, emitHost, plugins); | ||
// Resolve dependencies for all processed files | ||
@@ -249,0 +285,0 @@ for (const file of files) { |
import * as ts from "typescript"; | ||
import { Plugin } from "./plugins"; | ||
import { TranspileOptions } from "./transpile"; | ||
@@ -19,3 +20,3 @@ import { EmitFile, EmitHost, ProcessedFile } from "./utils"; | ||
private emitFiles; | ||
protected getEmitPlan(program: ts.Program, diagnostics: ts.Diagnostic[], files: ProcessedFile[]): { | ||
protected getEmitPlan(program: ts.Program, diagnostics: ts.Diagnostic[], files: ProcessedFile[], plugins: Plugin[]): { | ||
emitPlan: EmitFile[]; | ||
@@ -22,0 +23,0 @@ }; |
@@ -26,3 +26,3 @@ "use strict"; | ||
}); | ||
const { emitPlan } = this.getEmitPlan(program, transpileDiagnostics, freshFiles); | ||
const { emitPlan } = this.getEmitPlan(program, transpileDiagnostics, freshFiles, plugins); | ||
const emitDiagnostics = this.emitFiles(program, plugins, emitPlan, writeFile); | ||
@@ -64,3 +64,3 @@ return { | ||
} | ||
getEmitPlan(program, diagnostics, files) { | ||
getEmitPlan(program, diagnostics, files, plugins) { | ||
performance.startSection("getEmitPlan"); | ||
@@ -72,3 +72,3 @@ const options = program.getCompilerOptions(); | ||
// Resolve imported modules and modify output Lua requires | ||
const resolutionResult = (0, resolve_1.resolveDependencies)(program, files, this.emitHost); | ||
const resolutionResult = (0, resolve_1.resolveDependencies)(program, files, this.emitHost, plugins); | ||
diagnostics.push(...resolutionResult.diagnostics); | ||
@@ -75,0 +75,0 @@ const lualibRequired = resolutionResult.resolvedFiles.some(f => f.fileName === "lualib_bundle"); |
{ | ||
"name": "typescript-to-lua", | ||
"version": "1.16.2", | ||
"version": "1.16.3", | ||
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua", |
1058201
15142