diff options
-rw-r--r-- | cli/tests/integration/test_tests.rs | 6 | ||||
-rw-r--r-- | cli/tests/testdata/test/markdown_with_comment.md | 36 | ||||
-rw-r--r-- | cli/tests/testdata/test/markdown_with_comment.out | 5 | ||||
-rw-r--r-- | cli/tools/test.rs | 11 |
4 files changed, 57 insertions, 1 deletions
diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index 6424dabd0..f3cf9ebd0 100644 --- a/cli/tests/integration/test_tests.rs +++ b/cli/tests/integration/test_tests.rs @@ -103,6 +103,12 @@ itest!(markdown_full_block_names { output: "test/markdown_full_block_names.out", }); +itest!(markdown_ignore_html_comment { + args: "test --doc --allow-all test/markdown_with_comment.md", + exit_code: 1, + output: "test/markdown_with_comment.out", +}); + itest!(text { args: "test --doc --allow-all test/text.md", exit_code: 0, diff --git a/cli/tests/testdata/test/markdown_with_comment.md b/cli/tests/testdata/test/markdown_with_comment.md new file mode 100644 index 000000000..886e88103 --- /dev/null +++ b/cli/tests/testdata/test/markdown_with_comment.md @@ -0,0 +1,36 @@ +# Documentation + +The following examples are inside HTML comments and will not trigger the +type-checker: + +<!-- ```ts ignore +const value: Invalid = "ignored"; +``` --> + +<!-- +```ts +const a: string = 42; +``` +--> + +<!-- + +This is a comment. + +```ts +const a: string = 42; +``` + +Something something more comments. + +```typescript +const a: boolean = "true"; +``` + +--> + +The following example will trigger the type-checker to fail: + +```ts +const a: string = 42; +``` diff --git a/cli/tests/testdata/test/markdown_with_comment.out b/cli/tests/testdata/test/markdown_with_comment.out new file mode 100644 index 000000000..b202919d8 --- /dev/null +++ b/cli/tests/testdata/test/markdown_with_comment.out @@ -0,0 +1,5 @@ +Check [WILDCARD]/test/markdown_with_comment.md$34-37.ts +error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. +const a: string = 42; + ^ + at [WILDCARD]/test/markdown_with_comment.md$34-37.ts:1:7 diff --git a/cli/tools/test.rs b/cli/tools/test.rs index 74646986d..61a37c7ff 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -543,6 +543,10 @@ fn extract_files_from_regex_blocks( let files = blocks_regex .captures_iter(source) .filter_map(|block| { + if block.get(1) == None { + return None; + } + let maybe_attributes: Option<Vec<_>> = block .get(1) .map(|attributes| attributes.as_str().split(' ').collect()); @@ -663,7 +667,12 @@ fn extract_files_from_fenced_blocks( source: &str, media_type: MediaType, ) -> Result<Vec<File>, AnyError> { - let blocks_regex = Regex::new(r"```([^\r\n]*)\r?\n([\S\s]*?)```")?; + // The pattern matches code blocks as well as anything in HTML comment syntax, + // but it stores the latter without any capturing groups. This way, a simple + // check can be done to see if a block is inside a comment (and skip typechecking) + // or not by checking for the presence of capturing groups in the matches. + let blocks_regex = + Regex::new(r"(?s)<!--.*?-->|```([^\r\n]*)\r?\n([\S\s]*?)```")?; let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?; extract_files_from_regex_blocks( |