-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve errors for incorrectly nested export default #43967
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38301,7 +38301,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird because |
||
? 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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -465,13 +465,13 @@ namespace ts.FindAllReferences { | |
|
||
function getExport(): ExportedSymbol | ImportedSymbol | undefined { | ||
const { parent } = node; | ||
const grandParent = parent.parent; | ||
const grandparent = parent.parent; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. grandparent is one word, not "grand parent". That's just insulting. |
||
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,7 +174,7 @@ namespace ts.SymbolDisplay { | |
} | ||
|
||
let signature: Signature | undefined; | ||
type = isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol.exportSymbol || symbol, location); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first line of getTypeOfSymbolAtLocation does |
||
type = isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location); | ||
|
||
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) { | ||
const right = (<PropertyAccessExpression>location.parent).name; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in hindsight, creating an alias for an incorrect declaration is extremely likely to fail later.