Skip to content

Commit 53b934c

Browse files
committed
Addresses CR feedback
1 parent b1a8a5f commit 53b934c

File tree

7 files changed

+63
-33
lines changed

7 files changed

+63
-33
lines changed

bin/tsc

100644100755
File mode changed.

src/compiler/checker.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -8649,21 +8649,32 @@ module ts {
86498649
}
86508650
else if (typePredicateNode.parameterName) {
86518651
let hasReportedError = false;
8652-
outer: for (let param of node.parameters) {
8652+
for (var param of node.parameters) {
8653+
if (hasReportedError) {
8654+
break;
8655+
}
86538656
if (param.name.kind === SyntaxKind.ObjectBindingPattern ||
86548657
param.name.kind === SyntaxKind.ArrayBindingPattern) {
8655-
8656-
for (let element of (<BindingPattern>param.name).elements) {
8657-
if (element.name.kind === SyntaxKind.Identifier &&
8658-
(<Identifier>element.name).text === typePredicateNode.parameterName.text) {
8659-
8660-
error(typePredicateNode.parameterName,
8661-
Diagnostics.Type_predicate_cannot_reference_element_0_in_a_binding_pattern,
8662-
typePredicate.parameterName);
8663-
hasReportedError = true;
8664-
break outer;
8658+
8659+
(function checkBindingPattern(elements: NodeArray<BindingElement>) {
8660+
for (let element of elements) {
8661+
if (element.name.kind === SyntaxKind.Identifier &&
8662+
(<Identifier>element.name).text === typePredicate.parameterName) {
8663+
8664+
error(typePredicateNode.parameterName,
8665+
Diagnostics.Type_predicate_cannot_reference_element_0_in_a_binding_pattern,
8666+
typePredicate.parameterName);
8667+
hasReportedError = true;
8668+
break;
8669+
}
8670+
else if(!hasReportedError &&
8671+
(element.name.kind === SyntaxKind.ArrayBindingPattern ||
8672+
element.name.kind === SyntaxKind.ObjectBindingPattern)) {
8673+
8674+
checkBindingPattern((<BindingPattern>element.name).elements);
8675+
}
86658676
}
8666-
}
8677+
})((<BindingPattern>param.name).elements);
86678678
}
86688679
}
86698680
if (!hasReportedError) {
@@ -8675,7 +8686,7 @@ module ts {
86758686
}
86768687
else {
86778688
error(typePredicateNode,
8678-
Diagnostics.Type_predicates_are_only_allowed_in_return_type_position_for_functions_and_methods);
8689+
Diagnostics.Type_predicate_are_only_allowed_in_return_type_position_for_functions_and_methods);
86798690
}
86808691
}
86818692
else {
@@ -11317,7 +11328,7 @@ module ts {
1131711328

1131811329
function checkTypePredicate(node: TypePredicateNode) {
1131911330
if(!isInLegalTypePredicatePosition(node)) {
11320-
error(node, Diagnostics.Type_predicates_are_only_allowed_in_return_type_position_for_functions_and_methods);
11331+
error(node, Diagnostics.Type_predicate_are_only_allowed_in_return_type_position_for_functions_and_methods);
1132111332
}
1132211333
}
1132311334

src/compiler/diagnosticInformationMap.generated.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ module ts {
183183
Cannot_find_parameter_0: { code: 1225, category: DiagnosticCategory.Error, key: "Cannot find parameter '{0}'." },
184184
Type_predicate_0_is_not_assignable_to_1: { code: 1226, category: DiagnosticCategory.Error, key: "Type predicate '{0}' is not assignable to '{1}'." },
185185
Parameter_0_is_not_in_the_same_position_as_parameter_1: { code: 1227, category: DiagnosticCategory.Error, key: "Parameter '{0}' is not in the same position as parameter '{1}'." },
186-
Type_predicates_are_only_allowed_in_return_type_position_for_functions_and_methods: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicates are only allowed in return type position for functions and methods." },
186+
Type_predicate_are_only_allowed_in_return_type_position_for_functions_and_methods: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicate are only allowed in return type position for functions and methods." },
187187
Type_predicate_cannot_reference_a_rest_parameter: { code: 1229, category: DiagnosticCategory.Error, key: "Type predicate cannot reference a rest parameter." },
188188
Type_predicate_cannot_reference_element_0_in_a_binding_pattern: { code: 1230, category: DiagnosticCategory.Error, key: "Type predicate cannot reference element '{0}' in a binding pattern." },
189189
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@
719719
"category": "Error",
720720
"code": 1227
721721
},
722-
"Type predicates are only allowed in return type position for functions and methods.": {
722+
"Type predicate are only allowed in return type position for functions and methods.": {
723723
"category": "Error",
724724
"code": 1228
725725
},

tests/baselines/reference/typeGuardFunctionErrors.errors.txt

+24-17
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(81,1):
2020
Type predicate 'p2 is A' is not assignable to 'p1 is A'.
2121
Parameter 'p2' is not in the same position as parameter 'p1'.
2222
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(87,1): error TS2322: Type '(p1: any, p2: any, p3: any) => boolean' is not assignable to type '(p1: any, p2: any) => boolean'.
23-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(92,9): error TS1228: Type predicates are only allowed in return type position for functions and methods.
24-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(93,16): error TS1228: Type predicates are only allowed in return type position for functions and methods.
25-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(94,20): error TS1228: Type predicates are only allowed in return type position for functions and methods.
26-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(100,25): error TS1228: Type predicates are only allowed in return type position for functions and methods.
23+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(92,9): error TS1228: Type predicate are only allowed in return type position for functions and methods.
24+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(93,16): error TS1228: Type predicate are only allowed in return type position for functions and methods.
25+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(94,20): error TS1228: Type predicate are only allowed in return type position for functions and methods.
26+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(100,25): error TS1228: Type predicate are only allowed in return type position for functions and methods.
2727
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(101,16): error TS2409: Return type of constructor signature must be assignable to the instance type of the class
28-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(103,20): error TS1228: Type predicates are only allowed in return type position for functions and methods.
29-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(106,20): error TS1228: Type predicates are only allowed in return type position for functions and methods.
28+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(103,20): error TS1228: Type predicate are only allowed in return type position for functions and methods.
29+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(106,20): error TS1228: Type predicate are only allowed in return type position for functions and methods.
3030
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(107,16): error TS2408: Setters cannot return a value.
31-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(112,18): error TS1228: Type predicates are only allowed in return type position for functions and methods.
32-
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(116,22): error TS1228: Type predicates are only allowed in return type position for functions and methods.
31+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(112,18): error TS1228: Type predicate are only allowed in return type position for functions and methods.
32+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(116,22): error TS1228: Type predicate are only allowed in return type position for functions and methods.
3333
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(120,20): error TS1229: Type predicate cannot reference a rest parameter.
3434
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(125,34): error TS1230: Type predicate cannot reference element 'p1' in a binding pattern.
3535
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(129,34): error TS1230: Type predicate cannot reference element 'p1' in a binding pattern.
36+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(133,39): error TS1230: Type predicate cannot reference element 'p1' in a binding pattern.
3637

3738

38-
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (29 errors) ====
39+
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (30 errors) ====
3940

4041
class A {
4142
propA: number;
@@ -167,33 +168,33 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(129,34
167168
// Type predicates in non-return type positions
168169
var b1: b is A;
169170
~~~~~~
170-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
171+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
171172
function b2(a: b is A) {};
172173
~~~~~~
173-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
174+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
174175
function b3(): A | b is A {
175176
~~~~~~
176-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
177+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
177178
return true;
178179
};
179180

180181
// Non-compatiable type predicate positions for signature declarations
181182
class D {
182183
constructor(p1: A): p1 is C {
183184
~~~~~~~
184-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
185+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
185186
return true;
186187
~~~~
187188
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class
188189
}
189190
get m1(p1: A): p1 is C {
190191
~~~~~~~
191-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
192+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
192193
return true;
193194
}
194195
set m2(p1: A): p1 is C {
195196
~~~~~~~
196-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
197+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
197198
return true;
198199
~~~~
199200
!!! error TS2408: Setters cannot return a value.
@@ -203,13 +204,13 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(129,34
203204
interface I1 {
204205
new (p1: A): p1 is C;
205206
~~~~~~~
206-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
207+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
207208
}
208209

209210
interface I2 {
210211
[index: number]: p1 is C;
211212
~~~~~~~
212-
!!! error TS1228: Type predicates are only allowed in return type position for functions and methods.
213+
!!! error TS1228: Type predicate are only allowed in return type position for functions and methods.
213214
}
214215

215216
// Reference to rest parameter
@@ -228,6 +229,12 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(129,34
228229

229230
function b6([a, b, p1], p2, p3): p1 is A {
230231
~~
232+
!!! error TS1230: Type predicate cannot reference element 'p1' in a binding pattern.
233+
return true;
234+
}
235+
236+
function b7({a, b, c: {p1}}, p2, p3): p1 is A {
237+
~~
231238
!!! error TS1230: Type predicate cannot reference element 'p1' in a binding pattern.
232239
return true;
233240
}

tests/baselines/reference/typeGuardFunctionErrors.js

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ function b5({a, b, p1}, p2, p3): p1 is A {
129129

130130
function b6([a, b, p1], p2, p3): p1 is A {
131131
return true;
132+
}
133+
134+
function b7({a, b, c: {p1}}, p2, p3): p1 is A {
135+
return true;
132136
}
133137

134138
//// [typeGuardFunctionErrors.js]
@@ -253,3 +257,7 @@ function b6(_a, p2, p3) {
253257
var a = _a[0], b = _a[1], p1 = _a[2];
254258
return true;
255259
}
260+
function b7(_a, p2, p3) {
261+
var a = _a.a, b = _a.b, p1 = _a.c.p1;
262+
return true;
263+
}

tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts

+4
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,8 @@ function b5({a, b, p1}, p2, p3): p1 is A {
128128

129129
function b6([a, b, p1], p2, p3): p1 is A {
130130
return true;
131+
}
132+
133+
function b7({a, b, c: {p1}}, p2, p3): p1 is A {
134+
return true;
131135
}

0 commit comments

Comments
 (0)