From 9d6c0d6b7d1e663bb88a47382a108a79bdb979ec Mon Sep 17 00:00:00 2001 From: Ahmed Sayeed Wasif Date: Thu, 28 Dec 2023 16:52:50 +0600 Subject: [PATCH 1/6] Properly propagate quotes --- src/lib/cli/format.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/cli/format.ts b/src/lib/cli/format.ts index c6448a450..d4d8a3391 100644 --- a/src/lib/cli/format.ts +++ b/src/lib/cli/format.ts @@ -134,7 +134,8 @@ export function keyValue( values: Tuple[] ): string { export function requoteArgs( args: string[] ): string[] { return args.map( arg => { if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) { - return arg.replace( /^--([^=]*)=(.*)$/, '--$1="$2"' ); + const quote = arg.includes( '"' ) ? "'" : '"'; + return arg.replace( /^--([^=]*)=(.*)$/, `--$1=${ quote }$2${ quote }` ); } if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) { From e9e093e6d5f54e313dc3132738e02a717a0d103f Mon Sep 17 00:00:00 2001 From: chriszarate Date: Thu, 28 Dec 2023 13:05:56 -0500 Subject: [PATCH 2/6] Add failing test cases --- __tests__/lib/cli/format.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/__tests__/lib/cli/format.js b/__tests__/lib/cli/format.js index 26dd69ba1..828c0827d 100644 --- a/__tests__/lib/cli/format.js +++ b/__tests__/lib/cli/format.js @@ -35,6 +35,14 @@ describe( 'utils/cli/format', () => { input: [ '{"json":broken json with spaces}' ], expected: [ '"{"json":broken json with spaces}"' ], }, + { + input: [ '--foo=\'bar1 "bar2" "bar3"\'' ], + expected: [ '--foo=\'bar1 "bar2" "bar3"\'' ], + }, + { + input: [ '--foo', '\'bar1 "bar2" "bar3"\'' ], + expected: [ '--foo', '\'bar1 "bar2" "bar3"\'' ], + }, ] )( 'should requote args when needed - %o', ( { input, expected } ) => { const result = requoteArgs( input ); expect( result ).toStrictEqual( expected ); From 4a2413e5f30831c269a906c8f79524c526a35b74 Mon Sep 17 00:00:00 2001 From: Ahmed Sayeed Wasif Date: Fri, 12 Jan 2024 10:37:51 +0800 Subject: [PATCH 3/6] Escape the double quotes --- src/lib/cli/format.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/cli/format.ts b/src/lib/cli/format.ts index d4d8a3391..cb2fa8058 100644 --- a/src/lib/cli/format.ts +++ b/src/lib/cli/format.ts @@ -134,12 +134,11 @@ export function keyValue( values: Tuple[] ): string { export function requoteArgs( args: string[] ): string[] { return args.map( arg => { if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) { - const quote = arg.includes( '"' ) ? "'" : '"'; - return arg.replace( /^--([^=]*)=(.*)$/, `--$1=${ quote }$2${ quote }` ); + return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, `--$1="$2"` ); } if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) { - return `"${ arg }"`; + return `"${ arg.replace( /"/g, '\\"' ) }"`; } return arg; From 5d4f79b4c06d9dfaad99bd0037f9c3cad789c39e Mon Sep 17 00:00:00 2001 From: Ahmed Sayeed Wasif Date: Fri, 12 Jan 2024 11:26:47 +0800 Subject: [PATCH 4/6] Update a test to adjust to escaping --- __tests__/lib/cli/format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/lib/cli/format.js b/__tests__/lib/cli/format.js index 828c0827d..c3ac9455d 100644 --- a/__tests__/lib/cli/format.js +++ b/__tests__/lib/cli/format.js @@ -33,7 +33,7 @@ describe( 'utils/cli/format', () => { }, { input: [ '{"json":broken json with spaces}' ], - expected: [ '"{"json":broken json with spaces}"' ], + expected: [ '"{\\"json\\":broken json with spaces}"' ], }, { input: [ '--foo=\'bar1 "bar2" "bar3"\'' ], From 5941cca74c157a4b396e035c05b870546da9a6e8 Mon Sep 17 00:00:00 2001 From: Ahmed Sayeed Wasif Date: Fri, 12 Jan 2024 11:51:58 +0800 Subject: [PATCH 5/6] Update tests --- __tests__/lib/cli/format.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/__tests__/lib/cli/format.js b/__tests__/lib/cli/format.js index c3ac9455d..09f19a7e5 100644 --- a/__tests__/lib/cli/format.js +++ b/__tests__/lib/cli/format.js @@ -36,12 +36,12 @@ describe( 'utils/cli/format', () => { expected: [ '"{\\"json\\":broken json with spaces}"' ], }, { - input: [ '--foo=\'bar1 "bar2" "bar3"\'' ], - expected: [ '--foo=\'bar1 "bar2" "bar3"\'' ], + input: [ '--foo=bar1 "bar2" "bar3"' ], + expected: [ '--foo="bar1 \\"bar2\\" \\"bar3\\""' ], }, { - input: [ '--foo', '\'bar1 "bar2" "bar3"\'' ], - expected: [ '--foo', '\'bar1 "bar2" "bar3"\'' ], + input: [ '--foo', 'bar1 "bar2" "bar3"' ], + expected: [ '--foo', '"bar1 \\"bar2\\" \\"bar3\\""' ], }, ] )( 'should requote args when needed - %o', ( { input, expected } ) => { const result = requoteArgs( input ); From 8f24eb0001179c73e52eaad8296599001d06ba1c Mon Sep 17 00:00:00 2001 From: Ahmed Sayeed Wasif Date: Fri, 12 Jan 2024 11:55:51 +0800 Subject: [PATCH 6/6] Remove unnecessary backtick --- src/lib/cli/format.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/cli/format.ts b/src/lib/cli/format.ts index cb2fa8058..6697477c4 100644 --- a/src/lib/cli/format.ts +++ b/src/lib/cli/format.ts @@ -134,7 +134,7 @@ export function keyValue( values: Tuple[] ): string { export function requoteArgs( args: string[] ): string[] { return args.map( arg => { if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) { - return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, `--$1="$2"` ); + return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' ); } if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) {