Skip to content

Commit de11e6d

Browse files
committed
Don't add document for comments after ...
1 parent b1640a1 commit de11e6d

File tree

5 files changed

+73
-145
lines changed

5 files changed

+73
-145
lines changed

src/transforms/__snapshots__/document.test.ts.snap

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -167,95 +167,27 @@ documentBody (4:2 ~ 9:2)
167167
`;
168168
169169
exports[`"...\\n\\n#\\n\\n" 1`] = `
170-
document (1:1 ~ 1:4)
171-
1 | ...¶
172-
| ^^^
173-
2 | ¶
174-
3 | #¶
175-
4 | ¶
176-
5 | ¶
177-
<document documentEndMarker>
178-
<documentHead>
179-
180-
</documentHead>
181-
<documentBody>
182-
183-
</documentBody>
184-
</document>
185-
`;
186-
187-
exports[`"...\\n\\n#\\n\\n" 2`] = `
188-
documentHead (1:1 ~ 1:1)
189-
1 | ...¶
190-
| ~
191-
2 | ¶
192-
3 | #¶
193-
4 | ¶
194-
5 | ¶
195-
<documentHead>
196-
197-
</documentHead>
198-
`;
199-
200-
exports[`"...\\n\\n#\\n\\n" 3`] = `
201-
documentBody (1:1 ~ 1:1)
202-
1 | ...¶
203-
| ~
204-
2 | ¶
205-
3 | #¶
206-
4 | ¶
207-
5 | ¶
208-
<documentBody>
209-
210-
</documentBody>
211-
`;
212-
213-
exports[`"...\\n\\n#\\n\\n" 4`] = `
214-
document (3:1 ~ 5:1)
170+
root (1:1 ~ 5:1)
215171
1 | ...¶
172+
| ^^^^
216173
2 | ¶
217-
3 | #¶
218-
| ^^
219-
4 | ¶
220-
| ^
221-
5 | ¶
222174
| ^
223-
<document>
224-
<documentHead>
225-
226-
</documentHead>
227-
<documentBody>
228-
<endComment value="">
229-
</documentBody>
230-
</document>
231-
`;
232-
233-
exports[`"...\\n\\n#\\n\\n" 5`] = `
234-
documentHead (3:1 ~ 3:1)
235-
1 | ...¶
236-
2 | ¶
237-
3 | #¶
238-
| ~
239-
4 | ¶
240-
5 | ¶
241-
<documentHead>
242-
243-
</documentHead>
244-
`;
245-
246-
exports[`"...\\n\\n#\\n\\n" 6`] = `
247-
documentBody (3:1 ~ 5:1)
248-
1 | ...¶
249-
2 | ¶
250175
3 | #¶
251176
| ^^
252177
4 | ¶
253178
| ^
254179
5 | ¶
255180
| ^
256-
<documentBody>
257-
<endComment value="">
258-
</documentBody>
181+
<root>
182+
<document documentEndMarker>
183+
<documentHead>
184+
185+
</documentHead>
186+
<documentBody>
187+
<endComment value="">
188+
</documentBody>
189+
</document>
190+
</root>
259191
`;
260192
261193
exports[`"\\n" 1`] = `

src/transforms/document-body.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type Context from "./context.ts";
1111
export function transformDocumentBody(
1212
docStart: YAML_CST.DocStartSourceToken | null,
1313
tokensBeforeBody: YAML_CST.SourceToken[],
14-
cstNode: YAML.CST.Document,
14+
cstNode: YAML.CST.Document | null,
1515
document: YAML.Document.Parsed,
1616
tokensAfterBody: YAML_CST.SourceToken[],
1717
docEnd: YAML.CST.DocumentEnd | null,
@@ -63,7 +63,7 @@ export function transformDocumentBody(
6363

6464
function categorizeNodes(
6565
tokensBeforeBody: YAML_CST.SourceToken[],
66-
document: YAML.CST.Document,
66+
cstNode: YAML.CST.Document | null,
6767
tokensAfterBody: YAML_CST.SourceToken[],
6868
docEnd: YAML.CST.DocumentEnd | null,
6969
context: Context,
@@ -88,22 +88,24 @@ function categorizeNodes(
8888
const docEndPoint: null | Point = docEnd
8989
? context.transformOffset(docEnd.offset)
9090
: null;
91-
for (const token of YAML_CST.tokens(document.end, docEnd?.end)) {
92-
if (token.type === "comment") {
93-
const comment = context.transformComment(token);
94-
if (docEndPoint) {
95-
if (docEndPoint.line === comment.position.start.line) {
96-
documentTrailingComments.push(comment);
97-
} else if (comment.position.start.line < docEndPoint.line) {
91+
if (cstNode) {
92+
for (const token of YAML_CST.tokens(cstNode.end, docEnd?.end)) {
93+
if (token.type === "comment") {
94+
const comment = context.transformComment(token);
95+
if (docEndPoint) {
96+
if (docEndPoint.line === comment.position.start.line) {
97+
documentTrailingComments.push(comment);
98+
} else if (comment.position.start.line < docEndPoint.line) {
99+
endComments.push(comment);
100+
}
101+
} else {
98102
endComments.push(comment);
99103
}
100-
} else {
101-
endComments.push(comment);
104+
continue;
102105
}
103-
continue;
106+
// istanbul ignore next -- @preserve
107+
throw new Error(`Unexpected token type: ${token.type}`);
104108
}
105-
// istanbul ignore next -- @preserve
106-
throw new Error(`Unexpected token type: ${token.type}`);
107109
}
108110

109111
// istanbul ignore if -- @preserve

src/transforms/document-head.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { transformDirective } from "./directive.ts";
77

88
export function transformDocumentHead(
99
tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[],
10-
cstNode: YAML.CST.Document,
10+
cstNode: YAML.CST.Document | null,
1111
document: YAML.Document.Parsed,
1212
context: Context,
1313
) {
@@ -18,21 +18,23 @@ export function transformDocumentHead(
1818

1919
let betweenTokens: YAML_CST.SourceToken[] = [];
2020
let docStart: YAML_CST.DocStartSourceToken | null = null;
21-
for (const token of YAML_CST.tokens(cstNode.start)) {
22-
betweenTokens.push(token);
23-
if (!docStart && token.type === "doc-start") {
24-
// Collect comments between directives and doc-start
25-
for (const t of betweenTokens) {
26-
if (t.type === "comment") {
27-
const comment = context.transformComment(t);
28-
endCommentCandidates.push(comment);
21+
if (cstNode) {
22+
for (const token of YAML_CST.tokens(cstNode.start)) {
23+
betweenTokens.push(token);
24+
if (!docStart && token.type === "doc-start") {
25+
// Collect comments between directives and doc-start
26+
for (const t of betweenTokens) {
27+
if (t.type === "comment") {
28+
const comment = context.transformComment(t);
29+
endCommentCandidates.push(comment);
30+
}
2931
}
30-
}
3132

32-
// Reset betweenTokens to collect tokens after doc-start
33-
betweenTokens = [];
33+
// Reset betweenTokens to collect tokens after doc-start
34+
betweenTokens = [];
3435

35-
docStart = token;
36+
docStart = token;
37+
}
3638
}
3739
}
3840

src/transforms/document.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ testCases([
2222
["- AAA\n# comment\n---\n- BBB", selectors],
2323
["---\nhello\n... #documentEndComment\n", getDocument(0)],
2424
['&123 123 "123"\n\n... #123\n #\n\n123\n\n\n ', selectors],
25-
["...\n\n#\n\n", selectors],
25+
["...\n\n#\n\n", root => root],
2626
["#123\n#456\n---", getDocumentHead(0)],
2727
["123\n--- #666\n456", root => root],
2828
["123\n...\n456", [getDocument(0), getDocumentBody(0)]],

src/transforms/documents.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { transformDocument } from "./document.ts";
1111

1212
export type DocumentData = {
1313
tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[];
14-
cstNode: YAML.CST.Document;
14+
cstNode: YAML.CST.Document | null;
1515
node: YAML.Document.Parsed;
1616
tokensAfterBody: YAML_CST.CommentSourceToken[];
1717
documentEnd: YAML.CST.DocumentEnd | null;
@@ -81,45 +81,37 @@ export function transformDocuments(
8181
}
8282
}
8383

84-
// Append buffered comments to the last document
85-
if (currentDocumentData && !currentDocumentData.documentEnd) {
86-
currentDocumentData.tokensAfterBody.push(...bufferComments);
87-
bufferComments.length = 0;
84+
// istanbul ignore if -- @preserve
85+
if (tokensBeforeBody.length > 0) {
86+
const [firstToken] = tokensBeforeBody;
87+
throw new Error(
88+
`Unexpected '${firstToken.type}' token at ${getPointText(context.transformOffset(firstToken.offset))}`,
89+
);
8890
}
8991

90-
const nodes = documents.map(document => transformDocument(document, context));
92+
if (bufferComments.length > 0) {
93+
// If there is no document seen
94+
if (!currentDocumentData) {
95+
currentDocumentData = {
96+
tokensBeforeBody: [...bufferComments],
97+
cstNode: null,
98+
node: parsedDocuments[documents.length],
99+
tokensAfterBody: [],
100+
documentEnd: null,
101+
};
102+
103+
documents.push(currentDocumentData);
104+
bufferComments.length = 0;
105+
}
91106

92-
if (bufferComments.length === 0) {
93-
return nodes;
107+
// Append buffered comments to the last document
108+
if (bufferComments.length > 0) {
109+
currentDocumentData.tokensAfterBody.push(...bufferComments);
110+
bufferComments.length = 0;
111+
}
94112
}
95113

96-
// Append remaining comments as a new document
97-
const firstComment = bufferComments[0];
98-
const commentDoc: Document = createDocument(
99-
createPosition(
100-
context.transformOffset(firstComment.offset),
101-
context.transformOffset(context.text.length),
102-
),
103-
false,
104-
false,
105-
createDocumentHead(
106-
createPosition(
107-
context.transformOffset(firstComment.offset),
108-
context.transformOffset(firstComment.offset),
109-
),
110-
[],
111-
[],
112-
null,
113-
),
114-
createDocumentBody(
115-
createPosition(
116-
context.transformOffset(firstComment.offset),
117-
context.transformOffset(context.text.length),
118-
),
119-
null,
120-
bufferComments.map(token => context.transformComment(token)),
121-
),
122-
null,
123-
);
124-
return [...nodes, commentDoc];
114+
const nodes = documents.map(document => transformDocument(document, context));
115+
116+
return nodes;
125117
}

0 commit comments

Comments
 (0)