Skip to content

Commit

Permalink
fix: sanitize square brackets in description of commands/flags (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored and RasPhilCo committed Aug 1, 2019
1 parent 32f50da commit 3bf4821
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/commands/autocomplete/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ function sanitizeDescription(description?: string): string {
if (description === undefined) {
return ''
}
return description.replace(/`/g, '\\\\\\\`')
return description
.replace(/(`)/g, '\\\\\\$1') // backticks require triple-backslashes
.replace(/([\[\]])/g, '\\\\$1') // square brackets require double-backslashes
}

export default class Create extends AutocompleteBase {
Expand Down
5 changes: 3 additions & 2 deletions test/commands/autocomplete/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ _oclif-example()
local commands="
autocomplete --skip-instructions
autocomplete:foo --bar --baz --dangerous --json
autocomplete:foo --bar --baz --dangerous --brackets --json
"
if [[ "\${COMP_CWORD}" -eq 1 ]] ; then
Expand Down Expand Up @@ -126,7 +126,7 @@ _oclif-example () {
## public cli commands & flags
local -a _all_commands=(
"autocomplete:display autocomplete instructions"
"autocomplete\\:foo:cmd for autocomplete testing \\\\\\\`with some potentially dangerous script\\\\\\\`"
"autocomplete\\:foo:cmd for autocomplete testing \\\\\\\`with some potentially dangerous script\\\\\\\` and \\\\\[square brackets\\\\\]"
)
_set_flags () {
Expand All @@ -142,6 +142,7 @@ autocomplete:foo)
"--bar=-[bar for testing]:"
"--baz=-[baz for testing]:"
"--dangerous=-[\\\\\\\`with some potentially dangerous script\\\\\\\`]:"
"--brackets=-[\\\\\[square brackets\\\\\]]:"
"--json[output in json format]"
)
;;
Expand Down
7 changes: 6 additions & 1 deletion test/test.oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"autocomplete:foo": {
"id": "autocomplete:foo",
"description": "cmd for autocomplete testing `with some potentially dangerous script`\nwith a multi-line description",
"description": "cmd for autocomplete testing `with some potentially dangerous script` and [square brackets]\nand a multi-line description",
"pluginName": "@oclif/plugin-autocomplete",
"pluginType": "core",
"aliases": [],
Expand All @@ -50,6 +50,11 @@
"type": "option",
"description": "`with some potentially dangerous script`"
},
"brackets": {
"name": "brackets",
"type": "option",
"description": "[square brackets]"
},
"json": {
"name": "json",
"type": "boolean",
Expand Down

0 comments on commit 3bf4821

Please sign in to comment.