Skip to content

Commit

Permalink
Merge pull request #33 from bbc/ensure-snippets-are-compiled-independ…
Browse files Browse the repository at this point in the history
…ently

Use a separate compiler instance per snippet
  • Loading branch information
paulbrimicombe authored Feb 14, 2025
2 parents 3686a26 + 4506393 commit 7a402a8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"typescript",
"verify"
],
"version": "2.5.2",
"version": "2.5.3",
"main": "dist/index.js",
"@types": "dist/index.d.ts",
"bin": {
Expand Down
8 changes: 4 additions & 4 deletions src/SnippetCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions test/TypeScriptDocsVerifierSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,31 @@ export const bob = () => (<div></div>);
}
);

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,
Expand Down

0 comments on commit 7a402a8

Please sign in to comment.