Skip to content

Commit 2b8471a

Browse files
committed
Refactor
1 parent 81119ca commit 2b8471a

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

src/parse.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ function shouldIgnoreError(
3232

3333
export function parse(text: string, options?: ParseOptions): Root {
3434
const allowDuplicateKeysInMap = options?.allowDuplicateKeysInMap;
35-
const cst = YAML.parseCST(text);
36-
const context = new Context(cst, text);
37-
context.setOrigRanges();
35+
const context = new Context(text);
3836

3937
const documents: Document[] = [];
4038

41-
for (const cstDocument of cst) {
39+
const root = createRoot(
40+
context.transformRange({ origStart: 0, origEnd: text.length }),
41+
documents,
42+
context.comments,
43+
);
44+
45+
for (const cstDocument of context.cst) {
4246
const yamlDocument = new YAML.Document({
4347
merge: false,
4448
keepCstNodes: true,
@@ -57,12 +61,6 @@ export function parse(text: string, options?: ParseOptions): Root {
5761
documents.push(document);
5862
}
5963

60-
const root = createRoot(
61-
context.transformRange({ origStart: 0, origEnd: text.length }),
62-
documents,
63-
context.comments,
64-
);
65-
6664
attachComments(root);
6765
updatePositions(root);
6866
removeFakeNodes(root);

src/transforms/context.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import type * as YAML from "yaml";
1+
import YAML from "yaml";
22
import type * as YAMLTypes from "yaml/types";
33
import { createPosition } from "../factories/position.js";
4-
import type {
5-
Comment,
6-
Content,
7-
ParsedCST,
8-
Point,
9-
Position,
10-
Range,
11-
} from "../types.js";
4+
import type { Comment, Content, Point, Position, Range } from "../types.js";
125
import { transformContent } from "./content.js";
136
import { transformNode, type YamlNode, type YamlToUnist } from "./transform.js";
147

@@ -29,29 +22,30 @@ let rangeAsLinePosGetter: RangeAsLinePosGetter;
2922
class Context {
3023
text;
3124
comments: Comment[] = [];
32-
#cst;
25+
cst;
3326
#cstContext: CSTContext | undefined;
3427

35-
constructor(cst: ParsedCST, text: string) {
28+
constructor(text: string) {
3629
this.text = text;
37-
this.#cst = cst;
30+
this.cst = YAML.parseCST(text);
31+
this.setOrigRanges();
3832
}
3933

4034
setOrigRanges() {
41-
if (this.#cst.setOrigRanges()) {
35+
if (this.cst.setOrigRanges()) {
4236
return;
4337
}
4438

4539
// From `yaml/parse-cst`
4640
// https://github.com/eemeli/yaml/blob/4cdcde632ece71155f3108ec0120c1a0329a6914/src/cst/parse.js#L22
47-
for (const document of this.#cst) {
41+
for (const document of this.cst) {
4842
document.setOrigRanges([], 0);
4943
}
5044
}
5145

5246
#getRangePosition(range: Range): { start: Point; end: Point } {
5347
if (!rangeAsLinePosGetter) {
54-
const [document] = this.#cst;
48+
const [document] = this.cst;
5549
const Node = Object.getPrototypeOf(
5650
Object.getPrototypeOf(document),
5751
) as YAML.CST.Node;

0 commit comments

Comments
 (0)