UNPKG

2.3 kBPlain TextView Raw
1import path from 'path';
2
3import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
4import { StrykerOptions } from '@stryker-mutator/api/core';
5import type { disableTypeChecks } from '@stryker-mutator/instrumenter';
6import { Logger } from '@stryker-mutator/api/logging';
7
8import { optionsPath } from '../utils/index.js';
9import { coreTokens } from '../di/index.js';
10import { objectUtils } from '../utils/object-utils.js';
11import { FileMatcher } from '../config/index.js';
12
13import { Project } from '../fs/project.js';
14
15import { FilePreprocessor } from './file-preprocessor.js';
16
17/**
18 * Disabled type checking by inserting `@ts-nocheck` atop TS/JS files and removing other @ts-xxx directives from comments:
19 * @see https://github.com/stryker-mutator/stryker-js/issues/2438
20 */
21export class DisableTypeChecksPreprocessor implements FilePreprocessor {
22 public static readonly inject = tokens(commonTokens.logger, commonTokens.options, coreTokens.disableTypeChecksHelper);
23 constructor(private readonly log: Logger, private readonly options: StrykerOptions, private readonly impl: typeof disableTypeChecks) {}
24
25 public async preprocess(project: Project): Promise<void> {
26 const matcher = new FileMatcher(this.options.disableTypeChecks);
27 let warningLogged = false;
28 await Promise.all(
29 objectUtils.map(project.files, async (file, name) => {
30 if (matcher.matches(path.resolve(name))) {
31 try {
32 const { content } = await this.impl(await file.toInstrumenterFile(), { plugins: this.options.mutator.plugins });
33 file.setContent(content);
34 } catch (err) {
35 if (objectUtils.isWarningEnabled('preprocessorErrors', this.options.warnings)) {
36 warningLogged = true;
37 this.log.warn(
38 `Unable to disable type checking for file "${name}". Shouldn't type checking be disabled for this file? Consider configuring a more restrictive "${optionsPath(
39 'disableTypeChecks'
40 )}" settings (or turn it completely off with \`false\`)`,
41 err
42 );
43 }
44 }
45 }
46 })
47 );
48 if (warningLogged) {
49 this.log.warn(`(disable "${optionsPath('warnings', 'preprocessorErrors')}" to ignore this warning`);
50 }
51 }
52}