Skip to content

Commit eff2d70

Browse files
authored
fix: enhance quote to handle empty args (#1113)
closes #999 closes #1112
1 parent f4b0328 commit eff2d70

7 files changed

+21
-13
lines changed

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{
3131
"name": "all",
3232
"path": "build/*",
33-
"limit": "850.25 kB",
33+
"limit": "850.3 kB",
3434
"brotli": false,
3535
"gzip": false
3636
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zx",
3-
"version": "8.4.0",
3+
"version": "8.3.3",
44
"description": "A tool for writing better scripts",
55
"type": "module",
66
"main": "./build/index.cjs",

src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from './index.ts'
3232
import { installDeps, parseDeps } from './deps.ts'
3333
import { startRepl } from './repl.ts'
34-
import { randomId, bufToString } from './util.ts'
34+
import { randomId } from './util.ts'
3535
import { transformMarkdown } from './md.ts'
3636
import { createRequire, type minimist } from './vendor.ts'
3737

src/util.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ export function preferLocalBin(
123123
// }
124124

125125
export function quote(arg: string): string {
126-
if (/^[\w/.\-@:=]+$/.test(arg) || arg === '') {
127-
return arg
128-
}
126+
if (arg === '') return `$''`
127+
if (/^[\w/.\-@:=]+$/.test(arg)) return arg
128+
129129
return (
130130
`$'` +
131131
arg
@@ -142,9 +142,9 @@ export function quote(arg: string): string {
142142
}
143143

144144
export function quotePowerShell(arg: string): string {
145-
if (/^[\w/.\-]+$/.test(arg) || arg === '') {
146-
return arg
147-
}
145+
if (arg === '') return `''`
146+
if (/^[\w/.\-]+$/.test(arg)) return arg
147+
148148
return `'` + arg.replace(/'/g, "''") + `'`
149149
}
150150

test/core.test.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,14 @@ describe('core', () => {
156156
})
157157

158158
test('can use array as an argument', async () => {
159-
const args = ['-n', 'foo']
160-
assert.equal((await $`echo ${args}`).toString(), 'foo')
159+
const _$ = $({ prefix: '', postfix: '' })
160+
const p1 = _$`echo ${['-n', 'foo']}`
161+
assert.equal(p1.cmd, 'echo -n foo')
162+
assert.equal((await p1).toString(), 'foo')
163+
164+
const p2 = _$`echo ${[1, '', '*', '2']}`
165+
assert.equal(p2.cmd, `echo 1 $'' $'*' 2`)
166+
assert.equal((await p2).toString(), `1 * 2\n`)
161167
})
162168

163169
test('requires $.shell to be specified', async () => {

test/util.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ describe('util', () => {
6262

6363
test('quote()', () => {
6464
assert.ok(quote('string') === 'string')
65+
assert.ok(quote('') === `$''`)
6566
assert.ok(quote(`'\f\n\r\t\v\0`) === `$'\\'\\f\\n\\r\\t\\v\\0'`)
6667
})
6768

6869
test('quotePowerShell()', () => {
6970
assert.equal(quotePowerShell('string'), 'string')
7071
assert.equal(quotePowerShell(`'`), `''''`)
72+
assert.equal(quotePowerShell(''), `''`)
7173
})
7274

7375
test('duration parsing works', () => {

0 commit comments

Comments
 (0)