Skip to content

Commit

Permalink
Merge 5628d47 into 2fbba00
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 9, 2021
2 parents 2fbba00 + 5628d47 commit 518e26e
Showing 1 changed file with 66 additions and 51 deletions.
117 changes: 66 additions & 51 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ export function convertRoot(node: Root, code: string): YAMLProgram {
}
return c
})
const stripCommentCode = comments.reduce(
(text, comment) =>
text.slice(0, comment.range[0]) +
text.slice(...comment.range).replace(/\S/gu, " ") +
text.slice(comment.range[1]),
code,
)
let stripCommentCode = ""
let startIndex = 0
for (const comment of comments) {
stripCommentCode += code.slice(startIndex, comment.range[0])
stripCommentCode += code.slice(...comment.range).replace(/\S/gu, " ")
startIndex = comment.range[1]
}
stripCommentCode += code.slice(startIndex)

const tokens: Token[] = []
const ast: YAMLProgram = {
type: "Program",
Expand All @@ -85,61 +87,54 @@ export function convertRoot(node: Root, code: string): YAMLProgram {
for (const n of node.children) {
ast.body.push(convertDocument(n, tokens, stripCommentCode, ast))
}
const stripTokensCode = tokens.reduce(
(text, token) =>
text.slice(0, token.range[0]) +
text.slice(...token.range).replace(/\S/gu, " ") +
text.slice(token.range[1]),
stripCommentCode,
)

const useRanges = sort(tokens).map((t) => t.range)
let range = useRanges.shift()

let line = 1
let column = 0
for (let index = 0; index < stripTokensCode.length; index++) {
const c = stripTokensCode[index]
const len = stripCommentCode.length
for (let index = 0; index < len; index++) {
const c = stripCommentCode[index]
if (c === "\n") {
line++
column = 0
continue
} else if (c.trim()) {
if (isPunctuator(c)) {
addToken(
tokens,
"Punctuator",
{
range: [index, index + 1],
loc: {
start: {
line,
column,
},
end: {
line,
column: column + 1,
},
}
if (range) {
while (range && range[1] <= index) {
range = useRanges.shift()
}
if (range && range[0] <= index) {
column++
continue
}
}

if (isPunctuator(c)) {
addToken(
tokens,
"Punctuator",
{
range: [index, index + 1],
loc: {
start: {
line,
column,
},
end: {
line,
column: column + 1,
},
},
stripTokensCode,
)
}
},
stripCommentCode,
)
}

column++
}
tokens.sort((a, b) => {
if (a.range[0] > b.range[0]) {
return 1
}
if (a.range[0] < b.range[0]) {
return -1
}
if (a.range[1] > b.range[1]) {
return 1
}
if (a.range[1] < b.range[1]) {
return -1
}
return 0
})
sort(tokens)
return ast

/**
Expand All @@ -154,7 +149,6 @@ export function convertRoot(node: Root, code: string): YAMLProgram {
c === "}" ||
c === "[" ||
c === "]" ||
//
c === "?"
)
}
Expand Down Expand Up @@ -1085,3 +1079,24 @@ function addToken(
...loc,
})
}

/**
* Sort tokens
*/
function sort(tokens: (Token | Comment)[]) {
return tokens.sort((a, b) => {
if (a.range[0] > b.range[0]) {
return 1
}
if (a.range[0] < b.range[0]) {
return -1
}
if (a.range[1] > b.range[1]) {
return 1
}
if (a.range[1] < b.range[1]) {
return -1
}
return 0
})
}

0 comments on commit 518e26e

Please sign in to comment.