import * as Lint from 'tslint' ; import * as ts from 'typescript' ; export class Rule extends Lint . Rules . AbstractRule { public static FAILURE_STRING = 'arrow functions forbidden in global scope, use named function' ; public apply ( sourceFile ) { return this . applyWithFunction ( sourceFile , walk ) ; } } function walk ( ctx ) { return ts . forEachChild ( ctx . sourceFile , function cb ( node ) : void { if ( ts . isArrowFunction ( node ) && isGlobalScopeArrowFunction ( node ) ) { ctx . addFailureAtNode ( node , Rule . FAILURE_STRING ) ; } return ts . forEachChild ( node , cb ) ; } ) ; } function isGlobalScopeArrowFunction ( node ) { return checkParentChainType ( [ ts . SyntaxKind . ArrowFunction , ts . SyntaxKind . VariableDeclaration , ts . SyntaxKind . VariableDeclarationList , ts . SyntaxKind . VariableStatement , ts . SyntaxKind . SourceFile , ] , node ) ; } function checkParentChainType ( types , node ) { let temp = node ; for ( const t of types ) { if ( ! temp || ! ( temp . kind === t ) ) { return false ; } temp = temp . parent ; } return true ; }