Skip to content

Commit 1f8a535

Browse files
committed
feat(index): add a _missing comma_ fixer
1 parent c902974 commit 1f8a535

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ const extraChar = err =>
3030
const trailingChar = err => ['.', ',', 'x', 'b', 'o'].includes(err.found)
3131

3232
const missingChar = err => {
33-
return err
33+
return err.expected[0].text === ',' && ['"', '[', '{'].includes(err.found)
3434
}
3535

3636
/*eslint-disable no-console */
3737
const fixJson = (err, data) => {
3838
const lines = data.split('\n')
39-
// lines.forEach((l, i) => process.stdout.write(`${chalk.yellow(i)} ${l}\n`))
40-
// console.log(chalk.red('err='))
41-
// console.dir(err)
39+
lines.forEach((l, i) => process.stdout.write(`${chalk.yellow(i)} ${l}\n`))
40+
console.log(chalk.red('err='))
41+
console.dir(err)
4242
const start = err.location.start
4343
const fixedData = [...lines]
4444

@@ -50,7 +50,6 @@ const fixJson = (err, data) => {
5050
fixedLine = fixedLine.substr(0, fixedLine.length - 1)
5151
// console.log(`fixed line='${fixedLine}'`)
5252
fixedData[targetLine] = fixedLine
53-
return doubleCheck(fixedData.join('\n'))
5453
} else if (trailingChar(err)) {
5554
const targetLine = start.line - 1
5655
const brokenLine = removeLinebreak(lines[targetLine])
@@ -68,16 +67,16 @@ const fixJson = (err, data) => {
6867
}
6968

7069
fixedData[targetLine] = baseNumber
71-
return doubleCheck(fixedData.join('\n'))
7270
} else if (missingChar(err)) {
73-
//s5
74-
process.stdout.write(`${chalk.yellow('Fix not supported yet')}\n`)
71+
const targetLine = start.line - 2
72+
const brokenLine = removeLinebreak(lines[targetLine])
73+
console.log(`broken line='${brokenLine}'`)
74+
fixedData[targetLine] = `${brokenLine},`
7575
} else
7676
throw new Error(
7777
`Unsupported issue: ${err.message} (please open an issue at the repo)`,
7878
)
79-
80-
return null
79+
return doubleCheck(fixedData.join('\n'))
8180
}
8281
/*eslint-enable no-console */
8382

src/__tests__/index.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ describe('keeps a correct file intact', () => {
3737
// })
3838
// })
3939

40+
// it('fix missing quotes', () => {
41+
// const json = fs.readFileSync('./test/samples/noQuotes.json', 'utf-8')
42+
// const {data, changed} = jf(json)
43+
// expect(changed).toBeTruthy()
44+
// expect(data).toStrictEqual({
45+
// name: 'sample #10',
46+
// type: 'JSON',
47+
// error: 'missing quotes',
48+
// version: "one zero"
49+
// })
50+
// })
51+
4052
describe('fix trailing characters', () => {
4153
it('dots', () => {
4254
const json = fs.readFileSync('./test/samples/trailingDot.json', 'utf-8')
@@ -111,14 +123,14 @@ it('fix extra characters', () => {
111123
})
112124
})
113125

114-
// it('fix missing commas', () => {
115-
// const json = fs.readFileSync('./test/samples/missing.json', 'utf-8')
116-
// const {data, changed} = jf(json)
117-
// expect(changed).toBeTruthy()
118-
// expect(data).toEqual({
119-
// name: 'sample #5',
120-
// type: 'JSON',
121-
// error: 'missing comma',
122-
// version: 5
123-
// })
124-
// })
126+
it('fix missing commas', () => {
127+
const json = fs.readFileSync('./test/samples/missing.json', 'utf-8')
128+
const {data, changed} = jf(json)
129+
expect(changed).toBeTruthy()
130+
expect(data).toEqual({
131+
name: 'sample #5',
132+
type: 'JSON',
133+
error: 'missing comma',
134+
version: 5,
135+
})
136+
})

test/samples/noQuotes.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "sample #10",
3+
"type": "JSON",
4+
"error": "missing quotes",
5+
"version": one zero
6+
}

0 commit comments

Comments
 (0)