Format TS expressions when any script tag has lang="ts" (#14587)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Seiyaandfisker authored and GitHub committed 2023-03-27 10:47:54 +08:00
1 parent dd779dae55
commit 1bf47c1e95
4 files changed
+106 -11

No files matched your search

+25
View File
@@ -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>
```
+6 -11
View File
@@ -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;
@@ -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) "
>
{{ (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"]
@@ -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>