Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly quote parameters for vip wp #2184

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions __tests__/lib/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ describe( 'utils/cli/format', () => {
},
{
input: [ 'textnospaces' ],
expected: [ 'textnospaces' ],
expected: [ '"textnospaces"' ],
},
{
input: [ '{"json":"json with spaces"}' ],
expected: [ '{"json":"json with spaces"}' ],
expected: [ '"{\\"json\\":\\"json with spaces\\"}"' ],
},
{
input: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '"{ \\"json\\" : \\"json with spaces outside strings\\" }"' ],
},
{
input: [
' { "json" : "json with spaces outside strings and outside the object" } ',
],
expected: [
' { "json" : "json with spaces outside strings and outside the object" } ',
'" { \\"json\\" : \\"json with spaces outside strings and outside the object\\" } "',
],
},
{
input: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '"{ \\"json\\" : \\"spaces-outside-strings-only\\" }"' ],
},
{
input: [ '{"json":broken json with spaces}' ],
expected: [ '"{\\"json\\":broken json with spaces}"' ],
},
{
input: [ '--foo=bar1 "bar2" "bar3"' ],
expected: [ '--foo="bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo=bar1 \\"bar2\\" \\"bar3\\""' ],
},
{
input: [ '--foo', 'bar1 "bar2" "bar3"' ],
expected: [ '--foo', '"bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo"', '"bar1 \\"bar2\\" \\"bar3\\""' ],
},
] )( 'should requote args when needed - %o', ( { input, expected } ) => {
const result = requoteArgs( input );
Expand Down
25 changes: 1 addition & 24 deletions src/lib/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,7 @@
}

export function requoteArgs( args: string[] ): string[] {
return args.map( arg => {
if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) {
return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' );
}

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

return arg;
} );
}

export function isJsonObject( str: unknown ): boolean {
return typeof str === 'string' && str.trim().startsWith( '{' ) && isJson( str );
}

export function isJson( str: string ): boolean {
try {
JSON.parse( str );
return true;
} catch ( error ) {
return false;
}
return args.map( arg => `"${ arg.replace( /"/g, '\\"' ) }"` );
sjinks marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
}

export function capitalize( str: unknown ): string {
Expand Down
Loading