2017-05-28 00:38:50 +02:00
"use strict" ;
/ * *
* @ license
* Copyright 2016 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 ts = require ( "typescript" ) ;
2018-09-20 02:56:13 +02:00
var tsutils _1 = require ( "tsutils" ) ;
2017-05-28 00:38:50 +02:00
var Lint = require ( "../index" ) ;
2017-08-14 05:01:11 +02:00
var utils _1 = require ( "../language/utils" ) ;
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 ) {
2018-09-20 02:56:13 +02:00
return this . applyWithWalker ( new NoMagicNumbersWalker ( sourceFile , this . ruleName , this . ruleArguments . length > 0 ? this . ruleArguments : Rule . DEFAULT _ALLOWED ) ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "no-magic-numbers" ,
2018-09-20 02:56:13 +02:00
description : Lint . Utils . dedent ( templateObject _1 || ( templateObject _1 = tslib _1 . _ _makeTemplateObject ( [ "\n Disallows the use constant number values outside of variable assignments.\n When no list of allowed values is specified, -1, 0 and 1 are allowed by default." ] , [ "\n Disallows the use constant number values outside of variable assignments.\n When no list of allowed values is specified, -1, 0 and 1 are allowed by default." ] ) ) ) ,
rationale : Lint . Utils . dedent ( templateObject _2 || ( templateObject _2 = tslib _1 . _ _makeTemplateObject ( [ "\n Magic numbers should be avoided as they often lack documentation.\n Forcing them to be stored in variables gives them implicit documentation.\n " ] , [ "\n Magic numbers should be avoided as they often lack documentation.\n Forcing them to be stored in variables gives them implicit documentation.\n " ] ) ) ) ,
2017-08-14 05:01:11 +02:00
optionsDescription : "A list of allowed numbers." ,
options : {
type : "array" ,
items : {
type : "number" ,
} ,
minLength : 1 ,
} ,
optionExamples : [ true , [ true , 1 , 2 , 3 ] ] ,
type : "typescript" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING = "'magic numbers' are not allowed" ;
Rule . ALLOWED _NODES = new Set ( [
ts . SyntaxKind . ExportAssignment ,
ts . SyntaxKind . FirstAssignment ,
ts . SyntaxKind . LastAssignment ,
ts . SyntaxKind . PropertyAssignment ,
ts . SyntaxKind . ShorthandPropertyAssignment ,
ts . SyntaxKind . VariableDeclaration ,
ts . SyntaxKind . VariableDeclarationList ,
ts . SyntaxKind . EnumMember ,
ts . SyntaxKind . PropertyDeclaration ,
ts . SyntaxKind . Parameter ,
] ) ;
Rule . DEFAULT _ALLOWED = [ - 1 , 0 , 1 ] ;
2017-05-28 00:38:50 +02:00
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
2017-10-14 18:40:54 +02:00
var NoMagicNumbersWalker = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( NoMagicNumbersWalker , _super ) ;
function NoMagicNumbersWalker ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
NoMagicNumbersWalker . prototype . walk = function ( sourceFile ) {
var _this = this ;
var cb = function ( node ) {
2018-09-20 02:56:13 +02:00
if ( tsutils _1 . isCallExpression ( node ) && tsutils _1 . isIdentifier ( node . expression ) && node . expression . text === "parseInt" ) {
return node . arguments . length === 0 ? undefined : cb ( node . arguments [ 0 ] ) ;
}
2017-05-28 00:38:50 +02:00
if ( node . kind === ts . SyntaxKind . NumericLiteral ) {
return _this . checkNumericLiteral ( node , node . text ) ;
}
2017-08-14 05:01:11 +02:00
if ( utils _1 . isNegativeNumberLiteral ( node ) ) {
2017-05-28 00:38:50 +02:00
return _this . checkNumericLiteral ( node , "-" + node . operand . text ) ;
}
return ts . forEachChild ( node , cb ) ;
} ;
return ts . forEachChild ( sourceFile , cb ) ;
} ;
NoMagicNumbersWalker . prototype . checkNumericLiteral = function ( node , num ) {
2018-09-20 02:56:13 +02:00
/* Using Object.is() to differentiate between pos/neg zero */
if ( ! Rule . ALLOWED _NODES . has ( node . parent . kind ) &&
! this . options . some ( function ( allowedNum ) { return Object . is ( allowedNum , parseFloat ( num ) ) ; } ) ) {
2017-05-28 00:38:50 +02:00
this . addFailureAtNode ( node , Rule . FAILURE _STRING ) ;
}
} ;
return NoMagicNumbersWalker ;
} ( Lint . AbstractWalker ) ) ;
2018-09-20 02:56:13 +02:00
var templateObject _1 , templateObject _2 ;