Skip to content

fix(autocomplete): referenceText must not emit invalid MQL for unsafe field names#713

Open
Jacquelinezhong wants to merge 1 commit into
microsoft:nextfrom
Jacquelinezhong:dev/jacquelinezhong/fix-709-safe-reference-text
Open

fix(autocomplete): referenceText must not emit invalid MQL for unsafe field names#713
Jacquelinezhong wants to merge 1 commit into
microsoft:nextfrom
Jacquelinezhong:dev/jacquelinezhong/fix-709-safe-reference-text

Conversation

@Jacquelinezhong

Copy link
Copy Markdown
Contributor

Summary

Closes #709

toFieldCompletionItems() previously emitted referenceText as `$${entry.path}` for every field, which produces invalid MQL for field names containing special characters (e.g., $order-items, $my field).

Changes:

  • Check each dot-separated path segment against JS_IDENTIFIER_PATTERN individually (not the full path, so nested paths like address.city keep their valid $address.city form)
  • Flat field with an unsafe segment (e.g., order-items) → { $getField: "order-items" }
  • Nested path with an unsafe segment (e.g., order.item-count) → referenceText omitted; chained $getField form is complex and tracked as a follow-up in future-work.md
  • FieldCompletionData.referenceText widened from string to string? so the tRPC client-side inferred type stays consistent
  • future-work.md item 2 marked as resolved

Behaviour table

Field path Before After
age $age $age
address.city $address.city $address.city
order-items $order-items { $getField: "order-items" }
my field $my field { $getField: "my field" }
say"hi" $say"hi" { $getField: "say\"hi\"" }
order.item-count $order.item-count undefined ✅ (no invalid MQL emitted)

Test plan

  • toFieldCompletionItems.test.ts — 13 tests pass (3 new tests added for unsafe flat fields, unsafe nested paths, and safe nested paths)
  • tsc --noEmit — no type errors

🤖 Generated with Claude Code

… field names

Previously, toFieldCompletionItems() always emitted referenceText as
\`\$\${entry.path}\`, which produces invalid MQL for field names such as
"order-items", "my field", or "say\"hi\"" (e.g., \`\$order-items\`).

Fix: check each dot-separated path segment individually against
JS_IDENTIFIER_PATTERN.
- All segments valid → \`\$field.path\` (existing behaviour, e.g., \`\$address.city\`)
- Flat field with unsafe name → \`{ \$getField: "name" }\`
- Nested path with an unsafe segment → referenceText omitted (chained
  \$getField form is complex; tracked as a follow-up in future-work.md)

FieldCompletionData.referenceText widened from required \`string\` to
optional \`string | undefined\` (\`referenceText?: string\`) so that tRPC
type inference on the client side stays consistent.

Closes microsoft#709

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Jacquelinezhong Jacquelinezhong requested a review from a team as a code owner May 30, 2026 06:45
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