Skip to content

Commit 0fec674

Browse files
committed
Port symbol sorting changes
1 parent 0aac720 commit 0fec674

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/compiler/checker.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ import {
11331133
WideningContext,
11341134
WithStatement,
11351135
YieldExpression,
1136+
compareComparableValues,
11361137
} from "./_namespaces/ts.js";
11371138
import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers.js";
11381139
import * as performance from "./_namespaces/ts.performance.js";
@@ -1491,6 +1492,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
14911492
var cancellationToken: CancellationToken | undefined;
14921493

14931494
var scanner: Scanner | undefined;
1495+
1496+
var fileIndexMap = new Map(host.getSourceFiles().map((file, i) => [file, i]));
14941497

14951498
var Symbol = objectAllocator.getSymbolConstructor();
14961499
var Type = objectAllocator.getTypeConstructor();
@@ -5467,6 +5470,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
54675470
(result || (result = [])).push(symbol);
54685471
}
54695472
});
5473+
result?.sort(compareSymbols);
54705474
return result || emptyArray;
54715475
}
54725476

@@ -34521,18 +34525,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3452134525
// So the table *contains* `x` but `x` isn't actually in scope.
3452234526
// However, resolveNameHelper will continue and call this callback again, so we'll eventually get a correct suggestion.
3452334527
if (symbol) return symbol;
34524-
let candidates: Symbol[];
34528+
let candidates = arrayFrom(symbols.values()).sort(compareSymbols);
3452534529
if (symbols === globals) {
3452634530
const primitives = mapDefined(
3452734531
["string", "number", "boolean", "object", "bigint", "symbol"],
3452834532
s => symbols.has((s.charAt(0).toUpperCase() + s.slice(1)) as __String)
3452934533
? createSymbol(SymbolFlags.TypeAlias, s as __String) as Symbol
3453034534
: undefined,
3453134535
);
34532-
candidates = primitives.concat(arrayFrom(symbols.values()));
34533-
}
34534-
else {
34535-
candidates = arrayFrom(symbols.values());
34536+
candidates = concatenate(candidates, primitives);
3453634537
}
3453734538
return getSpellingSuggestionForName(unescapeLeadingUnderscores(name), candidates, meaning);
3453834539
}
@@ -34543,7 +34544,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3454334544
}
3454434545

3454534546
function getSuggestedSymbolForNonexistentModule(name: Identifier, targetModule: Symbol): Symbol | undefined {
34546-
return targetModule.exports && getSpellingSuggestionForName(idText(name), getExportsOfModuleAsArray(targetModule), SymbolFlags.ModuleMember);
34547+
return targetModule.exports && getSpellingSuggestionForName(idText(name), getExportsOfModuleAsArray(targetModule).sort(compareSymbols), SymbolFlags.ModuleMember);
3454734548
}
3454834549

3454934550
function getSuggestionForNonexistentIndexSignature(objectType: Type, expr: ElementAccessExpression, keyedType: Type): string | undefined {
@@ -52814,6 +52815,40 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5281452815
Debug.assert(specifier && nodeIsSynthesized(specifier) && specifier.text === "tslib", `Expected sourceFile.imports[0] to be the synthesized tslib import`);
5281552816
return specifier;
5281652817
}
52818+
52819+
function compareSymbols(s1: Symbol | undefined, s2: Symbol | undefined): number {
52820+
if (s1 === s2) return 0;
52821+
if (s1 === undefined) return 1;
52822+
if (s2 === undefined) return -1;
52823+
if (length(s1.declarations) !== 0 && length(s2.declarations) !== 0) {
52824+
const r = compareNodes(s1.declarations![0], s2.declarations![0]);
52825+
if (r !== 0) return r;
52826+
}
52827+
else if (length(s1.declarations) !== 0) {
52828+
return -1;
52829+
}
52830+
else if (length(s2.declarations) !== 0) {
52831+
return 1;
52832+
}
52833+
const r = compareComparableValues(s1.escapedName as string, s2.escapedName as string);
52834+
if (r !== 0) return r;
52835+
return getSymbolId(s1) - getSymbolId(s2);
52836+
}
52837+
52838+
function compareNodes(n1: Node | undefined, n2: Node | undefined): number {
52839+
if (n1 === n2) return 0;
52840+
if (n1 === undefined) return 1;
52841+
if (n2 === undefined) return -1;
52842+
const f1 = fileIndexMap.get(getSourceFileOfNode(n1))!;
52843+
const f2 = fileIndexMap.get(getSourceFileOfNode(n2))!;
52844+
if (f1 !== f2) {
52845+
// Order by index of file in the containing program
52846+
return f1 - f2;
52847+
}
52848+
// In the same file, order by source position
52849+
return n1.pos - n2.pos;
52850+
}
52851+
5281752852
}
5281852853

5281952854
function isNotAccessor(declaration: Declaration): boolean {

src/compiler/core.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,9 +1967,11 @@ export function equateStringsCaseSensitive(a: string, b: string): boolean {
19671967
return equateValues(a, b);
19681968
}
19691969

1970-
function compareComparableValues(a: string | undefined, b: string | undefined): Comparison;
1971-
function compareComparableValues(a: number | undefined, b: number | undefined): Comparison;
1972-
function compareComparableValues(a: string | number | undefined, b: string | number | undefined) {
1970+
/** @internal */
1971+
export function compareComparableValues(a: string | undefined, b: string | undefined): Comparison;
1972+
/** @internal */
1973+
export function compareComparableValues(a: number | undefined, b: number | undefined): Comparison;
1974+
export function compareComparableValues(a: string | number | undefined, b: string | number | undefined) {
19731975
return a === b ? Comparison.EqualTo :
19741976
a === undefined ? Comparison.LessThan :
19751977
b === undefined ? Comparison.GreaterThan :

0 commit comments

Comments
 (0)