Skip to content

Commit

Permalink
#42 - fixed parsing raw string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
springcomp committed Oct 27, 2024
1 parent 4ee2325 commit b8dcfa8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ class StreamLexer {
}
this._current += 1;
const literal = stream.slice(start + 1, this._current - 1);
return literal.replace(`\\'`, `'`);

// emulating es2021: String.prototype.replaceAll()
return literal
.split(`\\\\`).join(`\\`)
.split(`\\'`).join(`'`)
;
}

private consumeNumber(stream: string): LexerToken {
Expand Down
12 changes: 12 additions & 0 deletions test/jmespath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe('tokenize', () => {
it('should tokenize json literals', () => {
expect(tokenize('`true`')).toMatchObject([{ type: 'Literal', value: true, start: 0 }]);
});
it('should tokenize raw strings', () => {
expect(tokenize("'raw-string'")).toMatchObject([{ type: 'Literal', value: "raw-string", start: 0 }]);
});
it('should tokenize raw strings single quote', () => {
expect(tokenize("'\\''")).toMatchObject([{ type: 'Literal', value: "'", start: 0 }]);
});
it('should tokenize raw strings surrounding quotes', () => {
expect(tokenize("'\\'raw-string\\''")).toMatchObject([{ type: 'Literal', value: "'raw-string'", start: 0 }]);
});
it('should tokenize raw strings backslash characters', () => {
expect(tokenize("'\\\\'")).toMatchObject([{ type: 'Literal', value: "\\", start: 0 }]);
});
it('should not requiring surrounding quotes for strings', () => {
expect(tokenize('`foo`')).toMatchObject([{ type: 'Literal', value: 'foo', start: 0 }]);
});
Expand Down

0 comments on commit b8dcfa8

Please sign in to comment.