2017-05-28 00:38:50 +02:00
"use strict" ;
/ * *
* @ license
* Copyright 2017 Palantir Technologies , Inc .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
var tslib _1 = require ( "tslib" ) ;
var tsutils _1 = require ( "tsutils" ) ;
var ts = require ( "typescript" ) ;
var Lint = require ( "../index" ) ;
2017-08-14 05:01:11 +02:00
var OPTION _CHECK _ELSE _IF = "check-else-if" ;
2017-10-14 18:40:54 +02:00
var Rule = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( Rule , _super ) ;
function Rule ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING = function ( assigned ) {
return "Use a conditional expression instead of assigning to '" + assigned + "' in multiple places." ;
} ;
Rule . prototype . apply = function ( sourceFile ) {
2017-08-14 05:01:11 +02:00
return this . applyWithFunction ( sourceFile , walk , {
checkElseIf : this . ruleArguments . indexOf ( OPTION _CHECK _ELSE _IF ) !== - 1 ,
} ) ;
} ;
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "prefer-conditional-expression" ,
description : ( _a = [ "\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement." ] , _a . raw = [ "\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement." ] , Lint . Utils . dedent ( _a ) ) ,
rationale : ( _b = [ "\n This reduces duplication and can eliminate an unnecessary variable declaration." ] , _b . raw = [ "\n This reduces duplication and can eliminate an unnecessary variable declaration." ] , Lint . Utils . dedent ( _b ) ) ,
optionsDescription : "If `" + OPTION _CHECK _ELSE _IF + "` is specified, the rule also checks nested if-else-if statements." ,
options : {
type : "string" ,
enum : [ OPTION _CHECK _ELSE _IF ] ,
} ,
optionExamples : [ true , [ true , OPTION _CHECK _ELSE _IF ] ] ,
type : "functionality" ,
typescriptOnly : false ,
2017-05-28 00:38:50 +02:00
} ;
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
function walk ( ctx ) {
2017-08-14 05:01:11 +02:00
var sourceFile = ctx . sourceFile , checkElseIf = ctx . options . checkElseIf ;
2017-05-28 00:38:50 +02:00
return ts . forEachChild ( sourceFile , function cb ( node ) {
if ( tsutils _1 . isIfStatement ( node ) ) {
2017-08-14 05:01:11 +02:00
var assigned = detect ( node , sourceFile , checkElseIf ) ;
2017-05-28 00:38:50 +02:00
if ( assigned !== undefined ) {
2017-08-14 05:01:11 +02:00
ctx . addFailureAtNode ( node . getChildAt ( 0 , sourceFile ) , Rule . FAILURE _STRING ( assigned . getText ( sourceFile ) ) ) ;
}
if ( assigned !== undefined || ! checkElseIf ) {
2017-05-28 00:38:50 +02:00
// Be careful not to fail again for the "else if"
ts . forEachChild ( node . expression , cb ) ;
ts . forEachChild ( node . thenStatement , cb ) ;
if ( node . elseStatement !== undefined ) {
ts . forEachChild ( node . elseStatement , cb ) ;
}
return ;
}
}
return ts . forEachChild ( node , cb ) ;
} ) ;
}
2017-08-14 05:01:11 +02:00
function detect ( _a , sourceFile , elseIf ) {
2017-05-28 00:38:50 +02:00
var thenStatement = _a . thenStatement , elseStatement = _a . elseStatement ;
2017-08-14 05:01:11 +02:00
if ( elseStatement === undefined || ! elseIf && elseStatement . kind === ts . SyntaxKind . IfStatement ) {
2017-05-28 00:38:50 +02:00
return undefined ;
}
2017-08-14 05:01:11 +02:00
var elze = tsutils _1 . isIfStatement ( elseStatement ) ? detect ( elseStatement , sourceFile , elseIf ) : getAssigned ( elseStatement , sourceFile ) ;
2017-05-28 00:38:50 +02:00
if ( elze === undefined ) {
return undefined ;
}
var then = getAssigned ( thenStatement , sourceFile ) ;
return then !== undefined && nodeEquals ( elze , then , sourceFile ) ? then : undefined ;
}
/** Returns the left side of an assignment. */
function getAssigned ( node , sourceFile ) {
if ( tsutils _1 . isBlock ( node ) ) {
return node . statements . length === 1 ? getAssigned ( node . statements [ 0 ] , sourceFile ) : undefined ;
}
else if ( tsutils _1 . isExpressionStatement ( node ) && tsutils _1 . isBinaryExpression ( node . expression ) ) {
var _a = node . expression , kind = _a . operatorToken . kind , left = _a . left , right = _a . right ;
return kind === ts . SyntaxKind . EqualsToken && tsutils _1 . isSameLine ( sourceFile , right . getStart ( sourceFile ) , right . end ) ? left : undefined ;
}
else {
return undefined ;
}
}
function nodeEquals ( a , b , sourceFile ) {
return a . getText ( sourceFile ) === b . getText ( sourceFile ) ;
}
var _a , _b ;