Skip to content

Commit

Permalink
Children print rework (#160)
Browse files Browse the repository at this point in the history
Goals:

- switch logic around: prepare nodes before printing docs, try to avoid modifying printed result
- respect user's wish to have line breaks: fixes #143, fixes #117, closes #121
- generally try to be more in line with how prettier formats things

### BREAKING CHANGE
Tags are broken up differently now than before
  • Loading branch information
dummdidumm authored Jan 2, 2021
1 parent fe1e0be commit 8a59e40
Show file tree
Hide file tree
Showing 57 changed files with 1,034 additions and 288 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## Unreleased

* Rework of the tag breaking logic with the goal to be more in line with how Prettier formats HTML. This includes respecting the user's decision to have child tags in seperate lines even if they don't exceed the maximum line width ([#143](https://github.com/sveltejs/prettier-plugin-svelte/issues/143), [#117](https://github.com/sveltejs/prettier-plugin-svelte/issues/117)). This is a breaking change because tags are broken up differently now than before.

## 1.4.2

* Pass options to embedded parser ([#162](https://github.com/sveltejs/prettier-plugin-svelte/issues/162))
Expand Down
4 changes: 2 additions & 2 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { snippedTagContentAttribute } from './lib/snipTagContent';
import { PrintFn } from './print';
import {
getAttributeTextValue,
isNodeSupportedLanguage,
isIgnoreDirective,
getPreviousNode,
isIgnoreDirective,
isNodeSupportedLanguage,
} from './print/node-helpers';
import { Node } from './print/nodes';

Expand Down
54 changes: 22 additions & 32 deletions src/print/doc-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { Doc, doc } from 'prettier';
import { findLastIndex } from './helpers';

export function isLine(doc: Doc) {
return typeof doc === 'object' && doc.type === 'line'
}

export function isLineDiscardedIfLonely(doc: Doc) {
return isLine(doc) && !(doc as doc.builders.Line).keepIfLonely
export function isLine(docToCheck: Doc) {
return (
docToCheck === doc.builders.hardline ||
(typeof docToCheck === 'object' && docToCheck.type === 'line')
);
}

/**
* Check if the doc is empty, i.e. consists of nothing more than empty strings (possibly nested).
*/
export function isEmptyDoc(doc: Doc): boolean {
if (typeof doc === 'string') {
return doc.length === 0;
}
if (typeof doc === 'string') {
return doc.length === 0;
}

if (doc.type === 'line') {
return !doc.keepIfLonely;
}
if (doc.type === 'line') {
return !doc.keepIfLonely;
}

const { contents } = doc as { contents?: Doc };
const { contents } = doc as { contents?: Doc };

if (contents) {
return isEmptyDoc(contents);
}
if (contents) {
return isEmptyDoc(contents);
}

const { parts } = doc as { parts?: Doc[] };
const { parts } = doc as { parts?: Doc[] };

if (parts) {
return isEmptyGroup(parts);
}
if (parts) {
return isEmptyGroup(parts);
}

return false;
return false;
}

export function isEmptyGroup(group: Doc[]): boolean {
return !group.find(doc => !isEmptyDoc(doc))
return !group.find((doc) => !isEmptyDoc(doc));
}

/**
Expand Down Expand Up @@ -95,13 +95,3 @@ function getParts(doc: Doc): Doc[] | undefined {
return doc.parts;
}
}

function findLastIndex<T>(isMatch: (item: T) => boolean, items: T[]) {
for (let i = items.length - 1; i >= 0; i--) {
if (isMatch(items[i])) {
return i;
}
}

return -1;
}
10 changes: 10 additions & 0 deletions src/print/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ export function isPreTagContent(path: FastPath): boolean {
export function flatten<T>(arrays: T[][]): T[] {
return ([] as T[]).concat.apply([], arrays);
}

export function findLastIndex<T>(isMatch: (item: T, idx: number) => boolean, items: T[]) {
for (let i = items.length - 1; i >= 0; i--) {
if (isMatch(items[i], i)) {
return i;
}
}

return -1;
}
Loading

0 comments on commit 8a59e40

Please sign in to comment.