Skip to content

Commit 5d460a3

Browse files
mahirhirljharb
authored andcommitted
[Fix] parse: a backslash inside single quotes must not escape the closing quote
1 parent d390f9a commit 5d460a3

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

‎parse.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var CONTROL = /** @type {const} */ ('(?:') + /** @type {const} */ ([
2525
var controlRE = new RegExp('^' + CONTROL + '$');
2626
var META = /** @type {const} */ ('|&;()<> \\t');
2727
var SINGLE_QUOTE = /** @type {const} */ ('"((\\\\"|[^"])*?)"');
28-
var DOUBLE_QUOTE = /** @type {const} */ ('\'((\\\\\'|[^\'])*?)\'');
28+
var DOUBLE_QUOTE = /** @type {const} */ ('\'([^\']*?)\'');
2929
var hash = /^#$/;
3030

3131
var SQ = /** @type {const} */ ("'");

‎test/parse.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var test = require('tape');
44
var parse = require('../').parse;
5+
var quote = require('../').quote;
56

67
test('parse shell commands', function (t) {
78
t.same(parse(''), [], 'parses an empty string');
@@ -32,6 +33,12 @@ test('parse shell commands', function (t) {
3233
t.same(parse('a\\ b"c d"\\ e f'), ['a bc d e', 'f']);
3334
t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);
3435
t.same(parse("x \"bl'a\"'h'"), ['x', "bl'ah"]);
36+
37+
// inside single quotes everything is literal, so a backslash does not escape
38+
// the closing quote and a quoted token must end at its first closing quote
39+
t.same(parse("'\\' '\\'"), ['\\', '\\'], 'single-quoted backslashes parse as two separate tokens');
40+
t.same(parse("'\\'\\''"), ["\\'"], 'single-quoted backslash joined with an escaped quote');
41+
t.same(parse(quote(['\\', '\\'])), ['\\', '\\'], 'quote/parse round-trips a pair of backslashes');
3542
t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);
3643
t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);
3744

0 commit comments

Comments
 (0)