Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify hosts to directly assign tsModule.sys where possible #349

Merged
merged 1 commit into from
Jun 7, 2022
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
6 changes: 5 additions & 1 deletion __tests__/host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { LanguageServiceHost } from "../src/host";

setTypescriptModule(ts);

// mock for host.trace
(global as any).console = {
log: jest.fn(),
};

const defaultConfig = { fileNames: [], errors: [], options: {} };

const unaryFunc = "const unary = (x: string): string => x.reverse()";
Expand Down Expand Up @@ -72,7 +77,6 @@ test("LanguageServiceHost", async () => {
expect(host.getTypeRootsVersion()).toEqual(0);

// mock out trace
console.log = jest.fn();
host.trace('test log');
expect(console.log).toHaveBeenCalledWith('test log');
});
Expand Down
11 changes: 2 additions & 9 deletions src/diagnostics-format-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ export class FormatHost implements tsTypes.FormatDiagnosticsHost
return tsModule.sys.getCurrentDirectory();
}

public getCanonicalFileName(fileName: string): string
{
return path.normalize(fileName);
}

public getNewLine(): string
{
return tsModule.sys.newLine;
}
public getCanonicalFileName = path.normalize;
public getNewLine = () => tsModule.sys.newLine;
}

export const formatHost = new FormatHost();
82 changes: 17 additions & 65 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import { TransformerFactoryCreator } from "./ioptions";

export class LanguageServiceHost implements tsTypes.LanguageServiceHost
{
private cwd: string;
private snapshots: { [fileName: string]: tsTypes.IScriptSnapshot } = {};
private versions: { [fileName: string]: number } = {};
private service?: tsTypes.LanguageService;
private fileNames: Set<string>;

constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[], cwd: string)
constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[], private cwd: string)
{
this.fileNames = new Set(parsedConfig.fileNames);
this.cwd = cwd;
}

public reset()
Expand Down Expand Up @@ -58,10 +56,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
return undefined;
}

public getCurrentDirectory()
{
return this.cwd;
}
public getScriptFileNames = () => Array.from(this.fileNames.values());

public getScriptVersion(fileName: string)
{
Expand All @@ -70,61 +65,6 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
return (this.versions[fileName] || 0).toString();
}

public getScriptFileNames()
{
return Array.from(this.fileNames.values());
}

public getCompilationSettings(): tsTypes.CompilerOptions
{
return this.parsedConfig.options;
}

public getDefaultLibFileName(opts: tsTypes.CompilerOptions)
{
return tsModule.getDefaultLibFilePath(opts);
}

public useCaseSensitiveFileNames(): boolean
{
return tsModule.sys.useCaseSensitiveFileNames;
}

public readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]
{
return tsModule.sys.readDirectory(path, extensions, exclude, include);
}

public readFile(path: string, encoding?: string): string | undefined
{
return tsModule.sys.readFile(path, encoding);
}

public fileExists(path: string): boolean
{
return tsModule.sys.fileExists(path);
}

public realpath(path: string): string
{
return tsModule.sys.realpath!(path); // this exists in the default implementation: https://github.com/microsoft/TypeScript/blob/ab2523bbe0352d4486f67b73473d2143ad64d03d/src/compiler/sys.ts#L1288
}

public getTypeRootsVersion(): number
{
return 0;
}

public directoryExists(directoryName: string): boolean
{
return tsModule.sys.directoryExists(directoryName);
}

public getDirectories(directoryName: string): string[]
{
return tsModule.sys.getDirectories(directoryName);
}

public getCustomTransformers(): tsTypes.CustomTransformers | undefined
{
if (this.service === undefined || this.transformers === undefined || this.transformers.length === 0)
Expand All @@ -151,7 +91,19 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
return transformer;
}

public trace(line: string) {
console.log(line)
}
public getCompilationSettings = () => this.parsedConfig.options;
public getTypeRootsVersion = () => 0;
public getCurrentDirectory = () => this.cwd;

public useCaseSensitiveFileNames = () => tsModule.sys.useCaseSensitiveFileNames;
public getDefaultLibFileName = tsModule.getDefaultLibFilePath; // confusing naming: https://github.com/microsoft/TypeScript/issues/35318

public readDirectory = tsModule.sys.readDirectory;
public readFile = tsModule.sys.readFile;
public fileExists = tsModule.sys.fileExists;
public directoryExists = tsModule.sys.directoryExists;
public getDirectories = tsModule.sys.getDirectories;
public realpath = tsModule.sys.realpath!; // this exists in the default implementation: https://github.com/microsoft/TypeScript/blob/ab2523bbe0352d4486f67b73473d2143ad64d03d/src/compiler/sys.ts#L1288

public trace = console.log;
}