Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 7, 2022
1 parent b446cc3 commit 1458592
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 75 deletions.
74 changes: 7 additions & 67 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import type {
Comment,
Locations,
Position,
Range,
Token,
YAMLProgram,
} from "./ast"
import type { Comment, Locations, Position, Range, Token } from "./ast"
import lodash from "lodash"
import { traverseNodes } from "./traverse"
import type { CST } from "yaml"
import { ParseError } from "."

Expand All @@ -18,79 +10,27 @@ export class Context {

public readonly comments: Comment[] = []

public hasCR = false

private readonly locs: LinesAndColumns

private readonly locsMap = new Map<number, Position>()

private readonly crs: number[]

public constructor(origCode: string) {
const len = origCode.length
const lineStartIndices = [0]
const crs: number[] = []
let code = ""
for (let index = 0; index < len; ) {
const c = origCode[index++]
if (c === "\r") {
const next = origCode[index++] || ""
const next = origCode[index]
if (next === "\n") {
code += next
crs.push(index - 2)
} else {
code += `\n${next}`
}
lineStartIndices.push(code.length)
} else {
code += c
if (c === "\n") {
lineStartIndices.push(code.length)
index++
}
lineStartIndices.push(index)
} else if (c === "\n") {
lineStartIndices.push(index)
}
}
this.code = code
this.code = origCode
this.locs = new LinesAndColumns(lineStartIndices)
this.hasCR = Boolean(crs.length)
this.crs = crs
}

public remapCR(ast: YAMLProgram): void {
const cache: Record<number, number> = {}
const remapIndex = (index: number): number => {
let result = cache[index]
if (result != null) {
return result
}
result = index
for (const cr of this.crs) {
if (cr < result) {
result++
} else {
break
}
}
return (cache[index] = result)
}
// eslint-disable-next-line func-style -- ignore
const remapRange = (range: [number, number]): [number, number] => {
return [remapIndex(range[0]), remapIndex(range[1])]
}

traverseNodes(ast, {
enterNode(node) {
node.range = remapRange(node.range)
},
leaveNode() {
// ignore
},
})
for (const token of ast.tokens) {
token.range = remapRange(token.range)
}
for (const comment of ast.comments) {
comment.range = remapRange(comment.range)
}
}

public getLocFromIndex(index: number): { line: number; column: number } {
Expand Down
4 changes: 0 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export function parseForESLint(

const ast = convertRoot(docs.cstNodes, docs.nodes, ctx)

if (ctx.hasCR) {
ctx.remapCR(ast)
}

return {
ast,
visitorKeys: KEYS,
Expand Down
21 changes: 17 additions & 4 deletions tests/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ describe("yaml-test-suite.", () => {
const astForWin = parse(inputForWin, inputFileName)
// check tokens
checkTokens(astForWin, inputForWin)

// const astJson = JSON.stringify(astForWin, replacerForWin, 2)
// assert.strictEqual(
// astJson,
// JSON.stringify(JSON.parse(output), replacerForWin, 2),
// )

// const value = fs.readFileSync(valueFileName, "utf8")
// assert.strictEqual(
// JSON.stringify(
// getStaticYAMLValue(astForWin),
// valueReplacer,
// 2,
// ),
// value,
// )
})
})
}
Expand All @@ -254,10 +270,7 @@ function checkTokens(ast: YAMLProgram, input: string) {
for (const token of allTokens) {
const value = token.type === "Block" ? `#${token.value}` : token.value

assert.strictEqual(
value,
input.slice(...token.range).replace(/\r\n/g, "\n"),
)
assert.strictEqual(value, input.slice(...token.range))
}
}

Expand Down

0 comments on commit 1458592

Please sign in to comment.