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

Fix restrict-plus-operands for ternary ops #1925

Merged
merged 2 commits into from
Dec 30, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/rules/restrictPlusOperandsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import * as ts from "typescript";

import * as Lint from "../index";
import { isTypeFlagSet } from "../language/utils";

export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
Expand Down Expand Up @@ -45,8 +46,8 @@ class RestrictPlusOperandsWalker extends Lint.ProgramAwareRuleWalker {
if (node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
const tc = this.getTypeChecker();

const leftType = getAddableType(node.left, tc);
const rightType = getAddableType(node.right, tc);
const leftType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.left));
const rightType = getBaseTypeOfLiteralType(tc.getTypeAtLocation(node.right));

const width = node.getWidth();
const position = node.getStart();
Expand All @@ -60,12 +61,20 @@ class RestrictPlusOperandsWalker extends Lint.ProgramAwareRuleWalker {
}
}

function getAddableType(node: ts.Expression, tc: ts.TypeChecker): "string" | "number" | "invalid" {
const type = tc.getTypeAtLocation(node);
if (type.flags === ts.TypeFlags.NumberLiteral || type.flags === ts.TypeFlags.Number) {
return "number";
} else if (type.flags === ts.TypeFlags.StringLiteral || type.flags === ts.TypeFlags.String) {
function getBaseTypeOfLiteralType(type: ts.Type): "string" | "number" | "invalid" {
if (isTypeFlagSet(type, ts.TypeFlags.StringLiteral) || isTypeFlagSet(type, ts.TypeFlags.String)) {
return "string";
} else if (isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) || isTypeFlagSet(type, ts.TypeFlags.Number)) {
return "number";
} else if (isTypeFlagSet(type, ts.TypeFlags.Union) && !isTypeFlagSet(type, ts.TypeFlags.Enum)) {
const types = (type as ts.UnionType).types.map(getBaseTypeOfLiteralType);
return allSame(types) ? types[0] : "invalid";
} else if (isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
return getBaseTypeOfLiteralType((type as ts.EnumLiteralType).baseType);
}
return "invalid";
}

function allSame(array: string[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be nicer with Array#reduce

return array.every((value) => value === array[0]);
}
1 change: 1 addition & 0 deletions test/rules/restrict-plus-operands/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ var good6 = pair.first + 10;
var good7 = pair.first + (10 as MyNumber);
var good8 = "5.5" + pair.second;
var good9 = ("5.5" as MyString) + pair.second;
const good10 = 'hello' + (someBoolean ? 'a' : 'b') + (() => someBoolean ? 'c' : 'd')() + 'e';