@@ -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,25 @@ 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 (enclosingDeclaration && container.flags & getQualifiedLeftMeaning(meaning) && getAccessibleSymbolChain(container, enclosingDeclaration, SymbolFlags.Namespace, /*externalOnly*/ false)) {
3681
3682
return append(concatenate(concatenate([container], additionalContainers), reexportContainers), objectLiteralContainer); // This order expresses a preference for the real container if it is in scope
3682
3683
}
3684
+ // we potentially a symbol which is a member of the instance side of something - look for a variable in scope with the container's type
3685
+ // which may be acting like a namespace
3686
+ const firstVariableMatch = !(container.flags & getQualifiedLeftMeaning(meaning))
3687
+ && container.flags & SymbolFlags.Type
3688
+ && getDeclaredTypeOfSymbol(container).flags & TypeFlags.Object
3689
+ && meaning === SymbolFlags.Value
3690
+ ? forEachSymbolTableInScope(enclosingDeclaration, t => {
3691
+ return forEachEntry(t, s => {
3692
+ if (s.flags & getQualifiedLeftMeaning(meaning) && getTypeOfSymbol(s) === getDeclaredTypeOfSymbol(container)) {
3693
+ return s;
3694
+ }
3695
+ });
3696
+ }) : undefined;
3683
3697
const res = append(append(additionalContainers, container), objectLiteralContainer);
3684
- return concatenate(res, reexportContainers);
3698
+ const resWithReexports = concatenate(res, reexportContainers);
3699
+ return firstVariableMatch ? [firstVariableMatch, ...resWithReexports] : resWithReexports;
3685
3700
}
3686
3701
const candidates = mapDefined(symbol.declarations, d => {
3687
3702
if (!isAmbientModule(d) && d.parent && hasNonGlobalAugmentationExternalModuleSymbol(d.parent)) {
@@ -5104,8 +5119,9 @@ namespace ts {
5104
5119
}
5105
5120
}
5106
5121
}
5107
- context.enclosingDeclaration = saveEnclosingDeclaration;
5122
+ context.enclosingDeclaration = propertySymbol.valueDeclaration || propertySymbol.declarations?.[0] || saveEnclosingDeclaration;
5108
5123
const propertyName = getPropertyNameNodeForSymbol(propertySymbol, context);
5124
+ context.enclosingDeclaration = saveEnclosingDeclaration;
5109
5125
context.approximateLength += (symbolName(propertySymbol).length + 1);
5110
5126
const optionalToken = propertySymbol.flags & SymbolFlags.Optional ? factory.createToken(SyntaxKind.QuestionToken) : undefined;
5111
5127
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length && !isReadonlySymbol(propertySymbol)) {
@@ -5849,9 +5865,6 @@ namespace ts {
5849
5865
if (fromNameType) {
5850
5866
return fromNameType;
5851
5867
}
5852
- if (isKnownSymbol(symbol)) {
5853
- return factory.createComputedPropertyName(factory.createPropertyAccessExpression(factory.createIdentifier("Symbol"), (symbol.escapedName as string).substr(3)));
5854
- }
5855
5868
const rawName = unescapeLeadingUnderscores(symbol.escapedName);
5856
5869
const stringNamed = !!length(symbol.declarations) && every(symbol.declarations, isStringNamed);
5857
5870
return createPropertyNameNodeForIdentifierOrLiteral(rawName, stringNamed, singleQuote);
@@ -8704,8 +8717,18 @@ namespace ts {
8704
8717
return widenTypeForVariableLikeDeclaration(getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true), declaration, reportErrors);
8705
8718
}
8706
8719
8720
+ function isGlobalSymbolConstructor(node: Node) {
8721
+ const symbol = getSymbolOfNode(node);
8722
+ const globalSymbol = getGlobalESSymbolConstructorTypeSymbol(/*reportErrors*/ false);
8723
+ return globalSymbol && symbol && symbol === globalSymbol;
8724
+ }
8725
+
8707
8726
function widenTypeForVariableLikeDeclaration(type: Type | undefined, declaration: any, reportErrors?: boolean) {
8708
8727
if (type) {
8728
+ // TODO: Remove the following SymbolConstructor special case when back compat with pre-3.0 libs isn't required
8729
+ if (type.flags & TypeFlags.ESSymbol && isGlobalSymbolConstructor(declaration.parent)) {
8730
+ type = getESSymbolLikeTypeForNode(declaration);
8731
+ }
8709
8732
if (reportErrors) {
8710
8733
reportErrorsFromWidening(declaration, type);
8711
8734
}
@@ -12835,6 +12858,10 @@ namespace ts {
12835
12858
return deferredGlobalESSymbolConstructorSymbol || (deferredGlobalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol" as __String, reportErrors));
12836
12859
}
12837
12860
12861
+ function getGlobalESSymbolConstructorTypeSymbol(reportErrors: boolean) {
12862
+ return deferredGlobalESSymbolConstructorTypeSymbol || (deferredGlobalESSymbolConstructorTypeSymbol = getGlobalTypeSymbol("SymbolConstructor" as __String, reportErrors));
12863
+ }
12864
+
12838
12865
function getGlobalESSymbolType(reportErrors: boolean) {
12839
12866
return deferredGlobalESSymbolType || (deferredGlobalESSymbolType = getGlobalType("Symbol" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
12840
12867
}
@@ -13919,13 +13946,13 @@ namespace ts {
13919
13946
function getLiteralTypeFromProperty(prop: Symbol, include: TypeFlags) {
13920
13947
if (!(getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier)) {
13921
13948
let type = getSymbolLinks(getLateBoundSymbol(prop)).nameType;
13922
- if (!type && !isKnownSymbol(prop) ) {
13949
+ if (!type) {
13923
13950
if (prop.escapedName === InternalSymbolName.Default) {
13924
13951
type = getLiteralType("default");
13925
13952
}
13926
13953
else {
13927
13954
const name = prop.valueDeclaration && getNameOfDeclaration(prop.valueDeclaration) as PropertyName;
13928
- type = name && getLiteralTypeFromPropertyName(name) || getLiteralType(symbolName(prop));
13955
+ type = name && getLiteralTypeFromPropertyName(name) || (!isKnownSymbol(prop) ? getLiteralType(symbolName(prop)) : undefined );
13929
13956
}
13930
13957
}
13931
13958
if (type && type.flags & include) {
@@ -14153,11 +14180,8 @@ namespace ts {
14153
14180
}
14154
14181
14155
14182
function getPropertyNameFromIndex(indexType: Type, accessNode: StringLiteral | Identifier | PrivateIdentifier | ObjectBindingPattern | ArrayBindingPattern | ComputedPropertyName | NumericLiteral | IndexedAccessTypeNode | ElementAccessExpression | SyntheticExpression | undefined) {
14156
- const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
14157
14183
return isTypeUsableAsPropertyName(indexType) ?
14158
14184
getPropertyNameFromType(indexType) :
14159
- accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ?
14160
- getPropertyNameForKnownSymbolName(idText((<PropertyAccessExpression>accessExpression.argumentExpression).name)) :
14161
14185
accessNode && isPropertyName(accessNode) ?
14162
14186
// late bound names are handled in the first branch, so here we only need to handle normal names
14163
14187
getPropertyNameForPropertyNameNode(accessNode) :
@@ -25151,9 +25175,6 @@ namespace ts {
25151
25175
!isTypeAssignableTo(links.resolvedType, stringNumberSymbolType)) {
25152
25176
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any);
25153
25177
}
25154
- else {
25155
- checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true);
25156
- }
25157
25178
}
25158
25179
25159
25180
return links.resolvedType;
@@ -25214,15 +25235,15 @@ namespace ts {
25214
25235
// As otherwise they may not be checked until exports for the type at this position are retrieved,
25215
25236
// which may never occur.
25216
25237
for (const elem of node.properties) {
25217
- if (elem.name && isComputedPropertyName(elem.name) && !isWellKnownSymbolSyntactically(elem.name) ) {
25238
+ if (elem.name && isComputedPropertyName(elem.name)) {
25218
25239
checkComputedPropertyName(elem.name);
25219
25240
}
25220
25241
}
25221
25242
25222
25243
let offset = 0;
25223
25244
for (const memberDecl of node.properties) {
25224
25245
let member = getSymbolOfNode(memberDecl);
25225
- const computedNameType = memberDecl.name && memberDecl.name.kind === SyntaxKind.ComputedPropertyName && !isWellKnownSymbolSyntactically(memberDecl.name.expression) ?
25246
+ const computedNameType = memberDecl.name && memberDecl.name.kind === SyntaxKind.ComputedPropertyName ?
25226
25247
checkComputedPropertyName(memberDecl.name) : undefined;
25227
25248
if (memberDecl.kind === SyntaxKind.PropertyAssignment ||
25228
25249
memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment ||
@@ -25321,7 +25342,10 @@ namespace ts {
25321
25342
}
25322
25343
25323
25344
if (computedNameType && !(computedNameType.flags & TypeFlags.StringOrNumberLiteralOrUnique)) {
25324
- if (isTypeAssignableTo(computedNameType, stringNumberSymbolType)) {
25345
+ if (isTypeAny(computedNameType)) {
25346
+ hasComputedStringProperty = true; // string is the closest to a catch-all index signature we have
25347
+ }
25348
+ else if (isTypeAssignableTo(computedNameType, stringNumberSymbolType)) {
25325
25349
if (isTypeAssignableTo(computedNameType, numberType)) {
25326
25350
hasComputedNumberProperty = true;
25327
25351
}
@@ -26879,48 +26903,6 @@ namespace ts {
26879
26903
return checkIndexedAccessIndexType(getFlowTypeOfAccessExpression(node, indexedAccessType.symbol, indexedAccessType, indexExpression), node);
26880
26904
}
26881
26905
26882
- function checkThatExpressionIsProperSymbolReference(expression: Expression, expressionType: Type, reportError: boolean): boolean {
26883
- if (expressionType === errorType) {
26884
- // There is already an error, so no need to report one.
26885
- return false;
26886
- }
26887
-
26888
- if (!isWellKnownSymbolSyntactically(expression)) {
26889
- return false;
26890
- }
26891
-
26892
- // Make sure the property type is the primitive symbol type
26893
- if ((expressionType.flags & TypeFlags.ESSymbolLike) === 0) {
26894
- if (reportError) {
26895
- error(expression, Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, getTextOfNode(expression));
26896
- }
26897
- return false;
26898
- }
26899
-
26900
- // The name is Symbol.<someName>, so make sure Symbol actually resolves to the
26901
- // global Symbol object
26902
- const leftHandSide = <Identifier>(<PropertyAccessExpression>expression).expression;
26903
- const leftHandSideSymbol = getResolvedSymbol(leftHandSide);
26904
- if (!leftHandSideSymbol) {
26905
- return false;
26906
- }
26907
-
26908
- const globalESSymbol = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ true);
26909
- if (!globalESSymbol) {
26910
- // Already errored when we tried to look up the symbol
26911
- return false;
26912
- }
26913
-
26914
- if (leftHandSideSymbol !== globalESSymbol) {
26915
- if (reportError) {
26916
- error(leftHandSide, Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object);
26917
- }
26918
- return false;
26919
- }
26920
-
26921
- return true;
26922
- }
26923
-
26924
26906
function callLikeExpressionMayHaveTypeArguments(node: CallLikeExpression): node is CallExpression | NewExpression | TaggedTemplateExpression | JsxOpeningElement {
26925
26907
return isCallOrNewExpression(node) || isTaggedTemplateExpression(node) || isJsxOpeningLikeElement(node);
26926
26908
}
@@ -35198,6 +35180,12 @@ namespace ts {
35198
35180
}
35199
35181
}
35200
35182
35183
+ function getPropertyNameForKnownSymbolName(symbolName: string): __String {
35184
+ const ctorType = getGlobalESSymbolConstructorSymbol(/*reportErrors*/ false);
35185
+ const uniqueType = ctorType && getTypeOfPropertyOfType(getTypeOfSymbol(ctorType), escapeLeadingUnderscores(symbolName));
35186
+ return uniqueType && isTypeUsableAsPropertyName(uniqueType) ? getPropertyNameFromType(uniqueType) : `__@${symbolName}` as __String;
35187
+ }
35188
+
35201
35189
/**
35202
35190
* Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
35203
35191
* type from its members.
0 commit comments