Skip to content

For { type: "a" } | { type: "b" }, find references for the union property #21298

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

Merged
2 commits merged into from
Jan 20, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jan 19, 2018

Fixes #21288
Discovered #21297

This will not fix the issue if it is written like:

interface ResultA { kind: "success", code: number }
interface ResultB { kind: "failure", message: string }
type Result = ResultA | ResultB;

That case is harder, because we would have to find all references to the containing type to find out if it is used as part of a union.

@ghost ghost requested review from armanio123, RyanCavanaugh and sheetalkamat January 19, 2018 16:34
@@ -415,7 +415,11 @@ namespace ts.FindAllReferences.Core {
return checker.getImmediateAliasedSymbol(symbol);
}

return symbol;
// If the symbol is declared as part of a declaration like `{ type: "a" } | { type: "b" }`, use the property on the union type to get more references.
return symbol.declarations && firstDefined(symbol.declarations, decl =>
Copy link
Member

Choose a reason for hiding this comment

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

getReferencedSymbolsForSymbol instead of returning propertyOfUnion, shouldnt you be returning all properties of the constituents with that name as symbols from getReferencedSymbolsForSymbol

Copy link
Author

Choose a reason for hiding this comment

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

If we use the property on the union as the search symbol, populateSearchSymbolSet will call checker.getRootSymbols to get the constituents of the union.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah makes sense.. Whats the reason to check if declaration is type node.. why not just check if the type at that location is union and return union property ?

Copy link
Author

Choose a reason for hiding this comment

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

In: { type: "a" } | { type: "b" }
If we're doing find-all-references on the first type, the declaration will be a PropertyDeclaration type: "a". The parent is a TypeLiteralNode { type: "a" } and parent.parent is the whole thing.
We have to go at least to parent.parent to get a union type node. If we make calls at any child of that we won't get a union type, but the nameless type for the node { type: "a" } which is not a union, but a constituent of the union.

@ghost
Copy link
Author

ghost commented Jan 19, 2018

Waiting for approval from @sheetalkamat before merging.

@ghost ghost merged commit 6224d51 into master Jan 20, 2018
@ghost ghost deleted the findAllRefsUnionProperty branch January 20, 2018 00:04
errendir added a commit to errendir/TypeScript that referenced this pull request Jan 20, 2018
* origin/master: (134 commits)
  Fix isTypeOfExpression in compiler API (microsoft#20875). (microsoft#20884)
  add completion filter for function like body (microsoft#21257)
  Make nonnull assertions and binding patterns apparent declared type locations (microsoft#20995)
  For `{ type: "a" } | { type: "b" }`, find references for the union property (microsoft#21298)
  configureNightly -> configurePrerelease
  Create a 'configure-insiders' and 'publish-insiders' task.
  Add createProgram on WatchCompilerHost
  in goToDefinition, use array helpers and clean up code (microsoft#21304)
  Support testing definition range of a reference gruop (microsoft#21302)
  Handle `undefined` input to firstDefined (microsoft#21300)
  Avoid spreading array (microsoft#21291)
  LEGO: check in for master to temporary branch.
  Accept new baselines
  Add regression test
  Properly handle contravariant inferences in inferReverseMappedType
  Remove unused properties from interface Refactor (microsoft#21286)
  LEGO: check in for master to temporary branch.
  Fold newline logic into getNewLineOrDefaultFromHost
  External test runner updates (microsoft#21276)
  Report more detailed info during script debug failure
  ...
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renaming the discrimnant of a union should always rename all occurrences
2 participants