From 1e708c1c8a4d8ebe4ce30027411bee95f89430c4 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 1 Mar 2025 19:41:50 +0300 Subject: [PATCH] fix: enhance quote to handle empty args closes #999 closes #1112 --- .size-limit.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/cli.ts | 2 +- src/util.ts | 12 ++++++------ test/core.test.js | 10 ++++++++-- test/util.test.js | 2 ++ 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 31b3ba2ea7..10473ff117 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -30,7 +30,7 @@ { "name": "all", "path": "build/*", - "limit": "850.25 kB", + "limit": "850.3 kB", "brotli": false, "gzip": false } diff --git a/package-lock.json b/package-lock.json index fa3e659ed5..f5fe621b47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zx", - "version": "8.4.0", + "version": "8.3.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "zx", - "version": "8.4.0", + "version": "8.3.3", "license": "Apache-2.0", "bin": { "zx": "build/cli.js" diff --git a/package.json b/package.json index 4ab9aac041..4886277b23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zx", - "version": "8.4.0", + "version": "8.3.3", "description": "A tool for writing better scripts", "type": "module", "main": "./build/index.cjs", diff --git a/src/cli.ts b/src/cli.ts index cdfbe4f15b..77d8b1c8b4 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -31,7 +31,7 @@ import { } from './index.ts' import { installDeps, parseDeps } from './deps.ts' import { startRepl } from './repl.ts' -import { randomId, bufToString } from './util.ts' +import { randomId } from './util.ts' import { transformMarkdown } from './md.ts' import { createRequire, type minimist } from './vendor.ts' diff --git a/src/util.ts b/src/util.ts index 9f2d202f7f..243799b54f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -123,9 +123,9 @@ export function preferLocalBin( // } export function quote(arg: string): string { - if (/^[\w/.\-@:=]+$/.test(arg) || arg === '') { - return arg - } + if (arg === '') return `$''` + if (/^[\w/.\-@:=]+$/.test(arg)) return arg + return ( `$'` + arg @@ -142,9 +142,9 @@ export function quote(arg: string): string { } export function quotePowerShell(arg: string): string { - if (/^[\w/.\-]+$/.test(arg) || arg === '') { - return arg - } + if (arg === '') return `''` + if (/^[\w/.\-]+$/.test(arg)) return arg + return `'` + arg.replace(/'/g, "''") + `'` } diff --git a/test/core.test.js b/test/core.test.js index 46191e9ec9..186ccb9137 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -156,8 +156,14 @@ describe('core', () => { }) test('can use array as an argument', async () => { - const args = ['-n', 'foo'] - assert.equal((await $`echo ${args}`).toString(), 'foo') + const _$ = $({ prefix: '', postfix: '' }) + const p1 = _$`echo ${['-n', 'foo']}` + assert.equal(p1.cmd, 'echo -n foo') + assert.equal((await p1).toString(), 'foo') + + const p2 = _$`echo ${[1, '', '*', '2']}` + assert.equal(p2.cmd, `echo 1 $'' $'*' 2`) + assert.equal((await p2).toString(), `1 * 2\n`) }) test('requires $.shell to be specified', async () => { diff --git a/test/util.test.js b/test/util.test.js index 6f0609ece4..ecdc7387ae 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -62,12 +62,14 @@ describe('util', () => { test('quote()', () => { assert.ok(quote('string') === 'string') + assert.ok(quote('') === `$''`) assert.ok(quote(`'\f\n\r\t\v\0`) === `$'\\'\\f\\n\\r\\t\\v\\0'`) }) test('quotePowerShell()', () => { assert.equal(quotePowerShell('string'), 'string') assert.equal(quotePowerShell(`'`), `''''`) + assert.equal(quotePowerShell(''), `''`) }) test('duration parsing works', () => {