Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typeEvlauator): handle rest on object splat operations #232

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sgulseth
Copy link
Member

@sgulseth sgulseth commented May 7, 2024

When splatting an object we didn't map over the rest property, if it's an object we should append the attributes. If rest is unknown we should treat the entire object as unknown.

Fixes sanity-io/sanity#6555

@sgulseth sgulseth requested review from judofyr and a team May 7, 2024 10:49
@sgulseth sgulseth force-pushed the fix/rest-object-splat branch 2 times, most recently from 8ef8b24 to 15ec7ac Compare May 7, 2024 13:21
@judofyr
Copy link
Collaborator

judofyr commented May 7, 2024

Something looks a bit sketchy.

Here we invoke the mapper multiple times:

if (node.type === 'union') {
for (const scoped of node.of) {
mapObjectSplat(scoped, scope, mapper)
}
}

But here we assume that it's only being called once per name:

mapObjectSplat(value, scope, (name, attribute) => {
attributes[name] = attribute
})

mapObjectSplat also only handles union and object and ignores unknown. It seems that if you now splat something which we can't determine it ends up being ignored? 🤔

I'm not 100% sure what the right approach here is though. Maybe more usage of mapConcrete?

@sgulseth
Copy link
Member Author

sgulseth commented May 7, 2024

yess, this should probably be a concrete type with mapConcrete, will fix. Didn't think much about fixing other bugs than the one referenced 😅

@sgulseth
Copy link
Member Author

sgulseth commented May 7, 2024

@judofyr did another pass on comparing with mapped concrete type node

@sgulseth sgulseth force-pushed the fix/rest-object-splat branch 2 times, most recently from 1558ef1 to 8992280 Compare May 14, 2024 11:36
const attributes: Record<string, ObjectAttribute> = {}

const concreteNode = mapConcrete(node, scope, (node) => node)
if (concreteNode.type === 'object') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Doesn't this mean that if this returns false we end up returning {type: 'object', attributes: {}} below? What about we instead wrap all of this in mapConcrete?

return mapConcrete(node, scope, (node) => {
  if (node.type !== 'object') return {type: 'unknown'}
  const attributes: Record<string, ObjectAttribute> = {}
  // same same…
})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did another pass, moved the handling of the object splats out in its own methods

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.

Typegen not reading => @ correctly.
2 participants