Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading