Skip to content

Fix InlayHint.is throwing on undefined and accepting invalid hints - #1877

Open
Alexandre Kohler (kwy404) wants to merge 1 commit into
microsoft:mainfrom
kwy404:fix-inlay-hint-is-precedence
Open

Alexandre Kohler (kwy404) wants to merge 1 commit into
microsoft:mainfrom
kwy404:fix-inlay-hint-is-precedence

Conversation

@kwy404

Copy link
Copy Markdown
Contributor

InlayHint.is has a misplaced parenthesis on the textEdits check: && (candidate.textEdits === undefined) || Is.typedArray(candidate.textEdits, TextEdit.is). Because && binds tighter than ||, this splits the whole guard into two halves joined by ||. When textEdits is undefined, the tooltip, paddingLeft and paddingRight checks are ignored. When textEdits is an array, the object literal, position, label and kind checks are ignored. For undefined or null, the second half reads candidate.textEdits and throws. For example InlayHint.is({ textEdits: [] }) returns true, and InlayHint.is(undefined) throws TypeError: Cannot read properties of undefined (reading 'textEdits') instead of returning false.

The fix moves the closing parenthesis so the check reads (candidate.textEdits === undefined || Is.typedArray(candidate.textEdits, TextEdit.is)), like the other optional property checks in the same function.

I added an InlayHint.is suite to types/src/test/typeguards.test.ts. Before the change the invalid paddingLeft and missing position and label cases fail with true !== false and the undefined case fails with the TypeError above. After it they pass, and a valid InlayHint.create(...) is still accepted. The full types test suite (45 tests) passes and eslint is clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant