-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Avoid the double-symbol trick for enums #39955
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -541,7 +541,7 @@ namespace ts { | |
} | ||
|
||
function declareModuleMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags): Symbol { | ||
const hasExportModifier = getCombinedModifierFlags(node) & ModifierFlags.Export; | ||
const hasExportModifier = !!(getCombinedModifierFlags(node) & ModifierFlags.Export) || jsdocTreatAsExported(node); | ||
if (symbolFlags & SymbolFlags.Alias) { | ||
if (node.kind === SyntaxKind.ExportSpecifier || (node.kind === SyntaxKind.ImportEqualsDeclaration && hasExportModifier)) { | ||
return declareSymbol(container.symbol.exports!, container.symbol, node, symbolFlags, symbolExcludes); | ||
|
@@ -567,7 +567,7 @@ namespace ts { | |
// and this case is specially handled. Module augmentations should only be merged with original module definition | ||
// and should never be merged directly with other augmentation, and the latter case would be possible if automatic merge is allowed. | ||
if (isJSDocTypeAlias(node)) Debug.assert(isInJSFile(node)); // We shouldn't add symbols for JSDoc nodes if not in a JS file. | ||
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || isJSDocTypeAlias(node)) { | ||
if (!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) { | ||
if (!container.locals || (hasSyntacticModifier(node, ModifierFlags.Default) && !getDeclarationName(node))) { | ||
return declareSymbol(container.symbol.exports!, container.symbol, node, symbolFlags, symbolExcludes); // No local symbol for an unnamed default! | ||
} | ||
|
@@ -583,6 +583,21 @@ namespace ts { | |
} | ||
} | ||
|
||
function jsdocTreatAsExported(node: Node) { | ||
if (!isJSDocTypeAlias(node)) return false; | ||
// jsdoc typedef handling is a bit of a doozy, but to summarize, treat the typedef as exported if: | ||
// 1. It has an explicit name (since by default typedefs are always directly exported, either at the top level or in a container), or | ||
if (!isJSDocEnumTag(node) && !!node.fullName) return true; | ||
// 2. The thing a nameless typedef pulls its name from is implicitly a direct export (either by assignment or actual export flag). | ||
const declName = getNameOfDeclaration(node); | ||
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. is the code below here only for nameless typedefs, or can |
||
if (!declName) return false; | ||
if (isPropertyAccessEntityNameExpression(declName.parent) && isTopLevelNamespaceAssignment(declName.parent)) return true; | ||
if (isDeclaration(declName.parent) && getCombinedModifierFlags(declName.parent) & ModifierFlags.Export) return true; | ||
// This could potentially be simplified by having `delayedBindJSDocTypedefTag` pass in an override for `hasExportModifier`, since it should | ||
// already have calculated and branched on most of this. | ||
return false; | ||
} | ||
|
||
// All container nodes are kept on a linked list in declaration order. This list is used by | ||
// the getLocalNameOfContainer function in the type checker to validate that the local name | ||
// used for a container is unique. | ||
|
26 changes: 26 additions & 0 deletions
26
tests/baselines/reference/exportedEnumTypeAndValue.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
=== tests/cases/conformance/jsdoc/def.js === | ||
/** @enum {number} */ | ||
const MyEnum = { | ||
>MyEnum : Symbol(MyEnum, Decl(def.js, 1, 5), Decl(def.js, 0, 4)) | ||
|
||
a: 1, | ||
>a : Symbol(a, Decl(def.js, 1, 16)) | ||
|
||
b: 2 | ||
>b : Symbol(b, Decl(def.js, 2, 7)) | ||
|
||
}; | ||
export default MyEnum; | ||
>MyEnum : Symbol(MyEnum, Decl(def.js, 1, 5), Decl(def.js, 0, 4)) | ||
|
||
=== tests/cases/conformance/jsdoc/use.js === | ||
import MyEnum from "./def"; | ||
>MyEnum : Symbol(MyEnum, Decl(use.js, 0, 6)) | ||
|
||
/** @type {MyEnum} */ | ||
const v = MyEnum.b; | ||
>v : Symbol(v, Decl(use.js, 3, 5)) | ||
>MyEnum.b : Symbol(b, Decl(def.js, 2, 7)) | ||
>MyEnum : Symbol(MyEnum, Decl(use.js, 0, 6)) | ||
>b : Symbol(b, Decl(def.js, 2, 7)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
=== tests/cases/conformance/jsdoc/def.js === | ||
/** @enum {number} */ | ||
const MyEnum = { | ||
>MyEnum : { a: number; b: number; } | ||
>{ a: 1, b: 2} : { a: number; b: number; } | ||
|
||
a: 1, | ||
>a : number | ||
>1 : 1 | ||
|
||
b: 2 | ||
>b : number | ||
>2 : 2 | ||
|
||
}; | ||
export default MyEnum; | ||
>MyEnum : number | ||
|
||
=== tests/cases/conformance/jsdoc/use.js === | ||
import MyEnum from "./def"; | ||
>MyEnum : { a: number; b: number; } | ||
|
||
/** @type {MyEnum} */ | ||
const v = MyEnum.b; | ||
>v : number | ||
>MyEnum.b : number | ||
>MyEnum : { a: number; b: number; } | ||
>b : number | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @noEmit: true | ||
// @allowJs: true | ||
// @checkJs: true | ||
|
||
// @Filename: def.js | ||
/** @enum {number} */ | ||
const MyEnum = { | ||
a: 1, | ||
b: 2 | ||
}; | ||
export default MyEnum; | ||
|
||
// @Filename: use.js | ||
import MyEnum from "./def"; | ||
|
||
/** @type {MyEnum} */ | ||
const v = MyEnum.b; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.