license import * as Lint from "tslint" ; import { isCallExpression , isJsxAttribute , isJsxExpression } from "tsutils/typeguard/3.0" ; import * as ts from "typescript" ; export class Rule extends Lint . Rules . AbstractRule { public static metadata : Lint . IRuleMetadata = { ruleName : "jsx-no-bind" , description : Lint . Utils . dedent `Forbids function binding in JSX attributes. This has the same intent \
            as jsx-no-lambda in helping you avoid excessive re-renders.` , descriptionDetails : Lint . Utils . dedent `Note that this currently only does a simple syntactic check, \
            not a semantic one (it doesn't use the type checker). So it may \
            have some rare false positives if you define your own .bind function \
            and supply this as a parameter.` , options : null , optionsDescription : "" , optionExamples : [ "true" ] , type : "functionality" , typescriptOnly : false , } ; public static FAILURE_STRING = "Binds are forbidden in JSX attributes due to their rendering performance impact" ; public apply ( sourceFile ) { return this . applyWithFunction ( sourceFile , walk ) ; } } function walk ( ctx ) { return ts . forEachChild ( ctx . sourceFile , function cb ( node ) : void { if ( ! isJsxAttribute ( node ) ) { return ts . forEachChild ( node , cb ) ; } const initializer = node . initializer ; if ( initializer === undefined || ! isJsxExpression ( initializer ) ) { return ; } const { expression } = initializer ; if ( expression === undefined || ! isCallExpression ( expression ) || ! expression . getText ( ctx . sourceFile ) . includes ( ".bind(this)" ) ) { return ; } return ctx . addFailureAtNode ( expression , Rule . FAILURE_STRING ) ; } ) ; }