react-ast icon indicating copy to clipboard operation
react-ast copied to clipboard

How to render literals inside an expession?

Open cah4a opened this issue 2 years ago • 6 comments

There is no Literal component, which I expected from that library.

Tried to use Smart component:

<CallExpression
    name="fn"
    arguments={
        <Smart code="'a'"/>
    }/>

that gave totally unexpected output for me: fn('a';);

cah4a avatar Nov 17 '23 10:11 cah4a

Ok, I'll see if I can add that feature to this.

clayrisser avatar Nov 22 '23 20:11 clayrisser

@cah4a We've made a small but important update in react-ast version 0.4.1

<CallExpression 
           name="fn" 
           arguments={ 
    <StringLiteral>a</StringLiteral>
  } 
/>

dharmendraboddeda avatar Nov 28 '23 04:11 dharmendraboddeda

That's cool! Thanks!

BooleanLiterals, BigIntLiteral, RegExpLiteral might be useful too. Maybe we could have the Literal Component with value: unknown, so it will render whatever you pass in?

cah4a avatar Nov 28 '23 06:11 cah4a

@cah4a can you explain what you mean by value: unknown. You mean like a <Smart /> component, but one that doesn't add the semi-colon?

clayrisser avatar Nov 28 '23 06:11 clayrisser

I mean something like this:

<CallExpression
    name="fn"
    arguments={
        <Literal value={argument}/>
    }/>

where an argument could be string, number, boolean, bigint, object, symbol, null, or undefined.

I have a feeling the library should be able to render any javascript code, not a subset.

cah4a avatar Nov 28 '23 06:11 cah4a

Gotcha, yeah I think we can make that work. We just finished a big refactor to update all the modules to the latest. Should be able to get to this soon.

clayrisser avatar Nov 28 '23 19:11 clayrisser