Skip to content

Commit

Permalink
fix: quoting of newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jun 19, 2018
1 parent 5afe464 commit fbd6969
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/heroku-cli-plugin-config/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.nyc_output
*-debug.log
*-error.log
.oclif.manifest.json
Expand Down
6 changes: 3 additions & 3 deletions packages/heroku-cli-plugin-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
"@oclif/plugin-help": "^1.2.10",
"@oclif/test": "^1.0.6",
"@oclif/tslint": "1",
"@types/chai": "4",
"@types/chai": "^4.1.3",
"@types/lodash": "^4.14.108",
"@types/mocha": "5",
"@types/node": "^10.0.3",
"@types/supports-color": "^5.3.0",
"chai": "4",
"chai": "^4.1.2",
"globby": "8",
"mocha": "5",
"nyc": "11",
"ts-node": "6.0.2",
"tslib": "1",
"tslint": "5",
"tslint": "5.10.0",
"typescript": "2.8"
},
"engines": {
Expand Down
15 changes: 2 additions & 13 deletions packages/heroku-cli-plugin-config/src/commands/config/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {Command, flags} from '@heroku-cli/command'
import {cli} from 'cli-ux'
import * as _ from 'lodash'

const shell = require('shell-quote')
import {parse, quote} from '../../quote'

const edit = require('edit-string')

interface Config {
Expand Down Expand Up @@ -35,18 +36,6 @@ function stringToConfig(s: string): Config {
}, {})
}

function quote(a: string): string {
a = a.replace(/\n/g, '\\n')
if (a.match(/[:@]/)) return shell.quote([`'${a}`]).replace(/^"'/, '"')
return shell.quote([a])
}

function parse(a: string): string {
let parsed = shell.parse(a)
if (parsed.length > 1) throw new Error(`Invalid token: ${a}`)
return parsed[0].replace(/\\\\n/g, '\n')
}

function allKeys(a: Config, b: Config): string[] {
return _.uniq([...Object.keys(a), ...Object.keys(b)].sort())
}
Expand Down
26 changes: 26 additions & 0 deletions packages/heroku-cli-plugin-config/src/quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const shell = require('shell-quote')

// slightly modifed form of shell-quote to default to using single-quotes over backslashes
export function quote(s: string): string {
if (/["\s#!$&'()*,:;<=>?@\[\\\]^`{|}]/.test(s)) {
if (/['\n]/.test(s)) return '"'
+ s
.replace(/(["\\$`!])/g, '\\$1')
.replace(/\n/g, '\\n')
+ '"'
return "'" + s.replace(/(['\\])/g, '\\$1') + "'"
}
return s
}

export function parse(a: string): string {
if (a.startsWith('"')) {
a = a.replace(/\\n/g, '\n')
} else if (a.startsWith("'")) {
a = a.replace(/\\\\/g, '\\')
}
let parsed = shell.parse(a)
if (parsed.length > 1) throw new Error(`Invalid token: ${a}`)
return parsed[0]
// return parsed[0].replace(/\\\\n/g, '\n')
}
11 changes: 0 additions & 11 deletions packages/heroku-cli-plugin-config/test/commands/hello.test.ts

This file was deleted.

29 changes: 29 additions & 0 deletions packages/heroku-cli-plugin-config/test/quote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {expect} from 'chai'

import {parse, quote} from '../src/quote'

describe('quote', () => {
[
['abc', 'abc'],
['ab$c', "'ab$c'"],
['a\'bc', '"a\'bc"'],
['a\nb\nc', '"a\\nb\\nc"'],
['foo\\nb:ar\\bz', "'foo\\\\nb:ar\\\\bz'"],
].map(([a, b]) => {
it(`${a}===${b}`, () => {
expect(quote(a)).to.eq(b)
})
});

[
'abc',
'ab$c',
'a\'bc',
'a\nb\nc',
'foo\\nb:ar\\bz',
].map(s => {
it(`parses "${s}"`, () => {
expect(parse(quote(s))).to.eq(s)
})
})
})
10 changes: 5 additions & 5 deletions packages/heroku-cli-plugin-config/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
dependencies:
tslint-xo "^0.7.0"

"@types/chai@4":
"@types/chai@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.3.tgz#b8a74352977a23b604c01aa784f5b793443fb7dc"

Expand Down Expand Up @@ -458,7 +458,7 @@ center-align@^0.1.1:
align-text "^0.1.3"
lazy-cache "^1.0.3"

chai@4:
chai@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
dependencies:
Expand Down Expand Up @@ -2458,9 +2458,9 @@ tslint-xo@^0.7.0:
tslint-eslint-rules "^5.1.0"
tslint-microsoft-contrib "^5.0.2"

tslint@5:
version "5.9.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.9.1.tgz#1255f87a3ff57eb0b0e1f0e610a8b4748046c9ae"
tslint@5.10.0:
version "5.10.0"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.10.0.tgz#11e26bccb88afa02dd0d9956cae3d4540b5f54c3"
dependencies:
babel-code-frame "^6.22.0"
builtin-modules "^1.1.1"
Expand Down

0 comments on commit fbd6969

Please sign in to comment.