mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Merge pull request #10877 from RyanCavanaugh/disallowBadCommas
Disallow bad comma left operands
This commit is contained in:
@@ -13265,6 +13265,72 @@ namespace ts {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a *shallow* check: An expression is side-effect-free if the
|
||||
* evaluation of the expression *itself* cannot produce side effects.
|
||||
* For example, x++ / 3 is side-effect free because the / operator
|
||||
* does not have side effects.
|
||||
* The intent is to "smell test" an expression for correctness in positions where
|
||||
* its value is discarded (e.g. the left side of the comma operator).
|
||||
*/
|
||||
function isSideEffectFree(node: Node): boolean {
|
||||
node = skipParentheses(node);
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.Identifier:
|
||||
case SyntaxKind.StringLiteral:
|
||||
case SyntaxKind.RegularExpressionLiteral:
|
||||
case SyntaxKind.TaggedTemplateExpression:
|
||||
case SyntaxKind.TemplateExpression:
|
||||
case SyntaxKind.NoSubstitutionTemplateLiteral:
|
||||
case SyntaxKind.NumericLiteral:
|
||||
case SyntaxKind.TrueKeyword:
|
||||
case SyntaxKind.FalseKeyword:
|
||||
case SyntaxKind.NullKeyword:
|
||||
case SyntaxKind.UndefinedKeyword:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.ClassExpression:
|
||||
case SyntaxKind.ArrowFunction:
|
||||
case SyntaxKind.ArrayLiteralExpression:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
case SyntaxKind.TypeOfExpression:
|
||||
case SyntaxKind.NonNullExpression:
|
||||
case SyntaxKind.JsxSelfClosingElement:
|
||||
case SyntaxKind.JsxElement:
|
||||
return true;
|
||||
|
||||
case SyntaxKind.ConditionalExpression:
|
||||
return isSideEffectFree((node as ConditionalExpression).whenTrue) &&
|
||||
isSideEffectFree((node as ConditionalExpression).whenFalse);
|
||||
|
||||
case SyntaxKind.BinaryExpression:
|
||||
if (isAssignmentOperator((node as BinaryExpression).operatorToken.kind)) {
|
||||
return false;
|
||||
}
|
||||
return isSideEffectFree((node as BinaryExpression).left) &&
|
||||
isSideEffectFree((node as BinaryExpression).right);
|
||||
|
||||
case SyntaxKind.PrefixUnaryExpression:
|
||||
case SyntaxKind.PostfixUnaryExpression:
|
||||
// Unary operators ~, !, +, and - have no side effects.
|
||||
// The rest do.
|
||||
switch ((node as PrefixUnaryExpression).operator) {
|
||||
case SyntaxKind.ExclamationToken:
|
||||
case SyntaxKind.PlusToken:
|
||||
case SyntaxKind.MinusToken:
|
||||
case SyntaxKind.TildeToken:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
// Some forms listed here for clarity
|
||||
case SyntaxKind.VoidExpression: // Explicit opt-out
|
||||
case SyntaxKind.TypeAssertionExpression: // Not SEF, but can produce useful type warnings
|
||||
case SyntaxKind.AsExpression: // Not SEF, but can produce useful type warnings
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isTypeEqualityComparableTo(source: Type, target: Type) {
|
||||
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
|
||||
}
|
||||
@@ -13426,6 +13492,9 @@ namespace ts {
|
||||
checkAssignmentOperator(rightType);
|
||||
return getRegularTypeOfObjectLiteral(rightType);
|
||||
case SyntaxKind.CommaToken:
|
||||
if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left)) {
|
||||
error(left, Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects);
|
||||
}
|
||||
return rightType;
|
||||
}
|
||||
|
||||
|
||||
@@ -1955,6 +1955,10 @@
|
||||
"category": "Error",
|
||||
"code": 2694
|
||||
},
|
||||
"Left side of comma operator is unused and has no side effects.": {
|
||||
"category": "Error",
|
||||
"code": 2695
|
||||
},
|
||||
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
|
||||
Reference in New Issue
Block a user