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" ) ;
2017-08-14 05:01:11 +02:00
var tsutils _1 = require ( "tsutils" ) ;
2017-05-28 00:38:50 +02:00
var ts = require ( "typescript" ) ;
var Lint = require ( "../index" ) ;
var OPTION _BRACE = "check-open-brace" ;
var OPTION _CATCH = "check-catch" ;
var OPTION _ELSE = "check-else" ;
var OPTION _FINALLY = "check-finally" ;
var OPTION _WHITESPACE = "check-whitespace" ;
var Rule = ( function ( _super ) {
tslib _1 . _ _extends ( Rule , _super ) ;
function Rule ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
Rule . prototype . apply = function ( sourceFile ) {
var oneLineWalker = new OneLineWalker ( sourceFile , this . getOptions ( ) ) ;
return this . applyWithWalker ( oneLineWalker ) ;
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "one-line" ,
description : "Requires the specified tokens to be on the same line as the expression preceding them." ,
optionsDescription : ( _a = [ "\n Five arguments may be optionally provided:\n\n * `\"" , "\"` checks that `catch` is on the same line as the closing brace for `try`.\n * `\"" , "\"` checks that `finally` is on the same line as the closing brace for `catch`.\n * `\"" , "\"` checks that `else` is on the same line as the closing brace for `if`.\n * `\"" , "\"` checks that an open brace falls on the same line as its preceding expression.\n * `\"" , "\"` checks preceding whitespace for the specified tokens." ] , _a . raw = [ "\n Five arguments may be optionally provided:\n\n * \\`\"" , "\"\\` checks that \\`catch\\` is on the same line as the closing brace for \\`try\\`.\n * \\`\"" , "\"\\` checks that \\`finally\\` is on the same line as the closing brace for \\`catch\\`.\n * \\`\"" , "\"\\` checks that \\`else\\` is on the same line as the closing brace for \\`if\\`.\n * \\`\"" , "\"\\` checks that an open brace falls on the same line as its preceding expression.\n * \\`\"" , "\"\\` checks preceding whitespace for the specified tokens." ] , Lint . Utils . dedent ( _a , OPTION _CATCH , OPTION _FINALLY , OPTION _ELSE , OPTION _BRACE , OPTION _WHITESPACE ) ) ,
options : {
type : "array" ,
items : {
type : "string" ,
enum : [ OPTION _CATCH , OPTION _FINALLY , OPTION _ELSE , OPTION _BRACE , OPTION _WHITESPACE ] ,
} ,
minLength : 0 ,
maxLength : 5 ,
} ,
optionExamples : [ [ true , OPTION _CATCH , OPTION _FINALLY , OPTION _ELSE ] ] ,
type : "style" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . BRACE _FAILURE _STRING = "misplaced opening brace" ;
Rule . CATCH _FAILURE _STRING = "misplaced 'catch'" ;
Rule . ELSE _FAILURE _STRING = "misplaced 'else'" ;
Rule . FINALLY _FAILURE _STRING = "misplaced 'finally'" ;
Rule . WHITESPACE _FAILURE _STRING = "missing whitespace" ;
2017-05-28 00:38:50 +02:00
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
var OneLineWalker = ( function ( _super ) {
tslib _1 . _ _extends ( OneLineWalker , _super ) ;
function OneLineWalker ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
OneLineWalker . prototype . visitIfStatement = function ( node ) {
var thenStatement = node . thenStatement ;
var thenIsBlock = thenStatement . kind === ts . SyntaxKind . Block ;
if ( thenIsBlock ) {
var expressionCloseParen = node . getChildAt ( 3 ) ;
var thenOpeningBrace = thenStatement . getChildAt ( 0 ) ;
this . handleOpeningBrace ( expressionCloseParen , thenOpeningBrace ) ;
}
var elseStatement = node . elseStatement ;
if ( elseStatement != null ) {
// find the else keyword
var elseKeyword = Lint . childOfKind ( node , ts . SyntaxKind . ElseKeyword ) ;
if ( elseStatement . kind === ts . SyntaxKind . Block ) {
var elseOpeningBrace = elseStatement . getChildAt ( 0 ) ;
this . handleOpeningBrace ( elseKeyword , elseOpeningBrace ) ;
}
if ( thenIsBlock && this . hasOption ( OPTION _ELSE ) ) {
var thenStatementEndLine = this . getLineAndCharacterOfPosition ( thenStatement . getEnd ( ) ) . line ;
var elseKeywordLine = this . getLineAndCharacterOfPosition ( elseKeyword . getStart ( ) ) . line ;
if ( thenStatementEndLine !== elseKeywordLine ) {
this . addFailureAtNode ( elseKeyword , Rule . ELSE _FAILURE _STRING ) ;
}
}
}
_super . prototype . visitIfStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitCatchClause = function ( node ) {
var catchClosingParen = Lint . childOfKind ( node , ts . SyntaxKind . CloseParenToken ) ;
var catchOpeningBrace = node . block . getChildAt ( 0 ) ;
this . handleOpeningBrace ( catchClosingParen , catchOpeningBrace ) ;
_super . prototype . visitCatchClause . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitTryStatement = function ( node ) {
var catchClause = node . catchClause ;
var finallyBlock = node . finallyBlock ;
var finallyKeyword = Lint . childOfKind ( node , ts . SyntaxKind . FinallyKeyword ) ;
// "visit" try block
var tryKeyword = node . getChildAt ( 0 ) ;
var tryBlock = node . tryBlock ;
var tryOpeningBrace = tryBlock . getChildAt ( 0 ) ;
this . handleOpeningBrace ( tryKeyword , tryOpeningBrace ) ;
if ( this . hasOption ( OPTION _CATCH ) && catchClause != null ) {
var tryClosingBrace = node . tryBlock . getChildAt ( node . tryBlock . getChildCount ( ) - 1 ) ;
var catchKeyword = catchClause . getChildAt ( 0 ) ;
var tryClosingBraceLine = this . getLineAndCharacterOfPosition ( tryClosingBrace . getEnd ( ) ) . line ;
var catchKeywordLine = this . getLineAndCharacterOfPosition ( catchKeyword . getStart ( ) ) . line ;
if ( tryClosingBraceLine !== catchKeywordLine ) {
this . addFailureAtNode ( catchKeyword , Rule . CATCH _FAILURE _STRING ) ;
}
}
if ( finallyBlock != null && finallyKeyword != null ) {
var finallyOpeningBrace = finallyBlock . getChildAt ( 0 ) ;
this . handleOpeningBrace ( finallyKeyword , finallyOpeningBrace ) ;
if ( this . hasOption ( OPTION _FINALLY ) ) {
var previousBlock = catchClause != null ? catchClause . block : node . tryBlock ;
var closingBrace = previousBlock . getChildAt ( previousBlock . getChildCount ( ) - 1 ) ;
var closingBraceLine = this . getLineAndCharacterOfPosition ( closingBrace . getEnd ( ) ) . line ;
var finallyKeywordLine = this . getLineAndCharacterOfPosition ( finallyKeyword . getStart ( ) ) . line ;
if ( closingBraceLine !== finallyKeywordLine ) {
this . addFailureAtNode ( finallyKeyword , Rule . FINALLY _FAILURE _STRING ) ;
}
}
}
_super . prototype . visitTryStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitForStatement = function ( node ) {
this . handleIterationStatement ( node ) ;
_super . prototype . visitForStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitForInStatement = function ( node ) {
this . handleIterationStatement ( node ) ;
_super . prototype . visitForInStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitWhileStatement = function ( node ) {
this . handleIterationStatement ( node ) ;
_super . prototype . visitWhileStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitBinaryExpression = function ( node ) {
var rightkind = node . right . kind ;
var opkind = node . operatorToken . kind ;
if ( opkind === ts . SyntaxKind . EqualsToken && rightkind === ts . SyntaxKind . ObjectLiteralExpression ) {
var equalsToken = node . getChildAt ( 1 ) ;
var openBraceToken = node . right . getChildAt ( 0 ) ;
this . handleOpeningBrace ( equalsToken , openBraceToken ) ;
}
_super . prototype . visitBinaryExpression . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitVariableDeclaration = function ( node ) {
var initializer = node . initializer ;
if ( initializer != null && initializer . kind === ts . SyntaxKind . ObjectLiteralExpression ) {
var equalsToken = Lint . childOfKind ( node , ts . SyntaxKind . EqualsToken ) ;
var openBraceToken = initializer . getChildAt ( 0 ) ;
this . handleOpeningBrace ( equalsToken , openBraceToken ) ;
}
_super . prototype . visitVariableDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitDoStatement = function ( node ) {
var doKeyword = node . getChildAt ( 0 ) ;
var statement = node . statement ;
if ( statement . kind === ts . SyntaxKind . Block ) {
var openBraceToken = statement . getChildAt ( 0 ) ;
this . handleOpeningBrace ( doKeyword , openBraceToken ) ;
}
_super . prototype . visitDoStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitModuleDeclaration = function ( node ) {
var nameNode = node . name ;
var body = node . body ;
if ( body != null && body . kind === ts . SyntaxKind . ModuleBlock ) {
var openBraceToken = body . getChildAt ( 0 ) ;
this . handleOpeningBrace ( nameNode , openBraceToken ) ;
}
_super . prototype . visitModuleDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitEnumDeclaration = function ( node ) {
var nameNode = node . name ;
var openBraceToken = Lint . childOfKind ( node , ts . SyntaxKind . OpenBraceToken ) ;
this . handleOpeningBrace ( nameNode , openBraceToken ) ;
_super . prototype . visitEnumDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitSwitchStatement = function ( node ) {
var closeParenToken = node . getChildAt ( 3 ) ;
var openBraceToken = node . caseBlock . getChildAt ( 0 ) ;
this . handleOpeningBrace ( closeParenToken , openBraceToken ) ;
_super . prototype . visitSwitchStatement . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitInterfaceDeclaration = function ( node ) {
this . handleClassLikeDeclaration ( node ) ;
_super . prototype . visitInterfaceDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitClassDeclaration = function ( node ) {
this . handleClassLikeDeclaration ( node ) ;
_super . prototype . visitClassDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitFunctionDeclaration = function ( node ) {
this . handleFunctionLikeDeclaration ( node ) ;
_super . prototype . visitFunctionDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitMethodDeclaration = function ( node ) {
this . handleFunctionLikeDeclaration ( node ) ;
_super . prototype . visitMethodDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitConstructorDeclaration = function ( node ) {
this . handleFunctionLikeDeclaration ( node ) ;
_super . prototype . visitConstructorDeclaration . call ( this , node ) ;
} ;
OneLineWalker . prototype . visitArrowFunction = function ( node ) {
var body = node . body ;
if ( body != null && body . kind === ts . SyntaxKind . Block ) {
var arrowToken = Lint . childOfKind ( node , ts . SyntaxKind . EqualsGreaterThanToken ) ;
var openBraceToken = body . getChildAt ( 0 ) ;
this . handleOpeningBrace ( arrowToken , openBraceToken ) ;
}
_super . prototype . visitArrowFunction . call ( this , node ) ;
} ;
OneLineWalker . prototype . handleFunctionLikeDeclaration = function ( node ) {
var body = node . body ;
if ( body != null && body . kind === ts . SyntaxKind . Block ) {
var openBraceToken = body . getChildAt ( 0 ) ;
if ( node . type != null ) {
this . handleOpeningBrace ( node . type , openBraceToken ) ;
}
else {
var closeParenToken = Lint . childOfKind ( node , ts . SyntaxKind . CloseParenToken ) ;
this . handleOpeningBrace ( closeParenToken , openBraceToken ) ;
}
}
} ;
OneLineWalker . prototype . handleClassLikeDeclaration = function ( node ) {
var openBraceToken = Lint . childOfKind ( node , ts . SyntaxKind . OpenBraceToken ) ;
2017-08-14 05:01:11 +02:00
this . handleOpeningBrace ( tsutils _1 . getPreviousToken ( openBraceToken ) , openBraceToken ) ;
2017-05-28 00:38:50 +02:00
} ;
OneLineWalker . prototype . handleIterationStatement = function ( node ) {
// last child is the statement, second to last child is the close paren
var closeParenToken = node . getChildAt ( node . getChildCount ( ) - 2 ) ;
var statement = node . statement ;
if ( statement . kind === ts . SyntaxKind . Block ) {
var openBraceToken = statement . getChildAt ( 0 ) ;
this . handleOpeningBrace ( closeParenToken , openBraceToken ) ;
}
} ;
OneLineWalker . prototype . handleOpeningBrace = function ( previousNode , openBraceToken ) {
if ( previousNode == null || openBraceToken == null ) {
return ;
}
var previousNodeLine = this . getLineAndCharacterOfPosition ( previousNode . getEnd ( ) ) . line ;
var openBraceLine = this . getLineAndCharacterOfPosition ( openBraceToken . getStart ( ) ) . line ;
var failure ;
if ( this . hasOption ( OPTION _BRACE ) && previousNodeLine !== openBraceLine ) {
failure = Rule . BRACE _FAILURE _STRING ;
}
else if ( this . hasOption ( OPTION _WHITESPACE ) && previousNode . getEnd ( ) === openBraceToken . getStart ( ) ) {
failure = Rule . WHITESPACE _FAILURE _STRING ;
}
if ( failure !== undefined ) {
this . addFailureAtNode ( openBraceToken , failure ) ;
}
} ;
return OneLineWalker ;
} ( Lint . RuleWalker ) ) ;
var _a ;