Merge pull request #21014 from amcasey/GH19115

Handle different default export forms the same way in import code fixes
This commit is contained in:
Andrew Casey authored and GitHub committed 2018-01-08 19:07:43 -08:00
commit ef5a289966
8 files changed
+87 -17

No files matched your search

+21 -2
View File
@@ -780,8 +780,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 }));
@@ -794,6 +797,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();
+3
View File
@@ -496,6 +496,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,
+10 -2
View File
@@ -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) {
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) {
-13
View File
@@ -1274,19 +1274,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) &&
@@ -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 { }`,]);
@@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @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 { }`,]);
@@ -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");
@@ -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');