fix(typescript): prevent type assertions with array brackets or ? from being treated as HTML tags#4381
Open
KJyang-0114 wants to merge 1 commit intohighlightjs:mainfrom
Conversation
…treated as HTML tags Fixes highlightjs#4301 - `<string[]>` breaks highlighting in TypeScript Root cause: The `isTrulyOpeningTag` function only checked for `<` and `,` as indicators of non-HTML syntax. When encountering `<string[]>` or `<string?>`, 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. `<string []>`). Verified: - All 1570 existing tests pass - `<string[]>` no longer treated as HTML tag - `<string?>` no longer treated as HTML tag - JSX constructs like `<Component />` still correctly highlighted - Regular HTML like `<div>` still correctly highlighted
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4301 -
<string[]>(and<string?>) break TypeScript highlighting.Root Cause
The
isTrulyOpeningTagfunction insrc/languages/javascript.jschecks various patterns to determine whether a<followed by word characters is an HTML/XML tag or a TypeScript/JavaScript type expression. It handles<(nested generics) and,(generic parameters), but was missing checks for:[— TypeScript type assertions with array notation like<string[]>?— TypeScript non-null assertions like<string?>When encountering
<string[]>,nextCharafter the tag name<stringis[. Since this wasn't recognized, the function proceeded to look for a closing</string>tag, treating everything after the>as HTML text until it found one.Fix
Added two new conditions to the
isTrulyOpeningTagignoreMatch check:nextChar === "["— catches<string[]>,<number[]>,<T[]>directlynextChar === "?"— catches<string?>,<T?>directlyAlso added
afterMatchregex checks for whitespace variants:/^\s*\[\]/— catches<string []>/^\s+\?/— catches<string ?>Test Evidence
Before fix:
After fix:
JSX constructs like
<Component />and real HTML like<div>continue to be correctly highlighted as XML.Validation
<string[]>,<number[]>,<T[]>,<string?>,<string []>all no longer break highlighting<div>,<Component />,<MyComponent prop=\"val\" />still correctly highlightedRisk & Rollback
isTrulyOpeningTag, which already had similar ignore patterns for<and,src/languages/javascript.js) revert to previous state