Skip to content

Commit 797cb4d

Browse files
committed
Simplify printer
1 parent 1f0420c commit 797cb4d

2 files changed

Lines changed: 52 additions & 62 deletions

File tree

src/printer.js

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function isWhitespaceIgnorable(opts, name, attributes, content) {
6868
}
6969

7070
function printIToken(path) {
71-
const node = path.getValue();
71+
const { node } = path;
7272

7373
return {
7474
offset: node.startOffset,
@@ -79,7 +79,7 @@ function printIToken(path) {
7979
}
8080

8181
function printAttribute(path, opts, print) {
82-
const { Name, EQUALS, STRING } = path.getValue();
82+
const { Name, EQUALS, STRING } = path.node;
8383

8484
let attributeValue;
8585
if (opts.xmlQuoteAttributes === "double") {
@@ -97,7 +97,7 @@ function printAttribute(path, opts, print) {
9797
}
9898

9999
function printCharData(path, opts, print) {
100-
const { SEA_WS, TEXT } = path.getValue();
100+
const { SEA_WS, TEXT } = path.node;
101101
const image = SEA_WS || TEXT;
102102

103103
return image
@@ -110,34 +110,33 @@ function printContentFragments(path, print) {
110110
...path.map(printIToken, "CData"),
111111
...path.map(printIToken, "Comment"),
112112
...path.map(
113-
(charDataPath) => ({
114-
offset: charDataPath.getValue().location.startOffset,
115-
printed: print(charDataPath)
113+
({ node }) => ({
114+
offset: node.location.startOffset,
115+
printed: print()
116116
}),
117117
"chardata"
118118
),
119119
...path.map(
120-
(elementPath) => ({
121-
offset: elementPath.getValue().location.startOffset,
122-
printed: print(elementPath)
120+
({ node }) => ({
121+
offset: node.location.startOffset,
122+
printed: print()
123123
}),
124124
"element"
125125
),
126126
...path.map(printIToken, "PROCESSING_INSTRUCTION"),
127-
...path.map((referencePath) => {
128-
const referenceNode = referencePath.getValue();
129-
130-
return {
131-
offset: referenceNode.location.startOffset,
132-
printed: print(referencePath)
133-
};
134-
}, "reference")
127+
...path.map(
128+
({ node }) => ({
129+
offset: node.location.startOffset,
130+
printed: print()
131+
}),
132+
"reference"
133+
)
135134
];
136135
}
137136

138137
function printContent(path, opts, print) {
139138
let fragments = printContentFragments(path, print);
140-
const { Comment } = path.getValue();
139+
const { Comment } = path.node;
141140

142141
if (hasIgnoreRanges(Comment)) {
143142
Comment.sort((left, right) => left.startOffset - right.startOffset);
@@ -185,47 +184,45 @@ function printContent(path, opts, print) {
185184
}
186185

187186
function printDocTypeDecl(path, opts, print) {
188-
const { DocType, Name, externalID, CLOSE } = path.getValue();
187+
const { DocType, Name, externalID, CLOSE } = path.node;
189188
const parts = [DocType, " ", Name];
190189

191190
if (externalID) {
192-
parts.push(" ", path.call(print, "externalID"));
191+
parts.push(" ", print("externalID"));
193192
}
194193

195194
return group([...parts, CLOSE]);
196195
}
197196

198197
function printDocument(path, opts, print) {
199-
const { docTypeDecl, element, misc, prolog } = path.getValue();
198+
const { docTypeDecl, element, misc, prolog } = path.node;
200199
const fragments = [];
201200

202201
if (docTypeDecl) {
203202
fragments.push({
204203
offset: docTypeDecl.location.startOffset,
205-
printed: path.call(print, "docTypeDecl")
204+
printed: print("docTypeDecl")
206205
});
207206
}
208207

209208
if (prolog) {
210209
fragments.push({
211210
offset: prolog.location.startOffset,
212-
printed: path.call(print, "prolog")
211+
printed: print("prolog")
213212
});
214213
}
215214

216-
path.each((miscPath) => {
217-
const misc = miscPath.getValue();
218-
215+
path.each(({ node }) => {
219216
fragments.push({
220-
offset: misc.location.startOffset,
221-
printed: print(miscPath)
217+
offset: node.location.startOffset,
218+
printed: print()
222219
});
223220
}, "misc");
224221

225222
if (element) {
226223
fragments.push({
227224
offset: element.location.startOffset,
228-
printed: path.call(print, "element")
225+
printed: print("element")
229226
});
230227
}
231228

@@ -244,10 +241,9 @@ function printCharDataPreserve(path, print) {
244241
let prevLocation;
245242
const response = [];
246243

247-
path.each((charDataPath) => {
248-
const chardata = charDataPath.getValue();
244+
path.each(({ node: chardata }) => {
249245
const location = chardata.location;
250-
const content = print(charDataPath);
246+
const content = print();
251247

252248
if (
253249
prevLocation &&
@@ -278,8 +274,7 @@ function printCharDataPreserve(path, print) {
278274
function printCharDataIgnore(path) {
279275
const response = [];
280276

281-
path.each((charDataPath) => {
282-
const chardata = charDataPath.getValue();
277+
path.each(({ node: chardata }) => {
283278
if (!chardata.TEXT) {
284279
return;
285280
}
@@ -312,7 +307,7 @@ function printCharDataIgnore(path) {
312307
}
313308

314309
function printElementFragments(path, opts, print) {
315-
const children = path.getValue();
310+
const children = path.node;
316311
let response = [];
317312

318313
response = response.concat(path.map(printIToken, "Comment"));
@@ -329,32 +324,30 @@ function printElementFragments(path, opts, print) {
329324
}
330325

331326
response = response.concat(
332-
path.map((elementPath) => {
333-
const location = elementPath.getValue().location;
334-
335-
return {
327+
path.map(
328+
({ node: { location } }) => ({
336329
offset: location.startOffset,
337330
startLine: location.startLine,
338331
endLine: location.endLine,
339-
printed: print(elementPath)
340-
};
341-
}, "element")
332+
printed: print()
333+
}),
334+
"element"
335+
)
342336
);
343337

344338
response = response.concat(path.map(printIToken, "PROCESSING_INSTRUCTION"));
345339

346340
response = response.concat(
347-
path.map((referencePath) => {
348-
const referenceNode = referencePath.getValue();
349-
350-
return {
341+
path.map(
342+
({ node: referenceNode }) => ({
351343
type: "reference",
352344
offset: referenceNode.location.startOffset,
353345
startLine: referenceNode.location.startLine,
354346
endLine: referenceNode.location.endLine,
355-
printed: print(referencePath)
356-
};
357-
}, "reference")
347+
printed: print()
348+
}),
349+
"reference"
350+
)
358351
);
359352

360353
return response;
@@ -371,16 +364,13 @@ function printElement(path, opts, print) {
371364
END_NAME,
372365
END,
373366
SLASH_CLOSE
374-
} = path.getValue();
367+
} = path.node;
375368

376369
const parts = [OPEN, Name];
377370

378371
if (attribute.length > 0) {
379372
const attributes = path.map(
380-
(attributePath) => ({
381-
node: attributePath.getValue(),
382-
printed: print(attributePath)
383-
}),
373+
({ node }) => ({ node, printed: print() }),
384374
"attribute"
385375
);
386376

@@ -462,7 +452,7 @@ function printElement(path, opts, print) {
462452

463453
if (isWhitespaceIgnorable(opts, Name, attribute, content)) {
464454
const fragments = path.call(
465-
(childrenPath) => printElementFragments(childrenPath, opts, print),
455+
() => printElementFragments(path, opts, print),
466456
"content"
467457
);
468458

@@ -528,11 +518,11 @@ function printElement(path, opts, print) {
528518
return group([openTag, indent(docs), hardline, closeTag]);
529519
}
530520

531-
return group([openTag, indent(path.call(print, "content")), closeTag]);
521+
return group([openTag, indent(print("content")), closeTag]);
532522
}
533523

534524
function printExternalID(path, opts, print) {
535-
const { Public, PubIDLiteral, System, SystemLiteral } = path.getValue();
525+
const { Public, PubIDLiteral, System, SystemLiteral } = path.node;
536526

537527
if (System) {
538528
return group([System, indent([line, SystemLiteral])]);
@@ -545,13 +535,13 @@ function printExternalID(path, opts, print) {
545535
}
546536

547537
function printMisc(path, opts, print) {
548-
const { Comment, PROCESSING_INSTRUCTION, SEA_WS } = path.getValue();
538+
const { Comment, PROCESSING_INSTRUCTION, SEA_WS } = path.node;
549539

550540
return Comment || PROCESSING_INSTRUCTION || SEA_WS;
551541
}
552542

553543
function printProlog(path, opts, print) {
554-
const { XMLDeclOpen, attribute, SPECIAL_CLOSE } = path.getValue();
544+
const { XMLDeclOpen, attribute, SPECIAL_CLOSE } = path.node;
555545
const parts = [XMLDeclOpen];
556546

557547
if (attribute) {
@@ -566,7 +556,7 @@ function printProlog(path, opts, print) {
566556
}
567557

568558
function printReference(path, opts, print) {
569-
const { CharRef, EntityRef } = path.getValue();
559+
const { CharRef, EntityRef } = path.node;
570560

571561
return CharRef || EntityRef;
572562
}
@@ -579,7 +569,7 @@ const printer = {
579569
},
580570
embed,
581571
print(path, opts, print) {
582-
const node = path.getValue();
572+
const { node } = path;
583573

584574
switch (node.name) {
585575
case "attribute":

test/__snapshots__/format.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`bracketSameLine => true 1`] = `
44
"<?xml version="1.0" encoding="UTF-8" ?>

0 commit comments

Comments
 (0)