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

Commit 3737ee5

Browse files
authored
Merge pull request #31 from nd-02110114/fix-test-exmaple
Refactor test and fix Line feed code
2 parents 4b4f41e + b2631f1 commit 3737ee5

File tree

29 files changed

+440
-307
lines changed

29 files changed

+440
-307
lines changed

example/.babelrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

example/input.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

example/output.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "babel-plugin-object-to-json-parse",
3-
"version": "0.0.9",
3+
"version": "0.1.0",
44
"main": "dist/index.js",
55
"scripts": {
66
"build": "rm -rf dist/** && tsc",
7-
"example": "babel example/input.js -o example/output.js && node example/output.js",
87
"fmt": "prettier --write \"src/**/*.ts\"",
98
"lint": "eslint 'src/**/*.ts' --fix",
10-
"test": "jest"
9+
"test": "ls test/__fixtures__/**/*.js | xargs -L1 node && jest"
1110
},
1211
"dependencies": {
1312
"@babel/core": "^7.7.7",
@@ -48,7 +47,8 @@
4847
}
4948
},
5049
"lint-staged": {
51-
"src/**/*.ts": ["yarn lint", "yarn fmt", "git add"]
50+
"src/**/*.ts": ["yarn lint", "yarn fmt", "git add"],
51+
"test/**/*.ts": ["yarn lint", "yarn fmt", "git add"]
5252
},
5353
"author": "nd-02110114 <nd.12021218@gmail.com>",
5454
"license": "MIT",

src/utils.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +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, '\\"')
76+
}
77+
78+
if (/[\t\f\r\n\b]/g.test(value)) {
79+
const codes = ['\t', '\f', '\r', '\n', '\t', '\b']
80+
const replaceCodes = ['\\t', '\\f', '\\r', '\\n', '\\t', '\\b']
81+
for (let i = 0; i < codes.length; i++) {
82+
value = value.replace(new RegExp(codes[i]), replaceCodes[i])
83+
}
7284
}
7385

7486
return value

test/__fixtures__/array/input.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const test1 = [1, "two", {three: 3}]
2+
const test2 = [{one: 1}, {two: 2}, {three: 3}]
3+
const res = [test1, test2]

test/__fixtures__/array/output.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const test1 = JSON.parse('[1,"two",{"three":3}]')
2+
const test2 = JSON.parse('[{"one":1},{"two":2},{"three":3}]')
3+
const res = [test1, test2]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const test1 = { foo: [null, 10, 'foo'] }
2+
const test2 = { foo: [null, [10, 2], [{ foo: 'foo' }]] }
3+
const res = [test1, test2]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const test1 = JSON.parse('{"foo":[null,10,"foo"]}')
2+
const test2 = JSON.parse('{"foo":[null,[10,2],[{"foo":"foo"}]]}')
3+
const res = [test1, test2]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const test1 = { foo: "fo\'o" }
2+
const test2 = { foo: "fo\"o" }
3+
const test3 = { foo: 'fo\"o' }
4+
const test4 = { foo: 'fo\'o' }
5+
const test5 = { foo: 'fo\to' }
6+
const test6 = { foo: 'fo\fo' }
7+
const test7 = { foo: 'fo\ro' }
8+
const test8 = { foo: 'fo\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: '\\\\' }
16+
const res = [
17+
test1,
18+
test2,
19+
test3,
20+
test4,
21+
test5,
22+
test6,
23+
test7,
24+
test8,
25+
test9,
26+
test10,
27+
test11,
28+
test12,
29+
test13,
30+
test14,
31+
test15
32+
]

0 commit comments

Comments
 (0)