From 26e95652970bf46528fd165c8c15b3b570a364d7 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 21 May 2019 15:52:35 +0200 Subject: [PATCH 1/4] Fix spaces escaping with `command` --- index.js | 6 +++--- test.js | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3fbb537222..c99d2de663 100644 --- a/index.js +++ b/index.js @@ -23,13 +23,13 @@ function handleEscaping(tokens, token, index) { return [token]; } - const previousToken = tokens[index - 1]; + const lastToken = tokens[tokens.length - 1]; - if (!previousToken.endsWith('\\')) { + if (!lastToken.endsWith('\\')) { return [...tokens, token]; } - return [...tokens.slice(0, index - 1), `${previousToken.slice(0, -1)} ${token}`]; + return [...tokens.slice(0, -1), `${lastToken.slice(0, -1)} ${token}`]; } function parseCommand(command, args = []) { diff --git a/test.js b/test.js index 5b8bb6526b..aeefcc76c0 100644 --- a/test.js +++ b/test.js @@ -123,6 +123,11 @@ test('escape other whitespaces in string arguments', async t => { t.is(stdout, 'foo\tbar'); }); +test('allow escaping spaces in commands', async t => { + const {stdout} = await execa('./fixtures/command\\ with\\ space foo bar'); + t.is(stdout, 'foo\nbar'); +}); + test('allow escaping spaces in string arguments', async t => { const {stdout} = await execa('node fixtures/echo foo\\ bar'); t.is(stdout, 'foo bar'); From 760b0bcd047bdcfe0ac37bb80effede336a21b7f Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 21 May 2019 15:53:35 +0200 Subject: [PATCH 2/4] Add fixture --- fixtures/command with space | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 fixtures/command with space diff --git a/fixtures/command with space b/fixtures/command with space new file mode 100755 index 0000000000..e9baf7ef67 --- /dev/null +++ b/fixtures/command with space @@ -0,0 +1,3 @@ +#!/usr/bin/env node +'use strict'; +console.log(process.argv.slice(2).join('\n')); From 61c5677f4a3bf7368406f2537c787c44129d6758 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 21 May 2019 15:56:58 +0200 Subject: [PATCH 3/4] Rename variables --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c99d2de663..6e526d8804 100644 --- a/index.js +++ b/index.js @@ -23,13 +23,13 @@ function handleEscaping(tokens, token, index) { return [token]; } - const lastToken = tokens[tokens.length - 1]; + const previousToken = tokens[tokens.length - 1]; - if (!lastToken.endsWith('\\')) { + if (!previousToken.endsWith('\\')) { return [...tokens, token]; } - return [...tokens.slice(0, -1), `${lastToken.slice(0, -1)} ${token}`]; + return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; } function parseCommand(command, args = []) { From 6c309a6ddd46723aa480b194603047e6b92f674e Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 21 May 2019 16:07:03 +0200 Subject: [PATCH 4/4] Invert lines --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6e526d8804..52ffd4d1bc 100644 --- a/index.js +++ b/index.js @@ -25,11 +25,11 @@ function handleEscaping(tokens, token, index) { const previousToken = tokens[tokens.length - 1]; - if (!previousToken.endsWith('\\')) { - return [...tokens, token]; + if (previousToken.endsWith('\\')) { + return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; } - return [...tokens.slice(0, -1), `${previousToken.slice(0, -1)} ${token}`]; + return [...tokens, token]; } function parseCommand(command, args = []) {