Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of React JSX Props on newlines fails #20

Open
karlhorky opened this issue Oct 22, 2020 · 20 comments
Open

Conversion of React JSX Props on newlines fails #20

karlhorky opened this issue Oct 22, 2020 · 20 comments
Labels
bug Something isn't working

Comments

@karlhorky
Copy link

JSX props (React) on their own lines do not convert correctly to template strings:

function Div(props) {
  return (
    <div
      {...props}
      prop-on-new-line="abc"
      // comment
      another-on-new-line="def"
    />
  );
}

Kapture 2020-10-22 at 10 55 51

@karlhorky
Copy link
Author

This is a known issue with the extension (see #15 (comment) for background)

@karlhorky karlhorky changed the title React JSX Props on newlines fail Conversion of React JSX Props on newlines fails Oct 22, 2020
@meganrogge
Copy link
Owner

recording (78)

pushing this change soon after doing some more testing @karlhorky

meganrogge added a commit that referenced this issue Dec 14, 2020
@karlhorky
Copy link
Author

Looks really good for this example @meganrogge , thanks!! 🙌

@karlhorky
Copy link
Author

karlhorky commented Dec 20, 2020

Another unhandled case is returning to the JavaScript rules once inside of JSX opening tags.

Inside of opening tags in JSX, there may be the use of JavaScript expressions (starting with an opening curly bracket and ending with a closing curly). Since we back in regular JavaScript, the special JSX opening tag rules / behavior should not apply.

One common example is usage of a style prop:

Kapture 2020-12-20 at 19 10 48

<Link
  to="/"
  style={{
    float: 'left',
  }}
></Link>

The result of editing 'left' here is float: {`left${}`}, should be float: `left${}`


Another example would be no prop at all, using object spread:

<Link
  {...{
    a: 'b'
  }}
/>

The result of editing 'b' here is a: {`b${}`}, should be a: `b${}`


Both of these examples with objects fail when editing values or also any keys which are in the form of strings (eg. editing display here: <Link style={{"display": "block"}} />).


Also, an expression as a standalone string (no usage of an object):

<Link
  href={'abc'}
/>

The result of editing 'abc' here is href={{`abc${}`}}, should be href={`abc${}`}


This also happens on single lines:

<Link href={'abc'} />

The result of editing 'abc' here is href={{`abc${}`}}, should be href={`abc${}`}


Or a different expression (an admittedly unusual pattern):

<Link
  href={href='abc'}
/>

The result of editing 'abc' here is href={href={`abc${}`}}, should be href={href=`abc${}`}


All of these fail when adding interpolation in the string.

New issue again?

@meganrogge meganrogge reopened this Dec 20, 2020
@meganrogge
Copy link
Owner

@karlhorky thanks for the examples. reopened this issue and will look into it.

@meganrogge
Copy link
Owner

meganrogge commented Dec 24, 2020

@karlhorky after re-reading this, I'm kind of confused. Are you saying in the example you provided it shouldn't convert to a JSX prop, even though it's following an = sign and is within a start and end tag? What is the difference here? Do you have a suggestion for how I can tell if it's within normal JavaScript instead of a JSX prop?

@karlhorky
Copy link
Author

karlhorky commented Dec 28, 2020

Sorry, I have updated the examples above to include what happens now and what should happen.

The main takeaway here can be the following rules:

  1. as soon as you hit <\w+\s+ (approximate, incomplete regex) in a JavaScript file, you're inside a JSX opening tag (and the special rules for props in JSX opening tags are activated)
  2. as soon as you hit { inside that tag, you are in a JavaScript expression, with normal JavaScript rules (until you hit a < again, recursively - which goes back to 1.)

The 1st rule above is being applied correctly, but the 2nd - going back to JavaScript rules within that opening tag - is not.

In case you're using regex, there are a couple of regular expression implementations for JSX opening tags and JavaScript expression in Prism.js and VS Code that may help:

@meganrogge
Copy link
Owner

Hoping to get to this soon

@meganrogge meganrogge added bug Something isn't working good first issue Good for newcomers labels Oct 27, 2021
@meganrogge
Copy link
Owner

Having issues publishing the extension atm, but hopefully will be able to do so soon and thus reflect this fix.

@karlhorky
Copy link
Author

Great! When there's a new version, I'll test it out :)

@meganrogge
Copy link
Owner

It has been released so lmk @karlhorky

@karlhorky
Copy link
Author

ok trying on v0.5.4 now

@karlhorky
Copy link
Author

karlhorky commented Oct 31, 2021

The result of editing 'left' here is float: {`left${}`}, should be float: `left${}`

<Link
  to="/"
  style={{
    float: 'left',
  }}
></Link>

Hm, seems like this one is still open... Just tried in both a .tsx file and a .js file (both set as JavaScript and JavaScript (React) syntaxes)


didn't test the other cases yet in my comment above

@meganrogge
Copy link
Owner

Worked well for most of your examples when I tested it. Seems like there are a lot of cases to cover

@karlhorky
Copy link
Author

Hm, interesting... for me, I haven't seen a single one that does work. Maybe the publish didn't work properly?

For example, here's another one that fails (which is a very common pattern in React code):

The result of editing 'abc' here is href={{`abc${}`}}, should be href={`abc${}`}

<Link href={'abc'} />

Should this issue be reopened? Or should I create a new issue with the content of my comment above?

@meganrogge meganrogge reopened this Nov 1, 2021
@meganrogge
Copy link
Owner

feel free to make a PR @karlhorky i'm quite occupied working on vscode at the moment, so could take me awhile to get to this

@karlhorky
Copy link
Author

In Template String Converter 0.5.8 it seems like there's a new bug which is causing extra curly braces to appear outside of JSX code (in a file with TypeScript React syntax):

Kapture.2021-12-29.at.17.00.15.mp4

Opened #61 for this.

@meganrogge meganrogge removed the good first issue Good for newcomers label Dec 29, 2021
@meganrogge
Copy link
Owner

I just noticed that in your original example, if you delete the comment line, it will work

@meganrogge
Copy link
Owner

meganrogge commented Dec 30, 2021

that's a case I don't have time for at the moment, but about to push a fix for the other cases you mention in this issue.

Done

meganrogge added a commit that referenced this issue Dec 30, 2021
@hunterwilhelm
Copy link

hunterwilhelm commented Nov 19, 2024

For anyone here looking for what the setting was named:

Template string converter add brackets to props

or in your JSON

"template-string-converter.addBracketsToProps": true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants