From 8db3a9546b59fdd5e7203f2e63a828e3c5108e7e Mon Sep 17 00:00:00 2001 From: Geert-Jan Zwiers <34610306+GJZwiers@users.noreply.github.com> Date: Fri, 11 Mar 2022 02:14:32 +0100 Subject: [PATCH] fix(test): skip typechecking for blocks inside HTML comments (#13889) --- cli/tests/integration/test_tests.rs | 6 ++++ .../testdata/test/markdown_with_comment.md | 36 +++++++++++++++++++ .../testdata/test/markdown_with_comment.out | 5 +++ cli/tools/test.rs | 11 +++++- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 cli/tests/testdata/test/markdown_with_comment.md create mode 100644 cli/tests/testdata/test/markdown_with_comment.out diff --git a/cli/tests/integration/test_tests.rs b/cli/tests/integration/test_tests.rs index 6424dabd0e..f3cf9ebd08 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 0000000000..886e881033 --- /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: + + + + + + + +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 0000000000..b202919d8f --- /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 74646986d2..61a37c7ff5 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> = 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, 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(