Skip to content

Commit fca0377

Browse files
committed
Port symbol sorting changes
1 parent ee3dd72 commit fca0377

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/compiler/checker.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import {
8181
classOrConstructorParameterIsDecorated,
8282
ClassStaticBlockDeclaration,
8383
clear,
84+
compareComparableValues,
8485
compareDiagnostics,
8586
comparePaths,
8687
compareValues,
@@ -1492,6 +1493,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
14921493

14931494
var scanner: Scanner | undefined;
14941495

1496+
var fileIndexMap = new Map(host.getSourceFiles().map((file, i) => [file, i]));
1497+
14951498
var Symbol = objectAllocator.getSymbolConstructor();
14961499
var Type = objectAllocator.getTypeConstructor();
14971500
var Signature = objectAllocator.getSignatureConstructor();
@@ -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

@@ -34524,18 +34528,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3452434528
// So the table *contains* `x` but `x` isn't actually in scope.
3452534529
// However, resolveNameHelper will continue and call this callback again, so we'll eventually get a correct suggestion.
3452634530
if (symbol) return symbol;
34527-
let candidates: Symbol[];
34531+
let candidates = arrayFrom(symbols.values()).sort(compareSymbols);
3452834532
if (symbols === globals) {
3452934533
const primitives = mapDefined(
3453034534
["string", "number", "boolean", "object", "bigint", "symbol"],
3453134535
s => symbols.has((s.charAt(0).toUpperCase() + s.slice(1)) as __String)
3453234536
? createSymbol(SymbolFlags.TypeAlias, s as __String) as Symbol
3453334537
: undefined,
3453434538
);
34535-
candidates = primitives.concat(arrayFrom(symbols.values()));
34536-
}
34537-
else {
34538-
candidates = arrayFrom(symbols.values());
34539+
candidates = concatenate(candidates, primitives);
3453934540
}
3454034541
return getSpellingSuggestionForName(unescapeLeadingUnderscores(name), candidates, meaning);
3454134542
}
@@ -34546,7 +34547,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3454634547
}
3454734548

3454834549
function getSuggestedSymbolForNonexistentModule(name: Identifier, targetModule: Symbol): Symbol | undefined {
34549-
return targetModule.exports && getSpellingSuggestionForName(idText(name), getExportsOfModuleAsArray(targetModule), SymbolFlags.ModuleMember);
34550+
return targetModule.exports && getSpellingSuggestionForName(idText(name), getExportsOfModuleAsArray(targetModule).sort(compareSymbols), SymbolFlags.ModuleMember); // eslint-disable-line local/no-array-mutating-method-expressions
3455034551
}
3455134552

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

5282252856
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)