Skip to content

Commit b07f5dd

Browse files
committed
Simplify logic
1 parent 5f6e889 commit b07f5dd

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

src/transforms/document.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,77 @@ type DocumentData = {
1515
cstNode: YAML.CST.Document;
1616
node: YAML.Document.Parsed;
1717
tokensAfterBody: YAML_CST.CommentSourceToken[];
18-
docEnd: YAML.CST.DocumentEnd | null;
18+
documentEnd: YAML.CST.DocumentEnd | null;
1919
};
2020

2121
export function transformDocuments(
2222
parsedDocuments: YAML.Document.Parsed[],
2323
cstTokens: YAML.CST.Token[],
2424
context: Context,
2525
): Document[] {
26-
let bufferComments: YAML_CST.CommentSourceToken[] = [];
27-
let tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[] =
28-
[];
29-
let currentDoc: DocumentData | null = null;
26+
if (parsedDocuments.length === 0) {
27+
return [];
28+
}
29+
3030
const documents: DocumentData[] = [];
31+
32+
const bufferComments: YAML_CST.CommentSourceToken[] = [];
33+
const tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[] =
34+
[];
35+
let currentDocumentData: DocumentData | null = null;
3136
for (const token of YAML_CST.tokens(cstTokens)) {
32-
if (token.type === "comment") {
33-
bufferComments.push(token);
34-
continue;
35-
}
36-
if (token.type === "doc-end") {
37+
if (token.type === "document") {
3738
// istanbul ignore if -- @preserve
38-
if (!currentDoc || currentDoc.docEnd)
39+
if (parsedDocuments.length <= documents.length) {
3940
throw new Error(
40-
`Unexpected doc-end token at ${getPointText(context.transformOffset(token.offset))}`,
41+
`Unexpected 'document' token at ${getPointText(context.transformOffset(token.offset))}`,
4142
);
43+
}
4244

43-
currentDoc.tokensAfterBody = [...bufferComments];
44-
bufferComments = [];
45+
currentDocumentData = {
46+
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
47+
cstNode: token,
48+
node: parsedDocuments[documents.length],
49+
tokensAfterBody: [],
50+
documentEnd: null,
51+
};
4552

46-
currentDoc.docEnd = token;
47-
currentDoc = null;
53+
documents.push(currentDocumentData);
54+
tokensBeforeBody.length = 0;
55+
bufferComments.length = 0;
4856
continue;
4957
}
50-
if (currentDoc) {
51-
currentDoc = null;
58+
59+
if (token.type === "comment") {
60+
bufferComments.push(token);
61+
continue;
5262
}
63+
5364
if (token.type === "directive") {
5465
tokensBeforeBody.push(...bufferComments, token);
55-
bufferComments = [];
66+
bufferComments.length = 0;
5667
continue;
5768
}
58-
if (token.type === "document") {
69+
70+
if (token.type === "doc-end") {
5971
// istanbul ignore if -- @preserve
60-
if (parsedDocuments.length <= documents.length) {
72+
if (!currentDocumentData || currentDocumentData.documentEnd) {
6173
throw new Error(
62-
`Unexpected document token at ${getPointText(context.transformOffset(token.offset))}`,
74+
`Unexpected 'doc-end' token at ${getPointText(context.transformOffset(token.offset))}`,
6375
);
6476
}
65-
currentDoc = {
66-
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
67-
cstNode: token,
68-
node: parsedDocuments[documents.length],
69-
tokensAfterBody: [],
70-
docEnd: null,
71-
};
72-
documents.push(currentDoc);
73-
tokensBeforeBody = [];
74-
bufferComments = [];
77+
78+
currentDocumentData!.tokensAfterBody = [...bufferComments];
79+
bufferComments.length = 0;
80+
currentDocumentData!.documentEnd = token;
7581
continue;
7682
}
77-
// istanbul ignore next -- @preserve
78-
throw new Error(
79-
`Unexpected token type: ${token.type} at ${getPointText(context.transformOffset(token.offset))}`,
80-
);
8183
}
8284

83-
if (documents.length > 0 && !documents[documents.length - 1].docEnd) {
84-
// Append buffered comments to the last document
85-
const lastDoc = documents[documents.length - 1];
86-
lastDoc.tokensAfterBody.push(...bufferComments);
87-
bufferComments = [];
85+
// Append buffered comments to the last document
86+
if (currentDocumentData && !currentDocumentData.documentEnd) {
87+
currentDocumentData.tokensAfterBody.push(...bufferComments);
88+
bufferComments.length = 0;
8889
}
8990

9091
const nodes = documents.map(document => transformDocument(document, context));
@@ -139,14 +140,14 @@ function transformDocument(document: DocumentData, context: Context): Document {
139140
document.cstNode,
140141
document.node,
141142
document.tokensAfterBody,
142-
document.docEnd,
143+
document.documentEnd,
143144
context,
144145
);
145146

146147
return createDocument(
147148
createPosition(documentHead.position.start, documentEndPoint),
148149
Boolean(docStart),
149-
Boolean(document.docEnd),
150+
Boolean(document.documentEnd),
150151
documentHead,
151152
documentBody,
152153
documentTrailingComment,

0 commit comments

Comments
 (0)