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 TS expressions when any script tag has lang="ts" #14587

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions changelog_unreleased/vue/14587.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#### Format TS expressions when any script tag has `lang="ts"` (#14587 by @seiyab)

<!-- prettier-ignore -->
```vue
<!-- Input -->
<script></script>
<script setup lang="ts"></script>
<template>
{{ (x as number).toFixed(2) }}
</template>

<!-- Prettier stable -->
<script></script>
<script setup lang="ts"></script>
<template>
{{ (x as number).toFixed(2) }}
</template>

<!-- Prettier main -->
<script></script>
<script setup lang="ts"></script>
<template>
{{ (x as number).toFixed(2) }}
</template>
```
17 changes: 6 additions & 11 deletions src/language-html/print-preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,13 @@ function addIsSpaceSensitive(ast, options) {
}

function markTsScript(ast, options) {
if (options.parser === "vue") {
const vueScriptTag = ast.children.find((child) =>
isVueScriptTag(child, options)
options.__should_parse_vue_template_with_ts =
options.parser === "vue" &&
ast.children.some(
(child) =>
isVueScriptTag(child, options) &&
["ts", "typescript"].includes(child.attrMap.lang)
);
if (!vueScriptTag) {
return;
}
const { lang } = vueScriptTag.attrMap;
if (lang === "ts" || lang === "typescript") {
options.__should_parse_vue_template_with_ts = true;
}
}
}

module.exports = preprocess;
54 changes: 54 additions & 0 deletions tests/format/vue/multiparser/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,60 @@ export default {
================================================================================
`;

exports[`lang-ts-multiple-script-tags.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
printWidth: 80
| printWidth
=====================================input======================================
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script setup lang="ts">
let x: string | number = 1
</script>
<script></script>
<script></script>
<script></script>
<template>
<span
v-if=" (x as string).length > 0"
v-for="a in [1,2, 3,4,5].map( (x : number) => x * x)"
:foo=" (x as number).toFixed( 2) "
Comment on lines +93 to +95
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.

These aren't formatted in this branch due to #14432 .
Rebasing to next branch, they are formatted.
Feel free to ask me to

  • remove these lines
  • or, change base branch

Copy link
Member

Choose a reason for hiding this comment

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

We can ignore it.

>
{{ (x as number).toFixed(2) }}
</span>
</template>

=====================================output=====================================
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script setup lang="ts">
let x: string | number = 1;
</script>
<script></script>
<script></script>
<script></script>
<template>
<span
v-if=" (x as string).length > 0"
v-for="a in [1,2, 3,4,5].map( (x : number) => x * x)"
:foo=" (x as number).toFixed( 2) "
>
{{ (x as number).toFixed(2) }}
</span>
</template>

================================================================================
`;

exports[`lang-tsx.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
Expand Down
21 changes: 21 additions & 0 deletions tests/format/vue/multiparser/lang-ts-multiple-script-tags.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script></script>
<script setup lang="ts">
let x: string | number = 1
</script>
<script></script>
<script></script>
<script></script>
<template>
<span
v-if=" (x as string).length > 0"
v-for="a in [1,2, 3,4,5].map( (x : number) => x * x)"
:foo=" (x as number).toFixed( 2) "
>
{{ (x as number).toFixed(2) }}
</span>
</template>