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

fix: implement elements whose children should always have lines between them #399

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"editor.formatOnPaste": false,
"editor.formatOnType": false,
"editor.codeActionsOnSave": {
"source.organizeImports": false
"source.organizeImports": "never"
}
}
}
2 changes: 2 additions & 0 deletions src/printer/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export const blockElements: TagName[] = [
'html',
];

export const breakChildrenElements: TagName[] = ['html', 'head', 'body', 'ul', 'ol', 'select'];

/**
* HTML attributes that we may safely reformat (trim whitespace, add or remove newlines)
*/
Expand Down
3 changes: 3 additions & 0 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getPreferredQuote,
getUnencodedText,
hasSetDirectives,
isBreakChildrenElement,
isEmptyTextNode,
isIgnoreDirective,
isInlineElement,
Expand Down Expand Up @@ -182,6 +183,8 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
body = () => printRaw(node);
} else if (isInlineElement(path, opts, node) && !isPreTagContent(path)) {
body = () => path.map(print, 'children');
} else if (isBreakChildrenElement(node)) {
body = () => [breakParent, ...path.map(print, 'children')];
} else {
body = () => path.map(print, 'children');
}
Expand Down
15 changes: 12 additions & 3 deletions src/printer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
type Doc,
type ParserOptions as ParserOpts,
} from 'prettier';
import { blockElements, formattableAttributes, type TagName } from './elements';
import {
blockElements,
breakChildrenElements,
formattableAttributes,
type TagName,
} from './elements';
import type {
CommentNode,
ExpressionNode,
Expand All @@ -30,6 +35,10 @@ export function isInlineElement(path: AstPath, opts: ParserOptions, node: anyNod
return node && isTagLikeNode(node) && !isBlockElement(node, opts) && !isPreTagContent(path);
}

export function isBreakChildrenElement(node: anyNode): boolean {
return node && node.type === 'element' && breakChildrenElements.includes(node.name as TagName);
}

export function isBlockElement(node: anyNode, opts: ParserOptions): boolean {
return (
node &&
Expand Down Expand Up @@ -120,7 +129,7 @@ export function hasSetDirectives(node: TagLikeNode) {
* no whitespace between the `>` and the first child.
*/
export function shouldHugStart(node: anyNode, opts: ParserOptions): boolean {
if (isBlockElement(node, opts)) {
if (isBlockElement(node, opts) || isBreakChildrenElement(node)) {
return false;
}

Expand All @@ -142,7 +151,7 @@ export function shouldHugStart(node: anyNode, opts: ParserOptions): boolean {
* no whitespace between the last child and the `</`.
*/
export function shouldHugEnd(node: anyNode, opts: ParserOptions): boolean {
if (isBlockElement(node, opts)) {
if (isBlockElement(node, opts) || isBreakChildrenElement(node)) {
return false;
}

Expand Down
Loading