import _ts from 'typescript' ; import { createSourceFile } from '../../helpers' ; import { findTargetSyntaxKind } from '../../ast' ; export function parseEventNamesFromScrtipContent ( scriptContent , ts ) { const node = findEmits ( scriptContent , ts ) ; if ( ! node ) return [ ] ; if ( ts . isObjectLiteralExpression ( node ) ) { const emits = node . properties . map ( p => { if ( ts . isPropertyAssignment ( p ) && ts . isIdentifier ( p . name ) ) { const eventName = p . name . escapedText . toString ( ) ; return eventName ; } return '' ; } ) . filter ( Boolean ) ; return emits ; } else if ( ts . isArrayLiteralExpression ( node ) ) { const emits = node . elements . map ( e => { if ( ts . isStringLiteral ( e ) ) { const eventName = e . text ; return eventName ; } else { return '' ; } } ) . filter ( Boolean ) ; return emits ; } else { return [ ] ; } } function findEmits ( scriptContent , ts ) { const node = createSourceFile ( ts , scriptContent ) ; const exportAssignment = findTargetSyntaxKind ( ts , node , ts . SyntaxKind . ExportAssignment ) ; if ( ! exportAssignment ) { return undefined ; } const callExpression = findTargetSyntaxKind ( ts , exportAssignment , ts . SyntaxKind . CallExpression ) ; if ( ! callExpression ) { return undefined ; } const objectLiteralExpression = findTargetSyntaxKind ( ts , callExpression , ts . SyntaxKind . ObjectLiteralExpression ) ; if ( ! objectLiteralExpression ) { return undefined ; } function findEmitsProperty ( node ) { if ( ts . isPropertyAssignment ( node ) ) { const name = node . name ; if ( 'escapedText' in name && name . escapedText === 'emits' ) { return node ; } } return ts . forEachChild ( node , findEmitsProperty ) ; } const emitsProperty = findEmitsProperty ( objectLiteralExpression ) ; if ( ! emitsProperty ) return undefined ; return findTargetSyntaxKind ( ts , emitsProperty , ts . SyntaxKind . ObjectLiteralExpression ) || findTargetSyntaxKind ( ts , emitsProperty , ts . SyntaxKind . ArrayLiteralExpression ) ; }