Skip to content

Commit 476e9ee

Browse files
Extract node type printer (#59282)
1 parent 2c23bea commit 476e9ee

File tree

96 files changed

+1984
-1504
lines changed

Some content is hidden

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

96 files changed

+1984
-1504
lines changed

src/compiler/checker.ts

Lines changed: 380 additions & 809 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6998,7 +6998,7 @@
69986998
"category": "Error",
69996999
"code": 9008
70007000
},
7001-
"At least one accessor must have an explicit return type annotation with --isolatedDeclarations.": {
7001+
"At least one accessor must have an explicit type annotation with --isolatedDeclarations.": {
70027002
"category": "Error",
70037003
"code": 9009
70047004
},

src/compiler/expressionToTypeNode.ts

Lines changed: 982 additions & 154 deletions
Large diffs are not rendered by default.

src/compiler/factory/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ export function createAccessorPropertySetRedirector(factory: NodeFactory, node:
16681668
}
16691669

16701670
/** @internal */
1671-
export function findComputedPropertyNameCacheAssignment(name: ComputedPropertyName) {
1671+
export function findComputedPropertyNameCacheAssignment(name: ComputedPropertyName): AssignmentExpression<EqualsToken> & { readonly left: GeneratedIdentifier; } | undefined {
16721672
let node = name.expression;
16731673
while (true) {
16741674
node = skipOuterExpressions(node);

src/compiler/transformers/declarations.ts

Lines changed: 43 additions & 83 deletions
Large diffs are not rendered by default.

src/compiler/transformers/declarations/diagnostics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver): (nod
625625
[SyntaxKind.ArrowFunction]: Diagnostics.Function_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
626626
[SyntaxKind.MethodDeclaration]: Diagnostics.Method_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
627627
[SyntaxKind.ConstructSignature]: Diagnostics.Method_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
628-
[SyntaxKind.GetAccessor]: Diagnostics.At_least_one_accessor_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
629-
[SyntaxKind.SetAccessor]: Diagnostics.At_least_one_accessor_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations,
628+
[SyntaxKind.GetAccessor]: Diagnostics.At_least_one_accessor_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
629+
[SyntaxKind.SetAccessor]: Diagnostics.At_least_one_accessor_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
630630
[SyntaxKind.Parameter]: Diagnostics.Parameter_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
631631
[SyntaxKind.VariableDeclaration]: Diagnostics.Variable_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
632632
[SyntaxKind.PropertyDeclaration]: Diagnostics.Property_must_have_an_explicit_type_annotation_with_isolatedDeclarations,

src/compiler/types.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5849,7 +5849,7 @@ export interface EmitResolver {
58495849
requiresAddingImplicitUndefined(node: ParameterDeclaration, enclosingDeclaration: Node | undefined): boolean;
58505850
isExpandoFunctionDeclaration(node: FunctionDeclaration | VariableDeclaration): boolean;
58515851
getPropertiesOfContainerFunction(node: Declaration): Symbol[];
5852-
createTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration | PropertyAccessExpression | ElementAccessExpression | BinaryExpression, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): TypeNode | undefined;
5852+
createTypeOfDeclaration(declaration: HasInferredType, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): TypeNode | undefined;
58535853
createReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): TypeNode | undefined;
58545854
createTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: NodeBuilderFlags, internalFlags: InternalNodeBuilderFlags, tracker: SymbolTracker): TypeNode | undefined;
58555855
createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, tracker: SymbolTracker): Expression;
@@ -10504,37 +10504,66 @@ export interface EvaluationResolver {
1050410504

1050510505
/** @internal */
1050610506
export type HasInferredType =
10507-
| PropertyAssignment
10507+
| Exclude<VariableLikeDeclaration, JsxAttribute | EnumMember>
1050810508
| PropertyAccessExpression
10509-
| BinaryExpression
1051010509
| ElementAccessExpression
10511-
| VariableDeclaration
10512-
| ParameterDeclaration
10513-
| BindingElement
10514-
| PropertyDeclaration
10515-
| PropertySignature
10510+
| BinaryExpression
1051610511
| ExportAssignment;
1051710512

1051810513
/** @internal */
1051910514
export interface SyntacticTypeNodeBuilderContext {
10515+
flags: NodeBuilderFlags;
1052010516
tracker: Required<Pick<SymbolTracker, "reportInferenceFallback">>;
10517+
enclosingFile: SourceFile | undefined;
1052110518
enclosingDeclaration: Node | undefined;
10519+
approximateLength: number;
10520+
noInferenceFallback?: boolean;
10521+
suppressReportInferenceFallback: boolean;
1052210522
}
1052310523

1052410524
/** @internal */
1052510525
export interface SyntacticTypeNodeBuilderResolver {
10526+
isOptionalParameter(p: ParameterDeclaration): boolean;
1052610527
isUndefinedIdentifierExpression(name: Identifier): boolean;
1052710528
isExpandoFunctionDeclaration(name: FunctionDeclaration | VariableDeclaration): boolean;
1052810529
getAllAccessorDeclarations(declaration: AccessorDeclaration): AllAccessorDeclarations;
10529-
isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node, shouldComputeAliasToMakeVisible?: boolean): SymbolVisibilityResult;
10530-
requiresAddingImplicitUndefined(parameter: ParameterDeclaration | JSDocParameterTag, enclosingDeclaration: Node | undefined): boolean;
10530+
requiresAddingImplicitUndefined(declaration: ParameterDeclaration | PropertySignature | JSDocParameterTag | JSDocPropertyTag | PropertyDeclaration, symbol: Symbol | undefined, enclosingDeclaration: Node | undefined): boolean;
1053110531
isDefinitelyReferenceToGlobalSymbolObject(node: Node): boolean;
10532+
isEntityNameVisible(context: SyntacticTypeNodeBuilderContext, entityName: EntityNameOrEntityNameExpression, shouldComputeAliasToMakeVisible?: boolean): SymbolVisibilityResult;
10533+
serializeExistingTypeNode(context: SyntacticTypeNodeBuilderContext, node: TypeNode, addUndefined?: boolean): TypeNode | undefined;
10534+
serializeReturnTypeForSignature(context: SyntacticTypeNodeBuilderContext, signatureDeclaration: SignatureDeclaration | JSDocSignature): TypeNode | undefined;
10535+
serializeTypeOfExpression(context: SyntacticTypeNodeBuilderContext, expr: Expression): TypeNode;
10536+
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined): TypeNode | undefined;
10537+
serializeNameOfParameter(context: SyntacticTypeNodeBuilderContext, parameter: ParameterDeclaration): BindingName | string;
10538+
serializeTypeName(context: SyntacticTypeNodeBuilderContext, node: EntityName, isTypeOf?: boolean, typeArguments?: readonly TypeNode[]): TypeNode | undefined;
10539+
serializeEntityName(context: SyntacticTypeNodeBuilderContext, node: EntityNameExpression): Expression | undefined;
10540+
getJsDocPropertyOverride(context: SyntacticTypeNodeBuilderContext, jsDocTypeLiteral: JSDocTypeLiteral, jsDocProperty: JSDocPropertyLikeTag): TypeNode | undefined;
10541+
enterNewScope(context: SyntacticTypeNodeBuilderContext, node: IntroducesNewScopeNode | ConditionalTypeNode): () => void;
10542+
markNodeReuse<T extends Node>(context: SyntacticTypeNodeBuilderContext, range: T, location: Node | undefined): T;
10543+
trackExistingEntityName<T extends EntityNameOrEntityNameExpression>(context: SyntacticTypeNodeBuilderContext, node: T): { introducesError: boolean; node: T; };
10544+
trackComputedName(context: SyntacticTypeNodeBuilderContext, accessExpression: EntityNameOrEntityNameExpression): void;
10545+
evaluateEntityNameExpression(expression: EntityNameExpression): EvaluatorResult;
10546+
getModuleSpecifierOverride(context: SyntacticTypeNodeBuilderContext, parent: ImportTypeNode, lit: StringLiteral): string | undefined;
10547+
canReuseTypeNode(context: SyntacticTypeNodeBuilderContext, existing: TypeNode): boolean;
10548+
canReuseTypeNodeAnnotation(context: SyntacticTypeNodeBuilderContext, node: Declaration, existing: TypeNode, symbol: Symbol | undefined, requiresAddingUndefined?: boolean): boolean;
10549+
shouldRemoveDeclaration(context: SyntacticTypeNodeBuilderContext, node: DynamicNamedDeclaration): boolean;
10550+
hasLateBindableName(node: Declaration): node is LateBoundDeclaration | LateBoundBinaryExpressionDeclaration;
10551+
createRecoveryBoundary(context: SyntacticTypeNodeBuilderContext): {
10552+
startRecoveryScope(): () => void;
10553+
finalizeBoundary(): boolean;
10554+
markError(): void;
10555+
hadError(): boolean;
10556+
};
1053210557
}
1053310558

1053410559
/** @internal */
1053510560
export interface SyntacticNodeBuilder {
10536-
typeFromExpression: (node: Expression, context: SyntacticTypeNodeBuilderContext, isConstContext?: boolean, requiresAddingUndefined?: boolean, preserveLiterals?: boolean) => boolean | undefined;
10537-
serializeTypeOfDeclaration: (node: HasInferredType, context: SyntacticTypeNodeBuilderContext) => boolean | undefined;
10538-
serializeReturnTypeForSignature: (node: SignatureDeclaration | JSDocSignature, context: SyntacticTypeNodeBuilderContext) => boolean | undefined;
10539-
serializeTypeOfExpression: (expr: Expression, context: SyntacticTypeNodeBuilderContext, addUndefined?: boolean, preserveLiterals?: boolean) => boolean;
10561+
serializeTypeOfDeclaration: (node: HasInferredType, symbol: Symbol, context: SyntacticTypeNodeBuilderContext) => TypeNode | undefined;
10562+
serializeReturnTypeForSignature: (signature: SignatureDeclaration | JSDocSignature, symbol: Symbol, context: SyntacticTypeNodeBuilderContext) => TypeNode | undefined;
10563+
serializeTypeOfExpression: (expr: Expression | JsxAttributeValue, context: SyntacticTypeNodeBuilderContext, addUndefined?: boolean, preserveLiterals?: boolean) => TypeNode;
10564+
tryReuseExistingTypeNode: (context: SyntacticTypeNodeBuilderContext, existing: TypeNode) => TypeNode | undefined;
10565+
serializeTypeOfAccessor: (accessor: AccessorDeclaration, symbol: Symbol, context: SyntacticTypeNodeBuilderContext) => TypeNode | undefined;
1054010566
}
10567+
10568+
/** @internal */
10569+
export type IntroducesNewScopeNode = SignatureDeclaration | JSDocSignature | MappedTypeNode;

src/compiler/utilities.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ import {
246246
InterfaceDeclaration,
247247
InternalEmitFlags,
248248
InternalSymbolName,
249+
IntroducesNewScopeNode,
249250
isAccessor,
250251
isAnyDirectorySeparator,
251252
isArray,
@@ -322,6 +323,7 @@ import {
322323
isLeftHandSideExpression,
323324
isLineBreak,
324325
isLiteralTypeNode,
326+
isMappedTypeNode,
325327
isMemberName,
326328
isMetaProperty,
327329
isMethodDeclaration,
@@ -2920,11 +2922,6 @@ export function isVariableLike(node: Node): node is VariableLikeDeclaration {
29202922
return false;
29212923
}
29222924

2923-
/** @internal */
2924-
export function isVariableLikeOrAccessor(node: Node): node is AccessorDeclaration | VariableLikeDeclaration {
2925-
return isVariableLike(node) || isAccessor(node);
2926-
}
2927-
29282925
/** @internal */
29292926
export function isVariableDeclarationInVariableStatement(node: VariableDeclaration): boolean {
29302927
return node.parent.kind === SyntaxKind.VariableDeclarationList
@@ -11938,6 +11935,9 @@ export function hasInferredType(node: Node): node is HasInferredType {
1193811935
case SyntaxKind.VariableDeclaration:
1193911936
case SyntaxKind.ExportAssignment:
1194011937
case SyntaxKind.PropertyAssignment:
11938+
case SyntaxKind.ShorthandPropertyAssignment:
11939+
case SyntaxKind.JSDocParameterTag:
11940+
case SyntaxKind.JSDocPropertyTag:
1194111941
return true;
1194211942
default:
1194311943
assertType<never>(node);
@@ -12077,3 +12077,9 @@ function getNodeAtPosition(sourceFile: SourceFile, position: number, includeJSDo
1207712077
current = child;
1207812078
}
1207912079
}
12080+
/** @internal */
12081+
export function isNewScopeNode(node: Node): node is IntroducesNewScopeNode {
12082+
return isFunctionLike(node)
12083+
|| isJSDocSignature(node)
12084+
|| isMappedTypeNode(node);
12085+
}

src/services/codefixes/fixMissingTypeAnnotationOnExports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const extractExpression = "extract-expression";
119119
const errorCodes = [
120120
Diagnostics.Function_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations.code,
121121
Diagnostics.Method_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations.code,
122-
Diagnostics.At_least_one_accessor_must_have_an_explicit_return_type_annotation_with_isolatedDeclarations.code,
122+
Diagnostics.At_least_one_accessor_must_have_an_explicit_type_annotation_with_isolatedDeclarations.code,
123123
Diagnostics.Variable_must_have_an_explicit_type_annotation_with_isolatedDeclarations.code,
124124
Diagnostics.Parameter_must_have_an_explicit_type_annotation_with_isolatedDeclarations.code,
125125
Diagnostics.Property_must_have_an_explicit_type_annotation_with_isolatedDeclarations.code,

tests/baselines/reference/arrowFunctionWithObjectLiteralBody5.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ var a = () => <Error>{ name: "foo", message: "bar" };
2121

2222
var b = () => (<Error>{ name: "foo", message: "bar" });
2323
>b : () => Error
24-
> : ^^^^^^^^^^^
24+
> : ^^^^^^
2525
>() => (<Error>{ name: "foo", message: "bar" }) : () => Error
26-
> : ^^^^^^^^^^^
26+
> : ^^^^^^
2727
>(<Error>{ name: "foo", message: "bar" }) : Error
2828
> : ^^^^^
2929
><Error>{ name: "foo", message: "bar" } : Error
@@ -59,9 +59,9 @@ var c = () => ({ name: "foo", message: "bar" });
5959

6060
var d = () => ((<Error>({ name: "foo", message: "bar" })));
6161
>d : () => Error
62-
> : ^^^^^^^^^^^
62+
> : ^^^^^^
6363
>() => ((<Error>({ name: "foo", message: "bar" }))) : () => Error
64-
> : ^^^^^^^^^^^
64+
> : ^^^^^^
6565
>((<Error>({ name: "foo", message: "bar" }))) : Error
6666
> : ^^^^^
6767
>(<Error>({ name: "foo", message: "bar" })) : Error

0 commit comments

Comments
 (0)