Avoid printing attribute per line in Vue SFC blocks (#12895)

* Avoid print single attribute perline for Vue SFC blocks

* Add tests

* Add changelog

* Fix typo

* Add more tests
This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2022-06-04 23:25:27 +09:00
1 parent 83ec3c36e3
commit be9d63e543
5 files changed
+202 -2

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#### Avoid printing attribute per line in Vue SFC blocks (#12895 by @sosukesuzuki)
<!-- prettier-ignore -->
```vue
<!-- Input (singleAttributePerLine: true) -->
<script lang="ts" setup>
</script>
<!-- Prettier stable -->
<script
lang="ts"
setup
>
</script>
<!-- Prettier main -->
<script lang="ts" setup>
</script>
```
+6 -2
View File
@@ -17,6 +17,7 @@ const {
isPreLikeNode,
hasPrettierIgnore,
shouldPreserveContent,
isVueSfcBlock,
} = require("../utils/index.js");
function printClosingTag(node, options) {
@@ -251,8 +252,11 @@ function printAttributes(path, options, print) {
node.attrs[0].fullName === "src" &&
node.children.length === 0;
const attributeLine =
options.singleAttributePerLine && node.attrs.length > 1 ? hardline : line;
const shouldPrintAttributePerLine =
options.singleAttributePerLine &&
node.attrs.length > 1 &&
!isVueSfcBlock(node, options);
const attributeLine = shouldPrintAttributePerLine ? hardline : line;
/** @type {Doc[]} */
const parts = [
+1
View File
@@ -674,6 +674,7 @@ module.exports = {
isVueScriptTag,
isVueSlotAttribute,
isVueSfcBindingsAttribute,
isVueSfcBlock,
isDanglingSpaceSensitiveNode,
isIndentationSensitiveNode,
isLeadingSpaceSensitiveNode,
@@ -1,5 +1,152 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`sfc-blocks.vue - {"singleAttributePerLine":true} format 1`] = `
====================================options=====================================
parsers: ["vue"]
printWidth: 80
singleAttributePerLine: true
| printWidth
=====================================input======================================
<script setup lang="ts">
console.log("hello")
</script>
<style scoped lang="scss">
p {
color: red;
}
</style>
<unknown-block foo bar="bar" baz long_long_long_long_attribute></unknown-block>
<script lang="ts" src="./long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./long_long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./short">
</script>
<template lang="pug" src="./long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./long_long_long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./short">
</template>
=====================================output=====================================
<script setup lang="ts">
console.log("hello");
</script>
<style scoped lang="scss">
p {
color: red;
}
</style>
<unknown-block foo bar="bar" baz long_long_long_long_attribute></unknown-block>
<script lang="ts" src="./long_long_long_long_long_long_file_path.ts"></script>
<script
lang="ts"
src="./long_long_long_long_long_long_long_file_path.ts"
></script>
<script lang="ts" src="./short"></script>
<template lang="pug" src="./long_long_long_long_long_long_file_path.pug">
</template>
<template
lang="pug"
src="./long_long_long_long_long_long_long_long_file_path.pug"
>
</template>
<template lang="pug" src="./short">
</template>
================================================================================
`;
exports[`sfc-blocks.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
printWidth: 80
| printWidth
=====================================input======================================
<script setup lang="ts">
console.log("hello")
</script>
<style scoped lang="scss">
p {
color: red;
}
</style>
<unknown-block foo bar="bar" baz long_long_long_long_attribute></unknown-block>
<script lang="ts" src="./long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./long_long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./short">
</script>
<template lang="pug" src="./long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./long_long_long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./short">
</template>
=====================================output=====================================
<script setup lang="ts">
console.log("hello");
</script>
<style scoped lang="scss">
p {
color: red;
}
</style>
<unknown-block foo bar="bar" baz long_long_long_long_attribute></unknown-block>
<script lang="ts" src="./long_long_long_long_long_long_file_path.ts"></script>
<script
lang="ts"
src="./long_long_long_long_long_long_long_file_path.ts"
></script>
<script lang="ts" src="./short"></script>
<template lang="pug" src="./long_long_long_long_long_long_file_path.pug">
</template>
<template
lang="pug"
src="./long_long_long_long_long_long_long_long_file_path.pug"
>
</template>
<template lang="pug" src="./short">
</template>
================================================================================
`;
exports[`single-attribute-per-line.vue - {"singleAttributePerLine":true} format 1`] = `
====================================options=====================================
parsers: ["vue"]
@@ -0,0 +1,29 @@
<script setup lang="ts">
console.log("hello")
</script>
<style scoped lang="scss">
p {
color: red;
}
</style>
<unknown-block foo bar="bar" baz long_long_long_long_attribute></unknown-block>
<script lang="ts" src="./long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./long_long_long_long_long_long_long_file_path.ts">
</script>
<script lang="ts" src="./short">
</script>
<template lang="pug" src="./long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./long_long_long_long_long_long_long_long_file_path.pug">
</template>
<template lang="pug" src="./short">
</template>