From bf22f4dfb1282b91c799fe0b97f08d3afb73e1fc Mon Sep 17 00:00:00 2001 From: Paul Douglas Brimicombe Date: Fri, 14 Feb 2025 11:54:01 +0000 Subject: [PATCH 1/2] Use a separate compiler instance per snippet --- src/SnippetCompiler.ts | 8 ++++---- test/TypeScriptDocsVerifierSpec.ts | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/SnippetCompiler.ts b/src/SnippetCompiler.ts index aa91742..e41998f 100644 --- a/src/SnippetCompiler.ts +++ b/src/SnippetCompiler.ts @@ -24,7 +24,7 @@ export type SnippetCompilationResult = { }; export class SnippetCompiler { - private readonly compiler: TSNode.Service; + private readonly compilerConfig: TSNode.CreateOptions; constructor( private readonly workingDirectory: string, @@ -35,11 +35,10 @@ export class SnippetCompiler { packageDefinition.packageRoot, project ); - const tsConfig = { + this.compilerConfig = { ...(configOptions.config as TSNode.CreateOptions), transpileOnly: false, }; - this.compiler = TSNode.create(tsConfig); } private static loadTypeScriptConfig( @@ -124,7 +123,8 @@ export class SnippetCompiler { const id = process.hrtime.bigint().toString(); const codeFile = path.join(this.workingDirectory, `block-${id}.${type}`); await fsExtra.writeFile(codeFile, code); - this.compiler.compile(code, codeFile); + const compiler = TSNode.create(this.compilerConfig); + compiler.compile(code, codeFile); } private removeTemporaryFilePaths( diff --git a/test/TypeScriptDocsVerifierSpec.ts b/test/TypeScriptDocsVerifierSpec.ts index c8c6dd7..57d558b 100644 --- a/test/TypeScriptDocsVerifierSpec.ts +++ b/test/TypeScriptDocsVerifierSpec.ts @@ -388,6 +388,31 @@ export const bob = () => (
); } ); + verify.it("compiles snippets independently", async () => { + const snippet1 = `interface Foo { bar: 123 }`; + const snippet2 = `interface Foo { bar: () => void }`; + const typeScriptMarkdown = wrapSnippet(snippet1) + wrapSnippet(snippet2); + await createProject({ + markdownFiles: [{ name: "README.md", contents: typeScriptMarkdown }], + }); + return await TypeScriptDocsVerifier.compileSnippets().should.eventually.eql( + [ + { + file: "README.md", + index: 1, + snippet: snippet1, + linesWithErrors: [], + }, + { + file: "README.md", + index: 2, + snippet: snippet2, + linesWithErrors: [], + }, + ] + ); + }); + verify.it( "compiles snippets containing modules", genSnippet, From 4506393879b295585d7f62501bad43eb0075151d Mon Sep 17 00:00:00 2001 From: Paul Douglas Brimicombe Date: Fri, 14 Feb 2025 11:56:05 +0000 Subject: [PATCH 2/2] Bump version --- CHANGELOG | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 12b7700..8ede609 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## [2.5.3] +### Changed +- Use a separate `ts-node` compiler per-snippet to ensure that compilation of snippets is independent + ## [2.5.2] ### Removed - Obsolete Travis CI build badge from README diff --git a/package.json b/package.json index c56623c..0e9d117 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "typescript", "verify" ], - "version": "2.5.2", + "version": "2.5.3", "main": "dist/index.js", "@types": "dist/index.d.ts", "bin": {