Skip to content

Commit

Permalink
feat(index): added a single->double quote fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkmann18 committed May 16, 2019
1 parent 1f8a535 commit b39844a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ const extraChar = err =>

const trailingChar = err => ['.', ',', 'x', 'b', 'o'].includes(err.found)

const missingChar = err => {
return err.expected[0].text === ',' && ['"', '[', '{'].includes(err.found)
}
const missingChar = err =>
err.expected[0].text === ',' && ['"', '[', '{'].includes(err.found)

const singleQuotes = err => err.found === "'"

/*eslint-disable no-console */
const fixJson = (err, data) => {
Expand All @@ -45,10 +46,8 @@ const fixJson = (err, data) => {
if (extraChar(err)) {
const targetLine = start.line - 2
const brokenLine = removeLinebreak(lines[targetLine])
// console.log(`broken line='${brokenLine.toString()}'`);
let fixedLine = brokenLine.trimEnd()
fixedLine = fixedLine.substr(0, fixedLine.length - 1)
// console.log(`fixed line='${fixedLine}'`)
fixedData[targetLine] = fixedLine
} else if (trailingChar(err)) {
const targetLine = start.line - 1
Expand All @@ -70,8 +69,12 @@ const fixJson = (err, data) => {
} else if (missingChar(err)) {
const targetLine = start.line - 2
const brokenLine = removeLinebreak(lines[targetLine])
console.log(`broken line='${brokenLine}'`)
fixedData[targetLine] = `${brokenLine},`
} else if (singleQuotes(err)) {
const targetLine = start.line - 1
const brokenLine = removeLinebreak(lines[targetLine])
const fixedLine = brokenLine.replace(/(":\s*)'(.*?)'/g, '$1"$2"')
fixedData[targetLine] = fixedLine
} else
throw new Error(
`Unsupported issue: ${err.message} (please open an issue at the repo)`,
Expand Down
22 changes: 11 additions & 11 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ describe('keeps a correct file intact', () => {
})
})

// it('fix single quotes', () => {
// const json = fs.readFileSync('./test/samples/singleQuote.json', 'utf-8')
// const {data, changed} = jf(json)
// expect(changed).toBeTruthy()
// expect(data).toStrictEqual({
// name: 'sample #4',
// type: 'JSON',
// error: 'single quote',
// version: "1"
// })
// })
it('fix single quotes', () => {
const json = fs.readFileSync('./test/samples/singleQuote.json', 'utf-8')
const {data, changed} = jf(json)
expect(changed).toBeTruthy()
expect(data).toStrictEqual({
name: 'sample #1',
type: 'JSON',
error: 'single quote',
version: '1',
})
})

// it('fix missing quotes', () => {
// const json = fs.readFileSync('./test/samples/noQuotes.json', 'utf-8')
Expand Down

0 comments on commit b39844a

Please sign in to comment.