Skip to content

Avoid convertExport when there's a non-identifier or a bogus one #44106

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
merged 1 commit into from
May 24, 2021
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
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6397,6 +6397,10 @@
"category": "Message",
"code": 95163
},
"Can only convert named export": {
"category": "Message",
"code": 95164
},

"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",
Expand Down
19 changes: 14 additions & 5 deletions src/services/refactors/convertExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace ts.refactor {
};

function getInfo(context: RefactorContext, considerPartialSpans = true): ExportInfo | RefactorErrorInfo | undefined {
const { file } = context;
const { file, program } = context;
const span = getRefactorContextSpan(context);
const token = getTokenAtPosition(file, span.start);
const exportNode = !!(token.parent && getSyntacticModifierFlags(token.parent) & ModifierFlags.Export) && considerPartialSpans ? token.parent : getParentNodeInSpan(token, file, span);
Expand All @@ -75,6 +75,11 @@ namespace ts.refactor {
return { error: getLocaleSpecificMessage(Diagnostics.This_file_already_has_a_default_export) };
}

const checker = program.getTypeChecker();
const noSymbolError = (id: Node) =>
(isIdentifier(id) && checker.getSymbolAtLocation(id)) ? undefined
: { error: getLocaleSpecificMessage(Diagnostics.Can_only_convert_named_export) };

switch (exportNode.kind) {
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ClassDeclaration:
Expand All @@ -83,7 +88,9 @@ namespace ts.refactor {
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.ModuleDeclaration: {
const node = exportNode as FunctionDeclaration | ClassDeclaration | InterfaceDeclaration | EnumDeclaration | TypeAliasDeclaration | NamespaceDeclaration;
return node.name && isIdentifier(node.name) ? { exportNode: node, exportName: node.name, wasDefault, exportingModuleSymbol } : undefined;
if (!node.name) return undefined;
return noSymbolError(node.name)
|| { exportNode: node, exportName: node.name, wasDefault, exportingModuleSymbol };
}
case SyntaxKind.VariableStatement: {
const vs = exportNode as VariableStatement;
Expand All @@ -94,12 +101,14 @@ namespace ts.refactor {
const decl = first(vs.declarationList.declarations);
if (!decl.initializer) return undefined;
Debug.assert(!wasDefault, "Can't have a default flag here");
return isIdentifier(decl.name) ? { exportNode: vs, exportName: decl.name, wasDefault, exportingModuleSymbol } : undefined;
return noSymbolError(decl.name)
|| { exportNode: vs, exportName: decl.name as Identifier, wasDefault, exportingModuleSymbol };
}
case SyntaxKind.ExportAssignment: {
const node = exportNode as ExportAssignment;
const exp = node.expression as Identifier;
return node.isExportEquals ? undefined : { exportNode: node, exportName: exp, wasDefault, exportingModuleSymbol };
if (node.isExportEquals) return undefined;
return noSymbolError(node.expression)
|| { exportNode: node, exportName: node.expression as Identifier, wasDefault, exportingModuleSymbol };
}
default:
return undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @allowJs: true

// @Filename: /a.js
/////*[| |]*/ /** x */ export default 1;

// @Filename: /b.js
/////*[| |]*/ /** x */ export default (1);

// @Filename: /c.js
/////*[| |]*/ /** x */ export default x;

goTo.eachRange(r => {
goTo.selectRange(r);
Copy link
Member

Choose a reason for hiding this comment

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

interesting, do you have to select each range? I thought having the cursor there would be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But there are three ranges in three files... In any case, existing cases use markers, which I didn't want to mix with a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

The cursor there alone is not supposed to be enough unless invoked by the refactor command, because I thought it would be too annoying. See https://github.com/microsoft/TypeScript/blob/master/tests/cases/fourslash/refactorConvertExportForTriggerReason.ts

Copy link
Member

Choose a reason for hiding this comment

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

OK. Some refactors are offered in cases other than explicit invoking of the refactor command, right? I always forget about that.

verify.not.refactorAvailable("Convert export");
});

// goTo.selectRange(test.ranges()[0]);
// edit.applyRefactor({
// refactorName: "Convert export",
// actionName: "Convert default export to named export",
// actionDescription: "Convert default export to named export",
// newContent: { "/a.js": `...` },
// });