Skip to content

Commit

Permalink
Avoid printing attribute per line in Vue SFC blocks (prettier#12895)
Browse files Browse the repository at this point in the history
* Avoid print single attribute perline for Vue SFC blocks

* Add tests

* Add changelog

* Fix typo

* Add more tests
  • Loading branch information
sosukesuzuki authored and medikoo committed Jan 4, 2024
1 parent 81d60b6 commit d0edf20
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 2 deletions.
19 changes: 19 additions & 0 deletions changelog_unreleased/vue/12895.md
@@ -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>
```
8 changes: 6 additions & 2 deletions src/language-html/print/tag.js
Expand Up @@ -17,6 +17,7 @@ const {
isPreLikeNode,
hasPrettierIgnore,
shouldPreserveContent,
isVueSfcBlock,
} = require("../utils/index.js");

function printClosingTag(node, options) {
Expand Down Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions src/language-html/utils/index.js
Expand Up @@ -674,6 +674,7 @@ module.exports = {
isVueScriptTag,
isVueSlotAttribute,
isVueSfcBindingsAttribute,
isVueSfcBlock,
isDanglingSpaceSensitiveNode,
isIndentationSensitiveNode,
isLeadingSpaceSensitiveNode,
Expand Down
@@ -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"]
Expand Down
29 changes: 29 additions & 0 deletions tests/format/vue/single-attribute-per-line/sfc-blocks.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>

0 comments on commit d0edf20

Please sign in to comment.