Skip to content

Commit

Permalink
Merge pull request #7 from Rich-Harris/gh-6
Browse files Browse the repository at this point in the history
handle newlines correctly
  • Loading branch information
Rich-Harris committed Apr 2, 2018
2 parents c2b74e0 + d0faea2 commit abd62a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class ParseError extends Error {
}
}

// https://mathiasbynens.be/notes/javascript-escapes
const escapeable: Record<string, string> = {
b: '\b',
n: '\n',
f: '\f',
r: '\r',
t: '\t',
v: '\v',
0: '\0'
};

export default class Parser {
str: string;
index: number;
Expand Down Expand Up @@ -338,10 +349,16 @@ export default class Parser {
const char = this.str[this.index++];

if (escaped) {
if (char !== '\r' || this.str[this.index] !== '\n') {
escaped = false;
if (char === quote) value += char;
escaped = false;

// line continuations
if (char === '\n') continue;
if (char === '\r') {
if (this.str[this.index] === '\n') this.index += 1;
continue;
}

value += escapeable[char] || char;
} else if (char === '\\') {
escaped = true;
} else if (char === quote) {
Expand Down
5 changes: 5 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ multi-line string',
'trailing commas too',
],
}
},

{
input: `"\\n"`,
output: `\n`
}
];

Expand Down

0 comments on commit abd62a3

Please sign in to comment.