2017-05-28 00:38:50 +02:00
"use strict" ;
/ * *
* @ license
* Copyright 2013 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" ) ;
var OPTION _STATEMENTS = "statements" ;
var OPTION _MEMBERS = "members" ;
var OPTION _ELEMENTS = "elements" ;
var OPTION _PARAMETERS = "parameters" ;
var OPTION _ARGUMENTS = "arguments" ;
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 . applyWithWalker ( new AlignWalker ( sourceFile , this . ruleName , {
arguments : this . ruleArguments . indexOf ( OPTION _ARGUMENTS ) !== - 1 ,
elements : this . ruleArguments . indexOf ( OPTION _ELEMENTS ) !== - 1 ,
members : this . ruleArguments . indexOf ( OPTION _MEMBERS ) !== - 1 ,
parameters : this . ruleArguments . indexOf ( OPTION _PARAMETERS ) !== - 1 ,
statements : this . ruleArguments . indexOf ( OPTION _STATEMENTS ) !== - 1 ,
} ) ) ;
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "align" ,
description : "Enforces vertical alignment." ,
hasFix : true ,
rationale : "Helps maintain a readable, consistent style in your codebase." ,
optionsDescription : ( _a = [ "\n Five arguments may be optionally provided:\n\n * `\"" , "\"` checks alignment of function parameters.\n * `\"" , "\"` checks alignment of function call arguments.\n * `\"" , "\"` checks alignment of statements.\n * `\"" , "\"` checks alignment of members of classes, interfaces, type literal, object literals and\n object destructuring.\n * `\"" , "\"` checks alignment of elements of array iterals, array destructuring and tuple types." ] , _a . raw = [ "\n Five arguments may be optionally provided:\n\n * \\`\"" , "\"\\` checks alignment of function parameters.\n * \\`\"" , "\"\\` checks alignment of function call arguments.\n * \\`\"" , "\"\\` checks alignment of statements.\n * \\`\"" , "\"\\` checks alignment of members of classes, interfaces, type literal, object literals and\n object destructuring.\n * \\`\"" , "\"\\` checks alignment of elements of array iterals, array destructuring and tuple types." ] , Lint . Utils . dedent ( _a , OPTION _PARAMETERS , OPTION _ARGUMENTS , OPTION _STATEMENTS , OPTION _MEMBERS , OPTION _ELEMENTS ) ) ,
options : {
type : "array" ,
items : {
type : "string" ,
enum : [ OPTION _ARGUMENTS , OPTION _ELEMENTS , OPTION _MEMBERS , OPTION _PARAMETERS , OPTION _STATEMENTS ] ,
} ,
minLength : 1 ,
maxLength : 5 ,
} ,
optionExamples : [ [ true , "parameters" , "statements" ] ] ,
type : "style" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING _SUFFIX = " are not aligned" ;
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 AlignWalker = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( AlignWalker , _super ) ;
function AlignWalker ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
AlignWalker . prototype . walk = function ( sourceFile ) {
var _this = this ;
var cb = function ( node ) {
if ( _this . options . statements && tsutils _1 . isBlockLike ( node ) ) {
2017-08-14 05:01:11 +02:00
_this . checkAlignment ( node . statements . filter ( function ( s ) { return s . kind !== ts . SyntaxKind . EmptyStatement ; } ) , OPTION _STATEMENTS ) ;
2017-05-28 00:38:50 +02:00
}
else {
switch ( node . kind ) {
case ts . SyntaxKind . NewExpression :
if ( node . arguments === undefined ) {
break ;
}
// falls through
case ts . SyntaxKind . CallExpression :
if ( _this . options . arguments ) {
_this . checkAlignment ( node . arguments , OPTION _ARGUMENTS ) ;
}
break ;
case ts . SyntaxKind . FunctionDeclaration :
case ts . SyntaxKind . FunctionExpression :
case ts . SyntaxKind . Constructor :
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . ArrowFunction :
case ts . SyntaxKind . CallSignature :
case ts . SyntaxKind . ConstructSignature :
case ts . SyntaxKind . MethodSignature :
case ts . SyntaxKind . FunctionType :
case ts . SyntaxKind . ConstructorType :
if ( _this . options . parameters ) {
_this . checkAlignment ( node . parameters , OPTION _PARAMETERS ) ;
}
break ;
case ts . SyntaxKind . ArrayLiteralExpression :
case ts . SyntaxKind . ArrayBindingPattern :
if ( _this . options . elements ) {
_this . checkAlignment ( node . elements , OPTION _ELEMENTS ) ;
}
break ;
case ts . SyntaxKind . TupleType :
if ( _this . options . elements ) {
_this . checkAlignment ( node . elementTypes , OPTION _ELEMENTS ) ;
}
break ;
case ts . SyntaxKind . ObjectLiteralExpression :
if ( _this . options . members ) {
_this . checkAlignment ( node . properties , OPTION _MEMBERS ) ;
}
break ;
case ts . SyntaxKind . ObjectBindingPattern :
if ( _this . options . members ) {
_this . checkAlignment ( node . elements , OPTION _MEMBERS ) ;
}
break ;
case ts . SyntaxKind . ClassDeclaration :
2017-08-14 05:01:11 +02:00
case ts . SyntaxKind . ClassExpression :
if ( _this . options . members ) {
_this . checkAlignment ( node . members . filter ( function ( m ) { return m . kind !== ts . SyntaxKind . SemicolonClassElement ; } ) , OPTION _MEMBERS ) ;
}
break ;
2017-05-28 00:38:50 +02:00
case ts . SyntaxKind . InterfaceDeclaration :
case ts . SyntaxKind . TypeLiteral :
if ( _this . options . members ) {
_this . checkAlignment ( node . members , OPTION _MEMBERS ) ;
}
}
}
return ts . forEachChild ( node , cb ) ;
} ;
return cb ( sourceFile ) ;
} ;
AlignWalker . prototype . checkAlignment = function ( nodes , kind ) {
if ( nodes . length <= 1 ) {
return ;
}
var sourceFile = this . sourceFile ;
var pos = getLineAndCharacterWithoutBom ( sourceFile , this . getStart ( nodes [ 0 ] ) ) ;
var alignToColumn = pos . character ;
var line = pos . line ;
// skip first node in list
for ( var i = 1 ; i < nodes . length ; ++ i ) {
var node = nodes [ i ] ;
var start = this . getStart ( node ) ;
pos = ts . getLineAndCharacterOfPosition ( sourceFile , start ) ;
if ( line !== pos . line && pos . character !== alignToColumn ) {
var diff = alignToColumn - pos . character ;
var fix = void 0 ;
2017-08-14 05:01:11 +02:00
if ( diff >= 0 ) {
2017-05-28 00:38:50 +02:00
fix = Lint . Replacement . appendText ( start , " " . repeat ( diff ) ) ;
}
else if ( node . pos <= start + diff && /^\s+$/ . test ( sourceFile . text . substring ( start + diff , start ) ) ) {
// only delete text if there is only whitespace
fix = Lint . Replacement . deleteText ( start + diff , - diff ) ;
}
this . addFailure ( start , Math . max ( node . end , start ) , kind + Rule . FAILURE _STRING _SUFFIX , fix ) ;
}
line = pos . line ;
}
} ;
AlignWalker . prototype . getStart = function ( node ) {
return node . kind !== ts . SyntaxKind . OmittedExpression
? node . getStart ( this . sourceFile )
: tsutils _1 . getNextToken ( node , this . sourceFile ) . getStart ( this . sourceFile ) ;
} ;
return AlignWalker ;
} ( Lint . AbstractWalker ) ) ;
function getLineAndCharacterWithoutBom ( sourceFile , pos ) {
var result = ts . getLineAndCharacterOfPosition ( sourceFile , pos ) ;
if ( result . line === 0 && sourceFile . text [ 0 ] === "\uFEFF" ) {
result . character -= 1 ;
}
return result ;
}
var _a ;