From 6bc438af4ea065447046f3794488e65a2219e9b5 Mon Sep 17 00:00:00 2001 From: KJyang-0114 Date: Sat, 21 Mar 2026 01:43:48 +0800 Subject: [PATCH] fix(javascript): prevent type assertions with [\ ] or `?` from being treated as HTML tags Fixes #4301 - `` breaks highlighting in TypeScript Root cause: The `isTrulyOpeningTag` function only checked for `<` and `,` as indicators of non-HTML syntax. When encountering `` or ``, the `nextChar` after the tag name is `[` or `?`, which were not recognized as TypeScript type assertion patterns. Fix: Added `nextChar === "["` and `nextChar === "?"` to the ignoreMatch conditions, and added `afterMatch` checks for cases with intervening whitespace (e.g. ``). Verified: - All 1570 existing tests pass - `` no longer treated as HTML tag - `` no longer treated as HTML tag - JSX constructs like `` still correctly highlighted - Regular HTML like `
` still correctly highlighted --- src/languages/javascript.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/languages/javascript.js b/src/languages/javascript.js index 65b60779a4..9f04452a0f 100644 --- a/src/languages/javascript.js +++ b/src/languages/javascript.js @@ -47,7 +47,13 @@ export default function(hljs) { nextChar === "<" || // the , gives away that this is not HTML // `` - nextChar === "," + nextChar === "," || + // TypeScript type assertion with array notation + // ``, ``, ``, etc. + nextChar === "[" || + // TypeScript optional type assertion + // ``, etc. + nextChar === "?" ) { response.ignoreMatch(); return; @@ -86,6 +92,13 @@ export default function(hljs) { return; } } + + // TypeScript type assertions with whitespace before brackets or question mark + // ``, ``, ``, `` + if ((m = afterMatch.match(/^\s*\[\]/)) || (m = afterMatch.match(/^\s+\?/))) { + response.ignoreMatch(); + return; + } } }; const KEYWORDS = {