Skip to content

Commit f7b04cc

Browse files
committed
Refactor
1 parent 94cc161 commit f7b04cc

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/parse.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { createRoot } from "./factories/root.js";
55
import { removeCstBlankLine } from "./preprocess.js";
66
import Context from "./transforms/context.js";
77
import { transformError } from "./transforms/error.js";
8+
import type { Document } from "./types.js";
89
import { type ParseOptions, type Root } from "./types.js";
910
import { removeFakeNodes } from "./utils/remove-fake-nodes.js";
1011
import { updatePositions } from "./utils/update-positions.js";
11-
import type { Document } from "./types.js";
1212

1313
const MAP_KEY_DUPLICATE_ERROR_MESSAGE_PREFIX = 'Map keys must be unique; "';
1414
const MAP_KEY_DUPLICATE_ERROR_MESSAGE_SUFFIX = '" is repeated';
@@ -32,17 +32,11 @@ function shouldIgnoreError(
3232

3333
export function parse(text: string, options?: ParseOptions): Root {
3434
const allowDuplicateKeysInMap = options?.allowDuplicateKeysInMap;
35-
const context = new Context(text);
36-
35+
const cst = YAML.parseCST(text);
36+
const context = new Context(cst, text);
3737
const documents: Document[] = [];
3838

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) {
39+
for (const cstDocument of cst) {
4640
const yamlDocument = new YAML.Document({
4741
merge: false,
4842
keepCstNodes: true,
@@ -61,6 +55,12 @@ export function parse(text: string, options?: ParseOptions): Root {
6155
documents.push(document);
6256
}
6357

58+
const root = createRoot(
59+
context.transformRange({ origStart: 0, origEnd: text.length }),
60+
documents,
61+
context.comments,
62+
);
63+
6464
attachComments(root);
6565
updatePositions(root);
6666
removeFakeNodes(root);

src/transforms/context.ts

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

@@ -22,30 +29,30 @@ let rangeAsLinePosGetter: RangeAsLinePosGetter;
2229
class Context {
2330
text;
2431
comments: Comment[] = [];
25-
cst;
32+
#cst;
2633
#cstContext: CSTContext | undefined;
2734

28-
constructor(text: string) {
35+
constructor(cst: ParsedCST, text: string) {
2936
this.text = text;
30-
this.cst = YAML.parseCST(text);
37+
this.#cst = cst;
3138
this.setOrigRanges();
3239
}
3340

3441
setOrigRanges() {
35-
if (this.cst.setOrigRanges()) {
42+
if (this.#cst.setOrigRanges()) {
3643
return;
3744
}
3845

3946
// From `yaml/parse-cst`
4047
// https://github.com/eemeli/yaml/blob/4cdcde632ece71155f3108ec0120c1a0329a6914/src/cst/parse.js#L22
41-
for (const document of this.cst) {
48+
for (const document of this.#cst) {
4249
document.setOrigRanges([], 0);
4350
}
4451
}
4552

4653
#getRangePosition(range: Range): { start: Point; end: Point } {
4754
if (!rangeAsLinePosGetter) {
48-
const [document] = this.cst;
55+
const [document] = this.#cst;
4956
const Node = Object.getPrototypeOf(
5057
Object.getPrototypeOf(document),
5158
) as YAML.CST.Node;

0 commit comments

Comments
 (0)