Skip to content

Commit 842f0b5

Browse files
committed
1 parent ace79dc commit 842f0b5

File tree

6 files changed

+104
-67
lines changed

6 files changed

+104
-67
lines changed

src/document/utils.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,11 @@ function cleanDocFn(doc) {
350350

351351
return doc;
352352
}
353-
// A safer version of `normalizeDoc`
354-
// - `normalizeDoc` concat strings and flat array in `fill`, while `cleanDoc` don't
355-
// - On array, `normalizeDoc` always return object with `parts`, `cleanDoc` may return strings
356-
// - `cleanDoc` also remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible
353+
354+
// - concat strings
355+
// - flat arrays except for parts of `fill`
356+
// - merge arrays of strings into single strings
357+
// - remove nested `group`s and empty `fill`/`align`/`indent`/`line-suffix`/`if-break` if possible
357358
function cleanDoc(doc) {
358359
return mapDoc(doc, (currentDoc) => cleanDocFn(currentDoc));
359360
}
@@ -389,21 +390,6 @@ function normalizeParts(parts) {
389390
return newParts;
390391
}
391392

392-
function normalizeDoc(doc) {
393-
return mapDoc(doc, (currentDoc) => {
394-
if (Array.isArray(currentDoc)) {
395-
return normalizeParts(currentDoc);
396-
}
397-
if (!currentDoc.parts) {
398-
return currentDoc;
399-
}
400-
return {
401-
...currentDoc,
402-
parts: normalizeParts(currentDoc.parts),
403-
};
404-
});
405-
}
406-
407393
function replaceEndOfLine(doc, replacement = literalline) {
408394
return mapDoc(doc, (currentDoc) =>
409395
typeof currentDoc === "string"
@@ -436,7 +422,6 @@ export {
436422
getDocType,
437423
inheritLabel,
438424
mapDoc,
439-
normalizeDoc,
440425
normalizeParts,
441426
propagateBreaks,
442427
removeLines,

src/language-markdown/print-paragraph.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fill } from "../document/builders.js";
2-
import { normalizeDoc } from "../document/utils.js";
2+
import { DOC_TYPE_ARRAY, DOC_TYPE_FILL } from "../document/constants.js";
3+
import { getDocType } from "../document/utils.js";
34

45
/**
56
* @typedef {import("../common/ast-path.js").default} AstPath
@@ -14,7 +15,41 @@ import { normalizeDoc } from "../document/utils.js";
1415
*/
1516
function printParagraph(path, options, print) {
1617
const parts = path.map(print, "children");
17-
return normalizeDoc(fill(parts));
18+
return flattenFill(parts);
19+
}
20+
21+
/**
22+
* @param {Doc[]} docs
23+
* @returns {Doc}
24+
*/
25+
function flattenFill(docs) {
26+
/*
27+
* We assume parts always meet following conditions:
28+
* - parts.length is odd
29+
* - odd elements are line-like doc that comes from odd element off inner fill
30+
*/
31+
/** @type {Doc[]} */
32+
const parts = [""];
33+
34+
(function rec(/** @type {*} */ docArray) {
35+
for (const doc of docArray) {
36+
const docType = getDocType(doc);
37+
if (docType === DOC_TYPE_ARRAY) {
38+
rec(doc);
39+
continue;
40+
}
41+
42+
let head = doc;
43+
let rest = [];
44+
if (docType === DOC_TYPE_FILL) {
45+
[head, ...rest] = doc.parts;
46+
}
47+
48+
parts.push([parts.pop(), head], ...rest);
49+
}
50+
})(docs);
51+
52+
return fill(parts);
1853
}
1954

2055
export { printParagraph };
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @typedef {import("../common/ast-path.js").default} AstPath
3+
* @typedef {import("../document/builders.js").Doc} Doc
4+
*/
5+
6+
import { fill } from "../document/builders.js";
7+
import { DOC_TYPE_STRING } from "../document/constants.js";
8+
import { getDocType } from "../document/utils.js";
9+
10+
/**
11+
* @param {AstPath} path
12+
* @param {*} print
13+
* @returns {Doc}
14+
*/
15+
function printSentence(path, print) {
16+
/** @type {Doc[]} */
17+
const parts = [""];
18+
19+
path.each(() => {
20+
const { node } = path;
21+
const doc = print();
22+
switch (node.type) {
23+
case "whitespace":
24+
if (getDocType(doc) !== DOC_TYPE_STRING) {
25+
parts.push(doc, "");
26+
break;
27+
}
28+
// fallthrough
29+
default:
30+
parts.push([parts.pop(), doc]);
31+
}
32+
}, "children");
33+
34+
return fill(parts);
35+
}
36+
37+
export { printSentence };

src/language-markdown/printer-markdown.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import collapseWhiteSpace from "collapse-white-space";
33
import {
44
align,
55
breakParent,
6+
fill,
67
group,
78
hardline,
89
hardlineWithoutBreakParent,
@@ -14,8 +15,9 @@ import {
1415
markAsRoot,
1516
softline,
1617
} from "../document/builders.js";
18+
import { DOC_TYPE_STRING } from "../document/constants.js";
1719
import { printDocToString } from "../document/printer.js";
18-
import { cleanDoc, replaceEndOfLine } from "../document/utils.js";
20+
import { cleanDoc, getDocType, replaceEndOfLine } from "../document/utils.js";
1921
import getMaxContinuousCount from "../utils/get-max-continuous-count.js";
2022
import getMinNotPresentContinuousCount from "../utils/get-min-not-present-continuous-count.js";
2123
import getPreferredQuote from "../utils/get-preferred-quote.js";
@@ -29,6 +31,7 @@ import { locEnd, locStart } from "./loc.js";
2931
import { insertPragma } from "./pragma.js";
3032
import { printParagraph } from "./print-paragraph.js";
3133
import preprocess from "./print-preprocess.js";
34+
import { printSentence } from "./print-sentence.js";
3235
import { printWhitespace } from "./print-whitespace.js";
3336
import {
3437
getFencedCodeBlockValue,
@@ -53,16 +56,27 @@ function genericPrint(path, options, print) {
5356
const { node } = path;
5457

5558
if (shouldRemainTheSameContent(path)) {
56-
return splitText(
59+
/** @type {Doc} */
60+
const parts = [""];
61+
const textsNodes = splitText(
5762
options.originalText.slice(
5863
node.position.start.offset,
5964
node.position.end.offset,
6065
),
61-
).map((node) =>
62-
node.type === "word"
63-
? node.value
64-
: printWhitespace(path, node.value, options.proseWrap, true),
6566
);
67+
for (const node of textsNodes) {
68+
if (node.type === "word") {
69+
parts.push([parts.pop(), node.value]);
70+
continue;
71+
}
72+
const doc = printWhitespace(path, node.value, options.proseWrap, true);
73+
if (getDocType(doc) === DOC_TYPE_STRING) {
74+
parts.push([parts.pop(), doc]);
75+
continue;
76+
}
77+
parts.push(doc);
78+
}
79+
return fill(parts);
6680
}
6781

6882
switch (node.type) {
@@ -80,7 +94,7 @@ function genericPrint(path, options, print) {
8094
case "paragraph":
8195
return printParagraph(path, options, print);
8296
case "sentence":
83-
return printChildren(path, options, print);
97+
return printSentence(path, print);
8498
case "word": {
8599
let escapedValue = node.value
86100
.replaceAll("*", "\\*") // escape all `*`

tests/format/markdown/break/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ word very-very-very-very-very-very-very-very-very-very-long-word very-very-very-
4444
4545
=====================================output=====================================
4646
a
47-
very-very-very-very-very-very-very-very-very-very-long-word very-very-very-very-very-very-very-very-very-very-long-word
47+
very-very-very-very-very-very-very-very-very-very-long-word
48+
very-very-very-very-very-very-very-very-very-very-long-word
4849
very-very-very-very-very-very-very-very-very-very-long-word
4950
5051
\\
51-
word very-very-very-very-very-very-very-very-very-very-long-word very-very-very-very-very-very-very-very-very-very-long-word
52+
word very-very-very-very-very-very-very-very-very-very-long-word
53+
very-very-very-very-very-very-very-very-very-very-long-word
5254
5355
================================================================================
5456
`;

tests/integration/__tests__/normalize-doc.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)