Format TS expressions when any script tag has lang="ts" (#14587)
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
1 parent
dd779dae55
commit
1bf47c1e95
4 files changed
+106
-11
No files matched your search
@@ -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>
|
||||||
|
```
|
||||||
@@ -411,18 +411,13 @@ function addIsSpaceSensitive(ast, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function markTsScript(ast, options) {
|
function markTsScript(ast, options) {
|
||||||
if (options.parser === "vue") {
|
options.__should_parse_vue_template_with_ts =
|
||||||
const vueScriptTag = ast.children.find((child) =>
|
options.parser === "vue" &&
|
||||||
isVueScriptTag(child, options)
|
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;
|
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`] = `
|
exports[`lang-tsx.vue format 1`] = `
|
||||||
====================================options=====================================
|
====================================options=====================================
|
||||||
parsers: ["vue"]
|
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>
|
||||||
Reference in new issue
Block a user