Skip to content

Commit

Permalink
fix(typescript-checker): improve error reporting
Browse files Browse the repository at this point in the history
Correctly report error when the tsconfig file contains an error and is not located in the cwd on windows.
  • Loading branch information
nicojs committed Mar 6, 2021
1 parent 8cf9e8c commit 2502eba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
14 changes: 6 additions & 8 deletions packages/typescript-checker/src/fs/hybrid-file-system.ts
Expand Up @@ -3,11 +3,9 @@ import { Mutant } from '@stryker-mutator/api/core';
import { Logger } from '@stryker-mutator/api/logging';
import { tokens, commonTokens } from '@stryker-mutator/api/plugin';

import { ScriptFile } from './script-file';
import { toPosixFileName } from '../tsconfig-helpers';

function toTSFileName(fileName: string) {
return fileName.replace(/\\/g, '/');
}
import { ScriptFile } from './script-file';

/**
* A very simple hybrid file system.
Expand All @@ -24,7 +22,7 @@ export class HybridFileSystem {
constructor(private readonly log: Logger) {}

public writeFile(fileName: string, data: string): void {
fileName = toTSFileName(fileName);
fileName = toPosixFileName(fileName);
const existingFile = this.files.get(fileName);
if (existingFile) {
existingFile.write(data);
Expand All @@ -35,7 +33,7 @@ export class HybridFileSystem {
}

public mutate(mutant: Pick<Mutant, 'fileName' | 'range' | 'replacement'>): void {
const fileName = toTSFileName(mutant.fileName);
const fileName = toPosixFileName(mutant.fileName);
const file = this.files.get(fileName);
if (!file) {
throw new Error(`File "${mutant.fileName}" cannot be found.`);
Expand All @@ -57,7 +55,7 @@ export class HybridFileSystem {
}

public getFile(fileName: string): ScriptFile | undefined {
fileName = toTSFileName(fileName);
fileName = toPosixFileName(fileName);
if (!this.files.has(fileName)) {
const content = ts.sys.readFile(fileName);
if (typeof content === 'string') {
Expand All @@ -71,6 +69,6 @@ export class HybridFileSystem {
}

public existsInMemory(fileName: string): boolean {
return !!this.files.get(toTSFileName(fileName));
return !!this.files.get(toPosixFileName(fileName));
}
}
9 changes: 9 additions & 0 deletions packages/typescript-checker/src/tsconfig-helpers.ts
Expand Up @@ -73,3 +73,12 @@ export function retrieveReferencedProjects(parsedConfig: { config?: any }, fromD
}
return [];
}

/**
* Replaces backslashes with forward slashes (used by typescript)
* @param fileName The file name that may contain backslashes `\`
* @returns posix and ts complaint file name (with `/`)
*/
export function toPosixFileName(fileName: string): string {
return fileName.replace(/\\/g, '/');
}
4 changes: 2 additions & 2 deletions packages/typescript-checker/src/typescript-checker.ts
Expand Up @@ -9,7 +9,7 @@ import { Task, propertyPath } from '@stryker-mutator/util';
import { Mutant, StrykerOptions } from '@stryker-mutator/api/core';

import { HybridFileSystem } from './fs';
import { determineBuildModeEnabled, overrideOptions, retrieveReferencedProjects, guardTSVersion } from './tsconfig-helpers';
import { determineBuildModeEnabled, overrideOptions, retrieveReferencedProjects, guardTSVersion, toPosixFileName } from './tsconfig-helpers';
import * as pluginTokens from './plugin-tokens';

const diagnosticsHost: ts.FormatDiagnosticsHost = {
Expand Down Expand Up @@ -51,7 +51,7 @@ export class TypescriptChecker implements Checker {
private readonly tsconfigFile: string;

constructor(private readonly logger: Logger, options: StrykerOptions, private readonly fs: HybridFileSystem) {
this.tsconfigFile = options.tsconfigFile;
this.tsconfigFile = toPosixFileName(options.tsconfigFile);
this.allTSConfigFiles = new Set([path.resolve(this.tsconfigFile)]);
}

Expand Down

0 comments on commit 2502eba

Please sign in to comment.