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

Format TypeScript expressions in Vue template attributes #14506

Merged
merged 23 commits into from Mar 24, 2023

Conversation

seiyab
Copy link
Sponsor Contributor

@seiyab seiyab commented Mar 14, 2023

Description

Fix #14432

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@seiyab seiyab marked this pull request as ready for review March 20, 2023 14:04
@seiyab seiyab requested a review from fisker March 20, 2023 14:04
@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 21, 2023

it's ready for review 🚀

@fisker
Copy link
Member

fisker commented Mar 21, 2023

It seems we still missing some places,

https://github.com/seiyab/prettier/blob/5870d8dd7fa159c175b805fff422f7d68cfd57ed/src/language-html/embed.js#L130-L134

This place seems using wrong parser?

https://github.com/seiyab/prettier/blob/5870d8dd7fa159c175b805fff422f7d68cfd57ed/src/language-html/syntax-vue.js#L81

Maybe we need babel-ts?

@fisker
Copy link
Member

fisker commented Mar 21, 2023

Can you change base to next branch? In next branch, we don't parse flow syntax in babel parser, should be easier to test.

@seiyab seiyab changed the base branch from main to next March 21, 2023 22:04
@seiyab seiyab force-pushed the 14432-vue-typescript-in-attributes branch 3 times, most recently from f14bf65 to 9bbb0d5 Compare March 21, 2023 23:28
@seiyab seiyab force-pushed the 14432-vue-typescript-in-attributes branch from 9bbb0d5 to b8a2fbb Compare March 22, 2023 03:44
@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 22, 2023

<template>
<MyComponent :src="100000000000000000000000000000000000000000000000000000000000000000000000000"/>
</template>
<script lang="ts"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can put all attributes in one component, and please add final_new_line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it's copied from tests/format/vue/vue/expression-binding.vue, can you put them together instead of move it to another dir. maybe tests/format/vue/vue/expression-binding-ts.vue?

@fisker
Copy link
Member

fisker commented Mar 22, 2023

Looks like v-for don't allow typescript syntax? Ah it does

@fisker
Copy link
Member

fisker commented Mar 22, 2023

Prettier pr-14506
Playground link

--parser vue

Input:

<script lang="ts"></script>

<template>
  <ul>
    <li v-for="a:number 
               of x as number">{{a}}</li>
  </ul>
</template>

Output:

<script lang="ts"></script>

<template>
  <ul>
    <li
      v-for="a:number 
               of x as number"
    >
      {{ a }}
    </li>
  </ul>
</template>

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 22, 2023

Thanks, I'll also fix v-for.
v-for seems here
https://github.com/seiyab/prettier/blob/6959f30a81d277c7bc6c45b68991dac01dc14c65/src/language-html/syntax-vue.js#L14

I think I need to fix following as you said before. I think it relates to v-slot.
https://github.com/seiyab/prettier/blob/5870d8dd7fa159c175b805fff422f7d68cfd57ed/src/language-html/syntax-vue.js#L81

However, v-slot with TS seems to work mysteriously. Maybe I'm misunderstanding... 🤔
Playground (stable)

Edit:
The reason why v-slot with type works on stable is maybe babel parser used flow plugin.
That's why you suggested to change base branch 👍

@seiyab

This comment was marked as outdated.

@seiyab

This comment was marked as outdated.

return printVueFor(getValue(), attributeTextToDoc);
return printVueFor(getValue(), attributeTextToDoc, {
ts: options.__should_parse_vue_template_with_ts,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pass options, we may need access other props in future.

v-for="([longLongProp, longLongProp, [longLongProp, longLongProp='Hello, Prettier!', [longLongProp, longLongProp, anotherLongLongProp=[longLongProp, longLongProp, anotherLongLongProp, yetAnotherLongLongProp], yetAnotherLongLongProp], yetAnotherLongLongProp], yetAnotherLongLongProp], index) of longLongLongLongLongLongLongLongList"
>
</div>
</template>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a new line at last.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I found that I had disabled editorconfig plugin mistakenly 😅

@fisker
Copy link
Member

fisker commented Mar 23, 2023

Looks great! Thank you for your hard work. I'll take another look tomorrow.

@fisker fisker merged commit e154250 into prettier:next Mar 24, 2023
26 checks passed
@fisker
Copy link
Member

fisker commented Mar 24, 2023

Nice work! Thanks.

You may interest #14575

@seiyab seiyab deleted the 14432-vue-typescript-in-attributes branch March 24, 2023 01:58
@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 24, 2023

Thank so much for your friendly supports.
I'll take a look after #14476 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Typescript code in Vue template is not formatted
2 participants