Skip to content

Commit

Permalink
WIP children tags rework
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 sveltejs#143 sveltejs#117
- generally try to be more in line with how prettier formats things

TODO:
- either get tag break logic on par with prettier (breaking > into new lines etc) or adjust tests
- more readable tag breaking logic for multiple children tags
- tidy up
  • Loading branch information
Simon Holthausen committed Nov 9, 2020
1 parent 68b4953 commit 88d31b5
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 240 deletions.
51 changes: 21 additions & 30 deletions src/print/doc-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { Doc, doc } from 'prettier';
import { findLastIndex } from './helpers';

export function isLine(doc: Doc) {
return typeof doc === 'object' && doc.type === 'line'
}
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
return isLine(doc) && !(doc as doc.builders.Line).keepIfLonely;
}

/**
* 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 +96,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) => boolean, items: T[]) {
for (let i = items.length - 1; i >= 0; i--) {
if (isMatch(items[i])) {
return i;
}
}

return -1;
}
Loading

0 comments on commit 88d31b5

Please sign in to comment.