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
6 changes: 6 additions & 0 deletions .versionrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"preset": "angular",
"scripts": {
"postchangelog": "prettier --write CHANGELOG.md"
}
}
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ All notable changes to this project will be documented in this file. See [standa

### [2.0.3](https://github.com/prettier/yaml-unist-parser/compare/v2.0.2...v2.0.3) (2025-03-20)


### Bug Fixes

* remove `postinstall` script ([da9d58f](https://github.com/prettier/yaml-unist-parser/commit/da9d58fcfa572541415aefaaf703aa51ab631694))
- remove `postinstall` script ([da9d58f](https://github.com/prettier/yaml-unist-parser/commit/da9d58fcfa572541415aefaaf703aa51ab631694))

### [2.0.2](https://github.com/prettier/yaml-unist-parser/compare/v2.0.1...v2.0.2) (2025-03-20)

Expand Down
33 changes: 32 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import globals from "globals";
import eslintJs from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import eslintPluginSimpleImportSort from "eslint-plugin-simple-import-sort";
import globals from "globals";
import tseslint from "typescript-eslint";

export default tseslint.config(
Expand All @@ -11,6 +12,9 @@ export default tseslint.config(
languageOptions: {
globals: { ...globals.builtin, ...globals.node },
},
plugins: {
"simple-import-sort": eslintPluginSimpleImportSort,
},
rules: {
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
Expand All @@ -20,6 +24,33 @@ export default tseslint.config(
{ fixStyle: "inline-type-imports" },
],
"no-constant-condition": ["error", { checkLoops: false }],

"simple-import-sort/imports": [
"error",
{
groups: [
// https://github.com/lydell/eslint-plugin-simple-import-sort/blob/20e25f3b83c713825f96b8494e2091e6600954d6/src/imports.js#L5-L19
// Side effect imports.
[String.raw`^\u0000`],
// Remove blank lines between groups
// https://github.com/lydell/eslint-plugin-simple-import-sort#how-do-i-remove-all-blank-lines-between-imports
[
// Node.js builtins prefixed with `node:`.
"^node:",
// Packages.
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
String.raw`^@?\w`,
// Absolute imports and other imports such as Vue-style `@/foo`.
// Anything not matched in another group.
"^",
// Relative imports.
// Anything that starts with a dot.
String.raw`^\.`,
],
],
},
],
"simple-import-sort/exports": "error",
},
},
{ ignores: ["lib/", ".yarn/"] },
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint": "run-p \"lint:*\"",
"lint:eslint": "eslint .",
"lint:prettier": "prettier . --check",
"lint:types": "tsc",
"fix": "run-s \"fix:*\"",
"fix:eslint": "yarn lint:eslint --fix",
"fix:prettier": "prettier . --write",
Expand All @@ -42,6 +43,7 @@
"@vitest/coverage-v8": "3.0.8",
"eslint": "9.22.0",
"eslint-config-prettier": "10.1.1",
"eslint-plugin-simple-import-sort": "12.1.1",
"globals": "16.0.0",
"jest-snapshot-serializer-raw": "2.0.0",
"npm-run-all2": "7.0.2",
Expand Down
6 changes: 2 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { wrap } from "jest-snapshot-serializer-raw";
import { type YAMLSemanticError, type YAMLSyntaxError } from "yaml/util";
import { parse } from "./parse.js";
import {
type Anchor,
Expand All @@ -9,7 +10,6 @@ import {
type Tag,
type YamlUnistNode,
} from "./types.js";
import type * as YAML from "./yaml.js";

export type Arrayable<T> = T | T[];

Expand Down Expand Up @@ -335,9 +335,7 @@ export function testSyntaxError(text: string, message?: string) {
}
}

function isYAMLError(
e: any,
): e is YAML.YAMLSyntaxError | YAML.YAMLSemanticError {
function isYAMLError(e: any): e is YAMLSyntaxError | YAMLSemanticError {
return (
e instanceof Error &&
(e.name === "YAMLSyntaxError" || e.name === "YAMLSemanticError")
Expand Down
11 changes: 6 additions & 5 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Document, parseCST } from "yaml";
import { YAMLSemanticError } from "yaml/util";
import { attachComments } from "./attach.js";
import { createRoot } from "./factories/root.js";
import { removeCstBlankLine } from "./preprocess.js";
import Context from "./transforms/context.js";
import { transformError } from "./transforms/error.js";
import { type Root } from "./types.js";
import { addOrigRange } from "./utils/add-orig-range.js";
import { removeFakeNodes } from "./utils/remove-fake-nodes.js";
import { updatePositions } from "./utils/update-positions.js";
import * as YAML from "./yaml.js";
import Context from "./transforms/context.js";

export function parse(text: string): Root {
const cst = YAML.parseCST(text);
const cst = parseCST(text);

addOrigRange(cst);

const documents = cst.map(cstDocument =>
new YAML.Document({
new Document({
merge: false,
keepCstNodes: true,
}).parse(cstDocument),
Expand All @@ -26,7 +27,7 @@ export function parse(text: string): Root {
for (const document of documents) {
for (const error of document.errors) {
if (
error instanceof YAML.YAMLSemanticError &&
error instanceof YAMLSemanticError &&
error.message === 'Map keys must be unique; "<<" is repeated'
) {
continue;
Expand Down
44 changes: 22 additions & 22 deletions src/preprocess.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import type * as YAML from "./yaml.js";
import type * as YAML from "yaml";

type YamlCstNode =
| YAML.cst.Alias
| YAML.cst.BlankLine
| YAML.cst.BlockFolded
| YAML.cst.BlockLiteral
| YAML.cst.BlockValue
| YAML.cst.Comment
| YAML.cst.Directive
| YAML.cst.Document
| YAML.cst.FlowCollection
| YAML.cst.FlowMap
| YAML.cst.FlowSeq
| YAML.cst.Map
| YAML.cst.MapItem
| YAML.cst.MapKey
| YAML.cst.MapValue
| YAML.cst.PlainValue
| YAML.cst.QuoteDouble
| YAML.cst.QuoteSingle
| YAML.cst.QuoteValue
| YAML.cst.Seq
| YAML.cst.SeqItem;
| YAML.CST.Alias
| YAML.CST.BlankLine
| YAML.CST.BlockFolded
| YAML.CST.BlockLiteral
| YAML.CST.BlockValue
| YAML.CST.Comment
| YAML.CST.Directive
| YAML.CST.Document
| YAML.CST.FlowCollection
| YAML.CST.FlowMap
| YAML.CST.FlowSeq
| YAML.CST.Map
| YAML.CST.MapItem
| YAML.CST.MapKey
| YAML.CST.MapValue
| YAML.CST.PlainValue
| YAML.CST.QuoteDouble
| YAML.CST.QuoteSingle
| YAML.CST.QuoteValue
| YAML.CST.Seq
| YAML.CST.SeqItem;

export function removeCstBlankLine(node: YamlCstNode) {
switch (node.type) {
Expand Down
9 changes: 6 additions & 3 deletions src/transforms/alias.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type * as YAMLTypes from "yaml/types";
import { createAlias } from "../factories/alias.js";
import type Context from "./context.js";
import { type Alias } from "../types.js";
import type * as YAML from "../yaml.js";
import type Context from "./context.js";

export function transformAlias(alias: YAML.ast.Alias, context: Context): Alias {
export function transformAlias(
alias: YAMLTypes.Alias,
context: Context,
): Alias {
const cstNode = alias.cstNode!;
return createAlias(
context.transformRange({
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/block-folded.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type * as YAML from "yaml";
import { createBlockFolded } from "../factories/block-folded.js";
import type Context from "./context.js";
import { type BlockFolded } from "../types.js";
import type * as YAML from "../yaml.js";
import { transformAstBlockValue } from "./block-value.js";
import type Context from "./context.js";

export function transformBlockFolded(
blockFolded: YAML.ast.BlockFolded,
blockFolded: YAML.AST.BlockFolded,
context: Context,
): BlockFolded {
return createBlockFolded(transformAstBlockValue(blockFolded, context));
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/block-literal.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type * as YAML from "yaml";
import { createBlockLiteral } from "../factories/block-literal.js";
import type Context from "./context.js";
import { type BlockLiteral } from "../types.js";
import type * as YAML from "../yaml.js";
import { transformAstBlockValue } from "./block-value.js";
import type Context from "./context.js";

export function transformBlockLiteral(
blockLiteral: YAML.ast.BlockLiteral,
blockLiteral: YAML.AST.BlockLiteral,
context: Context,
): BlockLiteral {
return createBlockLiteral(transformAstBlockValue(blockLiteral, context));
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/block-value.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type * as YAML from "yaml";
import { createBlockValue } from "../factories/block-value.js";
import type Context from "./context.js";
import { type BlockValue, type Comment } from "../types.js";
import { getPointText } from "../utils/get-point-text.js";
import type * as YAML from "../yaml.js";
import { transformContent } from "./content.js";
import type Context from "./context.js";

enum Chomping {
CLIP = "clip",
Expand All @@ -12,7 +12,7 @@ enum Chomping {
}

export function transformAstBlockValue(
blockValue: YAML.ast.BlockFolded | YAML.ast.BlockLiteral,
blockValue: YAML.AST.BlockFolded | YAML.AST.BlockLiteral,
context: Context,
): BlockValue {
const cstNode = blockValue.cstNode!;
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/comment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type * as YAML from "yaml";
import { createComment } from "../factories/comment.js";
import type Context from "./context.js";
import { type Comment } from "../types.js";
import type * as YAML from "../yaml.js";
import type Context from "./context.js";

export function transformComment(
comment: YAML.cst.Comment,
comment: YAML.CST.Comment,
context: Context,
): Comment {
return createComment(context.transformRange(comment.range!), comment.comment);
Expand Down
9 changes: 5 additions & 4 deletions src/transforms/content.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type * as YAML from "yaml";
import type * as YAMLTypes from "yaml/types";
import { PropLeadingCharacter } from "../constants.js";
import { createAnchor } from "../factories/anchor.js";
import { createComment } from "../factories/comment.js";
import { createContent } from "../factories/content.js";
import { createTag } from "../factories/tag.js";
import type Context from "./context.js";
import { type Anchor, type Comment, type Content, type Tag } from "../types.js";
import type * as YAML from "../yaml.js";
import type Context from "./context.js";

export function transformContent(
node: YAML.ast.Node,
node: YAMLTypes.Node,
context: Context,
isNotMiddleComment: (comment: Comment) => boolean = () => false,
): Content {
const cstNode = node.cstNode!;

const middleComments: Comment[] = [];

let firstTagOrAnchorRange: YAML.cst.Range | null = null;
let firstTagOrAnchorRange: YAML.CST.Range | null = null;

let tag: Tag | null = null;
let anchor: Anchor | null = null;
Expand Down
18 changes: 13 additions & 5 deletions src/transforms/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type * as YAML from "../yaml.js";
import type * as YAML from "yaml";
import type * as YAMLTypes from "yaml/types";
import { createPosition } from "../factories/position.js";
import type { Comment, Point, Position, Content, Range } from "../types.js";
import type {
Comment,
Content,
ParsedCST,
Point,
Position,
Range,
} from "../types.js";
import { transformContent } from "./content.js";
import { transformNode, type YamlNode, type YamlToUnist } from "./transform.js";

Expand All @@ -24,7 +32,7 @@ class Context {
#cst;
#cstContext: CSTContext | undefined;

constructor(cst: YAML.ParsedCST, text: string) {
constructor(cst: ParsedCST, text: string) {
this.text = text;
this.#cst = cst;
}
Expand All @@ -34,7 +42,7 @@ class Context {
const [document] = this.#cst;
const Node = Object.getPrototypeOf(
Object.getPrototypeOf(document),
) as YAML.cst.Node;
) as YAML.CST.Node;
rangeAsLinePosGetter = Object.getOwnPropertyDescriptor(
Node,
"rangeAsLinePos",
Expand Down Expand Up @@ -88,7 +96,7 @@ class Context {
return transformNode(node, this);
}

transformContent(node: YAML.ast.Node): Content {
transformContent(node: YAMLTypes.Node): Content {
return transformContent(node, this);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/directive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type * as YAML from "yaml";
import { createDirective } from "../factories/directive.js";
import type Context from "./context.js";
import type { Directive, Range } from "../types.js";
import { extractPropComments } from "../utils/extract-prop-comments.js";
import type * as YAML from "../yaml.js";
import type Context from "./context.js";

export function transformDirective(
directive: YAML.cst.Directive,
directive: YAML.CST.Directive,
context: Context,
): Directive {
extractPropComments(directive, context);
Expand Down
Loading