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

refactor: Remove deprecated concat #334

Merged
Merged
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
22 changes: 11 additions & 11 deletions src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { snippedTagContentAttribute } from './lib/snipTagContent';
import { isBracketSameLine } from './options';
import { PrintFn } from './print';
import { isLine, removeParentheses, trimRight } from './print/doc-helpers';
import { groupConcat, printWithPrependedAttributeLine } from './print/helpers';
import { printWithPrependedAttributeLine } from './print/helpers';
import {
getAttributeTextValue,
getLeadingComment,
isIgnoreDirective,
isNodeSupportedLanguage,
isPugTemplate,
isTypeScript,
printRaw,
printRaw
} from './print/node-helpers';
import { ElementNode, Node, ScriptNode, StyleNode } from './print/nodes';

const {
builders: { concat, hardline, softline, indent, dedent, literalline },
builders: { group, hardline, softline, indent, dedent, literalline },
utils: { removeLines },
} = doc;

Expand Down Expand Up @@ -123,7 +123,7 @@ function preformattedBody(str: string): Doc {

// If we do not start with a new line prettier might try to break the opening tag
// to keep it together with the string. Use a literal line to skip indentation.
return concat([literalline, str.replace(firstNewline, '').replace(lastNewline, ''), hardline]);
return [literalline, str.replace(firstNewline, '').replace(lastNewline, ''), hardline];
}

function getSnippedContent(node: Node) {
Expand Down Expand Up @@ -159,13 +159,13 @@ function formatBodyContent(
.split('\n')
.map((line) => (line ? whitespace + line : line))
.join('\n');
return concat([hardline, pugBody]);
return [hardline, pugBody];
}

const indentIfDesired = (doc: Doc) =>
options.svelteIndentScriptAndStyle ? indent(doc) : doc;
trimRight([body], isLine);
return concat([indentIfDesired(concat([hardline, body])), hardline]);
return [indentIfDesired([hardline, body]), hardline];
} catch (error) {
if (process.env.PRETTIER_DEBUG) {
throw error;
Expand Down Expand Up @@ -210,27 +210,27 @@ function embedTag(
: hardline
: preformattedBody(content);

const openingTag = groupConcat([
const openingTag = group([
'<',
tag,
indent(
groupConcat([
group([
...path.map(printWithPrependedAttributeLine(node, options, print), 'attributes'),
isBracketSameLine(options) ? '' : dedent(softline),
]),
),
'>',
]);
let result = groupConcat([openingTag, body, '</', tag, '>']);
let result: Doc = group([openingTag, body, '</', tag, '>']);

if (isTopLevel && options.svelteSortOrder !== 'none') {
// top level embedded nodes have been moved from their normal position in the
// node tree. if there is a comment referring to it, it must be recreated at
// the new position.
if (previousComment) {
result = concat(['<!--', previousComment.data, '-->', hardline, result, hardline]);
result = ['<!--', previousComment.data, '-->', hardline, result, hardline];
} else {
result = concat([result, hardline]);
result = [result, hardline];
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/print/doc-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export function isLine(docToCheck: Doc): boolean {
return (
isHardline(docToCheck) ||
(isDocCommand(docToCheck) && docToCheck.type === 'line') ||
(isDocCommand(docToCheck) &&
docToCheck.type === 'concat' &&
docToCheck.parts.every(isLine)) ||
// Since Prettier 2.3.0, concats are represented as flat arrays
(Array.isArray(docToCheck) && docToCheck.every(isLine))
);
}
Expand All @@ -61,7 +57,6 @@ export function isEmptyDoc(doc: Doc): boolean {
return !doc.keepIfLonely;
}

// Since Prettier 2.3.0, concats are represented as flat arrays
if (Array.isArray(doc)) {
return doc.length === 0;
}
Expand Down Expand Up @@ -146,11 +141,10 @@ export function trimRight(group: Doc[], isWhitespace: (doc: Doc) => boolean): vo

function getParts(doc: Doc): Doc[] | undefined {
if (typeof doc === 'object') {
// Since Prettier 2.3.0, concats are represented as flat arrays
if (Array.isArray(doc)) {
return doc;
}
if (doc.type === 'fill' || doc.type === 'concat') {
if (doc.type === 'fill') {
return doc.parts;
}
if (doc.type === 'group') {
Expand Down
17 changes: 6 additions & 11 deletions src/print/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Doc, doc, FastPath, ParserOptions } from 'prettier';
import { PrintFn } from '.';
import { formattableAttributes } from '../lib/elements';
import { snippedTagContentAttribute } from '../lib/snipTagContent';
import {
ASTNode,
AttributeNode,
Expand All @@ -12,12 +16,8 @@ import {
SlotTemplateNode,
StyleNode,
TitleNode,
WindowNode,
WindowNode
} from './nodes';
import { Doc, doc, FastPath, ParserOptions } from 'prettier';
import { formattableAttributes } from '../lib/elements';
import { PrintFn } from '.';
import { snippedTagContentAttribute } from '../lib/snipTagContent';

/**
* Determines whether or not given node
Expand Down Expand Up @@ -66,11 +66,6 @@ export function replaceEndOfLineWith(text: string, replacement: Doc) {
return parts;
}

export function groupConcat(contents: doc.builders.Doc[]): doc.builders.Doc {
const { concat, group } = doc.builders;
return group(concat(contents));
}

export function getAttributeLine(
node:
| ElementNode
Expand Down Expand Up @@ -118,6 +113,6 @@ export function printWithPrependedAttributeLine(
): PrintFn {
return (path) =>
path.getNode().name !== snippedTagContentAttribute
? doc.builders.concat([getAttributeLine(node, options), path.call(print)])
? [getAttributeLine(node, options), path.call(print)]
: '';
}
Loading