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" ,
2018-09-20 02:56:13 +02:00
description : Lint . Utils . dedent ( templateObject _1 || ( templateObject _1 = tslib _1 . _ _makeTemplateObject ( [ "\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement." ] , [ "\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement." ] ) ) ) ,
rationale : Lint . Utils . dedent ( templateObject _2 || ( templateObject _2 = tslib _1 . _ _makeTemplateObject ( [ "\n This reduces duplication and can eliminate an unnecessary variable declaration." ] , [ "\n This reduces duplication and can eliminate an unnecessary variable declaration." ] ) ) ) ,
2017-08-14 05:01:11 +02:00
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 ) ) {
2018-09-20 02:56:13 +02:00
var assigned = detectAssignment ( 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"
2018-09-20 02:56:13 +02:00
do {
ts . forEachChild ( node . expression , cb ) ;
ts . forEachChild ( node . thenStatement , cb ) ;
if ( node . elseStatement === undefined ) {
return ;
}
node = node . elseStatement ;
while ( tsutils _1 . isBlock ( node ) && node . statements . length === 1 ) {
node = node . statements [ 0 ] ;
}
} while ( tsutils _1 . isIfStatement ( node ) ) ;
2017-05-28 00:38:50 +02:00
}
}
return ts . forEachChild ( node , cb ) ;
} ) ;
}
2018-09-20 02:56:13 +02:00
/ * *
* @ param inElse ` undefined ` when this is the top level if statement , ` false ` when inside the then branch , ` true ` when inside else
* /
function detectAssignment ( statement , sourceFile , checkElseIf , inElse ) {
if ( tsutils _1 . isIfStatement ( statement ) ) {
if ( inElse === false || ! checkElseIf && inElse || statement . elseStatement === undefined ) {
return undefined ;
}
var then = detectAssignment ( statement . thenStatement , sourceFile , checkElseIf , false ) ;
if ( then === undefined ) {
return undefined ;
}
var elze = detectAssignment ( statement . elseStatement , sourceFile , checkElseIf , true ) ;
return elze !== undefined && nodeEquals ( then , elze , sourceFile ) ? then : undefined ;
2017-05-28 00:38:50 +02:00
}
2018-09-20 02:56:13 +02:00
else if ( tsutils _1 . isBlock ( statement ) ) {
return statement . statements . length === 1
? detectAssignment ( statement . statements [ 0 ] , sourceFile , checkElseIf , inElse )
: undefined ;
2017-05-28 00:38:50 +02:00
}
2018-09-20 02:56:13 +02:00
else if ( tsutils _1 . isExpressionStatement ( statement ) && tsutils _1 . isBinaryExpression ( statement . expression ) ) {
var _a = statement . expression , kind = _a . operatorToken . kind , left = _a . left , right = _a . right ;
2017-05-28 00:38:50 +02:00
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 ) ;
}
2018-09-20 02:56:13 +02:00
var templateObject _1 , templateObject _2 ;