Repository: angular/angular
File: packages/compiler-cli/src/ngtsc/preprocessor.ts (TypeScript)

Code before:
    const transformers = this.compiler.prepareEmit().transformers;
    // We only care about 'before' transformers for source-to-source
    const beforeTransformers = (transformers.before ||
      []) as unknown as ts.TransformerFactory<ts.SourceFile>[];

    const result: {fileName: string; content: string}[] = [];
    const printer = ts.createPrinter({
      newLine: ts.NewLineKind.LineFeed,
      removeComments: false,
    });

    const program = this.compiler.getCurrentProgram();

    for (const sf of program.getSourceFiles()) {
      if (sf.isDeclarationFile) {
        continue;
      }

      // If it is a TCB file (ends in .ngtypecheck.ts), we want it as-is, without any transformations.
      if (sf.fileName.endsWith('.ngtypecheck.ts')) {
        const content = printer.printFile(sf);
        result.push({fileName: sf.fileName, content});
        continue;
      }

      if (this.compiler.ignoreForEmit.has(sf)) {
        continue;
      }

      // Manually transform the source file
      const transformationResult = ts.transform(sf, beforeTransformers, this.options);

      if (transformationResult.transformed.length > 0) {
        const transformedSf = transformationResult.transformed[0];
        const content = printer.printFile(transformedSf);
        result.push({fileName: sf.fileName, content});
      }

      transformationResult.dispose();
    }

    return result;
  }
}


Review comment (suggestion):
`ignoreForEmit` should be the set of type check files special cased above via the file name check.  Probably don't need both.

Code after:
    const transformers = this.compiler.prepareEmit().transformers;
    // We only care about 'before' transformers for source-to-source
    const beforeTransformers = (transformers.before ||
      []) as unknown as ts.TransformerFactory<ts.SourceFile>[];

    const result: {fileName: string; content: string}[] = [];
    const printer = ts.createPrinter({
      newLine: ts.NewLineKind.LineFeed,
      removeComments: false,
    });

    const program = this.compiler.getCurrentProgram();

    for (const sf of program.getSourceFiles()) {
      if (sf.isDeclarationFile) {
        continue;
      }

      // If it is a TCB file (ends in .ngtypecheck.ts), we want it as-is, without any transformations.
      if (sf.fileName.endsWith('.ngtypecheck.ts')) {
        const content = printer.printFile(sf);
        result.push({fileName: sf.fileName, content});
        continue;
      }

      // Manually transform the source file
      const transformationResult = ts.transform(sf, beforeTransformers, this.options);

      if (transformationResult.transformed.length !== 1) {
        transformationResult.dispose();
        throw new Error(
          `Expected exactly one transformed file, got ${transformationResult.transformed.length}`,
        );
      }

      const transformedSf = transformationResult.transformed[0];

      const content = printer.printFile(transformedSf);
      result.push({fileName: sf.fileName, content});

      transformationResult.dispose();

      this.compiler.incrementalCompilation.recordSuccessfulEmit(sf);
    }

    return result;
  }
}