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

"Jump to definition" doesn't work with v5's setup(...) API #479

Open
HerbCaudill opened this issue Mar 5, 2024 · 3 comments
Open

"Jump to definition" doesn't work with v5's setup(...) API #479

HerbCaudill opened this issue Mar 5, 2024 · 3 comments

Comments

@HerbCaudill
Copy link

This feature advertised on the extension README doesn't seem to work with APIs recommended for setup in v5. My state machine is instantiated like this

const machine = setup({
  types: { ... },
  actions: { ... },
  guards: { ... })
.createMachine({
  context: { ... },
  // ... state machine definition
})

As far as I can tell:

  • I can command-click on some (but not all) target states and it'll jump to the correct state definition. It seems to work when the target states are at the root level, or when they're linked via id like #disconnected.
  • I can't jump to the definitions of any actions or guards. Which is weird because they all show up in intellisense, so the extension knows they exist.

In this screen capture, everywhere I click is a command-click, so should jump to definition if one is available.

Screen.Recording.2024-03-05.at.7.33.55.PM.mov
@Andarist
Copy link
Member

Andarist commented Mar 5, 2024

We are in the process of rewriting the extension to improve v5 compatibility. This feature isn't exactly on our MVP list but the rewrite will allow us to implement this soon after we manage to cut that first MVP release.

@HerbCaudill
Copy link
Author

That's great to hear! I think this specifically would make a big difference for adoption. My take as a developer FWIW is that XState's biggest DX challenge is the whole string-literals-that-refer-to-functions thing, and that's largely to do with the friction that comes from not having IDE support for going back and forth between the functions and their string references.

@Andarist
Copy link
Member

Andarist commented Mar 6, 2024

I now really really wonder if we just couldn't improve this situation in TS itself.

It doesn't sound unreasonable for this to work:

declare function setup<TActions>(_: { actions: TActions }): {
  createMachine: (config: { entry: keyof TActions }) => void;
};

setup({
  actions: {
    doStuff: () => {},
    doMoreStuff: () => {},
  },
}).createMachine({
  // "go to definition" on the string here should bring us to the action's definition
  entry: "doStuff", 
});

Our situation is, of course, a little bit more complex and involves even more indirection... but making the above work could be the first step to improving our situation. All of this is highly related to this open ticket: microsoft/TypeScript#49033

note for myself: I think there is a good chance that this could be improved by creating the origin type for Anonymous objects (and maybe some others too) in getLiteralTypeFromProperties. Later on, this information could be read by getDefinitionAtPosition from the contextual type, and at that point, it should be fairly straightforward to create the definition based on that. This would, at the very least, improve the situation when this contextual type is a union (that's why my example above intentionally has 2 actions and not just 1). Fixing this the same for objects with a single property would require introducing an additional concept to the compiler - literal string types with .origin. It's not impossible but it would require more work and there are memory/perf concerns related to this. I think that fixing this just for unions, especially for the PoC, would be good enough for the first iteration.

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

No branches or pull requests

2 participants