Use head message at top level of elaboration if elaborating via possible call (#27107)

* Use head message at top level of elaboration if elaborating via possible call

* Accept updated baseline
This commit is contained in:
Wesley Wigham 2018-09-15 13:24:22 -07:00 committed by GitHub
parent e4718564e5
commit 921863eb31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 16 deletions

View file

@ -10592,7 +10592,7 @@ namespace ts {
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
if (isTypeRelatedTo(source, target, relation)) return true;
if (!errorNode || !elaborateError(expr, source, target, relation)) {
if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) {
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain);
}
return false;
@ -10602,20 +10602,20 @@ namespace ts {
return !!(type.flags & TypeFlags.Conditional || (type.flags & TypeFlags.Intersection && some((type as IntersectionType).types, isOrHasGenericConditional)));
}
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
function elaborateError(node: Expression | undefined, source: Type, target: Type, relation: Map<RelationComparisonResult>, headMessage: DiagnosticMessage | undefined): boolean {
if (!node || isOrHasGenericConditional(target)) return false;
if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation)) {
if (!checkTypeRelatedTo(source, target, relation, /*errorNode*/ undefined) && elaborateDidYouMeanToCallOrConstruct(node, source, target, relation, headMessage)) {
return true;
}
switch (node.kind) {
case SyntaxKind.JsxExpression:
case SyntaxKind.ParenthesizedExpression:
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation);
return elaborateError((node as ParenthesizedExpression | JsxExpression).expression, source, target, relation, headMessage);
case SyntaxKind.BinaryExpression:
switch ((node as BinaryExpression).operatorToken.kind) {
case SyntaxKind.EqualsToken:
case SyntaxKind.CommaToken:
return elaborateError((node as BinaryExpression).right, source, target, relation);
return elaborateError((node as BinaryExpression).right, source, target, relation, headMessage);
}
break;
case SyntaxKind.ObjectLiteralExpression:
@ -10628,7 +10628,7 @@ namespace ts {
return false;
}
function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>): boolean {
function elaborateDidYouMeanToCallOrConstruct(node: Expression, source: Type, target: Type, relation: Map<RelationComparisonResult>, headMessage: DiagnosticMessage | undefined): boolean {
const callSignatures = getSignaturesOfType(source, SignatureKind.Call);
const constructSignatures = getSignaturesOfType(source, SignatureKind.Construct);
for (const signatures of [constructSignatures, callSignatures]) {
@ -10637,7 +10637,7 @@ namespace ts {
return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined);
})) {
const resultObj: { error?: Diagnostic } = {};
checkTypeAssignableTo(source, target, node, /*errorMessage*/ undefined, /*containingChain*/ undefined, resultObj);
checkTypeAssignableTo(source, target, node, headMessage, /*containingChain*/ undefined, resultObj);
const diagnostic = resultObj.error!;
addRelatedInfo(diagnostic, createDiagnosticForNode(
node,
@ -10663,7 +10663,7 @@ namespace ts {
const sourcePropType = getIndexedAccessType(source, nameType, /*accessNode*/ undefined, errorType);
const targetPropType = getIndexedAccessType(target, nameType, /*accessNode*/ undefined, errorType);
if (sourcePropType !== errorType && targetPropType !== errorType && !isTypeAssignableTo(sourcePropType, targetPropType)) {
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation);
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined);
if (elaborated) {
reportedError = true;
}

View file

@ -2,7 +2,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(1
Property 'x' is missing in type 'typeof Bar'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
Property 'toDateString' is missing in type 'DateConstructor'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2322: Type '() => number' is not assignable to type 'number'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'.
@ -33,7 +33,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2
y: new Date()
}, getNum);
~~~~~~
!!! error TS2322: Type '() => number' is not assignable to type 'number'.
!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
!!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression?

View file

@ -0,0 +1,10 @@
tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
==== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts (1 errors) ====
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)
~~~~
!!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use 'new' with this expression?

View file

@ -0,0 +1,7 @@
//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts]
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)
//// [elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.js]
ff(ohno);

View file

@ -0,0 +1,12 @@
=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts ===
declare var ohno: new () => number;
>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11))
declare function ff(t: number): void;
>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35))
>t : Symbol(t, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 1, 20))
ff(ohno)
>ff : Symbol(ff, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 35))
>ohno : Symbol(ohno, Decl(elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts, 0, 11))

View file

@ -0,0 +1,13 @@
=== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts ===
declare var ohno: new () => number;
>ohno : new () => number
declare function ff(t: number): void;
>ff : (t: number) => void
>t : number
ff(ohno)
>ff(ohno) : void
>ff : (t: number) => void
>ohno : new () => number

View file

@ -1,6 +1,6 @@
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
Type '(x: string) => string' is not assignable to type 'string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
Type '(x: string) => string' is not assignable to type 'string'.
@ -13,12 +13,12 @@ tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5):
foo(g);
foo(() => g);
~~~~~~~
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'.
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:7:5: Did you mean to call this expression?
foo(x);
~
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
!!! error TS2322: Type '(x: string) => string' is not assignable to type 'string'.
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:8:5: Did you mean to call this expression?

View file

@ -0,0 +1,3 @@
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)