license import * as Lint from "tslint" ; import { isJsxSelfClosingElement } from "tsutils/typeguard/3.0" ; import * as ts from "typescript" ; export class Rule extends Lint . Rules . AbstractRule { public static metadata : Lint . IRuleMetadata = { ruleName : "jsx-space-before-trailing-slash" , description : "Checks that self-closing JSX elements have a space before the '/>' part." , options : null , optionsDescription : "" , optionExamples : [ "true" ] , type : "style" , typescriptOnly : false , } ; public static FAILURE_STRING = "Self-closing JSX elements must have a space before the '/>' part" ; public apply ( sourceFile ) { return this . applyWithFunction ( sourceFile , walk ) ; } } const closingLength = "/>" . length ; const isWhiteSpace = ( char ) => /\s/ . test ( char ) ; const hasWhitespaceBeforeClosing = ( nodeText ) => isWhiteSpace ( nodeText . charAt ( nodeText . length - closingLength - 1 ) ) ; function walk ( ctx ) { return ts . forEachChild ( ctx . sourceFile , function cb ( node ) : void { if ( isJsxSelfClosingElement ( node ) ) { if ( ! hasWhitespaceBeforeClosing ( node . getText ( ctx . sourceFile ) ) ) { const fix = Lint . Replacement . appendText ( node . getEnd ( ) - closingLength , " " ) ; ctx . addFailureAtNode ( node , Rule . FAILURE_STRING , fix ) ; } } return ts . forEachChild ( node , cb ) ; } ) ; }