Skip to content

Completion for default export should be '.default' #16742

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
5 commits merged into from
Jul 11, 2017
Merged

Conversation

ghost
Copy link

@ghost ghost commented Jun 26, 2017

Fixes #16706 and #12957

Just use symbol.name instead of getDeclaredName, since getDeclaredName intentionally gets the declared name of a default export instead of just "default".

This change apparently also affects completions for unicode escapes, but I think the change is good. In this code:

function \u0042() {
    /**/
}

We will now give you a completion for B, which is correct -- B is in scope because \u0042 is equivalent to it.

Update: Also fixed #16741.
Update:
We must also change the behavior for getSymbolsInScope() to return local symbols, not exported symbols. We need different behavior based on whether the symbols are in scope or from an export; today's example being that an export is accessed with '.default' while a local symbol is accessed by its name. Also fixed a bug where getSymbolAtLocation couldn't handle a local symbol.

@ghost ghost force-pushed the completionsDefaultExport branch 2 times, most recently from f58a503 to 88ce5b0 Compare June 26, 2017 20:32
@ghost ghost force-pushed the completionsDefaultExport branch from 88ce5b0 to 95613fb Compare June 26, 2017 21:16
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

I like the results but I have some questions about the implementation.

@@ -22226,11 +22228,6 @@ namespace ts {
}

switch (location.kind) {
case SyntaxKind.SourceFile:
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why this case goes away.

Copy link
Author

Choose a reason for hiding this comment

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

This comes from the second Update comment -- exported symbols aren't really the same as the symbols in scope.

////a./**/;

goTo.marker();
verify.completionListContains("default", "function f(): void");
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, is a.f legal here?

Copy link
Author

Choose a reason for hiding this comment

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

No, the property name is default; function f(): void is just the display text.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I misread that as varargs


/** See comment on `declareModuleMember` in `binder.ts`. */
export function getCombinedLocalAndExportSymbolFlags(symbol: Symbol): SymbolFlags {
return symbol.exportSymbol ? symbol.exportSymbol.flags | symbol.flags : symbol.flags;
Copy link
Member

Choose a reason for hiding this comment

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

why combine flags in the true case instead of just returning symbol.exportSymbol.flags?

Copy link
Author

Choose a reason for hiding this comment

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

In findAllRefsForDefaultExport08 (and findAllRefsForDefaultExport02), we have an exported class and a non-exported namespace merged with it. So the local symbol will have flags that the exported symbol is lacking; it has NamespaceModule|ExportValue|ExportType while the exported symbol just has Class.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense

@@ -949,6 +949,22 @@ namespace FourSlash {
this.verifySymbol(symbol, declarationRanges);
}

public symbolsInScope(range: Range): ts.Symbol[] {
const node = this.goToAndGetNode(range);
return this.getChecker().getSymbolsInScope(node, ts.SymbolFlags.Value | ts.SymbolFlags.Type | ts.SymbolFlags.Namespace);
Copy link
Member

Choose a reason for hiding this comment

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

nit: doesn't SymbolFlags have an alias defined like SymbolFlags.All that's equivalent?

Copy link
Author

@ghost ghost Jul 10, 2017

Choose a reason for hiding this comment

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

Not currently; All wouldn't be a good name since that excludes some (Constructor, Signature, and everything from ExportValue on), but this combination is often useful; I'm tempted to just name it ValueTypeNamespace.

@@ -3720,6 +3740,10 @@ namespace FourSlashInterface {
this.state.verifySymbolAtLocation(startRange, declarationRanges);
}

public typeOfSymbolAtLocation(range: FourSlash.Range, symbol: ts.Symbol, expected: string) {
Copy link
Member

Choose a reason for hiding this comment

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

can't we get this from the quick info? I don't know whether we need a separate method for comparing the stringified type of a symbol.

Copy link
Author

Choose a reason for hiding this comment

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

Well, quick info gets us the type of the symbol under the cursor -- this lets us call getTypeOfSymbolAtLocation on any symbol, at the current location.

// We want to store any numbers/strings if they were a name that could be
// related to a declaration. So, if we have 'import x = require("something")'
// then we want 'something' to be in the name table. Similarly, if we have
// "a['propname']" then we want to store "propname" in the name table.
Copy link
Member

Choose a reason for hiding this comment

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

should be jsdoc

@ghost ghost merged commit aa2d100 into master Jul 11, 2017
@ghost ghost deleted the completionsDefaultExport branch July 11, 2017 14:23
@ghost ghost mentioned this pull request Jul 27, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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.

Don't return empty string from getNameTable Incorrect completion offered for export default of named function/class
2 participants