Skip to content

Commit b0e6557

Browse files
committed
DRY
1 parent a03fe7f commit b0e6557

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/transforms/documents.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,31 @@ export function transformDocuments(
2828
const tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[] =
2929
[];
3030
let currentDocumentData: DocumentData | null = null;
31+
const createDocumentData = (cstNode: YAML.CST.Document | null) => {
32+
const documentData = {
33+
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
34+
cstNode,
35+
node: parsedDocuments[documents.length],
36+
tokensAfterBody: [],
37+
documentEnd: null,
38+
};
39+
40+
documents.push(documentData);
41+
tokensBeforeBody.length = 0;
42+
bufferComments.length = 0;
43+
return documentData;
44+
};
3145
for (const token of YAML_CST.tokens(cstTokens)) {
3246
if (token.type === "document") {
3347
// istanbul ignore if -- @preserve
34-
if (parsedDocuments.length <= documents.length) {
48+
if (documents.length >= parsedDocuments.length) {
3549
throw new Error(
3650
`Unexpected 'document' token at ${getPointText(context.transformOffset(token.offset))}`,
3751
);
3852
}
3953

40-
currentDocumentData = {
41-
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
42-
cstNode: token,
43-
node: parsedDocuments[documents.length],
44-
tokensAfterBody: [],
45-
documentEnd: null,
46-
};
54+
currentDocumentData = createDocumentData(token);
4755

48-
documents.push(currentDocumentData);
49-
tokensBeforeBody.length = 0;
50-
bufferComments.length = 0;
5156
continue;
5257
}
5358

@@ -88,16 +93,7 @@ export function transformDocuments(
8893
if (bufferComments.length > 0) {
8994
// If there is no document seen
9095
if (!currentDocumentData) {
91-
currentDocumentData = {
92-
tokensBeforeBody: [...bufferComments],
93-
cstNode: null,
94-
node: parsedDocuments[documents.length],
95-
tokensAfterBody: [],
96-
documentEnd: null,
97-
};
98-
99-
documents.push(currentDocumentData);
100-
bufferComments.length = 0;
96+
currentDocumentData = createDocumentData(null);
10197
}
10298

10399
// Append buffered comments to the last document

0 commit comments

Comments
 (0)