Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function getDeclarationContextType(

// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
const options = program.getCompilerOptions() as CompilerOptions;
if (options.noImplicitSelf && program.getRootFileNames().includes(signatureDeclaration.getSourceFile().fileName)) {
if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) {
return ContextType.Void;
}

Expand Down
13 changes: 9 additions & 4 deletions src/transpilation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from "path";
import * as ts from "typescript";
import { parseConfigFileWithSystem } from "../cli/tsconfig";
import { CompilerOptions } from "../CompilerOptions";
import { normalizeSlashes } from "../utils";
import { createEmitOutputCollector, TranspiledFile } from "./output-collector";
import { EmitResult, Transpiler } from "./transpiler";

Expand Down Expand Up @@ -44,8 +45,12 @@ const libCache: { [key: string]: ts.SourceFile } = {};

/** @internal */
export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program {
const normalizedFiles: Record<string, string> = {};
for (const [path, file] of Object.entries(input)) {
normalizedFiles[normalizeSlashes(path)] = file;
}
const compilerHost: ts.CompilerHost = {
fileExists: fileName => fileName in input || ts.sys.fileExists(fileName),
fileExists: fileName => fileName in normalizedFiles || ts.sys.fileExists(fileName),
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDefaultLibFileName: ts.getDefaultLibFileName,
Expand All @@ -55,8 +60,8 @@ export function createVirtualProgram(input: Record<string, string>, options: Com
writeFile() {},

getSourceFile(fileName) {
if (fileName in input) {
return ts.createSourceFile(fileName, input[fileName], ts.ScriptTarget.Latest, false);
if (fileName in normalizedFiles) {
return ts.createSourceFile(fileName, normalizedFiles[fileName], ts.ScriptTarget.Latest, false);
}

let filePath: string | undefined;
Expand All @@ -80,7 +85,7 @@ export function createVirtualProgram(input: Record<string, string>, options: Com
},
};

return ts.createProgram(Object.keys(input), options, compilerHost);
return ts.createProgram(Object.keys(normalizedFiles), options, compilerHost);
}

export interface TranspileVirtualProjectResult {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/functions/noImplicitSelfOption.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import * as path from "path";
import { transpileFiles } from "../../../src";
import { couldNotResolveRequire } from "../../../src/transpilation/diagnostics";
import * as util from "../../util";

test("enables noSelfInFile behavior for functions", () => {
util.testFunction`
function fooBar() {}
const test: (this: void) => void = fooBar;
fooBar();
`
.setOptions({ noImplicitSelf: true })
.expectToHaveNoDiagnostics();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1084
test.each(["\\", "/"])("transpileFiles handles paths with noImplicitSelf and %s separator (#1084)", separator => {
const projectDir = `${path.dirname(path.dirname(__dirname))}${separator}transpile${separator}project`;
const emittedFiles: Record<string, string> = {};
const { diagnostics } = transpileFiles(
[
`${projectDir}${separator}index.ts`,
`${projectDir}${separator}api.d.ts`,
`${projectDir}${separator}otherFile.ts`,
],
{ noImplicitSelf: true },
(fileName, text) => (emittedFiles[fileName] = text)
);
expect(diagnostics).toHaveLength(0);
expect(Object.keys(emittedFiles)).not.toHaveLength(0);
for (const fileContent of Object.values(emittedFiles)) {
expect(fileContent).toContain("getNumber()");
expect(fileContent).not.toContain("getNumber(self)");
expect(fileContent).not.toContain("getNumber(_G)");
}
});

test("enables noSelfInFile behavior for methods", () => {
util.testFunction`
class FooBar {
Expand Down
11 changes: 4 additions & 7 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ export abstract class TestBuilder {
protected mainFileName = "main.ts";
public setMainFileName(mainFileName: string): this {
expect(this.hasProgram).toBe(false);
this.mainFileName = normalizeSlashes(mainFileName);
this.mainFileName = mainFileName;
return this;
}

protected extraFiles: Record<string, string> = {};
public addExtraFile(fileName: string, code: string): this {
expect(this.hasProgram).toBe(false);
this.extraFiles[fileName] = code;
this.extraFiles[fileName] = normalizeSlashes(code);
return this;
}

Expand All @@ -206,10 +206,7 @@ export abstract class TestBuilder {
Object.entries(this.extraFiles).filter(([fileName]) => !fileName.endsWith(".lua"))
);

return tstl.createVirtualProgram(
{ ...nonLuaExtraFiles, [normalizeSlashes(this.mainFileName)]: this.getTsCode() },
this.options
);
return tstl.createVirtualProgram({ ...nonLuaExtraFiles, [this.mainFileName]: this.getTsCode() }, this.options);
}

private getEmitHost(): EmitHost {
Expand Down Expand Up @@ -247,7 +244,7 @@ export abstract class TestBuilder {
const mainFile = this.options.luaBundle
? transpiledFiles[0]
: transpiledFiles.find(({ sourceFiles }) =>
sourceFiles.some(f => normalizeSlashes(f.fileName) === this.mainFileName)
sourceFiles.some(f => f.fileName === normalizeSlashes(this.mainFileName))
);

expect(mainFile).toMatchObject({ lua: expect.any(String), luaSourceMap: expect.any(String) });
Expand Down