Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27104,7 +27104,7 @@ namespace ts {
return getRegularTypeOfObjectLiteral(rightType);
}
case SyntaxKind.CommaToken:
if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isEvalNode(right)) {
if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left) && !isInIndirectCall(<BinaryExpression>left.parent)) {
error(left, Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects);
}
return rightType;
Expand Down Expand Up @@ -27133,8 +27133,9 @@ namespace ts {
}
}

function isEvalNode(node: Expression) {
return node.kind === SyntaxKind.Identifier && (node as Identifier).escapedText === "eval";
function isInIndirectCall(node: BinaryExpression) {
return node.parent.kind === SyntaxKind.ParenthesizedExpression && (isCallExpression(node.parent.parent) && node.parent.parent.expression === node.parent || node.parent.parent.kind === SyntaxKind.TaggedTemplateExpression) &&
(isAccessExpression(node.right) || isIdentifier(node.right) && node.right.escapedText === "eval");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still have to check for eval? I think we want to silence the error for any indirect calls.

}

// Return true if there was no error, false if there was an error.
Expand Down
10 changes: 8 additions & 2 deletions tests/baselines/reference/commaOperatorLeftSideUnused.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(38,7): error TS2695: Left si
tests/cases/compiler/commaOperatorLeftSideUnused.ts(39,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(40,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left side of comma operator is unused and has no side effects.
tests/cases/compiler/commaOperatorLeftSideUnused.ts(42,7): error TS2695: Left side of comma operator is unused and has no side effects.


==== tests/cases/compiler/commaOperatorLeftSideUnused.ts (23 errors) ====
==== tests/cases/compiler/commaOperatorLeftSideUnused.ts (24 errors) ====
var xx: any;
var yy: any;

Expand Down Expand Up @@ -111,6 +112,9 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left si
xx = (+xx, 10);
~~~
!!! error TS2695: Left side of comma operator is unused and has no side effects.
xx = (0, xx)();
~
!!! error TS2695: Left side of comma operator is unused and has no side effects.

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -122,4 +126,6 @@ tests/cases/compiler/commaOperatorLeftSideUnused.ts(41,7): error TS2695: Left si
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);

xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;
13 changes: 12 additions & 1 deletion tests/baselines/reference/commaOperatorLeftSideUnused.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -51,9 +52,15 @@ xx = ((xx+= 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);

xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;

//// [commaOperatorLeftSideUnused.js]
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var xx;
var yy;
function fn() {
Expand Down Expand Up @@ -91,6 +98,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();
// OK cases
xx = (xx ? x++ : 4, 10);
xx = (--xx, 3);
Expand All @@ -101,3 +109,6 @@ xx = ((xx += 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx, 100);
xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)(__makeTemplateObject([""], [""]));
16 changes: 16 additions & 0 deletions tests/baselines/reference/commaOperatorLeftSideUnused.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ xx = (+xx, 10);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx)();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

// OK cases
xx = (xx ? x++ : 4, 10);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
Expand Down Expand Up @@ -151,3 +155,15 @@ xx = (xx as any, 100);
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx.fn)();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx['fn'])();
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

xx = (0, xx.fn)``;
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))
>xx : Symbol(xx, Decl(commaOperatorLeftSideUnused.ts, 0, 3))

43 changes: 43 additions & 0 deletions tests/baselines/reference/commaOperatorLeftSideUnused.types
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ xx = (+xx, 10);
>xx : any
>10 : 10

xx = (0, xx)();
>xx = (0, xx)() : any
>xx : any
>(0, xx)() : any
>(0, xx) : any
>0, xx : any
>0 : 0
>xx : any

// OK cases
xx = (xx ? x++ : 4, 10);
>xx = (xx ? x++ : 4, 10) : 10
Expand Down Expand Up @@ -335,3 +344,37 @@ xx = (xx as any, 100);
>xx : any
>100 : 100

xx = (0, xx.fn)();
>xx = (0, xx.fn)() : any
>xx : any
>(0, xx.fn)() : any
>(0, xx.fn) : any
>0, xx.fn : any
>0 : 0
>xx.fn : any
>xx : any
>fn : any

xx = (0, xx['fn'])();
>xx = (0, xx['fn'])() : any
>xx : any
>(0, xx['fn'])() : any
>(0, xx['fn']) : any
>0, xx['fn'] : any
>0 : 0
>xx['fn'] : any
>xx : any
>'fn' : "fn"

xx = (0, xx.fn)``;
>xx = (0, xx.fn)`` : any
>xx : any
>(0, xx.fn)`` : any
>(0, xx.fn) : any
>0, xx.fn : any
>0 : 0
>xx.fn : any
>xx : any
>fn : any
>`` : ""

4 changes: 4 additions & 0 deletions tests/cases/compiler/commaOperatorLeftSideUnused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ xx = (!xx, 10);
xx = (~xx, 10);
xx = (-xx, 10);
xx = (+xx, 10);
xx = (0, xx)();

// OK cases
xx = (xx ? x++ : 4, 10);
Expand All @@ -51,3 +52,6 @@ xx = ((xx+= 4), xx);
xx = (Math.pow(3, 2), 4);
xx = (void xx, 10);
xx = (xx as any, 100);
xx = (0, xx.fn)();
xx = (0, xx['fn'])();
xx = (0, xx.fn)``;