diff --git a/changelog_unreleased/vue/14587.md b/changelog_unreleased/vue/14587.md
new file mode 100644
index 000000000..707e519f2
--- /dev/null
+++ b/changelog_unreleased/vue/14587.md
@@ -0,0 +1,25 @@
+#### Format TS expressions when any script tag has `lang="ts"` (#14587 by @seiyab)
+
+
+```vue
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+```
diff --git a/src/language-html/print-preprocess.js b/src/language-html/print-preprocess.js
index 789ab7e56..43533907b 100644
--- a/src/language-html/print-preprocess.js
+++ b/src/language-html/print-preprocess.js
@@ -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;
diff --git a/tests/format/vue/multiparser/__snapshots__/jsfmt.spec.js.snap b/tests/format/vue/multiparser/__snapshots__/jsfmt.spec.js.snap
index a921d3a0d..54599abe5 100644
--- a/tests/format/vue/multiparser/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/vue/multiparser/__snapshots__/jsfmt.spec.js.snap
@@ -70,6 +70,60 @@ export default {
================================================================================
`;
+exports[`lang-ts-multiple-script-tags.vue format 1`] = `
+====================================options=====================================
+parsers: ["vue"]
+printWidth: 80
+ | printWidth
+=====================================input======================================
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+
+
+=====================================output=====================================
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+
+
+================================================================================
+`;
+
exports[`lang-tsx.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
diff --git a/tests/format/vue/multiparser/lang-ts-multiple-script-tags.vue b/tests/format/vue/multiparser/lang-ts-multiple-script-tags.vue
new file mode 100644
index 000000000..d60027bb2
--- /dev/null
+++ b/tests/format/vue/multiparser/lang-ts-multiple-script-tags.vue
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ (x as number).toFixed(2) }}
+
+