Skip to content

Commit 7d434b7

Browse files
author
Armando Aguirre Sepulveda
committed
Refactored node builder flags and tests
1 parent 9757109 commit 7d434b7

File tree

106 files changed

+1625
-1610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1625
-1610
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ import {
11191119
WideningContext,
11201120
WithStatement,
11211121
YieldExpression,
1122+
NodeBuilderFlagsIgnoreErrors,
11221123
} from "./_namespaces/ts.js";
11231124
import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers.js";
11241125
import * as performance from "./_namespaces/ts.performance.js";
@@ -5910,7 +5911,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
59105911
}
59115912

59125913
function symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags: SymbolFormatFlags = SymbolFormatFlags.AllowAnyNodeKind, writer?: EmitTextWriter): string {
5913-
let nodeFlags = NodeBuilderFlags.IgnoreErrors;
5914+
let nodeFlags = NodeBuilderFlagsIgnoreErrors;
59145915
if (flags & SymbolFormatFlags.UseOnlyExternalAliasing) {
59155916
nodeFlags |= NodeBuilderFlags.UseOnlyExternalAliasing;
59165917
}
@@ -5952,7 +5953,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
59525953
else {
59535954
sigOutput = kind === SignatureKind.Construct ? SyntaxKind.ConstructSignature : SyntaxKind.CallSignature;
59545955
}
5955-
const sig = nodeBuilder.signatureToSignatureDeclaration(signature, sigOutput, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName);
5956+
const sig = nodeBuilder.signatureToSignatureDeclaration(signature, sigOutput, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName);
59565957
const printer = createPrinterWithRemoveCommentsOmitTrailingSemicolon();
59575958
const sourceFile = enclosingDeclaration && getSourceFileOfNode(enclosingDeclaration);
59585959
printer.writeNode(EmitHint.Unspecified, sig!, /*sourceFile*/ sourceFile, getTrailingSemicolonDeferringWriter(writer)); // TODO: GH#18217
@@ -5961,8 +5962,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
59615962
}
59625963

59635964
function typeToString(type: Type, enclosingDeclaration?: Node, flags: TypeFormatFlags = TypeFormatFlags.AllowUniqueESSymbolType | TypeFormatFlags.UseAliasDefinedOutsideCurrentScope, writer: EmitTextWriter = createTextWriter("")): string {
5964-
const noTruncation = compilerOptions.noErrorTruncation || flags & TypeFormatFlags.NoTruncation;
5965-
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | (noTruncation ? NodeBuilderFlags.NoTruncation : 0));
5965+
const noTruncation = BigInt(compilerOptions.noErrorTruncation || flags & TypeFormatFlags.NoTruncation);
5966+
const typeNode = nodeBuilder.typeToTypeNode(type, enclosingDeclaration, toNodeBuilderFlags(flags) | NodeBuilderFlagsIgnoreErrors | (noTruncation ? NodeBuilderFlags.NoTruncation : NodeBuilderFlags.None));
59665967
if (typeNode === undefined) return Debug.fail("should always get typenode");
59675968
// The unresolved type gets a synthesized comment on `any` to hint to users that it's not a plain `any`.
59685969
// Otherwise, we always strip comments out.
@@ -5997,7 +5998,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
59975998
}
59985999

59996000
function toNodeBuilderFlags(flags = TypeFormatFlags.None): NodeBuilderFlags {
6000-
return flags & TypeFormatFlags.NodeBuilderFlagsMask;
6001+
return BigInt(flags & TypeFormatFlags.NodeBuilderFlagsMask);
60016002
}
60026003

60036004
function isClassInstanceSide(type: Type) {
@@ -6156,7 +6157,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
61566157

61576158
function withContext<T>(enclosingDeclaration: Node | undefined, flags: NodeBuilderFlags | undefined, tracker: SymbolTracker | undefined, cb: (context: NodeBuilderContext) => T): T | undefined {
61586159
const moduleResolverHost = tracker?.trackSymbol ? tracker.moduleResolverHost :
6159-
flags! & NodeBuilderFlags.DoNotIncludeSymbolChain ? createBasicNodeBuilderModuleSpecifierResolutionHost(host) :
6160+
(flags || NodeBuilderFlags.None) & NodeBuilderFlags.DoNotIncludeSymbolChain ? createBasicNodeBuilderModuleSpecifierResolutionHost(host) :
61606161
undefined;
61616162
const context: NodeBuilderContext = {
61626163
enclosingDeclaration,
@@ -10676,7 +10677,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1067610677
return writer ? typePredicateToStringWorker(writer).getText() : usingSingleLineStringWriter(typePredicateToStringWorker);
1067710678

1067810679
function typePredicateToStringWorker(writer: EmitTextWriter) {
10679-
const nodeBuilderFlags = toNodeBuilderFlags(flags) | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName;
10680+
const nodeBuilderFlags = toNodeBuilderFlags(flags) | NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.WriteTypeParametersInQualifiedName;
1068010681
const predicate = nodeBuilder.typePredicateToTypePredicateNode(typePredicate, enclosingDeclaration, nodeBuilderFlags)!; // TODO: GH#18217
1068110682
const printer = createPrinterWithRemoveComments();
1068210683
const sourceFile = enclosingDeclaration && getSourceFileOfNode(enclosingDeclaration);
@@ -27382,9 +27383,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2738227383
}
2738327384

2738427385
function isFunctionObjectType(type: ObjectType): boolean {
27385-
if (getObjectFlags(type) & ObjectFlags.EvolvingArray) {
27386-
return false;
27387-
}
2738827386
// We do a quick check for a "bind" property before performing the more expensive subtype
2738927387
// check. This gives us a quicker out in the common case where an object type is not a function.
2739027388
const resolved = resolveStructuredTypeMembers(type);

src/compiler/types.ts

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5427,52 +5427,61 @@ export const enum ContextFlags {
54275427
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
54285428
}
54295429

5430+
export type NodeBuilderFlags = bigint;
5431+
54305432
// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!
54315433
// dprint-ignore
5432-
export const enum NodeBuilderFlags {
5433-
None = 0,
5434+
export const NodeBuilderFlags = {
5435+
None : 0n,
54345436
// Options
5435-
NoTruncation = 1 << 0, // Don't truncate result
5436-
WriteArrayAsGenericType = 1 << 1, // Write Array<T> instead T[]
5437-
GenerateNamesForShadowedTypeParams = 1 << 2, // When a type parameter T is shadowing another T, generate a name for it so it can still be referenced
5438-
UseStructuralFallback = 1 << 3, // When an alias cannot be named by its symbol, rather than report an error, fallback to a structural printout if possible
5439-
ForbidIndexedAccessSymbolReferences = 1 << 4, // Forbid references like `I["a"]["b"]` - print `typeof I.a<x>.b<y>` instead
5440-
WriteTypeArgumentsOfSignature = 1 << 5, // Write the type arguments instead of type parameters of the signature
5441-
UseFullyQualifiedType = 1 << 6, // Write out the fully qualified type name (eg. Module.Type, instead of Type)
5442-
UseOnlyExternalAliasing = 1 << 7, // Only use external aliases for a symbol
5443-
SuppressAnyReturnType = 1 << 8, // If the return type is any-like and can be elided, don't offer a return type.
5444-
WriteTypeParametersInQualifiedName = 1 << 9,
5445-
MultilineObjectLiterals = 1 << 10, // Always write object literals across multiple lines
5446-
WriteClassExpressionAsTypeLiteral = 1 << 11, // Write class {} as { new(): {} } - used for mixin declaration emit
5447-
UseTypeOfFunction = 1 << 12, // Build using typeof instead of function type literal
5448-
OmitParameterModifiers = 1 << 13, // Omit modifiers on parameters
5449-
UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases
5450-
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
5451-
NoTypeReduction = 1 << 29, // Don't call getReducedType
5452-
OmitThisParameter = 1 << 25,
5437+
NoTruncation : 1n << 0n, // Don't truncate result
5438+
WriteArrayAsGenericType : 1n << 1n, // Write Array<T> instead T[]
5439+
GenerateNamesForShadowedTypeParams : 1n << 2n, // When a type parameter T is shadowing another T, generate a name for it so it can still be referenced
5440+
UseStructuralFallback : 1n << 3n, // When an alias cannot be named by its symbol, rather than report an error, fallback to a structural printout if possible
5441+
ForbidIndexedAccessSymbolReferences : 1n << 4n, // Forbid references like `I["a"]["b"]` - print `typeof I.a<x>.b<y>` instead
5442+
WriteTypeArgumentsOfSignature : 1n << 5n, // Write the type arguments instead of type parameters of the signature
5443+
UseFullyQualifiedType : 1n << 6n, // Write out the fully qualified type name (eg. Module.Type, instead of Type)
5444+
UseOnlyExternalAliasing : 1n << 7n, // Only use external aliases for a symbol
5445+
SuppressAnyReturnType : 1n << 8n, // If the return type is any-like and can be elided, don't offer a return type.
5446+
WriteTypeParametersInQualifiedName : 1n << 9n,
5447+
MultilineObjectLiterals : 1n << 10n, // Always write object literals across multiple lines
5448+
WriteClassExpressionAsTypeLiteral : 1n << 11n, // Write class {} as { new(): {} } - used for mixin declaration emit
5449+
UseTypeOfFunction : 1n << 12n, // Build using typeof instead of function type literal
5450+
OmitParameterModifiers : 1n << 13n, // Omit modifiers on parameters
5451+
UseAliasDefinedOutsideCurrentScope : 1n << 14n, // Allow non-visible aliases
5452+
UseSingleQuotesForStringLiteralType : 1n << 28n, // Use single quotes for string literal type
5453+
NoTypeReduction : 1n << 29n, // Don't call getReducedType
5454+
OmitThisParameter : 1n << 25n,
54535455

54545456
// Error handling
5455-
AllowThisInObjectLiteral = 1 << 15,
5456-
AllowQualifiedNameInPlaceOfIdentifier = 1 << 16,
5457-
AllowAnonymousIdentifier = 1 << 17,
5458-
AllowEmptyUnionOrIntersection = 1 << 18,
5459-
AllowEmptyTuple = 1 << 19,
5460-
AllowUniqueESSymbolType = 1 << 20,
5461-
AllowEmptyIndexInfoType = 1 << 21,
5462-
/** @internal */ WriteComputedProps = 1 << 30, // { [E.A]: 1 }
5463-
/** @internal */ NoSyntacticPrinter = 1 << 31,
5457+
AllowThisInObjectLiteral : 1n << 15n,
5458+
AllowQualifiedNameInPlaceOfIdentifier : 1n << 16n,
5459+
AllowAnonymousIdentifier : 1n << 17n,
5460+
AllowEmptyUnionOrIntersection : 1n << 18n,
5461+
AllowEmptyTuple : 1n << 19n,
5462+
AllowUniqueESSymbolType : 1n << 20n,
5463+
AllowEmptyIndexInfoType : 1n << 21n,
5464+
/** @internal */ WriteComputedProps : 1n << 30n, // { [E.A]: 1 }
5465+
/** @internal */ NoSyntacticPrinter : 1n << 31n,
54645466
// Errors (cont.)
5465-
AllowNodeModulesRelativePaths = 1 << 26,
5466-
/** @internal */ DoNotIncludeSymbolChain = 1 << 27, // Skip looking up and printing an accessible symbol chain
5467-
/** @internal */ AllowUnresolvedNames = 1 << 32,
5468-
5469-
IgnoreErrors = AllowThisInObjectLiteral | AllowQualifiedNameInPlaceOfIdentifier | AllowAnonymousIdentifier | AllowEmptyUnionOrIntersection | AllowEmptyTuple | AllowEmptyIndexInfoType | AllowNodeModulesRelativePaths,
5467+
AllowNodeModulesRelativePaths : 1n << 26n,
5468+
/** @internal */ DoNotIncludeSymbolChain : 1n << 27n, // Skip looking up and printing an accessible symbol chain
5469+
/** @internal */ AllowUnresolvedNames : 1n << 32n,
54705470

54715471
// State
5472-
InObjectTypeLiteral = 1 << 22,
5473-
InTypeAlias = 1 << 23, // Writing type in type alias declaration
5474-
InInitialEntityName = 1 << 24, // Set when writing the LHS of an entity name or entity name expression
5475-
}
5472+
InObjectTypeLiteral : 1n << 22n,
5473+
InTypeAlias : 1n << 23n, // Writing type in type alias declaration
5474+
InInitialEntityName : 1n << 24n, // Set when writing the LHS of an entity name or entity name expression
5475+
}
5476+
5477+
export const NodeBuilderFlagsIgnoreErrors =
5478+
NodeBuilderFlags.AllowThisInObjectLiteral
5479+
| NodeBuilderFlags.AllowQualifiedNameInPlaceOfIdentifier
5480+
| NodeBuilderFlags.AllowAnonymousIdentifier
5481+
| NodeBuilderFlags.AllowEmptyUnionOrIntersection
5482+
| NodeBuilderFlags.AllowEmptyTuple
5483+
| NodeBuilderFlags.AllowEmptyIndexInfoType
5484+
| NodeBuilderFlags.AllowNodeModulesRelativePaths;
54765485

54775486
// Ensure the shared flags between this and `NodeBuilderFlags` stay in alignment
54785487
// dprint-ignore

src/harness/typeWriter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ export class TypeWriterWalker {
279279
}
280280
else {
281281
const typeFormatFlags = ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType | ts.TypeFormatFlags.GenerateNamesForShadowedTypeParams;
282-
let typeNode = this.checker.typeToTypeNode(type, node.parent, (typeFormatFlags & ts.TypeFormatFlags.NodeBuilderFlagsMask) | ts.NodeBuilderFlags.IgnoreErrors)!;
282+
let typeNode = this.checker.typeToTypeNode(type, node.parent, BigInt((typeFormatFlags & ts.TypeFormatFlags.NodeBuilderFlagsMask)) | ts.NodeBuilderFlagsIgnoreErrors)!;
283283
if (ts.isIdentifier(node) && ts.isTypeAliasDeclaration(node.parent) && node.parent.name === node && ts.isIdentifier(typeNode) && ts.idText(typeNode) === ts.idText(node)) {
284284
// for a complex type alias `type T = ...`, showing "T : T" isn't very helpful for type tests. When the type produced is the same as
285285
// the name of the type alias, recreate the type string without reusing the alias name
286-
typeNode = this.checker.typeToTypeNode(type, node.parent, ((typeFormatFlags | ts.TypeFormatFlags.InTypeAlias) & ts.TypeFormatFlags.NodeBuilderFlagsMask) | ts.NodeBuilderFlags.IgnoreErrors)!;
286+
typeNode = this.checker.typeToTypeNode(type, node.parent, BigInt((typeFormatFlags | ts.TypeFormatFlags.InTypeAlias) & ts.TypeFormatFlags.NodeBuilderFlagsMask) | ts.NodeBuilderFlagsIgnoreErrors)!;
287287
}
288288

289289
const { printer, writer, underliner, reset } = createSyntheticNodeUnderliningPrinter();

src/services/codefixes/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export function addNewNodeForMemberSymbol(
221221
case SyntaxKind.PropertySignature:
222222
case SyntaxKind.PropertyDeclaration:
223223
let flags = NodeBuilderFlags.NoTruncation;
224-
flags |= quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : 0;
224+
flags |= quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : NodeBuilderFlags.None;
225225
let typeNode = checker.typeToTypeNode(type, enclosingDeclaration, flags, getNoopSymbolTrackerWithResolver(context));
226226
if (importAdder) {
227227
const importableReference = tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget);

src/services/inlayHints.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import {
102102
Node,
103103
NodeArray,
104104
NodeBuilderFlags,
105+
NodeBuilderFlagsIgnoreErrors,
105106
ParameterDeclaration,
106107
PrefixUnaryExpression,
107108
PropertyDeclaration,
@@ -464,7 +465,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
464465
}
465466

466467
function printTypeInSingleLine(type: Type) {
467-
const flags = NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.AllowUniqueESSymbolType | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
468+
const flags = NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.AllowUniqueESSymbolType | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
468469
const printer = createPrinterWithRemoveComments();
469470

470471
return usingSingleLineStringWriter(writer => {
@@ -479,7 +480,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
479480
return printTypeInSingleLine(type);
480481
}
481482

482-
const flags = NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.AllowUniqueESSymbolType | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
483+
const flags = NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.AllowUniqueESSymbolType | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
483484
const typeNode = checker.typeToTypeNode(type, /*enclosingDeclaration*/ undefined, flags);
484485
Debug.assertIsDefined(typeNode, "should always get typenode");
485486

src/services/signatureHelp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
mapToDisplayParts,
6565
Node,
6666
NodeBuilderFlags,
67+
NodeBuilderFlagsIgnoreErrors,
6768
ParameterDeclaration,
6869
ParenthesizedExpression,
6970
Printer,
@@ -643,7 +644,7 @@ function getEnclosingDeclarationFromInvocation(invocation: Invocation): Node {
643644
return invocation.kind === InvocationKind.Call ? invocation.node : invocation.kind === InvocationKind.TypeArgs ? invocation.called : invocation.node;
644645
}
645646

646-
const signatureHelpNodeBuilderFlags = NodeBuilderFlags.OmitParameterModifiers | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
647+
const signatureHelpNodeBuilderFlags = NodeBuilderFlags.OmitParameterModifiers | NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
647648
function createSignatureHelpItems(
648649
candidates: readonly Signature[],
649650
resolvedSignature: Signature,

src/services/symbolDisplay.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
NewExpression,
7676
Node,
7777
NodeBuilderFlags,
78+
NodeBuilderFlagsIgnoreErrors,
7879
ObjectFlags,
7980
operatorPart,
8081
PropertyAccessExpression,
@@ -110,7 +111,7 @@ import {
110111
VariableDeclaration,
111112
} from "./_namespaces/ts.js";
112113

113-
const symbolDisplayNodeBuilderFlags = NodeBuilderFlags.OmitParameterModifiers | NodeBuilderFlags.IgnoreErrors | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
114+
const symbolDisplayNodeBuilderFlags = NodeBuilderFlags.OmitParameterModifiers | NodeBuilderFlagsIgnoreErrors | NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope;
114115

115116
// TODO(drosen): use contextual SemanticMeaning.
116117
/** @internal */

tests/baselines/reference/ArrowFunction1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
=== ArrowFunction1.ts ===
44
var v = (a: ) => {
55
>v : (a: any) => void
6-
> : ^ ^^ ^^^^^^^^^
6+
> : ^ ^^^^^^^^^^^^^^
77
>(a: ) => { } : (a: any) => void
8-
> : ^ ^^ ^^^^^^^^^
8+
> : ^ ^^^^^^^^^^^^^^
99
>a : any
1010
> : ^^^
1111

tests/baselines/reference/ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module clodule1 {
2222

2323
function f(x: T) { }
2424
>f : (x: T) => void
25-
> : ^ ^^ ^^^^^^^^^
25+
> : ^ ^^^^^^^^^^^^
2626
>x : T
2727
> : ^
2828
}

0 commit comments

Comments
 (0)