Skip to content

Commit b4a13e4

Browse files
authored
Add YAMLSyntaxError (#309)
1 parent 7b29a97 commit b4a13e4

File tree

7 files changed

+27
-39
lines changed

7 files changed

+27
-39
lines changed

src/factories/error.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
type Position,
99
type Root,
1010
type Tag,
11-
type YAMLSyntaxError,
1211
type YamlUnistNode,
1312
} from "./types.ts";
13+
import { type YAMLSyntaxError } from "./yaml-syntax-error.ts";
1414

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

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from "./parse.ts";
2-
export * from "./types.ts";
1+
export { parse } from "./parse.ts";
2+
export type * from "./types.ts";
3+
export { YAMLSyntaxError } from "./yaml-syntax-error.ts";

src/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ 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 { transformError } from "./transforms/error.ts";
65
import type { ParseOptions, Root } from "./types.ts";
76
import { removeFakeNodes } from "./utils/remove-fake-nodes.ts";
87
import { updatePositions } from "./utils/update-positions.ts";
8+
import { YAMLSyntaxError } from "./yaml-syntax-error.ts";
99

1010
export function parse(text: string, options?: ParseOptions): Root {
1111
const lineCounter = new YAML.LineCounter();
@@ -48,7 +48,7 @@ export function parse(text: string, options?: ParseOptions): Root {
4848
function throwParseError(document: YAML.Document.Parsed, context: Context) {
4949
const { errors } = document;
5050
if (errors.length > 0) {
51-
throw transformError(errors[0], context);
51+
throw new YAMLSyntaxError(context, errors[0]);
5252
}
5353
return document;
5454
}

src/transforms/error.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ export interface EndCommentAttachable {
6262
endComments: Comment[];
6363
}
6464

65-
export interface YAMLSyntaxError extends SyntaxError {
66-
source: string;
67-
position: Position;
68-
}
69-
7065
export type YamlUnistNode =
7166
| Comment
7267
| Tag

src/yaml-syntax-error.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type * as YAML from "yaml";
2+
import type Context from "./transforms/context.ts";
3+
import { type Position } from "./types";
4+
5+
export class YAMLSyntaxError extends SyntaxError {
6+
name = "YAMLSyntaxError";
7+
8+
code: YAML.ErrorCode;
9+
10+
source: string;
11+
position: Position;
12+
13+
constructor(context: Context, error: YAML.YAMLError) {
14+
super(error.message, { cause: error });
15+
16+
this.code = error.code;
17+
18+
this.source = context.text;
19+
this.position = context.transformRange(error.pos);
20+
}
21+
}

0 commit comments

Comments
 (0)