Skip to content

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

Closed
Jacqueline Zhong (Jacquelinezhong) wants to merge 1 commit into
microsoft:nextfrom
Jacquelinezhong:dev/jacquelinezhong/fix-709-safe-reference-text
Closed

Jacqueline Zhong (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 <[email protected]>
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Thank you for this contribution, and apologies for the delay in responding.

With the larger volume of PRs coming in recently, we missed this submission and started reviewing another PR that addresses the same issue (#717). That was an oversight on our side, not a reflection of the quality of your work here. Your PR was actually the earlier submission, it was clean and isolated, and importantly it avoided emitting invalid MQL by returning an undefined reference for the nested unsafe case rather than producing a wrong expression.

While finalizing #717 we added a few improvements on top of the shared approach:

  • nested paths with an unsafe segment now produce a correct chained { $getField: { field, input } } reference instead of being omitted
  • field names containing $ are routed through $getField (a plain $-prefix would be misread as a variable)
  • expanded test coverage and a scope cleanup of that branch

Because #717 builds out those additional cases, we are going to merge #717 to close out #709. This is not a quality judgment against your PR, the two overlapped and we are consolidating on one. We appreciate the time and care you put into this, and we hope to see more contributions from you.

Closing this in favor of #717, with thanks.

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.

2 participants