Skip to content

Commit 0dffca2

Browse files
committed
Quoted empty strings are valid arguments.
1 parent f4d53e7 commit 0dffca2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/args-tokenizer.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,17 @@ test("empty command", () => {
116116
expect(tokenizeArgs(``)).toEqual([]);
117117
expect(tokenizeArgs(` `)).toEqual([]);
118118
});
119+
120+
test("empty quoted argument", () => {
121+
expect(tokenizeArgs(`a0 "" a1`)).toEqual(["a0", "", "a1"]);
122+
expect(tokenizeArgs(`a0 '' a1`)).toEqual(["a0", "", "a1"]);
123+
expect(tokenizeArgs(`a0 ""''""'' a1`)).toEqual(["a0", "", "a1"]);
124+
// the next test fails, when the erroneous unescaping of newlines in double quotes is fixed
125+
// correct test is:
126+
// expect(tokenizeArgs(`"a0" "\\\n" a1`)).toEqual(["a0", "", "a1"]);
127+
expect(tokenizeArgs(`"a0" "\\\n" a1`)).toEqual(["a0", "\n", "a1"]);
128+
// the next test fails, when the erroneous unescaping in single quotes is fixed
129+
// correct test is:
130+
// expect(tokenizeArgs(`"a0" '\\\n' a1`)).toEqual(["a0", "\\\n", "a1"]);
131+
expect(tokenizeArgs(`"a0" '\\\n' a1`)).toEqual(["a0", "\n", "a1"]);
132+
});

src/args-tokenizer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const tokenizeArgs = (
1515
let currentToken = "";
1616
let openningQuote: undefined | string;
1717
let escaped = false;
18+
let quoted_arg = false;
1819
for (let index = 0; index < argsString.length; index += 1) {
1920
const char = argsString[index];
2021

@@ -34,9 +35,10 @@ export const tokenizeArgs = (
3435
}
3536

3637
if (openningQuote === undefined && spaceRegex.test(char)) {
37-
if (currentToken.length > 0) {
38+
if (currentToken.length > 0 || quoted_arg) {
3839
tokens.push(currentToken);
3940
currentToken = "";
41+
quoted_arg = false;
4042
}
4143
continue;
4244
}
@@ -47,6 +49,7 @@ export const tokenizeArgs = (
4749
continue;
4850
}
4951
if (openningQuote === char) {
52+
quoted_arg = true;
5053
openningQuote = undefined;
5154
continue;
5255
}

0 commit comments

Comments
 (0)