|
1 | | -import type * as YAML from "yaml"; |
2 | | -import * as YAML_CST from "../cst.ts"; |
3 | 1 | import { createDocument } from "../factories/document.ts"; |
4 | | -import { createDocumentBody } from "../factories/document-body.ts"; |
5 | | -import { createDocumentHead } from "../factories/document-head.ts"; |
6 | 2 | import { createPosition } from "../factories/position.ts"; |
7 | 3 | import type { Document } from "../types.ts"; |
8 | | -import { getPointText } from "../utils/get-point-text.ts"; |
9 | 4 | import type Context from "./context.ts"; |
10 | 5 | import { transformDocumentBody } from "./document-body.ts"; |
11 | 6 | import { transformDocumentHead } from "./document-head.ts"; |
| 7 | +import { type DocumentData } from "./documents.ts"; |
12 | 8 |
|
13 | | -type DocumentData = { |
14 | | - tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[]; |
15 | | - cstNode: YAML.CST.Document; |
16 | | - node: YAML.Document.Parsed; |
17 | | - tokensAfterBody: YAML_CST.CommentSourceToken[]; |
18 | | - documentEnd: YAML.CST.DocumentEnd | null; |
19 | | -}; |
20 | | - |
21 | | -export function transformDocuments( |
22 | | - parsedDocuments: YAML.Document.Parsed[], |
23 | | - cstTokens: YAML.CST.Token[], |
| 9 | +export function transformDocument( |
| 10 | + document: DocumentData, |
24 | 11 | context: Context, |
25 | | -): Document[] { |
26 | | - if (parsedDocuments.length === 0) { |
27 | | - return []; |
28 | | - } |
29 | | - |
30 | | - 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; |
36 | | - for (const token of YAML_CST.tokens(cstTokens)) { |
37 | | - if (token.type === "document") { |
38 | | - // istanbul ignore if -- @preserve |
39 | | - if (parsedDocuments.length <= documents.length) { |
40 | | - throw new Error( |
41 | | - `Unexpected 'document' token at ${getPointText(context.transformOffset(token.offset))}`, |
42 | | - ); |
43 | | - } |
44 | | - |
45 | | - currentDocumentData = { |
46 | | - tokensBeforeBody: [...tokensBeforeBody, ...bufferComments], |
47 | | - cstNode: token, |
48 | | - node: parsedDocuments[documents.length], |
49 | | - tokensAfterBody: [], |
50 | | - documentEnd: null, |
51 | | - }; |
52 | | - |
53 | | - documents.push(currentDocumentData); |
54 | | - tokensBeforeBody.length = 0; |
55 | | - bufferComments.length = 0; |
56 | | - continue; |
57 | | - } |
58 | | - |
59 | | - if (token.type === "comment") { |
60 | | - bufferComments.push(token); |
61 | | - continue; |
62 | | - } |
63 | | - |
64 | | - if (token.type === "directive") { |
65 | | - tokensBeforeBody.push(...bufferComments, token); |
66 | | - bufferComments.length = 0; |
67 | | - continue; |
68 | | - } |
69 | | - |
70 | | - if (token.type === "doc-end") { |
71 | | - // istanbul ignore if -- @preserve |
72 | | - if (!currentDocumentData || currentDocumentData.documentEnd) { |
73 | | - throw new Error( |
74 | | - `Unexpected 'doc-end' token at ${getPointText(context.transformOffset(token.offset))}`, |
75 | | - ); |
76 | | - } |
77 | | - |
78 | | - currentDocumentData!.tokensAfterBody = [...bufferComments]; |
79 | | - bufferComments.length = 0; |
80 | | - currentDocumentData!.documentEnd = token; |
81 | | - continue; |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - // Append buffered comments to the last document |
86 | | - if (currentDocumentData && !currentDocumentData.documentEnd) { |
87 | | - currentDocumentData.tokensAfterBody.push(...bufferComments); |
88 | | - bufferComments.length = 0; |
89 | | - } |
90 | | - |
91 | | - const nodes = documents.map(document => transformDocument(document, context)); |
92 | | - |
93 | | - if (bufferComments.length === 0) { |
94 | | - return nodes; |
95 | | - } |
96 | | - |
97 | | - // Append remaining comments as a new document |
98 | | - const firstComment = bufferComments[0]; |
99 | | - const commentDoc: Document = createDocument( |
100 | | - createPosition( |
101 | | - context.transformOffset(firstComment.offset), |
102 | | - context.transformOffset(context.text.length), |
103 | | - ), |
104 | | - false, |
105 | | - false, |
106 | | - createDocumentHead( |
107 | | - createPosition( |
108 | | - context.transformOffset(firstComment.offset), |
109 | | - context.transformOffset(firstComment.offset), |
110 | | - ), |
111 | | - [], |
112 | | - [], |
113 | | - null, |
114 | | - ), |
115 | | - createDocumentBody( |
116 | | - createPosition( |
117 | | - context.transformOffset(firstComment.offset), |
118 | | - context.transformOffset(context.text.length), |
119 | | - ), |
120 | | - null, |
121 | | - bufferComments.map(token => context.transformComment(token)), |
122 | | - ), |
123 | | - null, |
124 | | - ); |
125 | | - return [...nodes, commentDoc]; |
126 | | -} |
127 | | - |
128 | | -function transformDocument(document: DocumentData, context: Context): Document { |
| 12 | +): Document { |
129 | 13 | const { documentHead, tokensBeforeBody, docStart } = transformDocumentHead( |
130 | 14 | document.tokensBeforeBody, |
131 | 15 | document.cstNode, |
|
0 commit comments