Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 1 addition & 57 deletions src/__snapshots__/yaml-test-suite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69177,63 +69177,7 @@ exports[`AB8U.yaml: Sequence entry that looks like two with wrong indentation 1`

exports[`AVM7.yaml: Empty Stream 1`] = `
{
"children": [
{
"children": [
{
"children": [],
"endComments": [],
"position": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"trailingComment": null,
"type": "documentHead",
},
{
"children": [],
"endComments": [],
"position": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "documentBody",
},
],
"directivesEndMarker": false,
"documentEndMarker": false,
"position": {
"end": {
"column": 1,
"line": 1,
"offset": 0,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"trailingComment": null,
"type": "document",
},
],
"children": [],
"comments": [],
"position": {
"end": {
Expand Down
3 changes: 3 additions & 0 deletions src/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { parse } from "./parse.ts";

for (const { type, text } of [
{ type: "mapping", text: "a: 1\na: 2" },
// `<<` is a special key in YAML 1.1
// https://github.com/eemeli/yaml/blob/main/src/schema/yaml-1.1/merge.ts
// { type: "mapping", text: "<<: 1\n<<: 2" },
{ type: "flowMapping", text: `{"a":1,"a":2}` },
{ type: "flowMapping", text: `{"a":1,'a':2}` },
]) {
Expand Down
29 changes: 10 additions & 19 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as YAML from "yaml";
import { attachComments } from "./attach.ts";
import { createRoot } from "./factories/root.ts";
import Context from "./transforms/context.ts";
import { transformDocuments } from "./transforms/document.ts";
import type { ParseOptions, Root } from "./types.ts";
import { removeFakeNodes } from "./utils/remove-fake-nodes.ts";
import { updatePositions } from "./utils/update-positions.ts";
import { YAMLSyntaxError } from "./yaml-syntax-error.ts";

export function parse(text: string, options?: ParseOptions): Root {
const lineCounter = new YAML.LineCounter();
const context = new Context(text, lineCounter);
const parser = new YAML.Parser(lineCounter.addNewLine);
const composer = new YAML.Composer({
keepSourceTokens: true,
Expand All @@ -17,24 +19,21 @@ export function parse(text: string, options?: ParseOptions): Root {
uniqueKeys: options?.uniqueKeys,
lineCounter,
});
const documentNodes: YAML.Document.Parsed[] = [];
const cstTokens: YAML.CST.Token[] = [];
const context = new Context(text, lineCounter);
const parsedDocuments: YAML.Document.Parsed[] = [];
const cstTokens = [...parser.parse(text)];

for (const cst of parser.parse(text)) {
cstTokens.push(cst);
for (const doc of composer.next(cst)) {
documentNodes.push(throwParseError(doc, context));
for (const parsedDocument of composer.compose(cstTokens, true, text.length)) {
const { errors } = parsedDocument;
if (errors.length > 0) {
throw new YAMLSyntaxError(context, errors[0]);
}
}

for (const doc of composer.end()) {
documentNodes.push(throwParseError(doc, context));
parsedDocuments.push(parsedDocument);
}

const root = createRoot(
context.transformRange([0, text.length]),
context.transformDocuments(documentNodes, cstTokens),
transformDocuments(parsedDocuments, cstTokens, context),
context.comments,
);

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

return root;
}

function throwParseError(document: YAML.Document.Parsed, context: Context) {
const { errors } = document;
if (errors.length > 0) {
throw new YAMLSyntaxError(context, errors[0]);
}
return document;
}
21 changes: 0 additions & 21 deletions src/transforms/__snapshots__/document.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ root (1:1 ~ 1:2)
1 | ·¶
| ^
<root>
<document>
<documentHead>

</documentHead>
<documentBody>

</documentBody>
</document>
</root>
`;

Expand All @@ -21,14 +14,7 @@ root (1:1 ~ 1:1)
1 | ¶
| ~
<root>
<document>
<documentHead>

</documentHead>
<documentBody>

</documentBody>
</document>
</root>
`;

Expand Down Expand Up @@ -279,14 +265,7 @@ root (1:1 ~ 2:1)
2 | ¶
| ^
<root>
<document>
<documentHead>

</documentHead>
<documentBody>

</documentBody>
</document>
</root>
`;

Expand Down
17 changes: 1 addition & 16 deletions src/transforms/context.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import type * as YAML from "yaml";
import type * as YAML_CST from "../cst.ts";
import { createPosition } from "../factories/position.ts";
import type {
Comment,
Content,
Document,
Point,
Position,
Range,
} from "../types.ts";
import type { Comment, Content, Point, Position, Range } from "../types.ts";
import { transformComment } from "./comment.ts";
import { transformContentProperties } from "./content.ts";
import { transformDocuments } from "./document.ts";
import {
transformNode,
type TransformNodeProperties,
Expand Down Expand Up @@ -39,13 +31,6 @@ class Context {
return createPosition(start, end);
}

transformDocuments(
documentNodes: YAML.Document.Parsed[],
cstTokens: YAML.CST.Token[],
): Document[] {
return transformDocuments(documentNodes, cstTokens, this);
}

transformNode<T extends YamlNode>(
node: T,
props: TransformNodeProperties,
Expand Down
44 changes: 4 additions & 40 deletions src/transforms/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DocumentData = {
};

export function transformDocuments(
documentNodes: YAML.Document.Parsed[],
parsedDocuments: YAML.Document.Parsed[],
cstTokens: YAML.CST.Token[],
context: Context,
): Document[] {
Expand Down Expand Up @@ -57,15 +57,15 @@ export function transformDocuments(
}
if (token.type === "document") {
// istanbul ignore if -- @preserve
if (documentNodes.length <= documents.length) {
if (parsedDocuments.length <= documents.length) {
throw new Error(
`Unexpected document token at ${getPointText(context.transformOffset(token.offset))}`,
);
}
currentDoc = {
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
cstNode: token,
node: documentNodes[documents.length],
node: parsedDocuments[documents.length],
tokensAfterBody: [],
docEnd: null,
};
Expand All @@ -79,13 +79,7 @@ export function transformDocuments(
`Unexpected token type: ${token.type} at ${getPointText(context.transformOffset(token.offset))}`,
);
}
// istanbul ignore if -- @preserve
if (documents.length < documentNodes.length) {
const errorIndex = documentNodes[documents.length].range[0];
throw new Error(
`Unexpected document token at ${getPointText(context.transformOffset(errorIndex))}`,
);
}

if (documents.length > 0 && !documents[documents.length - 1].docEnd) {
// Append buffered comments to the last document
const lastDoc = documents[documents.length - 1];
Expand All @@ -96,36 +90,6 @@ export function transformDocuments(
const nodes = documents.map(document => transformDocument(document, context));

if (bufferComments.length === 0) {
if (nodes.length === 0) {
// Create an empty document if there is no document but comments
const emptyDoc: Document = createDocument(
createPosition(
context.transformOffset(0),
context.transformOffset(context.text.length),
),
false,
false,
createDocumentHead(
createPosition(
context.transformOffset(0),
context.transformOffset(context.text.length),
),
[],
[],
null,
),
createDocumentBody(
createPosition(
context.transformOffset(0),
context.transformOffset(context.text.length),
),
null,
[],
),
null,
);
return [emptyDoc];
}
return nodes;
}

Expand Down
Loading