Repository: facebook/jest
File: packages/jest-reporters/src/CoverageReporter.ts (TypeScript)

Code before:

      const transformedCoverage = await Promise.all(
        mergedCoverages.result.map(async res => {
          const fileTransform = fileTransforms.get(res.url);

          let sourcemapContent: EncodedSourceMap | undefined = undefined;

          if (
            fileTransform?.sourceMapPath &&
            fs.existsSync(fileTransform.sourceMapPath)
          ) {
            sourcemapContent = JSON.parse(
              fs.readFileSync(fileTransform.sourceMapPath, 'utf8'),
            );
          }

          const converter = v8toIstanbul(
            res.url,
            fileTransform?.wrapperLength ?? 0,
            fileTransform && sourcemapContent
              ? {
                  originalSource: fileTransform.originalCode,
                  source: fileTransform.code,
                  sourceMap: {
                    sourcemap: {file: res.url, ...sourcemapContent} as any,
                  },
                }
              : {source: fs.readFileSync(res.url, 'utf8')},
          );

          await converter.load();

          converter.applyCoverage(res.functions);

          const istanbulData = converter.toIstanbul();

          converter.destroy();

          return istanbulData;
        }),
      );

      const map = istanbulCoverage.createCoverageMap({});

      transformedCoverage.forEach(res => map.merge(res));

      const reportContext = istanbulReport.createContext({
        coverageMap: map,
        dir: this._globalConfig.coverageDirectory,
        watermarks: getWatermarks(this._globalConfig),
      });

Review comment (suggestion):
<img width="539" alt="image" src="https://user-images.githubusercontent.com/1404810/164022791-89b4999b-eb77-440b-8455-abf1af85e4c6.png">

https://github.com/istanbuljs/v8-to-istanbul/blob/1f357fa2e994385b56ba505ca199eda46b233a89/index.d.ts#L14
https://github.com/mozilla/source-map/blob/58819f09018d56ef84dc41ba9c93f554e0645169/source-map.d.ts#L17
vs
https://github.com/jridgewell/trace-mapping/blob/d4ead19a18c805bd3af657f71ea72c48809f61db/src/types.ts#L7

Code after:

      const transformedCoverage = await Promise.all(
        mergedCoverages.result.map(async res => {
          const fileTransform = fileTransforms.get(res.url);

          let sourcemapContent: EncodedSourceMap | undefined = undefined;

          if (
            fileTransform?.sourceMapPath &&
            fs.existsSync(fileTransform.sourceMapPath)
          ) {
            sourcemapContent = JSON.parse(
              fs.readFileSync(fileTransform.sourceMapPath, 'utf8'),
            );
          }

          const converter = v8toIstanbul(
            res.url,
            fileTransform?.wrapperLength ?? 0,
            fileTransform && sourcemapContent
              ? {
                  originalSource: fileTransform.originalCode,
                  source: fileTransform.code,
                  sourceMap: {
                    // remove `as any` after https://github.com/istanbuljs/v8-to-istanbul/pull/186 is released
                    sourcemap: {file: res.url, ...sourcemapContent} as any,
                  },
                }
              : {source: fs.readFileSync(res.url, 'utf8')},
          );

          await converter.load();

          converter.applyCoverage(res.functions);

          const istanbulData = converter.toIstanbul();

          converter.destroy();

          return istanbulData;
        }),
      );

      const map = istanbulCoverage.createCoverageMap({});

      transformedCoverage.forEach(res => map.merge(res));

      const reportContext = istanbulReport.createContext({
        coverageMap: map,
        dir: this._globalConfig.coverageDirectory,
        watermarks: getWatermarks(this._globalConfig),