Skip to content

Commit 9017579

Browse files
committed
feat(index): added a square->curly brackets fixer
1 parent 21b4522 commit 9017579

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const doubleCheck = (data, verbose = false) => {
2929
}
3030

3131
const removeLinebreak = l => l.replace(/[\n\r]/g, '')
32+
const replaceChar = (str, idx, chr) =>
33+
str.substring(0, idx) + chr + str.substring(idx + 1)
3234

3335
const extraChar = err =>
3436
err.expected[0].type === 'other' && ['}', ']'].includes(err.found)
@@ -129,14 +131,29 @@ const fixJson = (err, data, verbose) => {
129131
const brokenLine = removeLinebreak(lines[targetLine])
130132
const fixedLine = brokenLine.replace(/(":\s*)'(.*?)'/g, '$1"$2"')
131133
fixedData[targetLine] = fixedLine
132-
} else if (missingQuotes(err)) {
133-
psw('pre')
134-
console.log(fixedData)
134+
} else if (missingQuotes(err))
135135
fixedData = fixMissingQuotes({start, fixedData, verbose})
136-
psw('post')
137-
console.log(fixedData)
138-
} else if (notSquare(err)) {
139-
console.log('WIP')
136+
else if (notSquare(err)) {
137+
let targetLine = start.line - 2
138+
const brokenLine = removeLinebreak(
139+
lines[targetLine].includes('[') ? lines[targetLine] : lines[++targetLine],
140+
)
141+
const fixedLine = replaceChar(brokenLine, start.column - 1, '{')
142+
fixedData[targetLine] = fixedLine
143+
144+
try {
145+
parse(fixedData.join('\n'))
146+
} catch (e) {
147+
const newStart = e.location.start
148+
const newTargetLine = newStart.line - 1
149+
const newLine = removeLinebreak(fixedData[newTargetLine]).replace(
150+
']',
151+
'}',
152+
)
153+
fixedData[newTargetLine] = newLine
154+
}
155+
156+
fixedData[targetLine] = fixedLine
140157
} else
141158
throw new Error(
142159
`Unsupported issue: ${err.message} (please open an issue at the repo)`,

src/__tests__/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,19 @@ it('fix missing commas', () => {
174174
})
175175

176176
describe('fix wrong brackets', () => {
177-
// it('square brackets', () => {
178-
// const json = fs.readFileSync('./test/samples/notSquare.json', 'utf-8')
179-
// const {data, changed} = jf(json)
180-
// expect(changed).toBeTruthy()
181-
// expect(data).toEqual({
182-
// name: 'sample #12',
183-
// error: 'wrong brackets',
184-
// info: {
185-
// type: 'JSON',
186-
// version: 12
187-
// }
188-
// })
189-
// })
177+
it('square brackets', () => {
178+
const json = fs.readFileSync('./test/samples/notSquare.json', 'utf-8')
179+
const {data, changed} = jf(json)
180+
expect(changed).toBeTruthy()
181+
expect(data).toEqual({
182+
name: 'sample #12',
183+
error: 'wrong brackets',
184+
info: {
185+
type: 'JSON',
186+
version: 12,
187+
},
188+
})
189+
})
190190
// it('curly brackets', () => {
191191
// })
192192
})

0 commit comments

Comments
 (0)