Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 7, 2022
1 parent e20a1e6 commit c9e114e
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/convert.ts
Expand Up @@ -23,6 +23,8 @@ import type {
YAMLWithMeta,
YAMLSequence,
YAMLNode,
Position,
SourceLocation,
} from "./ast"
import type { Context } from "./context"
import { tagResolvers } from "./tags"
Expand Down Expand Up @@ -1469,8 +1471,8 @@ function convertAnchorAndTag<V extends YAMLContent>(
parent,
...(valueLoc
? {
range: clone(valueLoc.range),
loc: clone(valueLoc.loc),
range: [...valueLoc.range],
loc: cloneLoc(valueLoc.loc),
}
: ctx.getConvertLocation(...toRange(cst))),
}
Expand Down Expand Up @@ -1653,18 +1655,21 @@ function sort(tokens: (Token | Comment)[]) {
/**
* clone the location.
*/
function clone<T>(loc: T): T {
if (typeof loc !== "object") {
return loc
function clonePos(loc: Position): Position {
return {
line: loc.line,
column: loc.column,
}
if (Array.isArray(loc)) {
return (loc as any).map(clone)
}
const n: any = {}
for (const key in loc) {
n[key] = clone(loc[key])
}

/**
* clone the location.
*/
function cloneLoc(loc: SourceLocation): SourceLocation {
return {
start: clonePos(loc.start),
end: clonePos(loc.end),
}
return n
}

/**
Expand Down Expand Up @@ -1692,7 +1697,7 @@ function adjustStartLoc(ast: YAMLNode, first: Locations | null | undefined) {
if (first && first.range[0] < ast.range[0]) {
// adjust location
ast.range[0] = first.range[0]
ast.loc.start = clone(first.loc.start)
ast.loc.start = clonePos(first.loc.start)
}
}

Expand All @@ -1701,6 +1706,6 @@ function adjustEndLoc(ast: YAMLNode, last: Locations | null | undefined) {
if (last && ast.range[1] < last.range[1]) {
// adjust location
ast.range[1] = last.range[1]
ast.loc.end = clone(last.loc.end)
ast.loc.end = clonePos(last.loc.end)
}
}

0 comments on commit c9e114e

Please sign in to comment.