license import * as Lint from "tslint" ; import { isJsxAttribute , isJsxExpression , isStringLiteral , isTemplateExpression } 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-string-ref" , description : "Checks for string literal refs" , descriptionDetails : "This syntax is deprecated and will be removed in a future version of React" , options : null , optionsDescription : "" , optionExamples : [ "true" ] , type : "style" , typescriptOnly : false , } ; public static FAILURE_STRING = "Pass a callback to ref prop instead of a string literal" ; public apply ( sourceFile ) { return this . applyWithFunction ( sourceFile , walk ) ; } } function walk ( ctx ) { return ts . forEachChild ( ctx . sourceFile , function cb ( node ) : void { if ( isJsxAttribute ( node ) ) { const { name , initializer } = node ; const isRefAttribute = name . text === "ref" ; if ( isRefAttribute && initializer !== undefined ) { const hasStringInitializer = initializer . kind === ts . SyntaxKind . StringLiteral ; const hasStringExpressionInitializer = isJsxExpression ( initializer ) && initializer . expression !== undefined && ( isStringLiteral ( initializer . expression ) || isTemplateExpression ( initializer . expression ) ) ; if ( hasStringInitializer || hasStringExpressionInitializer ) { ctx . addFailureAtNode ( initializer , Rule . FAILURE_STRING ) ; } } } return ts . forEachChild ( node , cb ) ; } ) ; }