Skip to content

Commit

Permalink
fail duplicate property access chains for preferOrOperatorOverTernary
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Feb 27, 2018
1 parent d0ea0a9 commit db7c619
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tslint-misc-rules",
"version": "3.2.1",
"version": "3.3.1",
"description": "Collection of miscellaneous TSLint rules",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 28 additions & 3 deletions rules/preferOrOperatorOverTernaryRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function check(ctx: Lint.WalkContext<void>, node: ts.ConditionalExpression) {
const { condition, whenTrue, questionToken, colonToken } = node

if (
condition.kind === ts.SyntaxKind.Identifier &&
whenTrue.kind === ts.SyntaxKind.Identifier &&
condition.getText(sf) === whenTrue.getText(sf)
condition.getText(sf) === whenTrue.getText(sf) &&
((isIdentifier(condition) && isIdentifier(whenTrue)) ||
(isPropertyAccessChain(condition) && isPropertyAccessChain(whenTrue)))
) {
const fix = Lint.Replacement.replaceFromTo(
questionToken.getStart(sf),
Expand All @@ -38,3 +38,28 @@ function check(ctx: Lint.WalkContext<void>, node: ts.ConditionalExpression) {
)
}
}

function isPropertyAccessChain(startNode: ts.Expression) {
if (!isPropertyAccess(startNode) || !isIdentifier(startNode.name)) {
return false
}

let node = startNode
while (isPropertyAccess(node.expression)) {
node = node.expression
}

return (
node.expression &&
(isIdentifier(node.expression) ||
node.expression.kind === ts.SyntaxKind.ThisKeyword)
)
}

function isIdentifier(node: ts.Node): node is ts.Identifier {
return node && node.kind === ts.SyntaxKind.Identifier
}

function isPropertyAccess(node: ts.Node): node is ts.PropertyAccessExpression {
return node && node.kind === ts.SyntaxKind.PropertyAccessExpression
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ const passesTwo = foo ? foo + 1 : bar;
const passesThree = foo || bar;
const passesFour = foo === 0 ? foo : bar;

const failsTwo = a.b.c || d;
const failsThree = this.x.y || z;

const passesFive = a.b.c() ? a.b.c() : d;
const passesSix = a.b().c ? a.b().c : d;
const passesSeven = a().b.c ? a().b.c : d;
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ const passesTwo = foo ? foo + 1 : bar;
const passesThree = foo || bar;
const passesFour = foo === 0 ? foo : bar;

const failsTwo = a.b.c ? a.b.c : d;
~~~~~ [prefer-or]
const failsThree = this.x.y ? this.x.y : z;
~~~~~~~~ [prefer-or]

const passesFive = a.b.c() ? a.b.c() : d;
const passesSix = a.b().c ? a.b().c : d;
const passesSeven = a().b.c ? a().b.c : d;
[prefer-or]: use '||' when first and second operands of ternary are identical
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"lib": ["es2015"],
"module": "commonjs",
"noEmitOnError": true,
"skipLibCheck": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
Expand Down

0 comments on commit db7c619

Please sign in to comment.