@@ -898,6 +898,7 @@ namespace ts {
898
898
// This allows users to just specify library files they want to used through --lib
899
899
// and they will not get an error from not having unrelated library files
900
900
let deferredGlobalESSymbolConstructorSymbol: Symbol | undefined;
901
+ let deferredGlobalESSymbolConstructorTypeSymbol: Symbol | undefined;
901
902
let deferredGlobalESSymbolType: ObjectType;
902
903
let deferredGlobalTypedPropertyDescriptorType: GenericType;
903
904
let deferredGlobalPromiseType: GenericType;
@@ -3677,11 +3678,30 @@ namespace ts {
3677
3678
const additionalContainers = mapDefined(container.declarations, fileSymbolIfFileSymbolExportEqualsContainer);
3678
3679
const reexportContainers = enclosingDeclaration && getAlternativeContainingModules(symbol, enclosingDeclaration);
3679
3680
const objectLiteralContainer = getVariableDeclarationOfObjectLiteral(container, meaning);
3680
- if (enclosingDeclaration && getAccessibleSymbolChain(container, enclosingDeclaration, SymbolFlags.Namespace, /*externalOnly*/ false)) {
3681
+ if (
3682
+ enclosingDeclaration &&
3683
+ container.flags & getQualifiedLeftMeaning(meaning) &&
3684
+ getAccessibleSymbolChain(container, enclosingDeclaration, SymbolFlags.Namespace, /*externalOnly*/ false)
3685
+ ) {
3681
3686
return append(concatenate(concatenate([container], additionalContainers), reexportContainers), objectLiteralContainer); // This order expresses a preference for the real container if it is in scope
3682
3687
}
3683
- const res = append(append(additionalContainers, container), objectLiteralContainer);
3684
- return concatenate(res, reexportContainers);
3688
+ // we potentially have a symbol which is a member of the instance side of something - look for a variable in scope with the container's type
3689
+ // which may be acting like a namespace (eg, `Symbol` acts like a namespace when looking up `Symbol.toStringTag`)
3690
+ const firstVariableMatch = !(container.flags & getQualifiedLeftMeaning(meaning))
3691
+ && container.flags & SymbolFlags.Type
3692
+ && getDeclaredTypeOfSymbol(container).flags & TypeFlags.Object
3693
+ && meaning === SymbolFlags.Value
3694
+ ? forEachSymbolTableInScope(enclosingDeclaration, t => {
3695
+ return forEachEntry(t, s => {
3696
+ if (s.flags & getQualifiedLeftMeaning(meaning) && getTypeOfSymbol(s) === getDeclaredTypeOfSymbol(container)) {
3697
+ return s;
3698
+ }
3699
+ });
3700
+ }) : undefined;
3701
+ let res = firstVariableMatch ? [firstVariableMatch, ...additionalContainers, container] : [...additionalContainers, container];
3702
+ res = append(res, objectLiteralContainer);
3703
+ res = addRange(res, reexportContainers);
3704
+ return res;
3685
3705
}
3686
3706
const candidates = mapDefined(symbol.declarations, d => {
3687
3707
if (!isAmbientModule(d) && d.parent && hasNonGlobalAugmentationExternalModuleSymbol(d.parent)) {
@@ -5107,8 +5127,9 @@ namespace ts {
5107
5127
}
5108
5128
}
5109
5129
}
5110
- context.enclosingDeclaration = saveEnclosingDeclaration;
5130
+ context.enclosingDeclaration = propertySymbol.valueDeclaration || propertySymbol.declarations?.[0] || saveEnclosingDeclaration;
5111
5131
const propertyName = getPropertyNameNodeForSymbol(propertySymbol, context);
5132
+ context.enclosingDeclaration = saveEnclosingDeclaration;
5112
5133
context.approximateLength += (symbolName(propertySymbol).length + 1);
5113
5134
const optionalToken = propertySymbol.flags & SymbolFlags.Optional ? factory.createToken(SyntaxKind.QuestionToken) : undefined;
5114
5135
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length && !isReadonlySymbol(propertySymbol)) {
@@ -5852,9 +5873,6 @@ namespace ts {
5852
5873
if (fromNameType) {
5853
5874
return fromNameType;
5854
5875
}
5855
- if (isKnownSymbol(symbol)) {
5856
- return factory.createComputedPropertyName(factory.createPropertyAccessExpression(factory.createIdentifier("Symbol"), (symbol.escapedName as string).substr(3)));
5857
- }
5858
5876
const rawName = unescapeLeadingUnderscores(symbol.escapedName);
5859
5877
const stringNamed = !!length(symbol.declarations) && every(symbol.declarations, isStringNamed);
5860
5878
return createPropertyNameNodeForIdentifierOrLiteral(rawName, stringNamed, singleQuote);
@@ -8707,8 +8725,18 @@ namespace ts {
8707
8725
return widenTypeForVariableLikeDeclaration(getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true), declaration, reportErrors);
8708
8726
}
8709
8727
8728
+ function isGlobalSymbolConstructor(node: Node) {
8729
+ const symbol = getSymbolOfNode(node);
8730
+ const globalSymbol = getGlobalESSymbolConstructorTypeSymbol(/*reportErrors*/ false);
8731
+ return globalSymbol && symbol && symbol === globalSymbol;
8732
+ }
8733
+
8710
8734
function widenTypeForVariableLikeDeclaration(type: Type | undefined, declaration: any, reportErrors?: boolean) {
8711
8735
if (type) {
8736
+ // TODO: If back compat with pre-3.0/4.0 libs isn't required, remove the following SymbolConstructor special case transforming `symbol` into `unique symbol`
8737
+ if (type.flags & TypeFlags.ESSymbol && isGlobalSymbolConstructor(declaration.parent)) {
8738
+ type = getESSymbolLikeTypeForNode(declaration);
8739
+ }
8712
8740
if (reportErrors) {
8713
8741
reportErrorsFromWidening(declaration, type);
8714
8742
}
@@ -12859,6 +12887,10 @@ namespace ts {
12859
12887
return deferredGlobalESSymbolConstructorSymbol || (deferredGlobalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol" as __String, reportErrors));
12860
12888
}
12861
12889
12890
+ function getGlobalESSymbolConstructorTypeSymbol(reportErrors: boolean) {
12891
+ return deferredGlobalESSymbolConstructorTypeSymbol || (deferredGlobalESSymbolConstructorTypeSymbol = getGlobalTypeSymbol("SymbolConstructor" as __String, reportErrors));
12892
+ }
12893
+
12862
12894
function getGlobalESSymbolType(reportErrors: boolean) {
12863
12895
return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
12864
12896
}
@@ -13941,13 +13973,13 @@ namespace ts {
13941
13973
function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) {
13942
13974
if (!(getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier)) {
13943
13975
let type = getSymbolLinks(getLateBoundSymbol(prop)).nameType;
13944
- if (!type && !isKnownSymbol(prop) ) {
13976
+ if (!type) {
13945
13977
if (prop.escapedName === InternalSymbolName.Default) {
13946
13978
type = getLiteralType("default");
13947
13979
}
13948
13980
else {
13949
13981
const name = prop.valueDeclaration && getNameOfDeclaration(prop.valueDeclaration) as PropertyName;
13950
- type = name && getLiteralTypeFromPropertyName(name) || getLiteralType(symbolName(prop));
13982
+ type = name && getLiteralTypeFromPropertyName(name) || (!isKnownSymbol(prop) ? getLiteralType(symbolName(prop)) : undefined );
13951
13983
}
13952
13984
}
13953
13985
if (type && type.flags & include) {
@@ -14174,11 +14206,8 @@ namespace ts {
14174
14206
}
14175
14207
14176
14208
function getPropertyNameFromIndex(indexType: Type, accessNode: StringLiteral | Identifier | PrivateIdentifier | ObjectBindingPattern | ArrayBindingPattern | ComputedPropertyName | NumericLiteral | IndexedAccessTypeNode | ElementAccessExpression | SyntheticExpression | undefined) {
14177
- const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
14178
14209
return isTypeUsableAsPropertyName(indexType) ?
14179
14210
getPropertyNameFromType(indexType) :
14180
- accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
14181
- getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name)) :
14182
14211
accessNode && isPropertyName(accessNode) ?
14183
14212
// late bound names are handled in the first branch, so here we only need to handle normal names
14184
14213
getPropertyNameForPropertyNameNode(accessNode) :
@@ -25279,9 +25308,6 @@ namespace ts {
25279
25308
!isTypeAssignableTo(links.resolvedType, stringNumberSymbolType)) {
25280
25309
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
25281
25310
}
25282
- else {
25283
- checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true);
25284
- }
25285
25311
}
25286
25312
25287
25313
return links.resolvedType;
@@ -25342,15 +25368,15 @@ namespace ts {
25342
25368
// As otherwise they may not be checked until exports for the type at this position are retrieved,
25343
25369
// which may never occur.
25344
25370
for (const elem of node.properties) {
25345
- if (elem.name && isComputedPropertyName(elem.name) && !isWellKnownSymbolSyntactically(elem.name) ) {
25371
+ if (elem.name && isComputedPropertyName(elem.name)) {
25346
25372
checkComputedPropertyName(elem.name);
25347
25373
}
25348
25374
}
25349
25375
25350
25376
let offset = 0;
25351
25377
for (const memberDecl of node.properties) {
25352
25378
let member = getSymbolOfNode(memberDecl);
25353
- const computedNameType = memberDecl.name && memberDecl.name.kind === SyntaxKind.ComputedPropertyName && !isWellKnownSymbolSyntactically(memberDecl.name.expression) ?
25379
+ const computedNameType = memberDecl.name && memberDecl.name.kind === SyntaxKind.ComputedPropertyName ?
25354
25380
checkComputedPropertyName(memberDecl.name) : undefined;
25355
25381
if (memberDecl.kind === SyntaxKind.PropertyAssignment ||
25356
25382
memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment ||
@@ -27006,48 +27032,6 @@ namespace ts {
27006
27032
return checkIndexedAccessIndexType(getFlowTypeOfAccessExpression(node, indexedAccessType.symbol, indexedAccessType, indexExpression), node);
27007
27033
}
27008
27034
27009
- function checkThatExpressionIsProperSymbolReference(expression: Expression, expressionType: Type, reportError: boolean): boolean {
27010
- if (expressionType === errorType) {
27011
- // There is already an error, so no need to report one.
27012
- return false;
27013
- }
27014
-
27015
- if (!isWellKnownSymbolSyntactically(expression)) {
27016
- return false;
27017
- }
27018
-
27019
- // Make sure the property type is the primitive symbol type
27020
- if ((expressionType.flags & TypeFlags.ESSymbolLike) === 0) {
27021
- if (reportError) {
27022
- error(expression, Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, getTextOfNode(expression));
27023
- }
27024
- return false;
27025
- }
27026
-
27027
- // The name is Symbol.<someName>, so make sure Symbol actually resolves to the
27028
- // global Symbol object
27029
- const leftHandSide = <Identifier>(<PropertyAccessExpression>expression).expression;
27030
- const leftHandSideSymbol = getResolvedSymbol(leftHandSide);
27031
- if (!leftHandSideSymbol) {
27032
- return false;
27033
- }
27034
-
27035
- const globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ true);
27036
- if (!globalESSymbol) {
27037
- // Already errored when we tried to look up the symbol
27038
- return false;
27039
- }
27040
-
27041
- if (leftHandSideSymbol !== globalESSymbol) {
27042
- if (reportError) {
27043
- error(leftHandSide, Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object);
27044
- }
27045
- return false;
27046
- }
27047
-
27048
- return true;
27049
- }
27050
-
27051
27035
function callLikeExpressionMayHaveTypeArguments(node: CallLikeExpression): node is CallExpression | NewExpression | TaggedTemplateExpression | JsxOpeningElement {
27052
27036
return isCallOrNewExpression(node) || isTaggedTemplateExpression(node) || isJsxOpeningLikeElement(node);
27053
27037
}
@@ -35355,6 +35339,12 @@ namespace ts {
35355
35339
}
35356
35340
}
35357
35341
35342
+ function getPropertyNameForKnownSymbolName(symbolName: string): __String {
35343
+ const ctorType = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ false);
35344
+ const uniqueType = ctorType && getTypeOfPropertyOfType(getTypeOfSymbol(ctorType), escapeLeadingUnderscores(symbolName));
35345
+ return uniqueType && isTypeUsableAsPropertyName(uniqueType) ? getPropertyNameFromType(uniqueType) : `__@${symbolName}` as __String;
35346
+ }
35347
+
35358
35348
/**
35359
35349
* Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
35360
35350
* type from its members.
0 commit comments