1 | import path from 'path';
|
2 |
|
3 | import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
|
4 | import { StrykerOptions } from '@stryker-mutator/api/core';
|
5 | import type { disableTypeChecks } from '@stryker-mutator/instrumenter';
|
6 | import { Logger } from '@stryker-mutator/api/logging';
|
7 |
|
8 | import { optionsPath } from '../utils/index.js';
|
9 | import { coreTokens } from '../di/index.js';
|
10 | import { objectUtils } from '../utils/object-utils.js';
|
11 | import { FileMatcher } from '../config/index.js';
|
12 |
|
13 | import { Project } from '../fs/project.js';
|
14 |
|
15 | import { FilePreprocessor } from './file-preprocessor.js';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export 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 | }
|