@@ -4967,7 +4967,7 @@ namespace ts {
4967
4967
const oldcontext = context;
4968
4968
context = {
4969
4969
...oldcontext,
4970
- usedSymbolNames: mapMap(symbolTable, (_symbol, name) => [unescapeLeadingUnderscores(name), true] ),
4970
+ usedSymbolNames: createMap( ),
4971
4971
remappedSymbolNames: createMap(),
4972
4972
tracker: {
4973
4973
...oldcontext.tracker,
@@ -4991,6 +4991,10 @@ namespace ts {
4991
4991
context.usedSymbolNames!.set(name, true);
4992
4992
});
4993
4993
}
4994
+ forEachEntry(symbolTable, (symbol, name) => {
4995
+ const baseName = unescapeLeadingUnderscores(name);
4996
+ void getInternalSymbolName(symbol, baseName); // Called to cache values into `usedSymbolNames` and `remappedSymbolNames`
4997
+ });
4994
4998
let addingDeclare = !bundled;
4995
4999
const exportEquals = symbolTable.get(InternalSymbolName.ExportEquals);
4996
5000
if (exportEquals && symbolTable.size > 1 && exportEquals.flags & SymbolFlags.Alias) {
@@ -5203,7 +5207,11 @@ namespace ts {
5203
5207
isPrivate = true;
5204
5208
}
5205
5209
const modifierFlags = (!isPrivate ? ModifierFlags.Export : 0) | (isDefault && !needsPostExportDefault ? ModifierFlags.Default : 0);
5206
- if (symbol.flags & SymbolFlags.Function) {
5210
+ const isConstMergedWithNS = symbol.flags & SymbolFlags.Module &&
5211
+ symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.FunctionScopedVariable | SymbolFlags.Property) &&
5212
+ symbol.escapedName !== InternalSymbolName.ExportEquals;
5213
+ const isConstMergedWithNSPrintableAsSignatureMerge = isConstMergedWithNS && isTypeRepresentableAsFunctionNamespaceMerge(getTypeOfSymbol(symbol), symbol);
5214
+ if (symbol.flags & SymbolFlags.Function || isConstMergedWithNSPrintableAsSignatureMerge) {
5207
5215
serializeAsFunctionNamespaceMerge(getTypeOfSymbol(symbol), symbol, getInternalSymbolName(symbol, symbolName), modifierFlags);
5208
5216
}
5209
5217
if (symbol.flags & SymbolFlags.TypeAlias) {
@@ -5214,7 +5222,8 @@ namespace ts {
5214
5222
if (symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.FunctionScopedVariable | SymbolFlags.Property)
5215
5223
&& symbol.escapedName !== InternalSymbolName.ExportEquals
5216
5224
&& !(symbol.flags & SymbolFlags.Prototype)
5217
- && !(symbol.flags & SymbolFlags.Class)) {
5225
+ && !(symbol.flags & SymbolFlags.Class)
5226
+ && !isConstMergedWithNSPrintableAsSignatureMerge) {
5218
5227
serializeVariableOrProperty(symbol, symbolName, isPrivate, needsPostExportDefault, propertyAsAlias, modifierFlags);
5219
5228
}
5220
5229
if (symbol.flags & SymbolFlags.Enum) {
@@ -5231,7 +5240,7 @@ namespace ts {
5231
5240
serializeAsClass(symbol, getInternalSymbolName(symbol, symbolName), modifierFlags);
5232
5241
}
5233
5242
}
5234
- if (symbol.flags & (SymbolFlags.ValueModule | SymbolFlags.NamespaceModule)) {
5243
+ if (( symbol.flags & (SymbolFlags.ValueModule | SymbolFlags.NamespaceModule) && (!isConstMergedWithNS || isTypeOnlyNamespace(symbol))) || isConstMergedWithNSPrintableAsSignatureMerge ) {
5235
5244
serializeModule(symbol, symbolName, modifierFlags);
5236
5245
}
5237
5246
if (symbol.flags & SymbolFlags.Interface) {
@@ -5258,7 +5267,9 @@ namespace ts {
5258
5267
}
5259
5268
5260
5269
function includePrivateSymbol(symbol: Symbol) {
5270
+ if (some(symbol.declarations, isParameterDeclaration)) return;
5261
5271
Debug.assertDefined(deferredPrivates);
5272
+ getUnusedName(unescapeLeadingUnderscores(symbol.escapedName), symbol); // Call to cache unique name for symbol
5262
5273
deferredPrivates!.set("" + getSymbolId(symbol), symbol);
5263
5274
}
5264
5275
@@ -5332,29 +5343,40 @@ namespace ts {
5332
5343
), modifierFlags);
5333
5344
}
5334
5345
5346
+ function getNamespaceMembersForSerialization(symbol: Symbol) {
5347
+ return !symbol.exports ? [] : filter(arrayFrom((symbol.exports).values()), p => !((p.flags & SymbolFlags.Prototype) || (p.escapedName === "prototype")));
5348
+ }
5349
+
5350
+ function isTypeOnlyNamespace(symbol: Symbol) {
5351
+ return every(getNamespaceMembersForSerialization(symbol), m => !(resolveSymbol(m).flags & SymbolFlags.Value));
5352
+ }
5353
+
5335
5354
function serializeModule(symbol: Symbol, symbolName: string, modifierFlags: ModifierFlags) {
5336
- const members = !symbol.exports ? [] : filter(arrayFrom(( symbol.exports).values()), p => !((p.flags & SymbolFlags.Prototype) || (p.escapedName === "prototype")) );
5355
+ const members = getNamespaceMembersForSerialization( symbol);
5337
5356
// Split NS members up by declaration - members whose parent symbol is the ns symbol vs those whose is not (but were added in later via merging)
5338
5357
const locationMap = arrayToMultiMap(members, m => m.parent && m.parent === symbol ? "real" : "merged");
5339
5358
const realMembers = locationMap.get("real") || emptyArray;
5340
5359
const mergedMembers = locationMap.get("merged") || emptyArray;
5341
5360
// TODO: `suppressNewPrivateContext` is questionable -we need to simply be emitting privates in whatever scope they were declared in, rather
5342
- // than whatever scope we traverse to them in. That's a bit of a complex rewrite, since we're not _actually_ tracking privates at all in advance,
5361
+ // than whatever scope we traverse to them in. That's a bit ofgetUnusedName a complex rewrite, since we're not _actually_ tracking privates at all in advance,
5343
5362
// so we don't even have placeholders to fill in.
5344
5363
if (length(realMembers)) {
5345
5364
const localName = getInternalSymbolName(symbol, symbolName);
5346
- serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, !!(symbol.flags & SymbolFlags.Function));
5365
+ serializeAsNamespaceDeclaration(realMembers, localName, modifierFlags, !!(symbol.flags & ( SymbolFlags.Function | SymbolFlags.Assignment) ));
5347
5366
}
5348
5367
if (length(mergedMembers)) {
5349
5368
const localName = getInternalSymbolName(symbol, symbolName);
5350
- forEach(mergedMembers, includePrivateSymbol);
5351
5369
const nsBody = createModuleBlock([createExportDeclaration(
5352
5370
/*decorators*/ undefined,
5353
5371
/*modifiers*/ undefined,
5354
5372
createNamedExports(map(filter(mergedMembers, n => n.escapedName !== InternalSymbolName.ExportEquals), s => {
5355
5373
const name = unescapeLeadingUnderscores(s.escapedName);
5356
5374
const localName = getInternalSymbolName(s, name);
5357
- return createExportSpecifier(name === localName ? undefined : localName, name);
5375
+ const aliasDecl = s.declarations && getDeclarationOfAliasSymbol(s);
5376
+ const target = aliasDecl && getTargetOfAliasDeclaration(aliasDecl, /*dontRecursivelyResolve*/ true);
5377
+ includePrivateSymbol(target || s);
5378
+ const targetName = target ? getInternalSymbolName(target, unescapeLeadingUnderscores(target.escapedName)) : localName;
5379
+ return createExportSpecifier(name === targetName ? undefined : targetName, name);
5358
5380
}))
5359
5381
)]);
5360
5382
addResult(createModuleDeclaration(
@@ -5629,6 +5651,7 @@ namespace ts {
5629
5651
serializeMaybeAliasAssignment(symbol);
5630
5652
break;
5631
5653
case SyntaxKind.BinaryExpression:
5654
+ case SyntaxKind.PropertyAccessExpression:
5632
5655
// Could be best encoded as though an export specifier or as though an export assignment
5633
5656
// If name is default or export=, do an export assignment
5634
5657
// Otherwise do an export specifier
@@ -5639,10 +5662,6 @@ namespace ts {
5639
5662
serializeExportSpecifier(localName, targetName);
5640
5663
}
5641
5664
break;
5642
- case SyntaxKind.PropertyAccessExpression:
5643
- // A PAE alias is _always_ going to exist as an append to a top-level export, where our top level
5644
- // handling should always be sufficient to encode the export action itself
5645
- break;
5646
5665
default:
5647
5666
return Debug.failBadSyntaxKind(node, "Unhandled alias declaration kind in symbol serializer!");
5648
5667
}
@@ -5671,7 +5690,8 @@ namespace ts {
5671
5690
const aliasDecl = symbol.declarations && getDeclarationOfAliasSymbol(symbol);
5672
5691
// serialize what the alias points to, preserve the declaration's initializer
5673
5692
const target = aliasDecl && getTargetOfAliasDeclaration(aliasDecl, /*dontRecursivelyResolve*/ true);
5674
- if (target) {
5693
+ // If the target resolves and resolves to a thing defined in this file, emit as an alias, otherwise emit as a const
5694
+ if (target && length(target.declarations) && some(target.declarations, d => getSourceFileOfNode(d) === getSourceFileOfNode(enclosingDeclaration))) {
5675
5695
// In case `target` refers to a namespace member, look at the declaration and serialize the leftmost symbol in it
5676
5696
// eg, `namespace A { export class B {} }; exports = A.B;`
5677
5697
// Technically, this is all that's required in the case where the assignment is an entity name expression
@@ -6111,11 +6131,8 @@ namespace ts {
6111
6131
return context.remappedSymbolNames!.get("" + getSymbolId(symbol))!;
6112
6132
}
6113
6133
}
6114
- if (input === InternalSymbolName.Default) {
6115
- input = "_default";
6116
- }
6117
- else if (input === InternalSymbolName.ExportEquals) {
6118
- input = "_exports";
6134
+ if (symbol) {
6135
+ input = getNameCandidateWorker(symbol, input);
6119
6136
}
6120
6137
let i = 0;
6121
6138
const original = input;
@@ -6130,17 +6147,29 @@ namespace ts {
6130
6147
return input;
6131
6148
}
6132
6149
6133
- function getInternalSymbolName(symbol: Symbol, localName: string) {
6134
- if (context.remappedSymbolNames!.has("" + getSymbolId(symbol))) {
6135
- return context.remappedSymbolNames!.get("" + getSymbolId(symbol))!;
6136
- }
6150
+ function getNameCandidateWorker(symbol: Symbol, localName: string) {
6137
6151
if (localName === InternalSymbolName.Default || localName === InternalSymbolName.Class || localName === InternalSymbolName.Function) {
6138
6152
const flags = context.flags;
6139
6153
context.flags |= NodeBuilderFlags.InInitialEntityName;
6140
6154
const nameCandidate = getNameOfSymbolAsWritten(symbol, context);
6141
6155
context.flags = flags;
6142
- localName = isIdentifierText(nameCandidate, languageVersion) && !isStringANonContextualKeyword(nameCandidate) ? nameCandidate : getUnusedName(`_default`, symbol);
6156
+ localName = nameCandidate.length > 0 && isSingleOrDoubleQuote(nameCandidate.charCodeAt(0)) ? stripQuotes(nameCandidate) : nameCandidate;
6157
+ }
6158
+ if (localName === InternalSymbolName.Default) {
6159
+ localName = "_default";
6160
+ }
6161
+ else if (localName === InternalSymbolName.ExportEquals) {
6162
+ localName = "_exports";
6163
+ }
6164
+ localName = isIdentifierText(localName, languageVersion) && !isStringANonContextualKeyword(localName) ? localName : "_" + localName.replace(/[^a-zA-Z0-9]/g, "_");
6165
+ return localName;
6166
+ }
6167
+
6168
+ function getInternalSymbolName(symbol: Symbol, localName: string) {
6169
+ if (context.remappedSymbolNames!.has("" + getSymbolId(symbol))) {
6170
+ return context.remappedSymbolNames!.get("" + getSymbolId(symbol))!;
6143
6171
}
6172
+ localName = getNameCandidateWorker(symbol, localName);
6144
6173
// The result of this is going to be used as the symbol's name - lock it in, so `getUnusedName` will also pick it up
6145
6174
context.remappedSymbolNames!.set("" + getSymbolId(symbol), localName);
6146
6175
return localName;
0 commit comments