import type { NodePath } from '@babel/core' ; import * as t from '@babel/types' ; import { COMPILED_DIRECTIVE_DISABLE_LINE , COMPILED_DIRECTIVE_DISABLE_NEXT_LINE , COMPILED_DIRECTIVE_TRANSFORM_CSS_PROP , } from '../constants' ; import type { Metadata } from '../types' ; import { buildCompiledComponent } from '../utils/build-compiled-component' ; import { getNodeComments } from '../utils/comments' ; import { buildCss } from '../utils/css-builders' ; const getJsxAttributeExpression = ( node ) => { if ( t . isStringLiteral ( node . value ) ) { return node . value ; } if ( t . isJSXExpressionContainer ( node . value ) ) { return node . value . expression as t . Expression ; } throw new Error ( 'Value of JSX attribute was unexpected.' ) ; } ; const isCssPropDisabled = ( path , meta ) : boolean => { const { before , current } = getNodeComments ( path , meta ) ; return ( before . some ( ( comment ) => comment . value . trim ( ) . startsWith ( `${ COMPILED_DIRECTIVE_DISABLE_NEXT_LINE } ${ COMPILED_DIRECTIVE_TRANSFORM_CSS_PROP }` ) ) || current . some ( ( comment ) => comment . value . trim ( ) . startsWith ( `${ COMPILED_DIRECTIVE_DISABLE_LINE } ${ COMPILED_DIRECTIVE_TRANSFORM_CSS_PROP }` ) ) ) ; } ; param path { NodePath } param meta { Metadata } export const visitCssPropPath = ( path , meta ) : void => { let cssPropIndex = - 1 ; const cssProp = path . get ( 'attributes' ) . find ( ( attr , index ) : attr is NodePath < t . JSXAttribute > => { if ( t . isJSXAttribute ( attr . node ) && attr . node . name . name === 'css' ) { cssPropIndex = index ; return true ; } return false ; } ) ; if ( ! cssProp || ! cssProp . node . value ) { return ; } if ( isCssPropDisabled ( path , meta ) || isCssPropDisabled ( cssProp , meta ) ) { return ; } const cssOutput = buildCss ( getJsxAttributeExpression ( cssProp . node ) , meta ) ; path . node . attributes . splice ( cssPropIndex , 1 ) ; if ( ! cssOutput . css . length ) { return ; } path . parentPath . replaceWith ( buildCompiledComponent ( path . parentPath . node as t . JSXElement , cssOutput , meta ) ) ; } ;