Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Children print rework #160

Merged
merged 26 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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