Skip to content

Commit d671f0c

Browse files
committed
fix(fixer): improved trailing character fixer
It can now fix trailing character that are at the end of a line right before an empty one fix #18
1 parent fbf17a4 commit d671f0c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/__tests__/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ describe('fix trailing characters', () => {
112112
})
113113
})
114114

115+
it('chars', () => {
116+
const json = fs.readFileSync('./test/samples/trailingChar.json', 'utf-8')
117+
const {data, changed} = jf(json)
118+
expect(changed).toBeTruthy()
119+
expect(data).toEqual([
120+
{
121+
test1: '1',
122+
test2: {
123+
a: 'b',
124+
c: {},
125+
},
126+
},
127+
])
128+
})
129+
115130
it('hex\'s "x"', () => {
116131
const json = fs.readFileSync('./test/samples/x.json', 'utf-8')
117132
const {data, changed} = jf(json)

src/fixer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const {parse} = require('./json.pjs')
44

55
const fixExtraChar = ({fixedData, verbose, targetLine}) => {
66
if (verbose) psw(chalk.magenta('Extra character'))
7+
if (fixedData[targetLine] === '') --targetLine
78
const brokenLine = removeLinebreak(fixedData[targetLine])
9+
810
let fixedLine = brokenLine.trimEnd()
911
fixedLine = fixedLine.substr(0, fixedLine.length - 1)
1012
fixedData[targetLine] = fixedLine

test/samples/trailingChar.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"test1": "1",
4+
"test2": {
5+
"a": "b",
6+
"c": {
7+
8+
},
9+
10+
}
11+
}
12+
]

0 commit comments

Comments
 (0)