Refactor CodeDiff

This commit is contained in:
Thomas Nordquist
2019-06-04 11:32:59 +02:00
parent 8cc3d416b8
commit e66f6d098a
5 changed files with 136 additions and 87 deletions

View File

@@ -1,8 +1,9 @@
const parse = require('json-to-ast')
interface JsonPropertyLocation {
export interface JsonPropertyLocation {
path: string
line: number
value: any
column: number
}
@@ -59,6 +60,7 @@ function jsonToPropertyPaths(ast: JsonAst, previousPath: Array<string> = []): Ar
let children: Array<Array<JsonPropertyLocation>> = []
if (ast.type === 'Literal') {
return [{
value: ast.value,
path: previousPath.join('.'),
line: ast.loc.start.line,
column: ast.loc.start.column,
@@ -75,3 +77,13 @@ function jsonToPropertyPaths(ast: JsonAst, previousPath: Array<string> = []): Ar
export function parseJson(formattedJson: string): Array<JsonPropertyLocation> {
return jsonToPropertyPaths((parse(formattedJson) as JsonAst), [])
}
export function literalsMappedByLines(formattedJson: string): Array<JsonPropertyLocation> {
const literals = jsonToPropertyPaths((parse(formattedJson) as JsonAst), [])
const lines = []
for (const literal of literals) {
lines[literal.line - 1] = literal
}
return lines
}