Improve errors for incorrectly nested export default (#43967)

* Improve errors for incorrectly nested export default

The compiler and services don't handle incorrectly nested
`export default` well right now:

```ts
export = (x,y) => {
  export default { }
}
```

Asking for document highlights, find all references or quick info on
'export' or 'default' cause a crash. After the crash is fixed, the error
message is confusing and wrong: "An export assignment cannot be used outside a module."

This PR:

1. Skips document highlights for incorrectly nested export default.
2. Skips find all refs for incorrectly nested export default.
3. Switches the fallback binding for incorrectly nested export default
from Alias to Property. Neither is correct, but Property doesn't cause a
crash in alias resolution.
4. Improves the error message to reflect a post-ES module world, which
has export default and 'module' means 'ES module', not 'namespace'.

Fixes #40082 and the related bugs mentioned above.

* address PR comments
This commit is contained in:
Nathan Shively-Sanders authored and GitHub committed 2021-05-07 08:09:38 -07:00
1 parent ad6ca7ae2c
commit f6303652d2
12 files changed
+387 -29

No files matched your search

+2 -2
View File
@@ -2768,8 +2768,8 @@ namespace ts {
function bindExportAssignment(node: ExportAssignment) {
if (!container.symbol || !container.symbol.exports) {
// Export assignment in some sort of block construct
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node)!);
// Incorrect export assignment in some sort of block construct
bindAnonymousDeclaration(node, SymbolFlags.Value, getDeclarationName(node)!);
}
else {
const flags = exportAssignmentIsAlias(node)
+4 -1
View File
@@ -38315,7 +38315,10 @@ namespace ts {
}
function checkExportAssignment(node: ExportAssignment) {
if (checkGrammarModuleElementContext(node, Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) {
const illegalContextMessage = node.isExportEquals
? Diagnostics.An_export_assignment_must_be_at_the_top_level_of_a_file_or_module_declaration
: Diagnostics.A_default_export_must_be_at_the_top_level_of_a_file_or_module_declaration;
if (checkGrammarModuleElementContext(node, illegalContextMessage)) {
// If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
return;
}
+5 -1
View File
@@ -743,7 +743,7 @@
"category": "Error",
"code": 1230
},
"An export assignment can only be used in a module.": {
"An export assignment must be at the top level of a file or module declaration.": {
"category": "Error",
"code": 1231
},
@@ -847,6 +847,10 @@
"category": "Error",
"code": 1257
},
"A default export must be at the top level of a file or module declaration.": {
"category": "Error",
"code": 1258
},
"Module '{0}' can only be default-imported using the '{1}' flag": {
"category": "Error",
"code": 1259
+2 -2
View File
@@ -921,9 +921,9 @@ namespace ts.FindAllReferences {
// When renaming at an export specifier, rename the export and not the thing being exported.
getReferencesAtExportSpecifier(exportSpecifier.name, symbol, exportSpecifier, state.createSearch(node, originalSymbol, /*comingFrom*/ undefined), state, /*addReferencesHere*/ true, /*alwaysGetReferences*/ true);
}
else if (node && node.kind === SyntaxKind.DefaultKeyword && symbol.escapedName === InternalSymbolName.Default) {
else if (node && node.kind === SyntaxKind.DefaultKeyword && symbol.escapedName === InternalSymbolName.Default && symbol.parent) {
addReference(node, symbol, state);
searchForImportsOfExport(node, symbol, { exportingModuleSymbol: Debug.checkDefined(symbol.parent, "Expected export symbol to have a parent"), exportKind: ExportKind.Default }, state);
searchForImportsOfExport(node, symbol, { exportingModuleSymbol: symbol.parent, exportKind: ExportKind.Default }, state);
}
else {
const search = state.createSearch(node, symbol, /*comingFrom*/ undefined, { allSearchSymbols: node ? populateSearchSymbolSet(symbol, node, checker, options.use === FindReferencesUse.Rename, !!options.providePrefixAndSuffixTextForRename, !!options.implementations) : [symbol] });
+10 -10
View File
@@ -465,13 +465,13 @@ namespace ts.FindAllReferences {
function getExport(): ExportedSymbol | ImportedSymbol | undefined {
const { parent } = node;
const grandParent = parent.parent;
const grandparent = parent.parent;
if (symbol.exportSymbol) {
if (parent.kind === SyntaxKind.PropertyAccessExpression) {
// When accessing an export of a JS module, there's no alias. The symbol will still be flagged as an export even though we're at the use.
// So check that we are at the declaration.
return symbol.declarations?.some(d => d === parent) && isBinaryExpression(grandParent)
? getSpecialPropertyExport(grandParent, /*useLhsSymbol*/ false)
return symbol.declarations?.some(d => d === parent) && isBinaryExpression(grandparent)
? getSpecialPropertyExport(grandparent, /*useLhsSymbol*/ false)
: undefined;
}
else {
@@ -502,26 +502,26 @@ namespace ts.FindAllReferences {
return getExportAssignmentExport(parent);
}
// If we are in `export = class A {};` (or `export = class A {};`) at `A`, `parent.parent` is the export assignment.
else if (isExportAssignment(grandParent)) {
return getExportAssignmentExport(grandParent);
else if (isExportAssignment(grandparent)) {
return getExportAssignmentExport(grandparent);
}
// Similar for `module.exports =` and `exports.A =`.
else if (isBinaryExpression(parent)) {
return getSpecialPropertyExport(parent, /*useLhsSymbol*/ true);
}
else if (isBinaryExpression(grandParent)) {
return getSpecialPropertyExport(grandParent, /*useLhsSymbol*/ true);
else if (isBinaryExpression(grandparent)) {
return getSpecialPropertyExport(grandparent, /*useLhsSymbol*/ true);
}
else if (isJSDocTypedefTag(parent)) {
return exportInfo(symbol, ExportKind.Named);
}
}
function getExportAssignmentExport(ex: ExportAssignment): ExportedSymbol {
function getExportAssignmentExport(ex: ExportAssignment): ExportedSymbol | undefined {
// Get the symbol for the `export =` node; its parent is the module it's the export of.
const exportingModuleSymbol = Debug.checkDefined(ex.symbol.parent, "Expected export symbol to have a parent");
if (!ex.symbol.parent) return undefined;
const exportKind = ex.isExportEquals ? ExportKind.ExportEquals : ExportKind.Default;
return { kind: ImportExport.Export, symbol, exportInfo: { exportingModuleSymbol, exportKind } };
return { kind: ImportExport.Export, symbol, exportInfo: { exportingModuleSymbol: ex.symbol.parent, exportKind } };
}
function getSpecialPropertyExport(node: BinaryExpression, useLhsSymbol: boolean): ExportedSymbol | undefined {
+1 -1
View File
@@ -174,7 +174,7 @@ namespace ts.SymbolDisplay {
}
let signature: Signature | undefined;
type = isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol.exportSymbol || symbol, location);
type = isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location);
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) {
const right = (<PropertyAccessExpression>location.parent).name;