Skip to content

Commit

Permalink
Merge pull request #32 from bbc/force-ts-node-to-typecheck
Browse files Browse the repository at this point in the history
Force ts-node to type check code
  • Loading branch information
paulbrimicombe authored Feb 14, 2025
2 parents 30a2d63 + 220e086 commit 2ff2293
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-requests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v2
Expand Down
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.1]
### Added
- Override any project-specific `ts-node` `transpileOnly` to force type checking when compiling code snippets

## [2.5.0]
### Added
- Support for `tsx` snippets
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.0",
"version": "2.5.1",
"main": "dist/index.js",
"@types": "dist/index.d.ts",
"bin": "./dist/bin/compile-typescript-docs.js",
Expand Down
6 changes: 5 additions & 1 deletion src/SnippetCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export class SnippetCompiler {
packageDefinition.packageRoot,
project
);
this.compiler = TSNode.create(configOptions.config as TSNode.CreateOptions);
const tsConfig = {
...(configOptions.config as TSNode.CreateOptions),
transpileOnly: false,
};
this.compiler = TSNode.create(tsConfig);
}

private static loadTypeScriptConfig(
Expand Down
44 changes: 44 additions & 0 deletions test/TypeScriptDocsVerifierSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,50 @@ Gen.string()
}
);

verify.it(
"reports compilation failures when ts-node is configured to transpile only in tsconfig.json",
genSnippet,
async (validSnippet) => {
const invalidSnippet = `import('fs').thisFunctionDoesNotExist();`;
const validTypeScriptMarkdown = wrapSnippet(validSnippet);
const invalidTypeScriptMarkdown = wrapSnippet(invalidSnippet);
const markdown = [
validTypeScriptMarkdown,
invalidTypeScriptMarkdown,
].join("\n");
await createProject({
markdownFiles: [{ name: "README.md", contents: markdown }],
tsConfig: JSON.stringify({
...defaultTsConfig,
"ts-node": {
transpileOnly: true,
},
}),
});
return await TypeScriptDocsVerifier.compileSnippets().should.eventually.satisfy(
(results: TypeScriptDocsVerifier.SnippetCompilationResult[]) => {
results.should.have.length(2);
results[0].should.not.have.property("error");
const errorResult = results[1];
errorResult.should.have.property("file", "README.md");
errorResult.should.have.property("index", 2);
errorResult.should.have.property("snippet", invalidSnippet);
errorResult.should.have.property("error");
errorResult.linesWithErrors.should.deep.equal([1]);
errorResult?.error?.message.should.include("README.md");
errorResult?.error?.message.should.include("Code Block 2");
errorResult?.error?.message.should.not.include("block-");

Object.values(errorResult.error || {}).forEach((value: unknown) => {
(value as string).should.not.include("block-");
});

return true;
}
);
}
);

verify.it("reports compilation failures on the correct line", async () => {
const mainFile = {
name: `${defaultPackageJson.main}`,
Expand Down

0 comments on commit 2ff2293

Please sign in to comment.