Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
name: Run automated fix
uses: prettier/shared-workflows/.github/workflows/automated-fix.yml@main
with:
repository: prettier/angular-estree-parser
repository: prettier/yaml-unist-parser
82 changes: 16 additions & 66 deletions src/transforms/__snapshots__/document.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,61 +166,39 @@ documentBody (4:2 ~ 9:2)
</documentBody>
`;

exports[`"...\\n\\n#\\n\\n" 1`] = `
document (1:1 ~ 1:4)
exports[`"...\\n\\n# comment 1\\n# comment 2\\n" 1`] = `
document (1:1 ~ 4:12)
1 | ...¶
| ^^^
| ^^^^
2 | ¶
3 | #¶
4 | ¶
| ^
3 | #·comment·1¶
| ^^^^^^^^^^^^
4 | #·comment·2¶
| ^^^^^^^^^^^
5 | ¶
<document documentEndMarker>
<documentHead>

</documentHead>
<documentBody>

<endComment value=" comment 1">
<endComment value=" comment 2">
</documentBody>
</document>
`;

exports[`"...\\n\\n#\\n\\n" 2`] = `
documentHead (1:1 ~ 1:1)
1 | ...¶
| ~
2 | ¶
3 | #¶
4 | ¶
5 | ¶
<documentHead>

</documentHead>
`;

exports[`"...\\n\\n#\\n\\n" 3`] = `
documentBody (1:1 ~ 1:1)
1 | ...¶
| ~
2 | ¶
3 | #¶
4 | ¶
5 | ¶
<documentBody>

</documentBody>
`;

exports[`"...\\n\\n#\\n\\n" 4`] = `
document (3:1 ~ 5:1)
exports[`"...\\n\\n#\\n\\n" 1`] = `
document (1:1 ~ 3:2)
1 | ...¶
| ^^^^
2 | ¶
| ^
3 | #¶
| ^^
4 | ¶
| ^
4 | ¶
5 | ¶
| ^
<document>
<document documentEndMarker>
<documentHead>

</documentHead>
Expand All @@ -230,34 +208,6 @@ document (3:1 ~ 5:1)
</document>
`;

exports[`"...\\n\\n#\\n\\n" 5`] = `
documentHead (3:1 ~ 3:1)
1 | ...¶
2 | ¶
3 | #¶
| ~
4 | ¶
5 | ¶
<documentHead>

</documentHead>
`;

exports[`"...\\n\\n#\\n\\n" 6`] = `
documentBody (3:1 ~ 5:1)
1 | ...¶
2 | ¶
3 | #¶
| ^^
4 | ¶
| ^
5 | ¶
| ^
<documentBody>
<endComment value="">
</documentBody>
`;

exports[`"\\n" 1`] = `
root (1:1 ~ 2:1)
1 | ¶
Expand Down
30 changes: 16 additions & 14 deletions src/transforms/document-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type Context from "./context.ts";
export function transformDocumentBody(
docStart: YAML_CST.DocStartSourceToken | null,
tokensBeforeBody: YAML_CST.SourceToken[],
cstNode: YAML.CST.Document,
cstNode: YAML.CST.Document | null,
document: YAML.Document.Parsed,
tokensAfterBody: YAML_CST.SourceToken[],
docEnd: YAML.CST.DocumentEnd | null,
Expand Down Expand Up @@ -63,7 +63,7 @@ export function transformDocumentBody(

function categorizeNodes(
tokensBeforeBody: YAML_CST.SourceToken[],
document: YAML.CST.Document,
cstNode: YAML.CST.Document | null,
tokensAfterBody: YAML_CST.SourceToken[],
docEnd: YAML.CST.DocumentEnd | null,
context: Context,
Expand All @@ -88,22 +88,24 @@ function categorizeNodes(
const docEndPoint: null | Point = docEnd
? context.transformOffset(docEnd.offset)
: null;
for (const token of YAML_CST.tokens(document.end, docEnd?.end)) {
if (token.type === "comment") {
const comment = context.transformComment(token);
if (docEndPoint) {
if (docEndPoint.line === comment.position.start.line) {
documentTrailingComments.push(comment);
} else if (comment.position.start.line < docEndPoint.line) {
if (cstNode) {
for (const token of YAML_CST.tokens(cstNode.end, docEnd?.end)) {
if (token.type === "comment") {
const comment = context.transformComment(token);
if (docEndPoint) {
if (docEndPoint.line === comment.position.start.line) {
documentTrailingComments.push(comment);
} else if (comment.position.start.line < docEndPoint.line) {
endComments.push(comment);
}
} else {
endComments.push(comment);
}
} else {
endComments.push(comment);
continue;
}
continue;
// istanbul ignore next -- @preserve
throw new Error(`Unexpected token type: ${token.type}`);
}
// istanbul ignore next -- @preserve
throw new Error(`Unexpected token type: ${token.type}`);
}

// istanbul ignore if -- @preserve
Expand Down
28 changes: 15 additions & 13 deletions src/transforms/document-head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { transformDirective } from "./directive.ts";

export function transformDocumentHead(
tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[],
cstNode: YAML.CST.Document,
cstNode: YAML.CST.Document | null,
document: YAML.Document.Parsed,
context: Context,
) {
Expand All @@ -18,21 +18,23 @@ export function transformDocumentHead(

let betweenTokens: YAML_CST.SourceToken[] = [];
let docStart: YAML_CST.DocStartSourceToken | null = null;
for (const token of YAML_CST.tokens(cstNode.start)) {
betweenTokens.push(token);
if (!docStart && token.type === "doc-start") {
// Collect comments between directives and doc-start
for (const t of betweenTokens) {
if (t.type === "comment") {
const comment = context.transformComment(t);
endCommentCandidates.push(comment);
if (cstNode) {
for (const token of YAML_CST.tokens(cstNode.start)) {
betweenTokens.push(token);
if (!docStart && token.type === "doc-start") {
// Collect comments between directives and doc-start
for (const t of betweenTokens) {
if (t.type === "comment") {
const comment = context.transformComment(t);
endCommentCandidates.push(comment);
}
}
}

// Reset betweenTokens to collect tokens after doc-start
betweenTokens = [];
// Reset betweenTokens to collect tokens after doc-start
betweenTokens = [];

docStart = token;
docStart = token;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/transforms/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ testCases([
["- AAA\n# comment\n---\n- BBB", selectors],
["---\nhello\n... #documentEndComment\n", getDocument(0)],
['&123 123 "123"\n\n... #123\n #\n\n123\n\n\n ', selectors],
["...\n\n#\n\n", selectors],
["...\n\n#\n\n", getDocument(0)],
["#123\n#456\n---", getDocumentHead(0)],
["...\n\n# comment 1\n# comment 2\n", getDocument(0)],
["123\n--- #666\n456", root => root],
["123\n...\n456", [getDocument(0), getDocumentBody(0)]],
["", root => root],
Expand Down
88 changes: 36 additions & 52 deletions src/transforms/documents.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type * as YAML from "yaml";
import * as YAML_CST from "../cst.ts";
import { createDocument } from "../factories/document.ts";
import { createDocumentBody } from "../factories/document-body.ts";
import { createDocumentHead } from "../factories/document-head.ts";
import { createPosition } from "../factories/position.ts";
import type { Document } from "../types.ts";
import { getPointText } from "../utils/get-point-text.ts";
import type Context from "./context.ts";
import { transformDocument } from "./document.ts";

export type DocumentData = {
tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[];
cstNode: YAML.CST.Document;
cstNode: YAML.CST.Document | null;
node: YAML.Document.Parsed;
tokensAfterBody: YAML_CST.CommentSourceToken[];
documentEnd: YAML.CST.DocumentEnd | null;
Expand All @@ -32,26 +28,31 @@ export function transformDocuments(
const tokensBeforeBody: (YAML_CST.CommentSourceToken | YAML.CST.Directive)[] =
[];
let currentDocumentData: DocumentData | null = null;
const createDocumentData = (cstNode: YAML.CST.Document | null) => {
const documentData = {
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
cstNode,
node: parsedDocuments[documents.length],
tokensAfterBody: [],
documentEnd: null,
};

documents.push(documentData);
tokensBeforeBody.length = 0;
bufferComments.length = 0;
return documentData;
};
for (const token of YAML_CST.tokens(cstTokens)) {
if (token.type === "document") {
// istanbul ignore if -- @preserve
if (parsedDocuments.length <= documents.length) {
if (documents.length >= parsedDocuments.length) {
throw new Error(
`Unexpected 'document' token at ${getPointText(context.transformOffset(token.offset))}`,
);
}

currentDocumentData = {
tokensBeforeBody: [...tokensBeforeBody, ...bufferComments],
cstNode: token,
node: parsedDocuments[documents.length],
tokensAfterBody: [],
documentEnd: null,
};
currentDocumentData = createDocumentData(token);

documents.push(currentDocumentData);
tokensBeforeBody.length = 0;
bufferComments.length = 0;
continue;
}

Expand Down Expand Up @@ -81,45 +82,28 @@ export function transformDocuments(
}
}

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

const nodes = documents.map(document => transformDocument(document, context));
if (bufferComments.length > 0) {
// If there is no document seen
if (!currentDocumentData) {
currentDocumentData = createDocumentData(null);
}

if (bufferComments.length === 0) {
return nodes;
// Append buffered comments to the last document
if (bufferComments.length > 0) {
currentDocumentData.tokensAfterBody.push(...bufferComments);
bufferComments.length = 0;
}
}

// Append remaining comments as a new document
const firstComment = bufferComments[0];
const commentDoc: Document = createDocument(
createPosition(
context.transformOffset(firstComment.offset),
context.transformOffset(context.text.length),
),
false,
false,
createDocumentHead(
createPosition(
context.transformOffset(firstComment.offset),
context.transformOffset(firstComment.offset),
),
[],
[],
null,
),
createDocumentBody(
createPosition(
context.transformOffset(firstComment.offset),
context.transformOffset(context.text.length),
),
null,
bufferComments.map(token => context.transformComment(token)),
),
null,
);
return [...nodes, commentDoc];
const nodes = documents.map(document => transformDocument(document, context));

return nodes;
}
4 changes: 2 additions & 2 deletions tests/yaml-test-suite/ast-snapshots/8G76.snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
"offset": 18,
},
"start": {
"column": 3,
"column": 1,
"line": 1,
"offset": 2,
"offset": 0,
},
},
"trailingComment": null,
Expand Down
Loading