@@ -56,16 +56,31 @@ export class Rule extends Lint.Rules.AbstractRule {
56
56
57
57
class OnlyArrowFunctionsWalker extends Lint . RuleWalker {
58
58
public visitFunctionDeclaration ( node : ts . FunctionDeclaration ) {
59
- if ( ! node . asteriskToken && ! this . hasOption ( OPTION_ALLOW_DECLARATIONS ) ) {
60
- this . addFailure ( this . createFailure ( node . getStart ( ) , "function" . length , Rule . FAILURE_STRING ) ) ;
59
+ if ( ! this . hasOption ( OPTION_ALLOW_DECLARATIONS ) ) {
60
+ this . failUnlessExempt ( node ) ;
61
61
}
62
62
super . visitFunctionDeclaration ( node ) ;
63
63
}
64
64
65
65
public visitFunctionExpression ( node : ts . FunctionExpression ) {
66
- if ( ! node . asteriskToken ) {
66
+ this . failUnlessExempt ( node ) ;
67
+ super . visitFunctionExpression ( node ) ;
68
+ }
69
+
70
+ private failUnlessExempt ( node : ts . FunctionLikeDeclaration ) {
71
+ if ( ! functionIsExempt ( node ) ) {
67
72
this . addFailure ( this . createFailure ( node . getStart ( ) , "function" . length , Rule . FAILURE_STRING ) ) ;
68
73
}
69
- super . visitFunctionExpression ( node ) ;
70
74
}
71
75
}
76
+
77
+ /** Generator functions and functions explicitly declaring `this` are allowed. */
78
+ function functionIsExempt ( node : ts . FunctionLikeDeclaration ) {
79
+ return node . asteriskToken || hasThisParameter ( node ) ;
80
+ }
81
+
82
+ function hasThisParameter ( node : ts . FunctionLikeDeclaration ) {
83
+ const first = node . parameters [ 0 ] ;
84
+ return first && first . name . kind === ts . SyntaxKind . Identifier &&
85
+ ( first . name as ts . Identifier ) . originalKeywordKind === ts . SyntaxKind . ThisKeyword ;
86
+ }
0 commit comments