Skip to content

Commit a8f29c7

Browse files
committed
Add flags to Signature
1 parent cfda624 commit a8f29c7

File tree

8 files changed

+101
-76
lines changed

8 files changed

+101
-76
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ namespace ts {
948948
}
949949
if (expression.kind === SyntaxKind.TrueKeyword && flags & FlowFlags.FalseCondition ||
950950
expression.kind === SyntaxKind.FalseKeyword && flags & FlowFlags.TrueCondition) {
951-
if (!isOptionalExpression(expression)) {
951+
if (!isExpressionOfOptionalChainRoot(expression)) {
952952
return unreachableFlow;
953953
}
954954
}

src/compiler/checker.ts

Lines changed: 64 additions & 55 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,8 +3443,7 @@ namespace ts {
34433443
resolvedReturnType: Type,
34443444
typePredicate: TypePredicate | undefined,
34453445
minArgumentCount: number,
3446-
hasRestParameter: boolean,
3447-
hasLiteralTypes: boolean,
3446+
flags: SignatureFlags
34483447
): Signature;
34493448
/* @internal */ createSymbol(flags: SymbolFlags, name: __String): TransientSymbol;
34503449
/* @internal */ createIndexInfo(type: Type, isReadonly: boolean, declaration?: SignatureDeclaration): IndexInfo;
@@ -4659,7 +4658,24 @@ namespace ts {
46594658
Construct,
46604659
}
46614660

4661+
/* @internal */
4662+
export const enum SignatureFlags {
4663+
None = 0,
4664+
HasRestParameter = 1 << 0,
4665+
HasLiteralTypes = 1 << 1,
4666+
IsOptionalCall = 1 << 2,
4667+
4668+
// We do not propagate `IsOptionalCall` to instantiated signatures, as that would result in us
4669+
// attempting to add `| undefined` on each recursive call to `getReturnTypeOfSignature` when
4670+
// instantiating the return type.
4671+
PropagatingFlags = HasRestParameter | HasLiteralTypes,
4672+
}
4673+
46624674
export interface Signature {
4675+
/* @internal */
4676+
flags: SignatureFlags;
4677+
/* @internal */
4678+
checker: TypeChecker;
46634679
declaration?: SignatureDeclaration | JSDocSignature; // Originating declaration
46644680
typeParameters?: readonly TypeParameter[]; // Type parameters (undefined if non-generic)
46654681
parameters: readonly Symbol[]; // Parameters
@@ -4676,10 +4692,6 @@ namespace ts {
46764692
/* @internal */
46774693
minArgumentCount: number; // Number of non-optional parameters
46784694
/* @internal */
4679-
hasRestParameter: boolean; // True if last parameter is rest parameter
4680-
/* @internal */
4681-
hasLiteralTypes: boolean; // True if specialized
4682-
/* @internal */
46834695
target?: Signature; // Instantiation target
46844696
/* @internal */
46854697
mapper?: TypeMapper; // Instantiation mapper
@@ -4693,8 +4705,6 @@ namespace ts {
46934705
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
46944706
/* @internal */
46954707
instantiations?: Map<Signature>; // Generic signature instantiation cache
4696-
/* @internal */
4697-
isOptionalCall?: boolean;
46984708
}
46994709

47004710
export const enum IndexKind {

src/compiler/utilities.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5917,8 +5917,8 @@ namespace ts {
59175917
* Determines whether a node is the expression preceding an optional chain (i.e. `a` in `a?.b`).
59185918
*/
59195919
/* @internal */
5920-
export function isOptionalExpression(node: Node): node is Expression & { parent: OptionalChainRoot } {
5921-
return isOptionalChainRoot(node.parent) && node === node.parent.expression;
5920+
export function isExpressionOfOptionalChainRoot(node: Node): node is Expression & { parent: OptionalChainRoot } {
5921+
return isOptionalChainRoot(node.parent) && node.parent.expression === node;
59225922
}
59235923

59245924
export function isNewExpression(node: Node): node is NewExpression {
@@ -7320,7 +7320,7 @@ namespace ts {
73207320
getSourceFileConstructor(): new (kind: SyntaxKind.SourceFile, pos?: number, end?: number) => SourceFile;
73217321
getSymbolConstructor(): new (flags: SymbolFlags, name: __String) => Symbol;
73227322
getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type;
7323-
getSignatureConstructor(): new (checker: TypeChecker) => Signature;
7323+
getSignatureConstructor(): new (checker: TypeChecker, flags: SignatureFlags) => Signature;
73247324
getSourceMapSourceConstructor(): new (fileName: string, text: string, skipTrivia?: (pos: number) => number) => SourceMapSource;
73257325
}
73267326

@@ -7341,7 +7341,12 @@ namespace ts {
73417341
}
73427342
}
73437343

7344-
function Signature() {}
7344+
function Signature(this: Signature, checker: TypeChecker, flags: SignatureFlags) {
7345+
this.flags = flags;
7346+
if (Debug.isDebugging) {
7347+
this.checker = checker;
7348+
}
7349+
}
73457350

73467351
function Node(this: Node, kind: SyntaxKind, pos: number, end: number) {
73477352
this.pos = pos;

src/services/codefixes/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,14 @@ namespace ts.codefix {
233233
let someSigHasRestParameter = false;
234234
for (const sig of signatures) {
235235
minArgumentCount = Math.min(sig.minArgumentCount, minArgumentCount);
236-
if (sig.hasRestParameter) {
236+
if (signatureHasRestParameter(sig)) {
237237
someSigHasRestParameter = true;
238238
}
239-
if (sig.parameters.length >= maxArgsSignature.parameters.length && (!sig.hasRestParameter || maxArgsSignature.hasRestParameter)) {
239+
if (sig.parameters.length >= maxArgsSignature.parameters.length && (!signatureHasRestParameter(sig) || signatureHasRestParameter(maxArgsSignature))) {
240240
maxArgsSignature = sig;
241241
}
242242
}
243-
const maxNonRestArgs = maxArgsSignature.parameters.length - (maxArgsSignature.hasRestParameter ? 1 : 0);
243+
const maxNonRestArgs = maxArgsSignature.parameters.length - (signatureHasRestParameter(maxArgsSignature) ? 1 : 0);
244244
const maxArgsParameterSymbolNames = maxArgsSignature.parameters.map(symbol => symbol.name);
245245

246246
const parameters = createDummyParameters(maxNonRestArgs, maxArgsParameterSymbolNames, /* types */ undefined, minArgumentCount, /*inJs*/ false);

src/services/codefixes/inferFromUsage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ namespace ts.codefix {
10541054
}
10551055
const returnType = combineFromUsage(combineUsages(calls.map(call => call.return_)));
10561056
// TODO: GH#18217
1057-
return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
1057+
return checker.createSignature(/*declaration*/ undefined!, /*typeParameters*/ undefined, /*thisParameter*/ undefined, parameters, returnType, /*typePredicate*/ undefined, length, SignatureFlags.None);
10581058
}
10591059

10601060
function addCandidateType(usage: Usage, type: Type | undefined) {

src/services/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ namespace ts {
464464
}
465465

466466
class SignatureObject implements Signature {
467+
flags: SignatureFlags;
467468
checker: TypeChecker;
468469
declaration!: SignatureDeclaration;
469470
typeParameters?: TypeParameter[];
@@ -473,8 +474,6 @@ namespace ts {
473474
resolvedTypePredicate: TypePredicate | undefined;
474475
minTypeArgumentCount!: number;
475476
minArgumentCount!: number;
476-
hasRestParameter!: boolean;
477-
hasLiteralTypes!: boolean;
478477

479478
// Undefined is used to indicate the value has not been computed. If, after computing, the
480479
// symbol has no doc comment, then the empty array will be returned.
@@ -484,9 +483,11 @@ namespace ts {
484483
// symbol has no doc comment, then the empty array will be returned.
485484
jsDocTags?: JSDocTagInfo[];
486485

487-
constructor(checker: TypeChecker) {
486+
constructor(checker: TypeChecker, flags: SignatureFlags) {
488487
this.checker = checker;
488+
this.flags = flags;
489489
}
490+
490491
getDeclaration(): SignatureDeclaration {
491492
return this.declaration;
492493
}

src/services/stringCompletions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ namespace ts.Completions.StringCompletions {
204204
const candidates: Signature[] = [];
205205
checker.getResolvedSignature(argumentInfo.invocation, candidates, argumentInfo.argumentCount);
206206
const types = flatMap(candidates, candidate => {
207-
if (!candidate.hasRestParameter && argumentInfo.argumentCount > candidate.parameters.length) return;
207+
if (!signatureHasRestParameter(candidate) && argumentInfo.argumentCount > candidate.parameters.length) return;
208208
const type = checker.getParameterType(candidate, argumentInfo.argumentIndex);
209209
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
210210
return getStringLiteralTypes(type, uniques);

0 commit comments

Comments
 (0)