Skip to content

Commit

Permalink
fix: Properly propagate quotes (#1626)
Browse files Browse the repository at this point in the history
* Properly propagate quotes

* Add failing test cases

* Escape the double quotes

* Update a test to adjust to escaping

* Update tests

* Remove unnecessary backtick

---------

Co-authored-by: chriszarate <[email protected]>
  • Loading branch information
aswasif007 and chriszarate authored Jan 12, 2024
1 parent 83a8bb7 commit 08f1b28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion __tests__/lib/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ 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"' ],
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 );
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +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( ' ' ) ) {
return arg.replace( /^--([^=]*)=(.*)$/, '--$1="$2"' );
return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' );
}

if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) {
return `"${ arg }"`;
return `"${ arg.replace( /"/g, '\\"' ) }"`;
}

return arg;
Expand Down

0 comments on commit 08f1b28

Please sign in to comment.