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" ) ;
2017-12-10 21:51:33 +01:00
var defaultOptions = fillOptions ( "ignore" ) ;
2017-05-28 00:38:50 +02:00
function fillOptions ( value ) {
return {
arrays : value ,
exports : value ,
functions : value ,
imports : value ,
objects : value ,
typeLiterals : value ,
} ;
}
function normalizeOptions ( options ) {
2017-12-10 21:51:33 +01:00
return { multiline : normalize ( options . multiline ) , singleline : normalize ( options . singleline ) , specCompliant : ! ! options . esSpecCompliant } ;
}
function normalize ( value ) {
return typeof value === "string" ? fillOptions ( value ) : tslib _1 . _ _assign ( { } , defaultOptions , value ) ;
2017-05-28 00:38:50 +02:00
}
/* tslint:disable:object-literal-sort-keys */
var metadataOptionShape = {
anyOf : [
{
type : "string" ,
enum : [ "always" , "never" ] ,
} ,
{
type : "object" ,
properties : fillOptions ( {
type : "string" ,
enum : [ "always" , "never" , "ignore" ] ,
} ) ,
} ,
] ,
} ;
/* tslint:enable:object-literal-sort-keys */
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 ) {
var options = normalizeOptions ( this . ruleArguments [ 0 ] ) ;
return this . applyWithWalker ( new TrailingCommaWalker ( sourceFile , this . ruleName , options ) ) ;
} ;
Rule . prototype . isEnabled = function ( ) {
return _super . prototype . isEnabled . call ( this ) && this . ruleArguments . length !== 0 ;
} ;
2017-08-14 05:01:11 +02:00
/* tslint:disable:object-literal-sort-keys */
Rule . metadata = {
ruleName : "trailing-comma" ,
description : ( _a = [ "\n Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings,\n named imports and exports and function parameters." ] , _a . raw = [ "\n Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings,\n named imports and exports and function parameters." ] , Lint . Utils . dedent ( _a ) ) ,
hasFix : true ,
2017-12-10 21:51:33 +01:00
optionsDescription : ( _b = [ "\n One argument which is an object with the keys `multiline` and `singleline`.\n Both can be set to a string (`\"always\"` or `\"never\"`) or an object.\n\n The object can contain any of the following keys: `\"arrays\"`, `\"objects\"`, `\"functions\"`,\n `\"imports\"`, `\"exports\"`, and `\"typeLiterals\"`; each key can have one of the following\n values: `\"always\"`, `\"never\"`, and `\"ignore\"`. Any missing keys will default to `\"ignore\"`.\n\n * `\"multiline\"` checks multi-line object literals.\n * `\"singleline\"` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters.\n\n To align this rule with the ECMAScript specification that is implemented in modern JavaScript VMs,\n there is a third option `esSpecCompliant`. Set this option to `true` to disallow trailing comma on\n object and array rest and rest parameters.\n " ] , _b . raw = [ "\n One argument which is an object with the keys \\`multiline\\` and \\`singleline\\`.\n Both can be set to a string (\\`\"always\"\\` or \\`\"never\"\\`) or an object.\n\n The object can contain any of the following keys: \\`\"arrays\"\\`, \\`\"objects\"\\`, \\`\"functions\"\\`,\n \\`\"imports\"\\`, \\`\"exports\"\\`, and \\`\"typeLiterals\"\\`; each key can have one of the following\n values: \\`\"always\"\\`, \\`\"never\"\\`, and \\`\"ignore\"\\`. Any missing keys will default to \\`\"ignore\"\\`.\n\n * \\`\"multiline\"\\` checks multi-line object literals.\n * \\`\"singleline\"\\` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters.\n\n To align this rule with the ECMAScript specification that is implemented in modern JavaScript VMs,\n there is a third option \\`esSpecCompliant\\`. Set this option to \\`true\\` to disallow trailing comma on\n object and array rest and rest parameters.\n " ] , Lint . Utils . dedent ( _b ) ) ,
2017-08-14 05:01:11 +02:00
options : {
type : "object" ,
properties : {
multiline : metadataOptionShape ,
singleline : metadataOptionShape ,
2017-12-10 21:51:33 +01:00
esSpecCompliant : { type : "boolean" } ,
2017-08-14 05:01:11 +02:00
} ,
additionalProperties : false ,
2017-05-28 00:38:50 +02:00
} ,
2017-08-14 05:01:11 +02:00
optionExamples : [
[ true , { multiline : "always" , singleline : "never" } ] ,
[
true ,
{
multiline : {
objects : "always" ,
arrays : "always" ,
functions : "never" ,
typeLiterals : "ignore" ,
} ,
2017-12-10 21:51:33 +01:00
esSpecCompliant : true ,
2017-05-28 00:38:50 +02:00
} ,
2017-08-14 05:01:11 +02:00
] ,
2017-05-28 00:38:50 +02:00
] ,
2017-08-14 05:01:11 +02:00
type : "maintainability" ,
typescriptOnly : false ,
} ;
/* tslint:enable:object-literal-sort-keys */
Rule . FAILURE _STRING _NEVER = "Unnecessary trailing comma" ;
2017-12-10 21:51:33 +01:00
Rule . FAILURE _STRING _FORBIDDEN = "Forbidden trailing comma" ;
2017-08-14 05:01:11 +02:00
Rule . FAILURE _STRING _ALWAYS = "Missing trailing comma" ;
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
2017-05-28 00:38:50 +02:00
exports . Rule = Rule ;
2017-10-14 18:40:54 +02:00
var TrailingCommaWalker = /** @class */ ( function ( _super ) {
2017-05-28 00:38:50 +02:00
tslib _1 . _ _extends ( TrailingCommaWalker , _super ) ;
function TrailingCommaWalker ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
TrailingCommaWalker . prototype . walk = function ( sourceFile ) {
var _this = this ;
var cb = function ( node ) {
switch ( node . kind ) {
case ts . SyntaxKind . ArrayLiteralExpression :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . elements , node . end , "arrays" , isArrayRest ) ;
break ;
2017-05-28 00:38:50 +02:00
case ts . SyntaxKind . ArrayBindingPattern :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . elements , node . end , "arrays" , isDestructuringRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . ObjectBindingPattern :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . elements , node . end , "objects" , isDestructuringRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . NamedImports :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . elements , node . end , "imports" , noRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . NamedExports :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . elements , node . end , "exports" , noRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . ObjectLiteralExpression :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . properties , node . end , "objects" , isObjectRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . EnumDeclaration :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . members , node . end , "objects" , noRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . NewExpression :
if ( node . arguments === undefined ) {
break ;
}
// falls through
case ts . SyntaxKind . CallExpression :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . arguments , node . end , "functions" , noRest ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . ArrowFunction :
2017-12-10 21:51:33 +01:00
// don't check arrow functions without parens around the parameter
if ( tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . OpenParenToken , _this . sourceFile ) === undefined ) {
break ;
}
// falls through
2017-05-28 00:38:50 +02:00
case ts . SyntaxKind . Constructor :
case ts . SyntaxKind . FunctionDeclaration :
case ts . SyntaxKind . FunctionExpression :
case ts . SyntaxKind . MethodDeclaration :
case ts . SyntaxKind . SetAccessor :
case ts . SyntaxKind . MethodSignature :
case ts . SyntaxKind . ConstructSignature :
case ts . SyntaxKind . ConstructorType :
case ts . SyntaxKind . FunctionType :
case ts . SyntaxKind . CallSignature :
2017-12-10 21:51:33 +01:00
_this . checkList ( node . parameters , tsutils _1 . getChildOfKind ( node , ts . SyntaxKind . CloseParenToken , _this . sourceFile ) . end , "functions" , isRestParameter ) ;
2017-05-28 00:38:50 +02:00
break ;
case ts . SyntaxKind . TypeLiteral :
_this . checkTypeLiteral ( node ) ;
break ;
default :
}
return ts . forEachChild ( node , cb ) ;
} ;
return ts . forEachChild ( sourceFile , cb ) ;
} ;
TrailingCommaWalker . prototype . checkTypeLiteral = function ( node ) {
var members = node . members ;
if ( members . length === 0 ) {
return ;
}
var sourceText = this . sourceFile . text ;
for ( var _i = 0 , members _1 = members ; _i < members _1 . length ; _i ++ ) {
var member = members _1 [ _i ] ;
// PropertySignature in TypeLiteral can end with semicolon or comma. If one ends with a semicolon don't check for trailing comma
if ( sourceText [ member . end - 1 ] === ";" ) {
return ;
}
}
// The trailing comma is part of the last member and therefore not present as hasTrailingComma on the NodeArray
var hasTrailingComma = sourceText [ members . end - 1 ] === "," ;
2017-12-10 21:51:33 +01:00
return this . checkComma ( hasTrailingComma , members , node . end , "typeLiterals" , noRest ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-12-10 21:51:33 +01:00
TrailingCommaWalker . prototype . checkList = function ( list , closeElementPos , optionKey , isRest ) {
2017-05-28 00:38:50 +02:00
if ( list . length === 0 ) {
return ;
}
2017-12-10 21:51:33 +01:00
return this . checkComma ( list . hasTrailingComma , list , closeElementPos , optionKey , isRest ) ;
2017-05-28 00:38:50 +02:00
} ;
2017-12-10 21:51:33 +01:00
/* Expects `list.length !== 0` */
TrailingCommaWalker . prototype . checkComma = function ( hasTrailingComma , list , closeTokenPos , optionKey , isRest ) {
var last = list [ list . length - 1 ] ;
if ( this . options . specCompliant && isRest ( last ) ) {
if ( hasTrailingComma ) {
this . addFailureAt ( list . end - 1 , 1 , Rule . FAILURE _STRING _FORBIDDEN , Lint . Replacement . deleteText ( list . end - 1 , 1 ) ) ;
}
2017-05-28 00:38:50 +02:00
return ;
}
2017-12-10 21:51:33 +01:00
var options = tsutils _1 . isSameLine ( this . sourceFile , last . end , closeTokenPos )
2017-05-28 00:38:50 +02:00
? this . options . singleline
: this . options . multiline ;
var option = options [ optionKey ] ;
if ( option === "always" && ! hasTrailingComma ) {
this . addFailureAt ( list . end , 0 , Rule . FAILURE _STRING _ALWAYS , Lint . Replacement . appendText ( list . end , "," ) ) ;
}
else if ( option === "never" && hasTrailingComma ) {
this . addFailureAt ( list . end - 1 , 1 , Rule . FAILURE _STRING _NEVER , Lint . Replacement . deleteText ( list . end - 1 , 1 ) ) ;
}
} ;
return TrailingCommaWalker ;
} ( Lint . AbstractWalker ) ) ;
2017-12-10 21:51:33 +01:00
function isRestParameter ( node ) {
return node . dotDotDotToken !== undefined ;
}
function isDestructuringRest ( node ) {
return node . kind === ts . SyntaxKind . BindingElement && node . dotDotDotToken !== undefined ;
}
function isObjectRest ( node ) {
return node . kind === ts . SyntaxKind . SpreadAssignment && tsutils _1 . isReassignmentTarget ( node . expression ) ;
}
function isArrayRest ( node ) {
return node . kind === ts . SyntaxKind . SpreadElement && tsutils _1 . isReassignmentTarget ( node ) ;
}
function noRest ( ) {
return false ;
}
2017-05-28 00:38:50 +02:00
var _a , _b ;