Skip to content

Commit e8aaeb8

Browse files
refactor: Remove deprecated concat (#334)
* refactor: Remove deprecated concat * Remove Concat node checks
1 parent fbc3fd1 commit e8aaeb8

File tree

4 files changed

+118
-162
lines changed

4 files changed

+118
-162
lines changed

src/embed.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { snippedTagContentAttribute } from './lib/snipTagContent';
44
import { isBracketSameLine } from './options';
55
import { PrintFn } from './print';
66
import { isLine, removeParentheses, trimRight } from './print/doc-helpers';
7-
import { groupConcat, printWithPrependedAttributeLine } from './print/helpers';
7+
import { printWithPrependedAttributeLine } from './print/helpers';
88
import {
99
getAttributeTextValue,
1010
getLeadingComment,
1111
isIgnoreDirective,
1212
isNodeSupportedLanguage,
1313
isPugTemplate,
1414
isTypeScript,
15-
printRaw,
15+
printRaw
1616
} from './print/node-helpers';
1717
import { ElementNode, Node, ScriptNode, StyleNode } from './print/nodes';
1818

1919
const {
20-
builders: { concat, hardline, softline, indent, dedent, literalline },
20+
builders: { group, hardline, softline, indent, dedent, literalline },
2121
utils: { removeLines },
2222
} = doc;
2323

@@ -123,7 +123,7 @@ function preformattedBody(str: string): Doc {
123123

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

129129
function getSnippedContent(node: Node) {
@@ -159,13 +159,13 @@ function formatBodyContent(
159159
.split('\n')
160160
.map((line) => (line ? whitespace + line : line))
161161
.join('\n');
162-
return concat([hardline, pugBody]);
162+
return [hardline, pugBody];
163163
}
164164

165165
const indentIfDesired = (doc: Doc) =>
166166
options.svelteIndentScriptAndStyle ? indent(doc) : doc;
167167
trimRight([body], isLine);
168-
return concat([indentIfDesired(concat([hardline, body])), hardline]);
168+
return [indentIfDesired([hardline, body]), hardline];
169169
} catch (error) {
170170
if (process.env.PRETTIER_DEBUG) {
171171
throw error;
@@ -210,27 +210,27 @@ function embedTag(
210210
: hardline
211211
: preformattedBody(content);
212212

213-
const openingTag = groupConcat([
213+
const openingTag = group([
214214
'<',
215215
tag,
216216
indent(
217-
groupConcat([
217+
group([
218218
...path.map(printWithPrependedAttributeLine(node, options, print), 'attributes'),
219219
isBracketSameLine(options) ? '' : dedent(softline),
220220
]),
221221
),
222222
'>',
223223
]);
224-
let result = groupConcat([openingTag, body, '</', tag, '>']);
224+
let result: Doc = group([openingTag, body, '</', tag, '>']);
225225

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

src/print/doc-helpers.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ export function isLine(docToCheck: Doc): boolean {
4141
return (
4242
isHardline(docToCheck) ||
4343
(isDocCommand(docToCheck) && docToCheck.type === 'line') ||
44-
(isDocCommand(docToCheck) &&
45-
docToCheck.type === 'concat' &&
46-
docToCheck.parts.every(isLine)) ||
47-
// Since Prettier 2.3.0, concats are represented as flat arrays
4844
(Array.isArray(docToCheck) && docToCheck.every(isLine))
4945
);
5046
}
@@ -61,7 +57,6 @@ export function isEmptyDoc(doc: Doc): boolean {
6157
return !doc.keepIfLonely;
6258
}
6359

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

147142
function getParts(doc: Doc): Doc[] | undefined {
148143
if (typeof doc === 'object') {
149-
// Since Prettier 2.3.0, concats are represented as flat arrays
150144
if (Array.isArray(doc)) {
151145
return doc;
152146
}
153-
if (doc.type === 'fill' || doc.type === 'concat') {
147+
if (doc.type === 'fill') {
154148
return doc.parts;
155149
}
156150
if (doc.type === 'group') {

src/print/helpers.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { Doc, doc, FastPath, ParserOptions } from 'prettier';
2+
import { PrintFn } from '.';
3+
import { formattableAttributes } from '../lib/elements';
4+
import { snippedTagContentAttribute } from '../lib/snipTagContent';
15
import {
26
ASTNode,
37
AttributeNode,
@@ -12,12 +16,8 @@ import {
1216
SlotTemplateNode,
1317
StyleNode,
1418
TitleNode,
15-
WindowNode,
19+
WindowNode
1620
} from './nodes';
17-
import { Doc, doc, FastPath, ParserOptions } from 'prettier';
18-
import { formattableAttributes } from '../lib/elements';
19-
import { PrintFn } from '.';
20-
import { snippedTagContentAttribute } from '../lib/snipTagContent';
2121

2222
/**
2323
* Determines whether or not given node
@@ -66,11 +66,6 @@ export function replaceEndOfLineWith(text: string, replacement: Doc) {
6666
return parts;
6767
}
6868

69-
export function groupConcat(contents: doc.builders.Doc[]): doc.builders.Doc {
70-
const { concat, group } = doc.builders;
71-
return group(concat(contents));
72-
}
73-
7469
export function getAttributeLine(
7570
node:
7671
| ElementNode
@@ -118,6 +113,6 @@ export function printWithPrependedAttributeLine(
118113
): PrintFn {
119114
return (path) =>
120115
path.getNode().name !== snippedTagContentAttribute
121-
? doc.builders.concat([getAttributeLine(node, options), path.call(print)])
116+
? [getAttributeLine(node, options), path.call(print)]
122117
: '';
123118
}

0 commit comments

Comments
 (0)