Skip to content

Commit eeee0cb

Browse files
fix: string value
1 parent 06e9df3 commit eeee0cb

File tree

4 files changed

+737
-32
lines changed

4 files changed

+737
-32
lines changed

src/parser/scalar.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
"use strict";
77

88
const specialChar = {
9-
"\\r": "\r",
10-
"\\n": "\n",
11-
"\\t": "\t",
12-
"\\v": String.fromCharCode(11),
13-
"\\e": String.fromCharCode(27),
14-
"\\f": String.fromCharCode(12),
15-
"\\\\": "\\",
16-
"\\$": "$",
17-
'\\"': '"',
18-
"\\'": "'"
9+
"\\": "\\",
10+
$: "$",
11+
n: "\n",
12+
r: "\r",
13+
t: "\t",
14+
f: String.fromCharCode(12),
15+
v: String.fromCharCode(11),
16+
e: String.fromCharCode(27)
1917
};
2018

2119
module.exports = {
@@ -25,13 +23,24 @@ module.exports = {
2523
resolve_special_chars: function(text, doubleQuote) {
2624
if (!doubleQuote) {
2725
// single quote fix
28-
return text.replace(/\\['\\]/g, function(seq) {
29-
return specialChar[seq];
30-
});
26+
return text.replace(/\\\\/g, "\\").replace(/\\'/g, "'");
3127
}
32-
return text.replace(/\\[rntvef"'\\$]/g, function(seq) {
33-
return specialChar[seq];
34-
});
28+
return text
29+
.replace(/\\"/, '"')
30+
.replace(
31+
/\\([\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}|u{([0-9a-fA-F]+)})/g,
32+
($match, p1, p2) => {
33+
if (specialChar[p1]) {
34+
return specialChar[p1];
35+
} else if ("x" === p1[0] || "X" === p1[0]) {
36+
return String.fromCodePoint(parseInt(p1.substr(1), 16));
37+
} else if ("u" === p1[0]) {
38+
return String.fromCodePoint(parseInt(p2, 16));
39+
} else {
40+
return String.fromCodePoint(parseInt(p1, 8));
41+
}
42+
}
43+
);
3544
},
3645
/**
3746
* ```ebnf
@@ -59,12 +68,13 @@ module.exports = {
5968
}
6069
const isDoubleQuote = text[offset] === '"';
6170
this.next();
71+
const textValue = this.resolve_special_chars(
72+
text.substring(offset + 1, text.length - 1),
73+
isDoubleQuote
74+
);
6275
value = value(
6376
isDoubleQuote,
64-
this.resolve_special_chars(
65-
text.substring(offset + 1, text.length - 1),
66-
isDoubleQuote
67-
),
77+
textValue,
6878
offset === 1, // unicode flag
6979
text
7080
);

test/snapshot/__snapshots__/break.test.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ Program {
6161
}
6262
`;
6363

64-
exports[`break with parens 1`] = `
64+
exports[`break with expression 1`] = `
6565
Program {
6666
"children": Array [
6767
Break {
6868
"kind": "break",
69-
"level": Number {
70-
"kind": "number",
71-
"parenthesizedExpression": true,
72-
"value": "1",
69+
"level": Variable {
70+
"curly": false,
71+
"kind": "variable",
72+
"name": "var",
7373
},
7474
},
7575
],
@@ -78,15 +78,15 @@ Program {
7878
}
7979
`;
8080

81-
exports[`break with var 1`] = `
81+
exports[`break with parens 1`] = `
8282
Program {
8383
"children": Array [
8484
Break {
8585
"kind": "break",
86-
"level": Variable {
87-
"curly": false,
88-
"kind": "variable",
89-
"name": "var",
86+
"level": Number {
87+
"kind": "number",
88+
"parenthesizedExpression": true,
89+
"value": "1",
9090
},
9191
},
9292
],

0 commit comments

Comments
 (0)