Skip to content

Commit 967911c

Browse files
authored
Report assignability errors on the return/yield keywords (#52943)
1 parent 5240f06 commit 967911c

File tree

53 files changed

+92
-90
lines changed

Some content is hidden

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

53 files changed

+92
-90
lines changed

src/compiler/utilities.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,13 +2179,14 @@ function getErrorSpanForArrowFunction(sourceFile: SourceFile, node: ArrowFunctio
21792179
export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan {
21802180
let errorNode: Node | undefined = node;
21812181
switch (node.kind) {
2182-
case SyntaxKind.SourceFile:
2182+
case SyntaxKind.SourceFile: {
21832183
const pos = skipTrivia(sourceFile.text, 0, /*stopAfterLineBreak*/ false);
21842184
if (pos === sourceFile.text.length) {
21852185
// file is empty - return span for the beginning of the file
21862186
return createTextSpan(0, 0);
21872187
}
21882188
return getSpanOfTokenAtPosition(sourceFile, pos);
2189+
}
21892190
// This list is a work in progress. Add missing node kinds to improve their error
21902191
// spans.
21912192
case SyntaxKind.VariableDeclaration:
@@ -2210,10 +2211,16 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
22102211
case SyntaxKind.ArrowFunction:
22112212
return getErrorSpanForArrowFunction(sourceFile, node as ArrowFunction);
22122213
case SyntaxKind.CaseClause:
2213-
case SyntaxKind.DefaultClause:
2214+
case SyntaxKind.DefaultClause: {
22142215
const start = skipTrivia(sourceFile.text, (node as CaseOrDefaultClause).pos);
22152216
const end = (node as CaseOrDefaultClause).statements.length > 0 ? (node as CaseOrDefaultClause).statements[0].pos : (node as CaseOrDefaultClause).end;
22162217
return createTextSpanFromBounds(start, end);
2218+
}
2219+
case SyntaxKind.ReturnStatement:
2220+
case SyntaxKind.YieldExpression: {
2221+
const pos = skipTrivia(sourceFile.text, (node as ReturnStatement | YieldExpression).pos);
2222+
return getSpanOfTokenAtPosition(sourceFile, pos);
2223+
}
22172224
}
22182225

22192226
if (errorNode === undefined) {

tests/baselines/reference/accessors_spec_section-4.5_error-cases.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(11,51): error TS2
88
class LanguageSpec_section_4_5_error_cases {
99
public set AnnotatedSetter_SetterFirst(a: number) { }
1010
public get AnnotatedSetter_SetterFirst() { return ""; }
11-
~~~~~~~~~~
11+
~~~~~~
1212
!!! error TS2322: Type 'string' is not assignable to type 'number'.
1313

1414
public get AnnotatedSetter_SetterLast() { return ""; }
15-
~~~~~~~~~~
15+
~~~~~~
1616
!!! error TS2322: Type 'string' is not assignable to type 'number'.
1717
public set AnnotatedSetter_SetterLast(a: number) { }
1818

tests/baselines/reference/asyncArrowFunction_allowJs.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/file.js(19,3): error TS2
2828
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<string>'?
2929
const c = async () => {
3030
return 0
31-
~~~~~~~~
31+
~~~~~~
3232
!!! error TS2322: Type 'number' is not assignable to type 'string'.
3333
}
3434

tests/baselines/reference/baseConstraintOfDecorator.errors.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,17 @@ tests/cases/compiler/baseConstraintOfDecorator.ts(12,18): error TS2545: A mixin
77
==== tests/cases/compiler/baseConstraintOfDecorator.ts (3 errors) ====
88
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
99
return class decoratorFunc extends superClass {
10-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
~~~~~~
11+
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
12+
!!! error TS2322: 'TFunction' could be instantiated with an arbitrary type which could be unrelated to 'typeof decoratorFunc'.
1113
~~~~~~~~~~
1214
!!! error TS2507: Type 'TFunction' is not a constructor function type.
1315
!!! related TS2735 tests/cases/compiler/baseConstraintOfDecorator.ts:1:31: Did you mean for 'TFunction' to be constrained to type 'new (...args: any[]) => unknown'?
1416
constructor(...args: any[]) {
15-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1617
super(...args);
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1818
_instanceModifier(this, args);
19-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2019
}
21-
~~~~~~~~~
2220
};
23-
~~~~~~
24-
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
25-
!!! error TS2322: 'TFunction' could be instantiated with an arbitrary type which could be unrelated to 'typeof decoratorFunc'.
2621
}
2722

2823
class MyClass { private x; }

tests/baselines/reference/checkJsdocReturnTag2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tests/cases/conformance/jsdoc/returns.js(13,5): error TS2322: Type 'number | boo
1212
*/
1313
function f() {
1414
return 5;
15-
~~~~~~~~~
15+
~~~~~~
1616
!!! error TS2322: Type 'number' is not assignable to type 'string'.
1717
}
1818

@@ -21,7 +21,7 @@ tests/cases/conformance/jsdoc/returns.js(13,5): error TS2322: Type 'number | boo
2121
*/
2222
function f1() {
2323
return 5 || true;
24-
~~~~~~~~~~~~~~~~~
24+
~~~~~~
2525
!!! error TS2322: Type 'number | boolean' is not assignable to type 'string | number'.
2626
!!! error TS2322: Type 'boolean' is not assignable to type 'string | number'.
2727
}

tests/baselines/reference/checkJsdocTypeTag5.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ tests/cases/conformance/jsdoc/test.js(34,5): error TS2322: Type '1 | 2' is not a
1313
// all 6 should error on return statement/expression
1414
/** @type {(x: number) => string} */
1515
function h(x) { return x }
16-
~~~~~~~~
16+
~~~~~~
1717
!!! error TS2322: Type 'number' is not assignable to type 'string'.
1818
/** @type {(x: number) => string} */
1919
var f = x => x
2020
~
2121
!!! error TS2322: Type 'number' is not assignable to type 'string'.
2222
/** @type {(x: number) => string} */
2323
var g = function (x) { return x }
24-
~~~~~~~~
24+
~~~~~~
2525
!!! error TS2322: Type 'number' is not assignable to type 'string'.
2626

2727
/** @type {{ (x: number): string }} */
2828
function i(x) { return x }
29-
~~~~~~~~
29+
~~~~~~
3030
!!! error TS2322: Type 'number' is not assignable to type 'string'.
3131
/** @type {{ (x: number): string }} */
3232
var j = x => x
3333
~
3434
!!! error TS2322: Type 'number' is not assignable to type 'string'.
3535
/** @type {{ (x: number): string }} */
3636
var k = function (x) { return x }
37-
~~~~~~~~
37+
~~~~~~
3838
!!! error TS2322: Type 'number' is not assignable to type 'string'.
3939

4040

tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tests/cases/conformance/jsdoc/0.js(22,22): error TS2345: Argument of type 'strin
1919
/** @type {function(number): number} */
2020
method1(n1) {
2121
return "42";
22-
~~~~~~~~~~~~
22+
~~~~~~
2323
!!! error TS2322: Type 'string' is not assignable to type 'number'.
2424
},
2525
/** @type {function(number): number} */

tests/baselines/reference/complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tests/cases/compiler/complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound.t
5353
export function makeNewChannel<T extends ChannelType>(type: T): NewChannel<ChannelOfType<T>> {
5454
const localChannelId = `blahblahblah`;
5555
return { type, localChannelId };
56-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56+
~~~~~~
5757
!!! error TS2322: Type '{ type: T; localChannelId: string; }' is not assignable to type 'NewChannel<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>>'.
5858
!!! error TS2322: Type '{ type: T; localChannelId: string; }' is not assignable to type 'Pick<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>, "type">'.
5959
!!! error TS2322: Types of property 'type' are incompatible.

tests/baselines/reference/conditionalTypeAssignabilityWhenDeferred.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T
129129

130130
function f3<Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> {
131131
return x;
132-
~~~~~~~~~
132+
~~~~~~
133133
!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'.
134134
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'.
135135
}
@@ -142,7 +142,7 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T
142142
x: Q
143143
): InferBecauseWhyNotDistributive<Q> {
144144
return x; // should fail
145-
~~~~~~~~~
145+
~~~~~~
146146
!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNotDistributive<Q>'.
147147
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNotDistributive<Q>'.
148148
}

tests/baselines/reference/constEnumPropertyAccess1.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts(25,9): error TS23
2727
[G.A]() { }
2828
get [G.B]() {
2929
return true;
30-
~~~~~~~~~~~~
30+
~~~~~~
3131
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
3232
}
3333
set [G.B](x: number) { }

0 commit comments

Comments
 (0)