Skip to content

Commit 57be95d

Browse files
committed
fix review
1 parent 8227034 commit 57be95d

16 files changed

+93
-24
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,6 @@ namespace ts {
13751375
}
13761376
}
13771377

1378-
// TODO(rbuckton): Determine how to hook ?? into flow typing
13791378
function bindBinaryExpressionFlow(node: BinaryExpression) {
13801379
const operator = node.operatorToken.kind;
13811380
if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.BarBarToken || operator === SyntaxKind.QuestionQuestionToken) {
@@ -2730,7 +2729,7 @@ namespace ts {
27302729
init = init && getRightMostAssignedExpression(init);
27312730
if (init) {
27322731
const isPrototypeAssignment = isPrototypeAccess(isVariableDeclaration(node) ? node.name : isBinaryExpression(node) ? node.left : node);
2733-
return !!getExpandoInitializer(isBinaryExpression(init) && init.operatorToken.kind === SyntaxKind.BarBarToken ? init.right : init, isPrototypeAssignment);
2732+
return !!getExpandoInitializer(isBinaryExpression(init) && (init.operatorToken.kind === SyntaxKind.BarBarToken || init.operatorToken.kind === SyntaxKind.QuestionQuestionToken) ? init.right : init, isPrototypeAssignment);
27342733
}
27352734
return false;
27362735
}

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24514,10 +24514,10 @@ namespace ts {
2451424514
const { left, operatorToken, right } = node;
2451524515
if (operatorToken.kind === SyntaxKind.QuestionQuestionToken) {
2451624516
if (isBinaryExpression(left) && (left.operatorToken.kind === SyntaxKind.BarBarToken || left.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)) {
24517-
error(left, Diagnostics.Operator_0_cannot_immediately_contain_or_be_contained_within_an_1_operation, tokenToString(left.operatorToken.kind), tokenToString(operatorToken.kind));
24517+
grammarErrorOnNode(left, Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, tokenToString(left.operatorToken.kind), tokenToString(operatorToken.kind));
2451824518
}
2451924519
if (isBinaryExpression(right) && (right.operatorToken.kind === SyntaxKind.BarBarToken || right.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)) {
24520-
error(right, Diagnostics.Operator_0_cannot_immediately_contain_or_be_contained_within_an_1_operation, tokenToString(right.operatorToken.kind), tokenToString(operatorToken.kind));
24520+
grammarErrorOnNode(right, Diagnostics._0_and_1_operations_cannot_be_mixed_without_parentheses, tokenToString(right.operatorToken.kind), tokenToString(operatorToken.kind));
2452124521
}
2452224522
}
2452324523
}

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3180,7 +3180,7 @@
31803180
"category": "Error",
31813181
"code": 5075
31823182
},
3183-
"Operator '{0}' cannot immediately contain, or be contained within an '{1}' operation.": {
3183+
"'{0}' and '{1}' operations cannot be mixed without parentheses.": {
31843184
"category": "Error",
31853185
"code": 5076
31863186
},

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3006,7 +3006,7 @@ namespace ts {
30063006
return parseJSDocAllType(/*postfixEquals*/ true);
30073007
case SyntaxKind.QuestionQuestionToken:
30083008
// If there is '??', consider that is prefix '?' in JSDoc type.
3009-
scanner.reScanQuestionQuestionToken();
3009+
scanner.reScanQuestionToken();
30103010
// falls through
30113011
case SyntaxKind.QuestionToken:
30123012
return parseJSDocUnknownOrNullableType();

src/compiler/scanner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace ts {
3333
scanJsxAttributeValue(): SyntaxKind;
3434
reScanJsxToken(): JsxTokenSyntaxKind;
3535
reScanLessThanToken(): SyntaxKind;
36-
reScanQuestionQuestionToken(): SyntaxKind;
36+
reScanQuestionToken(): SyntaxKind;
3737
scanJsxToken(): JsxTokenSyntaxKind;
3838
scanJsDocToken(): JSDocSyntaxKind;
3939
scan(): SyntaxKind;
@@ -902,7 +902,7 @@ namespace ts {
902902
scanJsxAttributeValue,
903903
reScanJsxToken,
904904
reScanLessThanToken,
905-
reScanQuestionQuestionToken,
905+
reScanQuestionToken,
906906
scanJsxToken,
907907
scanJsDocToken,
908908
scan,
@@ -2023,8 +2023,8 @@ namespace ts {
20232023
return token;
20242024
}
20252025

2026-
function reScanQuestionQuestionToken(): SyntaxKind {
2027-
Debug.assert(token === SyntaxKind.QuestionQuestionToken, "'reScanQuestionQuestionToken' should only be called on a '??'");
2026+
function reScanQuestionToken(): SyntaxKind {
2027+
Debug.assert(token === SyntaxKind.QuestionQuestionToken, "'reScanQuestionToken' should only be called on a '??'");
20282028
pos = tokenPos + 1;
20292029
return token = SyntaxKind.QuestionToken;
20302030
}

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ namespace ts {
19821982
/** Given an expando initializer, return its declaration name, or the left-hand side of the assignment if it's part of an assignment declaration. */
19831983
export function getNameOfExpando(node: Declaration): DeclarationName | undefined {
19841984
if (isBinaryExpression(node.parent)) {
1985-
const parent = (node.parent.operatorToken.kind === SyntaxKind.BarBarToken && isBinaryExpression(node.parent.parent)) ? node.parent.parent : node.parent;
1985+
const parent = ((node.parent.operatorToken.kind === SyntaxKind.BarBarToken || node.parent.operatorToken.kind === SyntaxKind.QuestionQuestionToken) && isBinaryExpression(node.parent.parent)) ? node.parent.parent : node.parent;
19861986
if (parent.operatorToken.kind === SyntaxKind.EqualsToken && isIdentifier(parent.left)) {
19871987
return parent.left;
19881988
}

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,7 @@ declare namespace ts {
32033203
scanJsxAttributeValue(): SyntaxKind;
32043204
reScanJsxToken(): JsxTokenSyntaxKind;
32053205
reScanLessThanToken(): SyntaxKind;
3206-
reScanQuestionQuestionToken(): SyntaxKind;
3206+
reScanQuestionToken(): SyntaxKind;
32073207
scanJsxToken(): JsxTokenSyntaxKind;
32083208
scanJsDocToken(): JSDocSyntaxKind;
32093209
scan(): SyntaxKind;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,7 @@ declare namespace ts {
32033203
scanJsxAttributeValue(): SyntaxKind;
32043204
reScanJsxToken(): JsxTokenSyntaxKind;
32053205
reScanLessThanToken(): SyntaxKind;
3206-
reScanQuestionQuestionToken(): SyntaxKind;
3206+
reScanQuestionToken(): SyntaxKind;
32073207
scanJsxToken(): JsxTokenSyntaxKind;
32083208
scanJsDocToken(): JSDocSyntaxKind;
32093209
scan(): SyntaxKind;

tests/baselines/reference/nullishCoalescingOperator3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const aa1 = a1 ?? a2 ?? a3 ?? a4 ?? a5 ?? a6 ?? 'whatever'
1111

1212

1313
//// [nullishCoalescingOperator3.js]
14-
var _a, _b, _c, _d, _e;
1514
"use strict";
15+
var _a, _b, _c, _d, _e;
1616
var aa1 = (_e = (_d = (_c = (_b = (_a = (a1 != null ? a1 : a2), (_a != null ? _a : a3)), (_b != null ? _b : a4)), (_c != null ? _c : a5)), (_d != null ? _d : a6)), (_e != null ? _e : 'whatever'));

tests/baselines/reference/nullishCoalescingOperator5.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(6,6): error TS5076: Operator '||' cannot immediately contain, or be contained within an '??' operation.
2-
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(9,1): error TS5076: Operator '||' cannot immediately contain, or be contained within an '??' operation.
3-
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(12,6): error TS5076: Operator '&&' cannot immediately contain, or be contained within an '??' operation.
4-
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(15,1): error TS5076: Operator '&&' cannot immediately contain, or be contained within an '??' operation.
1+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(6,6): error TS5076: '||' and '??' operations cannot be mixed without parentheses.
2+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(9,1): error TS5076: '||' and '??' operations cannot be mixed without parentheses.
3+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(12,6): error TS5076: '&&' and '??' operations cannot be mixed without parentheses.
4+
tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts(15,1): error TS5076: '&&' and '??' operations cannot be mixed without parentheses.
55

66

77
==== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator5.ts (4 errors) ====
@@ -12,22 +12,22 @@ tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingO
1212
// should be a syntax error
1313
a ?? b || c;
1414
~~~~~~
15-
!!! error TS5076: Operator '||' cannot immediately contain, or be contained within an '??' operation.
15+
!!! error TS5076: '||' and '??' operations cannot be mixed without parentheses.
1616

1717
// should be a syntax error
1818
a || b ?? c;
1919
~~~~~~
20-
!!! error TS5076: Operator '||' cannot immediately contain, or be contained within an '??' operation.
20+
!!! error TS5076: '||' and '??' operations cannot be mixed without parentheses.
2121

2222
// should be a syntax error
2323
a ?? b && c;
2424
~~~~~~
25-
!!! error TS5076: Operator '&&' cannot immediately contain, or be contained within an '??' operation.
25+
!!! error TS5076: '&&' and '??' operations cannot be mixed without parentheses.
2626

2727
// should be a syntax error
2828
a && b ?? c;
2929
~~~~~~
30-
!!! error TS5076: Operator '&&' cannot immediately contain, or be contained within an '??' operation.
30+
!!! error TS5076: '&&' and '??' operations cannot be mixed without parentheses.
3131

3232
// Valid according to spec
3333
a ?? (b || c);

tests/baselines/reference/nullishCoalescingOperator5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ a && (b ?? c);
4141

4242

4343
//// [nullishCoalescingOperator5.js]
44-
var _a, _b, _c, _d;
4544
"use strict";
45+
var _a, _b, _c, _d;
4646
// should be a syntax error
4747
(a != null ? a : b || c);
4848
// should be a syntax error

tests/baselines/reference/nullishCoalescingOperator8.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const n3 = a.m() ?? b.p ?? b.m() ?? "default";;
88

99

1010
//// [nullishCoalescingOperator8.js]
11-
var _a, _b, _c, _d, _e;
1211
"use strict";
12+
var _a, _b, _c, _d, _e;
1313
var n1 = (_a = a.p, (_a != null ? _a : "default"));
1414
var n2 = (_b = a.m(), (_b != null ? _b : "default"));
1515
var n3 = (_e = (_d = (_c = a.m(), (_c != null ? _c : b.p)), (_d != null ? _d : b.m())), (_e != null ? _e : "default"));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [nullishCoalescingOperator9.ts]
2+
declare let f: null | ((x: string) => void);
3+
4+
let g = f || (abc => { void abc.toLowerCase() })
5+
let gg = f ?? (abc => { void abc.toLowerCase() })
6+
7+
8+
//// [nullishCoalescingOperator9.js]
9+
"use strict";
10+
var g = f || (function (abc) { void abc.toLowerCase(); });
11+
var gg = (f != null ? f : (function (abc) { void abc.toLowerCase(); }));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator9.ts ===
2+
declare let f: null | ((x: string) => void);
3+
>f : Symbol(f, Decl(nullishCoalescingOperator9.ts, 0, 11))
4+
>x : Symbol(x, Decl(nullishCoalescingOperator9.ts, 0, 24))
5+
6+
let g = f || (abc => { void abc.toLowerCase() })
7+
>g : Symbol(g, Decl(nullishCoalescingOperator9.ts, 2, 3))
8+
>f : Symbol(f, Decl(nullishCoalescingOperator9.ts, 0, 11))
9+
>abc : Symbol(abc, Decl(nullishCoalescingOperator9.ts, 2, 14))
10+
>abc.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
11+
>abc : Symbol(abc, Decl(nullishCoalescingOperator9.ts, 2, 14))
12+
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
13+
14+
let gg = f ?? (abc => { void abc.toLowerCase() })
15+
>gg : Symbol(gg, Decl(nullishCoalescingOperator9.ts, 3, 3))
16+
>f : Symbol(f, Decl(nullishCoalescingOperator9.ts, 0, 11))
17+
>abc : Symbol(abc, Decl(nullishCoalescingOperator9.ts, 3, 15))
18+
>abc.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
19+
>abc : Symbol(abc, Decl(nullishCoalescingOperator9.ts, 3, 15))
20+
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
21+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator9.ts ===
2+
declare let f: null | ((x: string) => void);
3+
>f : ((x: string) => void) | null
4+
>null : null
5+
>x : string
6+
7+
let g = f || (abc => { void abc.toLowerCase() })
8+
>g : (x: string) => void
9+
>f || (abc => { void abc.toLowerCase() }) : (x: string) => void
10+
>f : ((x: string) => void) | null
11+
>(abc => { void abc.toLowerCase() }) : (abc: string) => void
12+
>abc => { void abc.toLowerCase() } : (abc: string) => void
13+
>abc : string
14+
>void abc.toLowerCase() : undefined
15+
>abc.toLowerCase() : string
16+
>abc.toLowerCase : () => string
17+
>abc : string
18+
>toLowerCase : () => string
19+
20+
let gg = f ?? (abc => { void abc.toLowerCase() })
21+
>gg : (x: string) => void
22+
>f ?? (abc => { void abc.toLowerCase() }) : (x: string) => void
23+
>f : ((x: string) => void) | null
24+
>(abc => { void abc.toLowerCase() }) : (abc: string) => void
25+
>abc => { void abc.toLowerCase() } : (abc: string) => void
26+
>abc : string
27+
>void abc.toLowerCase() : undefined
28+
>abc.toLowerCase() : string
29+
>abc.toLowerCase : () => string
30+
>abc : string
31+
>toLowerCase : () => string
32+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @strict: true
2+
3+
declare let f: null | ((x: string) => void);
4+
5+
let g = f || (abc => { void abc.toLowerCase() })
6+
let gg = f ?? (abc => { void abc.toLowerCase() })

0 commit comments

Comments
 (0)