Skip to content

Commit

Permalink
Escape symbol strings
Browse files Browse the repository at this point in the history
Ensure they do not contain linebreaks. Fixes #5.
  • Loading branch information
novemberborn committed May 22, 2017
1 parent 8eae908 commit d8d710b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/primitiveValues/symbol.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const stringEscape = require('js-string-escape')
const wellKnownSymbols = require('well-known-symbols')

const constants = require('../constants')
Expand All @@ -14,7 +15,7 @@ function describe (value) {

const key = Symbol.keyFor(value)
if (key !== undefined) {
stringCompare = `Symbol.for(${key})`
stringCompare = `Symbol.for(${stringEscape(key)})`
} else if (wellKnownSymbols.isWellKnown(value)) {
stringCompare = wellKnownSymbols.getLabel(value)
}
Expand Down Expand Up @@ -63,8 +64,7 @@ class SymbolValue {

formatString () {
if (this.stringCompare !== null) return this.stringCompare
// TODO: Properly indent symbols that stringify to multiple lines
return this.value.toString()
return stringEscape(this.value.toString())
}

formatDeep (theme) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"esutils": "^2.0.2",
"fast-diff": "^1.1.1",
"function-name-support": "^0.2.0",
"js-string-escape": "^1.0.1",
"lodash.clonedeep": "^4.5.0",
"lodash.flattendeep": "^4.4.0",
"lodash.merge": "^4.6.0",
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/format.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,12 @@ exports[`formats primitive: Symbol(Symbol.iterator) 1`] = `"%symbol.open%Symbol.
exports[`formats primitive: Symbol(bar) 1`] = `"%symbol.open%Symbol.for(bar)%symbol.close%"`;
exports[`formats primitive: Symbol(bar\\nbaz) 1`] = `"%symbol.open%Symbol.for(bar\\\\nbaz)%symbol.close%"`;
exports[`formats primitive: Symbol(foo) 1`] = `"%symbol.open%Symbol(foo)%symbol.close%"`;
exports[`formats primitive: Symbol(foo\\nbar) 1`] = `"%symbol.open%Symbol(foo\\\\nbar)%symbol.close%"`;
exports[`formats primitive: false 1`] = `"%boolean.open%false%boolean.close%"`;
exports[`formats primitive: foo 1`] = `"%string.line.open#'%%string.open%foo%string.close%%string.line.close#'%"`;
Expand Down
2 changes: 2 additions & 0 deletions test/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void (
NaN,
Symbol(), // eslint-disable-line symbol-description
Symbol('foo'),
Symbol('foo\nbar'),
Symbol.for('bar'),
Symbol.for('bar\nbaz'),
Symbol.iterator
]) {
test(formatsPrimitive, value)
Expand Down

0 comments on commit d8d710b

Please sign in to comment.