Skip to content

Commit

Permalink
fix: literal 'raw' value now matches input string
Browse files Browse the repository at this point in the history
fixes #192
  • Loading branch information
6utt3rfly committed Oct 13, 2021
1 parent b1e7fc9 commit 432c514
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/jsep.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,8 @@ export class Jsep {
*/
gobbleStringLiteral() {
let str = '';
let quote = this.expr.charAt(this.index++);
const startIndex = this.index;
const quote = this.expr.charAt(this.index++);
let closed = false;

while (this.index < this.expr.length) {
Expand Down Expand Up @@ -717,7 +718,7 @@ export class Jsep {
return {
type: Jsep.LITERAL,
value: str,
raw: quote + str + quote
raw: this.expr.substring(startIndex, this.index),
};
}

Expand Down
15 changes: 15 additions & 0 deletions test/jsep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ import {testParser, testOpExpression, esprimaComparisonTest, resetJsepDefaults}
testParser('12.3', { value: 12.3 }, assert);
});

QUnit.module('Literal Parsing', () => {
[
["'a \\w b'", "a w b"],
["'a \\' b'", "a ' b"],
["'a \\n b'", "a \n b"],
["'a \\r b'", "a \r b"],
["'a \\t b'", "a \t b"],
["'a \\b b'", "a \b b"],
["'a \\f b'", "a \f b"],
["'a \\v b'", "a \v b"],
].forEach((t) => QUnit.test(`Should parse ${t[0]}`, (assert) => {
testParser(t[0], { value: t[1], raw: t[0] }, assert);
}));
});

QUnit.test('Variables', function (assert) {
testParser('abc', { name: 'abc' }, assert);
testParser('a.b[c[0]]', {
Expand Down

0 comments on commit 432c514

Please sign in to comment.