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-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 ;
}
Rule . prototype . apply = function ( sourceFile ) {
return this . applyWithFunction ( sourceFile , walk ) ;
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "no-unnecessary-initializer" ,
description : "Forbids a 'var'/'let' statement or destructuring initializer to be initialized to 'undefined'." ,
hasFix : true ,
optionsDescription : "Not configurable." ,
options : null ,
optionExamples : [ true ] ,
2018-09-20 02:56:13 +02:00
rationale : Lint . Utils . dedent ( templateObject _1 || ( templateObject _1 = tslib _1 . _ _makeTemplateObject ( [ "\n Values in JavaScript default to `undefined`.\n There's no need to do so manually.\n " ] , [ "\n Values in JavaScript default to \\`undefined\\`.\n There's no need to do so manually.\n " ] ) ) ) ,
2017-08-14 05:01:11 +02:00
type : "style" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING = "Unnecessary initialization to 'undefined'." ;
Rule . FAILURE _STRING _PARAMETER = "Use an optional parameter instead of initializing to 'undefined'. " +
"Also, the type declaration does not need to include '| undefined'." ;
2017-05-28 00:38:50 +02:00
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
function walk ( ctx ) {
ts . forEachChild ( ctx . sourceFile , function cb ( node ) {
switch ( node . kind ) {
case ts . SyntaxKind . BindingElement :
checkInitializer ( node ) ;
break ;
case ts . SyntaxKind . VariableDeclaration :
2018-09-20 02:56:13 +02:00
if ( ! tsutils _1 . isBindingPattern ( node . name ) && ! tsutils _1 . isNodeFlagSet ( node . parent , ts . NodeFlags . Const ) ) {
2017-05-28 00:38:50 +02:00
checkInitializer ( node ) ;
}
break ;
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . FunctionDeclaration :
case ts . SyntaxKind . Constructor : {
var parameters _1 = node . parameters ;
parameters _1 . forEach ( function ( parameter , i ) {
if ( isUndefined ( parameter . initializer ) ) {
if ( parametersAllOptionalAfter ( parameters _1 , i ) ) {
// No fix since they may want to remove '| undefined' from the type.
ctx . addFailureAtNode ( parameter , Rule . FAILURE _STRING _PARAMETER ) ;
}
else {
failWithFix ( parameter ) ;
}
}
} ) ;
}
}
ts . forEachChild ( node , cb ) ;
} ) ;
function checkInitializer ( node ) {
if ( isUndefined ( node . initializer ) ) {
failWithFix ( node ) ;
}
}
function failWithFix ( node ) {
2018-09-20 02:56:13 +02:00
var fix = Lint . Replacement . deleteFromTo ( tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . EqualsToken ) . pos , node . end ) ;
2017-05-28 00:38:50 +02:00
ctx . addFailureAtNode ( node , Rule . FAILURE _STRING , fix ) ;
}
}
function parametersAllOptionalAfter ( parameters , idx ) {
for ( var i = idx + 1 ; i < parameters . length ; i ++ ) {
if ( parameters [ i ] . questionToken !== undefined ) {
return true ;
}
if ( parameters [ i ] . initializer === undefined ) {
return false ;
}
}
return true ;
}
function isUndefined ( node ) {
return node !== undefined &&
node . kind === ts . SyntaxKind . Identifier &&
node . originalKeywordKind === ts . SyntaxKind . UndefinedKeyword ;
}
2018-09-20 02:56:13 +02:00
var templateObject _1 ;