Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,11 @@ namespace ts.codefix {
const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol);
if (defaultExport) {
const localSymbol = getLocalSymbolForExportDefault(defaultExport);
if ((localSymbol && localSymbol.escapedName === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName)
&& checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) {
if ((
localSymbol && localSymbol.escapedName === symbolName ||
getEscapedNameForExportDefault(defaultExport) === symbolName ||
moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName
) && checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) {
// check if this symbol is already used
const symbolId = getUniqueSymbolId(localSymbol || defaultExport, checker);
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Default }));
Expand All @@ -784,6 +787,22 @@ namespace ts.codefix {
const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName, checker);
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Named }));
}

function getEscapedNameForExportDefault(symbol: Symbol): __String | undefined {
return firstDefined(symbol.declarations, declaration => {
if (isExportAssignment(declaration)) {
if (isIdentifier(declaration.expression)) {
return declaration.expression.escapedText;
}
}
else if (isExportSpecifier(declaration)) {
Debug.assert(declaration.name.escapedText === InternalSymbolName.Default);
if (declaration.propertyName) {
return declaration.propertyName.escapedText;
}
}
});
}
});

return symbolIdActionMap.getAllActions();
Expand Down
3 changes: 3 additions & 0 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ namespace ts.FindAllReferences.Core {
/** @param allSearchSymbols set of additinal symbols for use by `includes`. */
createSearch(location: Node, symbol: Symbol, comingFrom: ImportExport | undefined, searchOptions: { text?: string, allSearchSymbols?: Symbol[] } = {}): Search {
// Note: if this is an external module symbol, the name doesn't include quotes.
// Note: getLocalSymbolForExportDefault handles `export default class C {}`, but not `export default C` or `export { C as default }`.
// The other two forms seem to be handled downstream (e.g. in `skipPastExportOrImportSpecifier`), so special-casing the first form
// here appears to be intentional).
const {
text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)),
allSearchSymbols = undefined,
Expand Down
12 changes: 10 additions & 2 deletions src/services/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ namespace ts.Rename {
return undefined;
}

const displayName = stripQuotes(getDeclaredName(typeChecker, symbol, node));
const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node);
return kind ? getRenameInfoSuccess(displayName, typeChecker.getFullyQualifiedName(symbol), kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile) : undefined;
if (!kind) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This change could use a test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't have a good way to test that it shows up in the right place in the UI, but I can try to add a lower-level test of the rename info.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just to clarify, what is the expected change to rename behavior due to this PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Different strings are displayed in VS dialogs. No impact on renaming.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There must be some function that returns a different value than before in some situation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, of course. And I believe I've found a fourslash helper for testing it. I just meant that the same user actions would result in the same result (i.e. renaming) after the change.

return undefined;
}

const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteral(node) && node.parent.kind === SyntaxKind.ComputedPropertyName)
? stripQuotes(getTextOfIdentifierOrLiteral(node))
: undefined;
const displayName = specifierName || typeChecker.symbolToString(symbol);
const fullDisplayName = specifierName || typeChecker.getFullyQualifiedName(symbol);
return getRenameInfoSuccess(displayName, fullDisplayName, kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile);
}
}
else if (node.kind === SyntaxKind.StringLiteral) {
Expand Down
13 changes: 0 additions & 13 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,19 +1278,6 @@ namespace ts {
});
}

export function getDeclaredName(typeChecker: TypeChecker, symbol: Symbol, location: Node): string {
// If this is an export or import specifier it could have been renamed using the 'as' syntax.
// If so we want to search for whatever is under the cursor.
if (isImportOrExportSpecifierName(location) || isStringOrNumericLiteral(location) && location.parent.kind === SyntaxKind.ComputedPropertyName) {
return getTextOfIdentifierOrLiteral(location);
}

// Try to get the local symbol if we're dealing with an 'export default'
// since that symbol has the "true" name.
const localExportDefaultSymbol = getLocalSymbolForExportDefault(symbol);
return typeChecker.symbolToString(localExportDefaultSymbol || symbol);
}

export function isImportOrExportSpecifierName(location: Node): location is Identifier {
return location.parent &&
(location.parent.kind === SyntaxKind.ImportSpecifier || location.parent.kind === SyntaxKind.ExportSpecifier) &&
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

// @Filename: /lib.ts
////class Base { }
////export default Base;

// @Filename: /test.ts
////[|class Derived extends Base { }|]

goTo.file("/test.ts");
verify.importFixAtPosition([
`import Base from "./lib";

class Derived extends Base { }`,]);
21 changes: 21 additions & 0 deletions tests/cases/fourslash/importNameCodeFixDefaultExport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />

// @allowJs: true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This doesn't really need to be a JS test, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

While I can no longer recall my original motivation, it appears to be a JS version of the TS test in importNameCodeFixDefaultExport2.ts.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well, I don't think we need the same test twice -- the same services code will be tested either way.

// @checkJs: true

// @Filename: /lib.js
////class Base { }
////export { Base as default };

// @Filename: /test.js
////[|class Derived extends Base { }|]

goTo.file("/test.js");
verify.importFixAtPosition([
`// @ts-ignore
class Derived extends Base { }`,
`// @ts-nocheck
class Derived extends Base { }`,
`import Base from "./lib";

class Derived extends Base { }`,]);
9 changes: 9 additions & 0 deletions tests/cases/fourslash/renameForAliasingExport01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts'/>

// @Filename: foo.ts
////let x = 1;
////
////export { /**/[|x|] as y };

goTo.marker();
verify.renameInfoSucceeded(/*displayName*/"x", /*fullDisplayName*/"x");
9 changes: 9 additions & 0 deletions tests/cases/fourslash/renameForAliasingExport02.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts'/>

// @Filename: foo.ts
////let x = 1;
////
////export { x as /**/[|y|] };

goTo.marker();
verify.renameInfoSucceeded(/*displayName*/"y", /*fullDisplayName*/'"/tests/cases/fourslash/foo".y');