Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

restrict-plus-operands: Minor error message change. #3220

Merged
merged 1 commit into from
Sep 13, 2017
Merged
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: 6 additions & 1 deletion src/rules/restrictPlusOperandsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Rule extends Lint.Rules.TypedRule {
/* tslint:enable:object-literal-sort-keys */

public static INVALID_TYPES_ERROR = "Operands of '+' operation must either be both strings or both numbers";
public static SUGGEST_TEMPLATE_LITERALS = ", consider using template literals";

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
Expand All @@ -47,7 +48,11 @@ function walk(ctx: Lint.WalkContext<void>, tc: ts.TypeChecker) {
const leftType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.left));
const rightType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.right));
if (leftType === "invalid" || rightType === "invalid" || leftType !== rightType) {
return ctx.addFailureAtNode(node, Rule.INVALID_TYPES_ERROR);
if (leftType === "string" || rightType === "string") {
return ctx.addFailureAtNode(node, Rule.INVALID_TYPES_ERROR + Rule.SUGGEST_TEMPLATE_LITERALS);
} else {
return ctx.addFailureAtNode(node, Rule.INVALID_TYPES_ERROR);
}
}
}
return ts.forEachChild(node, cb);
Expand Down
18 changes: 9 additions & 9 deletions test/rules/restrict-plus-operands/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var pair: NumberStringPair = {

// bad
var bad1 = 5 + "10";
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad2 = [] + 5;
~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
var bad3 = [] + {};
Expand All @@ -27,23 +27,23 @@ var bad4 = [] + [];
var bad4 = 5 + [];
~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
var bad5 = "5" + {};
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad6 = 5.5 + "5";
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad7 = "5.5" + 5;
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad8 = x + y;
~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad9 = y + x;
~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad10 = x + {};
~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
var bad11 = [] + y;
~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad12 = pair.first + "10";
~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad13 = 5 + pair.second;
~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]
~~~~~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers, consider using template literals]
var bad14 = pair + pair;
~~~~~~~~~~~ [Operands of '+' operation must either be both strings or both numbers]

Expand Down