Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit b2631f1

Browse files
committed
🐛 fix bug for slash
1 parent a83a929 commit b2631f1

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/utils.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,21 @@ export function converter(node: object | null | undefined): unknown {
6666
}
6767

6868
if (isStringLiteral(node)) {
69-
const { value } = node
69+
let { value } = node
70+
if (/\\/.test(value)) {
71+
value = value.replace(/\\/g, '\\\\')
72+
}
73+
7074
if (/"/.test(value)) {
71-
return value.replace(/"/g, '\\"')
75+
value = value.replace(/"/g, '\\"')
7276
}
7377

7478
if (/[\t\f\r\n\b]/g.test(value)) {
7579
const codes = ['\t', '\f', '\r', '\n', '\t', '\b']
7680
const replaceCodes = ['\\t', '\\f', '\\r', '\\n', '\\t', '\\b']
77-
let replaceValue = value
7881
for (let i = 0; i < codes.length; i++) {
79-
replaceValue = replaceValue.replace(
80-
new RegExp(codes[i]),
81-
replaceCodes[i]
82-
)
82+
value = value.replace(new RegExp(codes[i]), replaceCodes[i])
8383
}
84-
return replaceValue
8584
}
8685

8786
return value

test/__fixtures__/object/backslash/input.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ const test3 = { foo: 'fo\"o' }
44
const test4 = { foo: 'fo\'o' }
55
const test5 = { foo: 'fo\to' }
66
const test6 = { foo: 'fo\fo' }
7-
const test7= { foo: 'fo\ro' }
7+
const test7 = { foo: 'fo\ro' }
88
const test8 = { foo: 'fo\no' }
9-
const test9= { foo: 'fo\bo' }
10-
const test10= { foo: 'fo\r\no' }
9+
const test9 = { foo: 'fo\bo' }
10+
const test10 = { foo: 'fo\r\no' }
11+
const test11 = { foo: 'fo\o' }
12+
const test12 = { foo: 'fo\\o' }
13+
const test13 = { foo: 'fo\\\o' }
14+
const test14 = { foo: 'fo\\\\o' }
15+
const test15 = { foo: '\\\\' }
1116
const res = [
1217
test1,
1318
test2,
@@ -18,5 +23,10 @@ const res = [
1823
test7,
1924
test8,
2025
test9,
21-
test10
26+
test10,
27+
test11,
28+
test12,
29+
test13,
30+
test14,
31+
test15
2232
]

test/__fixtures__/object/backslash/output.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const test7 = JSON.parse('{"foo":"fo\\ro"}')
88
const test8 = JSON.parse('{"foo":"fo\\no"}')
99
const test9 = JSON.parse('{"foo":"fo\\bo"}')
1010
const test10 = JSON.parse('{"foo":"fo\\r\\no"}')
11+
const test11 = JSON.parse('{"foo":"foo"}')
12+
const test12 = JSON.parse('{"foo":"fo\\\\o"}')
13+
const test13 = JSON.parse('{"foo":"fo\\\\o"}')
14+
const test14 = JSON.parse('{"foo":"fo\\\\\\\\o"}')
15+
const test15 = JSON.parse('{"foo":"\\\\\\\\"}')
1116
const res = [
1217
test1,
1318
test2,
@@ -18,5 +23,10 @@ const res = [
1823
test7,
1924
test8,
2025
test9,
21-
test10
26+
test10,
27+
test11,
28+
test12,
29+
test13,
30+
test14,
31+
test15
2232
]

0 commit comments

Comments
 (0)