11import { existsSync } from "node:fs" ;
22import {
3- compile ,
3+ compile as typespecCompile ,
44 createSuppressCodeFix ,
55 DiagnosticTarget ,
66 NodeHost ,
99 resolveCompilerOptions ,
1010 applyCodeFixes ,
1111 formatTypeSpec ,
12+ CompilerOptions ,
1213} from "@typespec/compiler" ;
1314
1415import { findSuppressTarget } from "./typespec-imports.js" ;
@@ -54,6 +55,22 @@ export async function suppressEverything(
5455 await applyCodeFixes ( p . host , codeFixes ) ;
5556}
5657
58+ async function compile (
59+ entryPoint : string ,
60+ compilerOptions : CompilerOptions ,
61+ ) : Promise < Program > {
62+ /* We prevent the compiler from writing files to disk by overriding the writeFile method on the NodeHost. */
63+ const originalWriteFile = NodeHost . writeFile ;
64+ try {
65+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
66+ NodeHost . writeFile = ( _path : string , _content : string ) => Promise . resolve ( ) ;
67+ return await typespecCompile ( NodeHost , entryPoint , compilerOptions ) ;
68+ } finally {
69+ /* Restore the original writeFile method after compilation. */
70+ NodeHost . writeFile = originalWriteFile ;
71+ }
72+ }
73+
5774async function formatSourceFile ( filePath : string ) {
5875 const sourceCode = await NodeHost . readFile ( filePath ) ;
5976 const formattedSource = await formatTypeSpec ( sourceCode . text ) ;
@@ -66,8 +83,8 @@ async function formatSourceFile(filePath: string) {
6683export async function parseTypeSpecAndSuppressEverything (
6784 options : SuppressionOptions ,
6885) {
69- if ( options . ruleSets . length === 0 ) {
70- throw new Error ( "At least one rule set must be provided." ) ;
86+ if ( options . ruleSets . length === 0 && options . emitters . length === 0 ) {
87+ throw new Error ( "At least one rule set or emitter must be provided." ) ;
7188 }
7289
7390 if ( ! options . entryPoint ) {
@@ -85,14 +102,15 @@ export async function parseTypeSpecAndSuppressEverything(
85102 cwd : process . cwd ( ) ,
86103 entrypoint : options . entryPoint ,
87104 overrides : {
105+ emit : options . emitters ,
88106 linter : {
89107 extends : options . ruleSets ,
90108 } ,
91109 } ,
92110 } ) ;
93111
94112 // Create the TypeSpec program
95- const program = await compile ( NodeHost , options . entryPoint , compilerOptions ) ;
113+ const program = await compile ( options . entryPoint , compilerOptions ) ;
96114
97115 if (
98116 program . diagnostics . some (
@@ -117,8 +135,10 @@ export async function parseTypeSpecAndSuppressEverything(
117135export interface SuppressionOptions {
118136 /** The entry point file for the TypeSpec program */
119137 entryPoint : string ;
120- /** The rule sets to apply. At least one rule set must be provided. */
138+ /** The rule sets to apply. At least one rule or one emitter must be provided. */
121139 ruleSets : `${string } /${string } `[ ] ;
140+ /** The emitters to apply. At least one rule or one emitter must be provided. */
141+ emitters : string [ ] ;
122142 /** The message to include with each suppression directive */
123143 message ?: string ;
124144}
0 commit comments