-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable type-checked code blocks (#106)
- Loading branch information
1 parent
7726268
commit 469b021
Showing
11 changed files
with
121 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
'mdxts': minor | ||
--- | ||
|
||
Enables type-checking for the `CodeBlock` component. To opt-out of type-checking, use the `allowErrors` prop on the code block: | ||
|
||
```tsx allowErrors | ||
const a = 1 | ||
a + b | ||
``` | ||
|
||
This will disable type-checking for the code block and prevent erroring. To show the errors, usually for educational purposes, use the `showErrors` prop: | ||
|
||
```tsx allowErrors showErrors | ||
const a = 1 | ||
a + b | ||
``` | ||
|
||
### Breaking Changes | ||
|
||
`CodeBlock` now throws an error if the code block is not valid TypeScript. This is to ensure that all code blocks are type-checked and work as expected. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/mdxts/src/components/CodeBlock/get-diagnostics-or-throw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { SourceFile } from 'ts-morph' | ||
import { getDiagnosticMessageText } from '@tsxmod/utils' | ||
import { join, sep } from 'node:path' | ||
|
||
/** Retrieves diagnostics from a source file and throws an error if errors are found. */ | ||
export function getDiagnosticsOrThrow( | ||
sourceFile: SourceFile | undefined, | ||
allowErrors: string | boolean = false, | ||
showErrors: boolean = false | ||
): any[] { | ||
const allowedErrorCodes: number[] = | ||
typeof allowErrors === 'string' | ||
? allowErrors.split(',').map((code) => parseInt(code, 10)) | ||
: [] | ||
|
||
if (!sourceFile) { | ||
return [] | ||
} | ||
|
||
const diagnostics = sourceFile | ||
.getPreEmitDiagnostics() | ||
.filter((diagnostic) => diagnostic.getSourceFile()) | ||
|
||
if (showErrors) { | ||
if (allowedErrorCodes.length > 0) { | ||
return diagnostics.filter((diagnostic) => { | ||
return allowedErrorCodes.includes(diagnostic.getCode()) | ||
}) | ||
} | ||
|
||
return diagnostics | ||
} | ||
|
||
if (allowErrors === false) { | ||
if (diagnostics.length > 0) { | ||
const workingDirectory = join(process.cwd(), 'mdxts', sep) | ||
const filePath = sourceFile.getFilePath().replace(workingDirectory, '') | ||
const errorDetails = diagnostics | ||
.map((diagnostic) => { | ||
const message = getDiagnosticMessageText(diagnostic.getMessageText()) | ||
const line = diagnostic.getLineNumber() | ||
return `line ${line} (${diagnostic.getCode()}): ${message.replaceAll(workingDirectory, '')}` | ||
}) | ||
.join('\n\n') | ||
|
||
throw new Error( | ||
`[mdxts] CodeBlock type errors found for filename "${filePath}"\n\n${errorDetails}` | ||
) | ||
} | ||
|
||
return [] | ||
} | ||
|
||
if (allowErrors && allowedErrorCodes.length === 0) { | ||
return [] | ||
} | ||
|
||
return diagnostics.filter((diagnostic) => { | ||
return !allowedErrorCodes.includes(diagnostic.getCode()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters