Skip to content

Commit 5f6e889

Browse files
authored
Don't generate doc for empty files (#310)
1 parent 426f3a5 commit 5f6e889

File tree

6 files changed

+19
-153
lines changed

6 files changed

+19
-153
lines changed

src/__snapshots__/yaml-test-suite.test.ts.snap

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -69177,63 +69177,7 @@ exports[`AB8U.yaml: Sequence entry that looks like two with wrong indentation 1`
6917769177

6917869178
exports[`AVM7.yaml: Empty Stream 1`] = `
6917969179
{
69180-
"children": [
69181-
{
69182-
"children": [
69183-
{
69184-
"children": [],
69185-
"endComments": [],
69186-
"position": {
69187-
"end": {
69188-
"column": 1,
69189-
"line": 1,
69190-
"offset": 0,
69191-
},
69192-
"start": {
69193-
"column": 1,
69194-
"line": 1,
69195-
"offset": 0,
69196-
},
69197-
},
69198-
"trailingComment": null,
69199-
"type": "documentHead",
69200-
},
69201-
{
69202-
"children": [],
69203-
"endComments": [],
69204-
"position": {
69205-
"end": {
69206-
"column": 1,
69207-
"line": 1,
69208-
"offset": 0,
69209-
},
69210-
"start": {
69211-
"column": 1,
69212-
"line": 1,
69213-
"offset": 0,
69214-
},
69215-
},
69216-
"type": "documentBody",
69217-
},
69218-
],
69219-
"directivesEndMarker": false,
69220-
"documentEndMarker": false,
69221-
"position": {
69222-
"end": {
69223-
"column": 1,
69224-
"line": 1,
69225-
"offset": 0,
69226-
},
69227-
"start": {
69228-
"column": 1,
69229-
"line": 1,
69230-
"offset": 0,
69231-
},
69232-
},
69233-
"trailingComment": null,
69234-
"type": "document",
69235-
},
69236-
],
69180+
"children": [],
6923769181
"comments": [],
6923869182
"position": {
6923969183
"end": {

src/options.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { parse } from "./parse.ts";
22

33
for (const { type, text } of [
44
{ type: "mapping", text: "a: 1\na: 2" },
5+
// `<<` is a special key in YAML 1.1
6+
// https://github.com/eemeli/yaml/blob/main/src/schema/yaml-1.1/merge.ts
7+
// { type: "mapping", text: "<<: 1\n<<: 2" },
58
{ type: "flowMapping", text: `{"a":1,"a":2}` },
69
{ type: "flowMapping", text: `{"a":1,'a':2}` },
710
]) {

src/parse.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import * as YAML from "yaml";
22
import { attachComments } from "./attach.ts";
33
import { createRoot } from "./factories/root.ts";
44
import Context from "./transforms/context.ts";
5+
import { transformDocuments } from "./transforms/document.ts";
56
import type { ParseOptions, Root } from "./types.ts";
67
import { removeFakeNodes } from "./utils/remove-fake-nodes.ts";
78
import { updatePositions } from "./utils/update-positions.ts";
89
import { YAMLSyntaxError } from "./yaml-syntax-error.ts";
910

1011
export function parse(text: string, options?: ParseOptions): Root {
1112
const lineCounter = new YAML.LineCounter();
13+
const context = new Context(text, lineCounter);
1214
const parser = new YAML.Parser(lineCounter.addNewLine);
1315
const composer = new YAML.Composer({
1416
keepSourceTokens: true,
@@ -17,24 +19,21 @@ export function parse(text: string, options?: ParseOptions): Root {
1719
uniqueKeys: options?.uniqueKeys,
1820
lineCounter,
1921
});
20-
const documentNodes: YAML.Document.Parsed[] = [];
21-
const cstTokens: YAML.CST.Token[] = [];
22-
const context = new Context(text, lineCounter);
22+
const parsedDocuments: YAML.Document.Parsed[] = [];
23+
const cstTokens = [...parser.parse(text)];
2324

24-
for (const cst of parser.parse(text)) {
25-
cstTokens.push(cst);
26-
for (const doc of composer.next(cst)) {
27-
documentNodes.push(throwParseError(doc, context));
25+
for (const parsedDocument of composer.compose(cstTokens, true, text.length)) {
26+
const { errors } = parsedDocument;
27+
if (errors.length > 0) {
28+
throw new YAMLSyntaxError(context, errors[0]);
2829
}
29-
}
3030

31-
for (const doc of composer.end()) {
32-
documentNodes.push(throwParseError(doc, context));
31+
parsedDocuments.push(parsedDocument);
3332
}
3433

3534
const root = createRoot(
3635
context.transformRange([0, text.length]),
37-
context.transformDocuments(documentNodes, cstTokens),
36+
transformDocuments(parsedDocuments, cstTokens, context),
3837
context.comments,
3938
);
4039

@@ -44,11 +43,3 @@ export function parse(text: string, options?: ParseOptions): Root {
4443

4544
return root;
4645
}
47-
48-
function throwParseError(document: YAML.Document.Parsed, context: Context) {
49-
const { errors } = document;
50-
if (errors.length > 0) {
51-
throw new YAMLSyntaxError(context, errors[0]);
52-
}
53-
return document;
54-
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@ root (1:1 ~ 1:2)
55
1 | ·¶
66
| ^
77
<root>
8-
<document>
9-
<documentHead>
10-
11-
</documentHead>
12-
<documentBody>
138
14-
</documentBody>
15-
</document>
169
</root>
1710
`;
1811

@@ -21,14 +14,7 @@ root (1:1 ~ 1:1)
2114
1 | ¶
2215
| ~
2316
<root>
24-
<document>
25-
<documentHead>
2617
27-
</documentHead>
28-
<documentBody>
29-
30-
</documentBody>
31-
</document>
3218
</root>
3319
`;
3420

@@ -279,14 +265,7 @@ root (1:1 ~ 2:1)
279265
2 | ¶
280266
| ^
281267
<root>
282-
<document>
283-
<documentHead>
284268
285-
</documentHead>
286-
<documentBody>
287-
288-
</documentBody>
289-
</document>
290269
</root>
291270
`;
292271

src/transforms/context.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import type * as YAML from "yaml";
22
import type * as YAML_CST from "../cst.ts";
33
import { createPosition } from "../factories/position.ts";
4-
import type {
5-
Comment,
6-
Content,
7-
Document,
8-
Point,
9-
Position,
10-
Range,
11-
} from "../types.ts";
4+
import type { Comment, Content, Point, Position, Range } from "../types.ts";
125
import { transformComment } from "./comment.ts";
136
import { transformContentProperties } from "./content.ts";
14-
import { transformDocuments } from "./document.ts";
157
import {
168
transformNode,
179
type TransformNodeProperties,
@@ -39,13 +31,6 @@ class Context {
3931
return createPosition(start, end);
4032
}
4133

42-
transformDocuments(
43-
documentNodes: YAML.Document.Parsed[],
44-
cstTokens: YAML.CST.Token[],
45-
): Document[] {
46-
return transformDocuments(documentNodes, cstTokens, this);
47-
}
48-
4934
transformNode<T extends YamlNode>(
5035
node: T,
5136
props: TransformNodeProperties,

src/transforms/document.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type DocumentData = {
1919
};
2020

2121
export function transformDocuments(
22-
documentNodes: YAML.Document.Parsed[],
22+
parsedDocuments: YAML.Document.Parsed[],
2323
cstTokens: YAML.CST.Token[],
2424
context: Context,
2525
): Document[] {
@@ -57,15 +57,15 @@ export function transformDocuments(
5757
}
5858
if (token.type === "document") {
5959
// istanbul ignore if -- @preserve
60-
if (documentNodes.length <= documents.length) {
60+
if (parsedDocuments.length <= documents.length) {
6161
throw new Error(
6262
`Unexpected document token at ${getPointText(context.transformOffset(token.offset))}`,
6363
);
6464
}
6565
currentDoc = {
6666
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
6767
cstNode: token,
68-
node: documentNodes[documents.length],
68+
node: parsedDocuments[documents.length],
6969
tokensAfterBody: [],
7070
docEnd: null,
7171
};
@@ -79,13 +79,7 @@ export function transformDocuments(
7979
`Unexpected token type: ${token.type} at ${getPointText(context.transformOffset(token.offset))}`,
8080
);
8181
}
82-
// istanbul ignore if -- @preserve
83-
if (documents.length < documentNodes.length) {
84-
const errorIndex = documentNodes[documents.length].range[0];
85-
throw new Error(
86-
`Unexpected document token at ${getPointText(context.transformOffset(errorIndex))}`,
87-
);
88-
}
82+
8983
if (documents.length > 0 && !documents[documents.length - 1].docEnd) {
9084
// Append buffered comments to the last document
9185
const lastDoc = documents[documents.length - 1];
@@ -96,36 +90,6 @@ export function transformDocuments(
9690
const nodes = documents.map(document => transformDocument(document, context));
9791

9892
if (bufferComments.length === 0) {
99-
if (nodes.length === 0) {
100-
// Create an empty document if there is no document but comments
101-
const emptyDoc: Document = createDocument(
102-
createPosition(
103-
context.transformOffset(0),
104-
context.transformOffset(context.text.length),
105-
),
106-
false,
107-
false,
108-
createDocumentHead(
109-
createPosition(
110-
context.transformOffset(0),
111-
context.transformOffset(context.text.length),
112-
),
113-
[],
114-
[],
115-
null,
116-
),
117-
createDocumentBody(
118-
createPosition(
119-
context.transformOffset(0),
120-
context.transformOffset(context.text.length),
121-
),
122-
null,
123-
[],
124-
),
125-
null,
126-
);
127-
return [emptyDoc];
128-
}
12993
return nodes;
13094
}
13195

0 commit comments

Comments
 (0)