Skip to content

Commit bf22f4d

Browse files
Use a separate compiler instance per snippet
1 parent 3686a26 commit bf22f4d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/SnippetCompiler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type SnippetCompilationResult = {
2424
};
2525

2626
export class SnippetCompiler {
27-
private readonly compiler: TSNode.Service;
27+
private readonly compilerConfig: TSNode.CreateOptions;
2828

2929
constructor(
3030
private readonly workingDirectory: string,
@@ -35,11 +35,10 @@ export class SnippetCompiler {
3535
packageDefinition.packageRoot,
3636
project
3737
);
38-
const tsConfig = {
38+
this.compilerConfig = {
3939
...(configOptions.config as TSNode.CreateOptions),
4040
transpileOnly: false,
4141
};
42-
this.compiler = TSNode.create(tsConfig);
4342
}
4443

4544
private static loadTypeScriptConfig(
@@ -124,7 +123,8 @@ export class SnippetCompiler {
124123
const id = process.hrtime.bigint().toString();
125124
const codeFile = path.join(this.workingDirectory, `block-${id}.${type}`);
126125
await fsExtra.writeFile(codeFile, code);
127-
this.compiler.compile(code, codeFile);
126+
const compiler = TSNode.create(this.compilerConfig);
127+
compiler.compile(code, codeFile);
128128
}
129129

130130
private removeTemporaryFilePaths(

test/TypeScriptDocsVerifierSpec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,31 @@ export const bob = () => (<div></div>);
388388
}
389389
);
390390

391+
verify.it("compiles snippets independently", async () => {
392+
const snippet1 = `interface Foo { bar: 123 }`;
393+
const snippet2 = `interface Foo { bar: () => void }`;
394+
const typeScriptMarkdown = wrapSnippet(snippet1) + wrapSnippet(snippet2);
395+
await createProject({
396+
markdownFiles: [{ name: "README.md", contents: typeScriptMarkdown }],
397+
});
398+
return await TypeScriptDocsVerifier.compileSnippets().should.eventually.eql(
399+
[
400+
{
401+
file: "README.md",
402+
index: 1,
403+
snippet: snippet1,
404+
linesWithErrors: [],
405+
},
406+
{
407+
file: "README.md",
408+
index: 2,
409+
snippet: snippet2,
410+
linesWithErrors: [],
411+
},
412+
]
413+
);
414+
});
415+
391416
verify.it(
392417
"compiles snippets containing modules",
393418
genSnippet,

0 commit comments

Comments
 (0)