Skip to content

Commit da8dfac

Browse files
authored
refactor: remove yaml.js (#295)
1 parent 16c3178 commit da8dfac

37 files changed

+248
-234
lines changed

.versionrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"preset": "angular",
3+
"scripts": {
4+
"postchangelog": "prettier --write CHANGELOG.md"
5+
}
6+
}

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ All notable changes to this project will be documented in this file. See [standa
44

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

7-
87
### Bug Fixes
98

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

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

eslint.config.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import globals from "globals";
21
import eslintJs from "@eslint/js";
32
import eslintConfigPrettier from "eslint-config-prettier/flat";
3+
import eslintPluginSimpleImportSort from "eslint-plugin-simple-import-sort";
4+
import globals from "globals";
45
import tseslint from "typescript-eslint";
56

67
export default tseslint.config(
@@ -11,6 +12,9 @@ export default tseslint.config(
1112
languageOptions: {
1213
globals: { ...globals.builtin, ...globals.node },
1314
},
15+
plugins: {
16+
"simple-import-sort": eslintPluginSimpleImportSort,
17+
},
1418
rules: {
1519
"@typescript-eslint/no-namespace": "off",
1620
"@typescript-eslint/no-non-null-assertion": "off",
@@ -20,6 +24,33 @@ export default tseslint.config(
2024
{ fixStyle: "inline-type-imports" },
2125
],
2226
"no-constant-condition": ["error", { checkLoops: false }],
27+
28+
"simple-import-sort/imports": [
29+
"error",
30+
{
31+
groups: [
32+
// https://github.com/lydell/eslint-plugin-simple-import-sort/blob/20e25f3b83c713825f96b8494e2091e6600954d6/src/imports.js#L5-L19
33+
// Side effect imports.
34+
[String.raw`^\u0000`],
35+
// Remove blank lines between groups
36+
// https://github.com/lydell/eslint-plugin-simple-import-sort#how-do-i-remove-all-blank-lines-between-imports
37+
[
38+
// Node.js builtins prefixed with `node:`.
39+
"^node:",
40+
// Packages.
41+
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
42+
String.raw`^@?\w`,
43+
// Absolute imports and other imports such as Vue-style `@/foo`.
44+
// Anything not matched in another group.
45+
"^",
46+
// Relative imports.
47+
// Anything that starts with a dot.
48+
String.raw`^\.`,
49+
],
50+
],
51+
},
52+
],
53+
"simple-import-sort/exports": "error",
2354
},
2455
},
2556
{ ignores: ["lib/", ".yarn/"] },

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"lint": "run-p \"lint:*\"",
2626
"lint:eslint": "eslint .",
2727
"lint:prettier": "prettier . --check",
28+
"lint:types": "tsc",
2829
"fix": "run-s \"fix:*\"",
2930
"fix:eslint": "yarn lint:eslint --fix",
3031
"fix:prettier": "prettier . --write",
@@ -42,6 +43,7 @@
4243
"@vitest/coverage-v8": "3.0.8",
4344
"eslint": "9.22.0",
4445
"eslint-config-prettier": "10.1.1",
46+
"eslint-plugin-simple-import-sort": "12.1.1",
4547
"globals": "16.0.0",
4648
"jest-snapshot-serializer-raw": "2.0.0",
4749
"npm-run-all2": "7.0.2",

src/helpers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { wrap } from "jest-snapshot-serializer-raw";
2+
import { type YAMLSemanticError, type YAMLSyntaxError } from "yaml/util";
23
import { parse } from "./parse.js";
34
import {
45
type Anchor,
@@ -9,7 +10,6 @@ import {
910
type Tag,
1011
type YamlUnistNode,
1112
} from "./types.js";
12-
import type * as YAML from "./yaml.js";
1313

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

@@ -335,9 +335,7 @@ export function testSyntaxError(text: string, message?: string) {
335335
}
336336
}
337337

338-
function isYAMLError(
339-
e: any,
340-
): e is YAML.YAMLSyntaxError | YAML.YAMLSemanticError {
338+
function isYAMLError(e: any): e is YAMLSyntaxError | YAMLSemanticError {
341339
return (
342340
e instanceof Error &&
343341
(e.name === "YAMLSyntaxError" || e.name === "YAMLSemanticError")

src/parse.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
import { Document, parseCST } from "yaml";
2+
import { YAMLSemanticError } from "yaml/util";
13
import { attachComments } from "./attach.js";
24
import { createRoot } from "./factories/root.js";
35
import { removeCstBlankLine } from "./preprocess.js";
6+
import Context from "./transforms/context.js";
47
import { transformError } from "./transforms/error.js";
58
import { type Root } from "./types.js";
69
import { addOrigRange } from "./utils/add-orig-range.js";
710
import { removeFakeNodes } from "./utils/remove-fake-nodes.js";
811
import { updatePositions } from "./utils/update-positions.js";
9-
import * as YAML from "./yaml.js";
10-
import Context from "./transforms/context.js";
1112

1213
export function parse(text: string): Root {
13-
const cst = YAML.parseCST(text);
14+
const cst = parseCST(text);
1415

1516
addOrigRange(cst);
1617

1718
const documents = cst.map(cstDocument =>
18-
new YAML.Document({
19+
new Document({
1920
merge: false,
2021
keepCstNodes: true,
2122
}).parse(cstDocument),
@@ -26,7 +27,7 @@ export function parse(text: string): Root {
2627
for (const document of documents) {
2728
for (const error of document.errors) {
2829
if (
29-
error instanceof YAML.YAMLSemanticError &&
30+
error instanceof YAMLSemanticError &&
3031
error.message === 'Map keys must be unique; "<<" is repeated'
3132
) {
3233
continue;

src/preprocess.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import type * as YAML from "./yaml.js";
1+
import type * as YAML from "yaml";
22

33
type YamlCstNode =
4-
| YAML.cst.Alias
5-
| YAML.cst.BlankLine
6-
| YAML.cst.BlockFolded
7-
| YAML.cst.BlockLiteral
8-
| YAML.cst.BlockValue
9-
| YAML.cst.Comment
10-
| YAML.cst.Directive
11-
| YAML.cst.Document
12-
| YAML.cst.FlowCollection
13-
| YAML.cst.FlowMap
14-
| YAML.cst.FlowSeq
15-
| YAML.cst.Map
16-
| YAML.cst.MapItem
17-
| YAML.cst.MapKey
18-
| YAML.cst.MapValue
19-
| YAML.cst.PlainValue
20-
| YAML.cst.QuoteDouble
21-
| YAML.cst.QuoteSingle
22-
| YAML.cst.QuoteValue
23-
| YAML.cst.Seq
24-
| YAML.cst.SeqItem;
4+
| YAML.CST.Alias
5+
| YAML.CST.BlankLine
6+
| YAML.CST.BlockFolded
7+
| YAML.CST.BlockLiteral
8+
| YAML.CST.BlockValue
9+
| YAML.CST.Comment
10+
| YAML.CST.Directive
11+
| YAML.CST.Document
12+
| YAML.CST.FlowCollection
13+
| YAML.CST.FlowMap
14+
| YAML.CST.FlowSeq
15+
| YAML.CST.Map
16+
| YAML.CST.MapItem
17+
| YAML.CST.MapKey
18+
| YAML.CST.MapValue
19+
| YAML.CST.PlainValue
20+
| YAML.CST.QuoteDouble
21+
| YAML.CST.QuoteSingle
22+
| YAML.CST.QuoteValue
23+
| YAML.CST.Seq
24+
| YAML.CST.SeqItem;
2525

2626
export function removeCstBlankLine(node: YamlCstNode) {
2727
switch (node.type) {

src/transforms/alias.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import type * as YAMLTypes from "yaml/types";
12
import { createAlias } from "../factories/alias.js";
2-
import type Context from "./context.js";
33
import { type Alias } from "../types.js";
4-
import type * as YAML from "../yaml.js";
4+
import type Context from "./context.js";
55

6-
export function transformAlias(alias: YAML.ast.Alias, context: Context): Alias {
6+
export function transformAlias(
7+
alias: YAMLTypes.Alias,
8+
context: Context,
9+
): Alias {
710
const cstNode = alias.cstNode!;
811
return createAlias(
912
context.transformRange({

src/transforms/block-folded.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type * as YAML from "yaml";
12
import { createBlockFolded } from "../factories/block-folded.js";
2-
import type Context from "./context.js";
33
import { type BlockFolded } from "../types.js";
4-
import type * as YAML from "../yaml.js";
54
import { transformAstBlockValue } from "./block-value.js";
5+
import type Context from "./context.js";
66

77
export function transformBlockFolded(
8-
blockFolded: YAML.ast.BlockFolded,
8+
blockFolded: YAML.AST.BlockFolded,
99
context: Context,
1010
): BlockFolded {
1111
return createBlockFolded(transformAstBlockValue(blockFolded, context));

src/transforms/block-literal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type * as YAML from "yaml";
12
import { createBlockLiteral } from "../factories/block-literal.js";
2-
import type Context from "./context.js";
33
import { type BlockLiteral } from "../types.js";
4-
import type * as YAML from "../yaml.js";
54
import { transformAstBlockValue } from "./block-value.js";
5+
import type Context from "./context.js";
66

77
export function transformBlockLiteral(
8-
blockLiteral: YAML.ast.BlockLiteral,
8+
blockLiteral: YAML.AST.BlockLiteral,
99
context: Context,
1010
): BlockLiteral {
1111
return createBlockLiteral(transformAstBlockValue(blockLiteral, context));

0 commit comments

Comments
 (0)