Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { Transpiler } from '@stryker-mutator/api/transpile';
import ConfigLoader from './compiler/ConfigLoader';
import WebpackCompiler from './compiler/WebpackCompiler';
import { pluginTokens } from './pluginTokens';
const DEFAULT_STRYKER_WEBPACK_CONFIG = Object.freeze({ configFile: undefined, silent: true, context: process.cwd() });
export default class WebpackTranspiler implements Transpiler {
private readonly config: StrykerWebpackConfig;
private webpackCompiler: WebpackCompiler;
public static inject = tokens(commonTokens.options, commonTokens.produceSourceMaps, pluginTokens.configLoader);
constructor(options: StrykerOptions, produceSourceMaps: boolean, private readonly configLoader: ConfigLoader) {
if (produceSourceMaps) {
throw new Error(
`Invalid \`coverageAnalysis\` "${options.coverageAnalysis}" is not supported by the stryker-webpack-transpiler (yet). It is not able to produce source maps yet. Please set it "coverageAnalysis" to "off".`
);
}
this.config = this.getStrykerWebpackConfig(options.webpack);
}
public async transpile(files: readonly File[]): Promise {
if (!this.webpackCompiler) {
// Initialize the webpack compiler with the current directory (process.cwd)
const config = await this.configLoader.load(this.config);
this.webpackCompiler = new WebpackCompiler(config);
}
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { LoggerFactoryMethod } from '@stryker-mutator/api/logging';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { Transpiler } from '@stryker-mutator/api/transpile';
import * as ts from 'typescript';
import { getProjectDirectory, getTSConfig, guardTypescriptVersion, isHeaderFile } from './helpers/tsHelpers';
import TranspileFilter from './transpiler/TranspileFilter';
import TranspilingLanguageService from './transpiler/TranspilingLanguageService';
export default class TypescriptTranspiler implements Transpiler {
private languageService: TranspilingLanguageService;
private readonly filter: TranspileFilter;
public static inject = tokens(commonTokens.options, commonTokens.produceSourceMaps, commonTokens.getLogger);
constructor(
private readonly options: StrykerOptions,
private readonly produceSourceMaps: boolean,
private readonly getLogger: LoggerFactoryMethod
) {
guardTypescriptVersion();
this.filter = TranspileFilter.create(this.options);
}
public transpile(files: readonly File[]): Promise {
const typescriptFiles = this.filterIsIncluded(files);
if (this.languageService) {
this.languageService.replace(typescriptFiles);
} else {
this.languageService = this.createLanguageService(typescriptFiles);
}
constructor(options: StrykerOptions, loggingContext: LoggingClientContext, produceSourceMaps: boolean) {
this.childProcess = ChildProcessProxy.create(
require.resolve(`./${ChildProcessTranspilerWorker.name}`),
loggingContext,
options,
{ [commonTokens.produceSourceMaps]: produceSourceMaps },
process.cwd(),
ChildProcessTranspilerWorker
);
}
);
this.timer.reset();
const inputFiles = await this.injector.injectClass(InputFileResolver).resolve();
if (inputFiles.files.length) {
this.temporaryDirectory.initialize();
const inputFileInjector = this.injector.provideValue(coreTokens.loggingContext, loggingContext).provideValue(coreTokens.inputFiles, inputFiles);
const initialTestRunProcess = inputFileInjector
.provideValue(commonTokens.produceSourceMaps, this.options.coverageAnalysis !== 'off')
.provideFactory(coreTokens.pluginCreatorTranspiler, PluginCreator.createFactory(PluginKind.Transpiler))
.provideClass(coreTokens.transpiler, TranspilerFacade)
.injectClass(InitialTestExecutor);
const initialRunResult = await initialTestRunProcess.run();
const mutator = inputFileInjector.injectClass(MutatorFacade);
const transpilerProvider = inputFileInjector
.provideValue(coreTokens.initialRunResult, initialRunResult)
.provideValue(commonTokens.produceSourceMaps, false)
.provideFactory(coreTokens.transpiler, transpilerFactory);
const transpiler = transpilerProvider.resolve(coreTokens.transpiler);
const transpiledFiles = await transpiler.transpile(inputFiles.files);
const mutationTestProcessInjector = transpilerProvider
.provideValue(coreTokens.transpiledFiles, transpiledFiles)
.provideClass(coreTokens.mutantTranspileScheduler, MutantTranspileScheduler)
.provideClass(coreTokens.sandboxPool, SandboxPool);
const testableMutants = await mutationTestProcessInjector
.injectClass(MutantTestMatcher)
.matchWithMutants(mutator.mutate(inputFiles.filesToMutate));
try {
if (initialRunResult.runResult.tests.length && testableMutants.length) {
const mutationTestExecutor = mutationTestProcessInjector.injectClass(MutationTestExecutor);
const mutantResults = await mutationTestExecutor.run(testableMutants);
await this.reportScore(mutantResults, inputFileInjector);
await this.logDone();
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { Transpiler } from '@stryker-mutator/api/transpile';
import { Disposable } from 'typed-inject';
import ChildProcessProxy from '../child-proxy/ChildProcessProxy';
import { coreTokens } from '../di';
import LoggingClientContext from '../logging/LoggingClientContext';
import { ChildProcessTranspilerWorker } from './ChildProcessTranspilerWorker';
export class ChildProcessTranspiler implements Transpiler, Disposable {
private readonly childProcess: ChildProcessProxy;
public static inject = tokens(commonTokens.options, coreTokens.loggingContext, commonTokens.produceSourceMaps);
constructor(options: StrykerOptions, loggingContext: LoggingClientContext, produceSourceMaps: boolean) {
this.childProcess = ChildProcessProxy.create(
require.resolve(`./${ChildProcessTranspilerWorker.name}`),
loggingContext,
options,
{ [commonTokens.produceSourceMaps]: produceSourceMaps },
process.cwd(),
ChildProcessTranspilerWorker
);
}
public transpile(files: readonly File[]): Promise {
return this.childProcess.proxy.transpile(files);
}
public async runMutationTest(): Promise {
const loggingContext = await LogConfigurator.configureLoggingServer(
this.options.logLevel,
this.options.fileLogLevel,
this.options.allowConsoleColors
);
this.timer.reset();
const inputFiles = await this.injector.injectClass(InputFileResolver).resolve();
if (inputFiles.files.length) {
this.temporaryDirectory.initialize();
const inputFileInjector = this.injector.provideValue(coreTokens.loggingContext, loggingContext).provideValue(coreTokens.inputFiles, inputFiles);
const initialTestRunProcess = inputFileInjector
.provideValue(commonTokens.produceSourceMaps, this.options.coverageAnalysis !== 'off')
.provideFactory(coreTokens.pluginCreatorTranspiler, PluginCreator.createFactory(PluginKind.Transpiler))
.provideClass(coreTokens.transpiler, TranspilerFacade)
.injectClass(InitialTestExecutor);
const initialRunResult = await initialTestRunProcess.run();
const mutator = inputFileInjector.injectClass(MutatorFacade);
const transpilerProvider = inputFileInjector
.provideValue(coreTokens.initialRunResult, initialRunResult)
.provideValue(commonTokens.produceSourceMaps, false)
.provideFactory(coreTokens.transpiler, transpilerFactory);
const transpiler = transpilerProvider.resolve(coreTokens.transpiler);
const transpiledFiles = await transpiler.transpile(inputFiles.files);
const mutationTestProcessInjector = transpilerProvider
.provideValue(coreTokens.transpiledFiles, transpiledFiles)
.provideClass(coreTokens.mutantTranspileScheduler, MutantTranspileScheduler)
.provideClass(coreTokens.sandboxPool, SandboxPool);
const testableMutants = await mutationTestProcessInjector
import * as babel from './helpers/babelWrapper';
import { toJSFileName } from './helpers/helpers';
const DEFAULT_EXTENSIONS: readonly string[] = (babel as any).DEFAULT_EXTENSIONS;
export function babelTranspilerFactory(injector: Injector) {
return injector.provideClass('babelConfigReader', BabelConfigReader).injectClass(BabelTranspiler);
}
babelTranspilerFactory.inject = tokens(commonTokens.injector);
export class BabelTranspiler implements Transpiler {
private readonly babelConfig: StrykerBabelConfig;
private readonly projectRoot: string;
private readonly extensions: readonly string[];
public static inject = tokens(commonTokens.options, commonTokens.produceSourceMaps, 'babelConfigReader');
constructor(options: StrykerOptions, produceSourceMaps: boolean, babelConfigReader: BabelConfigReader) {
if (produceSourceMaps) {
throw new Error(
`Invalid \`coverageAnalysis\` "${options.coverageAnalysis}" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".`
);
}
this.babelConfig = babelConfigReader.readConfig(options);
this.projectRoot = this.determineProjectRoot();
this.extensions = [...DEFAULT_EXTENSIONS, ...this.babelConfig.extensions];
}
public async transpile(files: readonly File[]): Promise {
return files.map(file => this.transpileFileIfNeeded(file));
}
private transpileFileIfNeeded(file: File): File {