Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
006f706
Create test project with node_modules
Perryvw Apr 17, 2021
9fd7b52
testProject test util
Perryvw May 13, 2021
07e5a78
Fix typos in module resolution test
Perryvw May 14, 2021
3ff3b49
Base case module resolution
Perryvw May 14, 2021
01e24e4
Replace requires in source maps too
Perryvw May 15, 2021
29b5d62
Fixed incorrect path behavior
Perryvw May 16, 2021
af82e4e
More tests
Perryvw May 16, 2021
243a83a
Merge branch 'master' into feature/module-resolution
Perryvw May 22, 2021
73a350a
add module resolution with sourceDir node_modules
Perryvw May 22, 2021
737e266
Get all module-resolution testcases to work
Perryvw May 23, 2021
83b0dea
Restrict resolver to only lua files
Perryvw May 24, 2021
c080110
All tests working
Perryvw May 24, 2021
7a79881
Added more in-project dependency checks to sourceDir test
Perryvw May 24, 2021
a24708e
Fixed problem with lua sibling files
Perryvw May 24, 2021
4f8bec0
Also resolve JSON modules
Perryvw May 24, 2021
7b8bd80
Add debug to resolution test to try to figure out why CI is failing
Perryvw May 24, 2021
5dd98f1
fix test runner path preloading
Perryvw May 24, 2021
30216bc
Revert "Add debug to resolution test to try to figure out why CI is f…
Perryvw May 24, 2021
1aac594
Changed resolution failure from error to warning
Perryvw May 24, 2021
fb6cc93
move json.lua from dist to src in benchmark
Perryvw May 24, 2021
6d833c8
Use commit version instead of master to compile benchmark scripts
Perryvw May 25, 2021
51ff390
Added module resolution test project with lua sources
Perryvw May 25, 2021
ffd8a47
Add library compilation mode
Perryvw May 29, 2021
89a046c
renamed compilemode to buildmode
Perryvw May 29, 2021
f50b945
clean up resolve
Perryvw Jun 3, 2021
5ce4b72
Fix tests
Perryvw Jun 5, 2021
c28f357
Removed old project test runner
Perryvw Jun 5, 2021
da6d32a
PR comments
Perryvw Jun 5, 2021
d5e0217
Remove file casing test
Perryvw Jun 5, 2021
5e68191
Resolution + library mode combined test
Perryvw Jun 6, 2021
258d7dd
remove out path logic from printer
Perryvw Jun 6, 2021
853ca6c
Merge branch 'master' into feature/module-resolution
Perryvw Jun 11, 2021
5487382
Fixed bundle entry point require not being resolved correctly
Perryvw Jun 12, 2021
2a309ce
Add header to bundle
Perryvw Jun 13, 2021
9188649
Made couldNotResolveRequire an error instead of warning
Perryvw Jun 13, 2021
ce4abf8
updated couldnotResolveRequire snapshot
Perryvw Jun 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/test/cli/errors
/test/cli/watch
/test/transpile/directories
/test/transpile/module-resolution/*/node_modules
/test/transpile/outFile
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,29 @@ jobs:
run: rm -rf ./master/benchmark && cp -rf ./commit/benchmark ./master/benchmark
# Run master benchmark first and output to commit benchmark data
- name: Build benchmark Lua 5.3 master
run: node ../dist/tstl.js -p tsconfig.53.json
run: node ../../commit/dist/tstl.js -p tsconfig.53.json
working-directory: master/benchmark
- name: Run benchmark Lua 5.3 master
id: benchmark-lua-master
run: lua5.3 -- run.lua ../../../commit/benchmark/data/benchmark_master_53.json
working-directory: master/benchmark/dist
- name: Build benchmark LuaJIT master
run: node ../dist/tstl.js -p tsconfig.jit.json
run: node ../../commit/dist/tstl.js -p tsconfig.jit.json
working-directory: master/benchmark
- name: Run benchmark LuaJIT master
id: benchmark-jit-master
run: luajit -- run.lua ../../../commit/benchmark/data/benchmark_master_jit.json
working-directory: master/benchmark/dist
# Run commit benchmark and compare with master
- name: Build benchmark Lua 5.3 commit
run: node ../dist/tstl.js -p tsconfig.53.json
run: node ../../commit/dist/tstl.js -p tsconfig.53.json
working-directory: commit/benchmark
- name: Run benchmark Lua 5.3 commit
id: benchmark-lua-commit
run: lua5.3 -- run.lua ../data/benchmark_master_vs_commit_53.json ../data/benchmark_master_53.json
working-directory: commit/benchmark/dist
- name: Build benchmark LuaJIT commit
run: node ../dist/tstl.js -p tsconfig.jit.json
run: node ../../commit/dist/tstl.js -p tsconfig.jit.json
working-directory: commit/benchmark
- name: Run benchmark LuaJIT commit
id: benchmark-jit-commit
Expand Down
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current jest test",
"type": "node",
"request": "launch",
"env": { "CI": "true" },
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest",
"args": ["--runInBand", "--no-cache", "--runTestsByPath", "${relativeFile}"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
File renamed without changes.
46 changes: 37 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"node": ">=12.13.0"
},
"dependencies": {
"enhanced-resolve": "^5.8.2",
"resolve": "^1.15.1",
"source-map": "^0.7.3",
"typescript": "~4.3.2"
Expand Down
6 changes: 6 additions & 0 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface LuaPluginImport {
}

export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
buildMode?: BuildMode;
noImplicitSelf?: boolean;
noHeader?: boolean;
luaBundle?: string;
Expand Down Expand Up @@ -53,6 +54,11 @@ export enum LuaTarget {
LuaJIT = "JIT",
}

export enum BuildMode {
Default = "default",
Library = "library",
}

export const isBundleEnabled = (options: CompilerOptions) =>
options.luaBundle !== undefined && options.luaBundleEntry !== undefined;

Expand Down
24 changes: 5 additions & 19 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as path from "path";
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import * as lua from "./LuaAST";
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
import { EmitHost } from "./transpilation";
import { intersperse, normalizeSlashes, trimExtension } from "./utils";
import { intersperse, trimExtension } from "./utils";

// https://www.lua.org/pil/2.4.html
// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
Expand All @@ -25,6 +24,8 @@ const escapeStringMap: Record<string, string> = {

export const escapeString = (value: string) => `"${value.replace(escapeStringRegExp, char => escapeStringMap[char])}"`;

export const tstlHeader = "--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n";

/**
* Checks that a name is valid for use in lua function declaration syntax:
*
Expand Down Expand Up @@ -124,22 +125,7 @@ export class LuaPrinter {

constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) {
this.options = program.getCompilerOptions();

if (this.options.outDir) {
const relativeFileName = path.relative(program.getCommonSourceDirectory(), fileName);
if (this.options.sourceRoot) {
// When sourceRoot is specified, just use relative path inside rootDir
this.sourceFile = relativeFileName;
} else {
// Calculate relative path from rootDir to outDir
const outputPath = path.resolve(this.options.outDir, relativeFileName);
this.sourceFile = path.relative(path.dirname(outputPath), fileName);
}
// We want forward slashes, even in windows
this.sourceFile = normalizeSlashes(this.sourceFile);
} else {
this.sourceFile = path.basename(fileName); // File will be in same dir as source
}
this.sourceFile = fileName;
}

public print(file: lua.File): PrintResult {
Expand Down Expand Up @@ -201,7 +187,7 @@ export class LuaPrinter {
let header = file.trivia;

if (!this.options.noHeader) {
header += "--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n";
header += tstlHeader;
}

const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;
Expand Down
8 changes: 7 additions & 1 deletion src/cli/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions";
import { BuildMode, CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions";
import * as cliDiagnostics from "./diagnostics";

export interface ParsedCommandLine extends ts.ParsedCommandLine {
Expand All @@ -24,6 +24,12 @@ interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;

export const optionDeclarations: CommandLineOption[] = [
{
name: "buildMode",
description: "'default' or 'library'. Compiling as library will not resolve external dependencies.",
type: "enum",
choices: Object.values(BuildMode),
},
{
name: "luaBundle",
description: "The name of the lua file to bundle output lua to. Requires luaBundleEntry.",
Expand Down
4 changes: 0 additions & 4 deletions src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ export const invalidAmbientIdentifierName = createErrorDiagnosticFactory(
(text: string) => `Invalid ambient identifier name '${text}'. Ambient identifiers must be valid lua identifiers.`
);

export const unresolvableRequirePath = createErrorDiagnosticFactory(
(path: string) => `Cannot create require path. Module '${path}' does not exist within --rootDir.`
);

export const unsupportedVarDeclaration = createErrorDiagnosticFactory(
"`var` declarations are not supported. Use `let` or `const` instead."
);
Expand Down
34 changes: 5 additions & 29 deletions src/transformation/visitors/modules/import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from "path";
import * as ts from "typescript";
import * as lua from "../../../LuaAST";
import { formatPathToLuaPath } from "../../../utils";
import { FunctionVisitor, TransformationContext } from "../../context";
import { AnnotationKind, getSymbolAnnotations } from "../../utils/annotations";
import { createDefaultExportStringLiteral } from "../../utils/export";
Expand All @@ -10,36 +9,13 @@ import { createSafeName } from "../../utils/safe-names";
import { peekScope } from "../../utils/scope";
import { transformIdentifier } from "../identifier";
import { transformPropertyName } from "../literal";
import { unresolvableRequirePath } from "../../utils/diagnostics";

const getAbsoluteImportPath = (relativePath: string, directoryPath: string, options: ts.CompilerOptions): string =>
!relativePath.startsWith(".") && options.baseUrl
? path.resolve(options.baseUrl, relativePath)
: path.resolve(directoryPath, relativePath);

function getImportPath(context: TransformationContext, relativePath: string, node: ts.Node): string {
const { options, sourceFile } = context;
const { fileName } = sourceFile;
const rootDir = options.rootDir ? path.resolve(options.rootDir) : path.resolve(".");

const absoluteImportPath = path.format(
path.parse(getAbsoluteImportPath(relativePath, path.dirname(fileName), options))
);
const absoluteRootDirPath = path.format(path.parse(rootDir));
if (absoluteImportPath.includes(absoluteRootDirPath)) {
return formatPathToLuaPath(absoluteImportPath.replace(absoluteRootDirPath, "").slice(1));
} else {
context.diagnostics.push(unresolvableRequirePath(node, relativePath));
return relativePath;
}
}

function shouldResolveModulePath(context: TransformationContext, moduleSpecifier: ts.Expression): boolean {
function isNoResolutionPath(context: TransformationContext, moduleSpecifier: ts.Expression): boolean {
const moduleOwnerSymbol = context.checker.getSymbolAtLocation(moduleSpecifier);
if (!moduleOwnerSymbol) return true;
if (!moduleOwnerSymbol) return false;

const annotations = getSymbolAnnotations(moduleOwnerSymbol);
return !annotations.has(AnnotationKind.NoResolution);
return annotations.has(AnnotationKind.NoResolution);
}

export function createModuleRequire(
Expand All @@ -49,8 +25,8 @@ export function createModuleRequire(
): lua.CallExpression {
const params: lua.Expression[] = [];
if (ts.isStringLiteral(moduleSpecifier)) {
const modulePath = shouldResolveModulePath(context, moduleSpecifier)
? getImportPath(context, moduleSpecifier.text.replace(/"/g, ""), moduleSpecifier)
const modulePath = isNoResolutionPath(context, moduleSpecifier)
? `@NoResolution:${moduleSpecifier.text}`
: moduleSpecifier.text;

params.push(lua.createStringLiteral(modulePath));
Expand Down
Loading