JsxAttribute import * as ts from 'typescript' ; import { isJsxAttribute , isJsxExpression , isStringLiteral , isNumericLiteral , isJsxElement , isJsxSelfClosingElement , isJsxOpeningElement , isFalseKeyword , isTrueKeyword , } from './TypeGuard' ; returns { [ propName : string ] export function getJsxAttributesFromJsxElement ( node ) { const attributesDictionary = { } ; getAllAttributesFromJsxElement ( node ) . forEach ( attr => { if ( isJsxAttribute ( attr ) ) { attributesDictionary [ getPropName ( attr ) . toLowerCase ( ) ] = attr ; } } ) ; return attributesDictionary ; } /**
 * Get an array of attributes in the given node.
 * It contains JsxAttribute and JsxSpreadAttribute.
 */ export function getAllAttributesFromJsxElement ( node ) { let attributes ; if ( node == null ) { return < ts . NodeArray < ts . JsxAttributeLike > > ( < any > [ ] ) ; } else if ( isJsxElement ( node ) ) { attributes = node . openingElement . attributes . properties ; } else if ( isJsxSelfClosingElement ( node ) ) { attributes = node . attributes . properties ; } else if ( isJsxOpeningElement ( node ) ) { attributes = node . attributes . properties ; } else { throw new Error ( 'The node must be a JsxElement, JsxSelfClosingElement or JsxOpeningElement.' ) ; } return attributes ; } export function getPropName ( node ) { if ( ! isJsxAttribute ( node ) ) { throw new Error ( 'The node must be a JsxAttribute collected by the AST parser.' ) ; } return node . name ? node . name . text : undefined ; } param code param exceptTagName return { ts . JsxElement | ts . JsxSelfClosingElement } export function getJsxElementFromCode ( code , exceptTagName ) { const sourceFile = ts . createSourceFile ( 'test.tsx' , code , ts . ScriptTarget . ES2015 , true ) ; return delintNode ( sourceFile , exceptTagName ) ; } function delintNode ( node , tagName ) { if ( isJsxElement ( node ) && node . openingElement . tagName . getText ( ) === tagName ) { return node ; } else if ( isJsxSelfClosingElement ( node ) && node . tagName . getText ( ) === tagName ) { return node ; } else if ( ! node || node . getChildCount ( ) === 0 ) { return undefined ; } return ts . forEachChild ( node , ( childNode ) => delintNode ( childNode , tagName ) ) ; } return { ts . JsxElement } export function getAncestorNode ( node , ancestorTagName ) { if ( ! node ) { return undefined ; } const ancestorNode = node . parent ; if ( isJsxElement ( ancestorNode ) && ancestorNode . openingElement . tagName . getText ( ) === ancestorTagName ) { return ancestorNode ; } else { return getAncestorNode ( ancestorNode , ancestorTagName ) ; } } example example example export function getBooleanLiteral ( node ) { if ( ! isJsxAttribute ( node ) ) { throw new Error ( 'The node must be a JsxAttribute collected by the AST parser.' ) ; } const initializer = node == null ? null : node . initializer ; const getBooleanFromString = ( value ) => { if ( value . toLowerCase ( ) === 'true' ) { return true ; } else if ( value . toLowerCase ( ) === 'false' ) { return false ; } else { return undefined ; } } ; if ( isStringLiteral ( initializer ) ) { return getBooleanFromString ( initializer . text ) ; } else if ( isJsxExpression ( initializer ) ) { const expression = initializer . expression ; if ( isStringLiteral ( expression ) ) { return getBooleanFromString ( expression . text ) ; } else { if ( isTrueKeyword ( expression ) ) { return true ; } else if ( isFalseKeyword ( expression ) ) { return false ; } else { return undefined ; } } } return false ; } example export function getNumericLiteral ( node ) { if ( ! isJsxAttribute ( node ) ) { throw new Error ( 'The node must be a JsxAttribute collected by the AST parser.' ) ; } const initializer = node == null ? null : node . initializer ; return isJsxExpression ( initializer ) && isNumericLiteral ( initializer . expression ) ? ( < ts . LiteralExpression > initializer . expression ) . text : undefined ; } example example export function getStringLiteral ( node ) { if ( ! isJsxAttribute ( node ) ) { throw new Error ( 'The node must be a JsxAttribute collected by the AST parser.' ) ; } const initializer = node == null ? null : node . initializer ; if ( ! initializer ) { return '' ; } else if ( isStringLiteral ( initializer ) ) { return initializer . text . trim ( ) ; } else if ( isJsxExpression ( initializer ) && isStringLiteral ( initializer . expression ) ) { return ( < ts . StringLiteral > initializer . expression ) . text ; } else if ( isJsxExpression ( initializer ) && ! initializer . expression ) { return '' ; } else { return undefined ; } } export function isEmpty ( node ) { const initializer = node == null ? null : node . initializer ; if ( initializer == null ) { return true ; } else if ( isStringLiteral ( initializer ) ) { return initializer . text . trim ( ) === '' ; } else if ( initializer . kind === ts . SyntaxKind . Identifier ) { return initializer . getText ( ) === 'undefined' ; } else if ( initializer . kind === ts . SyntaxKind . NullKeyword ) { return true ; } else if ( ( < any > initializer ) . expression != null ) { const expression = ( < any > initializer ) . expression ; if ( expression . kind === ts . SyntaxKind . Identifier ) { return expression . getText ( ) === 'undefined' ; } else if ( expression . kind === ts . SyntaxKind . NullKeyword ) { return true ; } } return false ; }