Skip to content

Commit

Permalink
[Fix] internal ToString operation should throw on Symbols
Browse files Browse the repository at this point in the history
In particular, this happened with `String.raw`, however, in every engine with Symbols but without `String.raw`, `String(Symbol())` also incorrectly throws, so this isn’t actually a bug
  • Loading branch information
ljharb committed Oct 12, 2020
1 parent b81dcfa commit b293878
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
},

ToNumber: function (value) {
if (_toString(value) === '[object Symbol]') {
if (hasSymbols && _toString(value) === '[object Symbol]') {
throw new TypeError('Cannot convert a Symbol value to a number');
}
return +value;
Expand Down Expand Up @@ -527,6 +527,9 @@
},

ToString: function ToString(string) {
if (hasSymbols && _toString(string) === '[object Symbol]') {
throw new TypeError('Cannot convert a Symbol value to a number');
}
return $String(string);
}
};
Expand Down
9 changes: 9 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ var runStringTests = function (it) {
var str = 'The total is 10 ($ with tax)';
expect(String.raw(template, 10)).to.equal(str);
});

ifSymbolsDescribe('Symbol substitutions', function () {
it('throws', function () {
var template = {
raw: { 0: 'The total is ', 1: ' ($', 2: ' with tax)', length: 3 }
};
var str = 'The total is 10 ($ with tax)';
expect(function () { String.raw(template, Symbol()); }).to['throw'](TypeError);
});
});
});

Expand Down

0 comments on commit b293878

Please sign in to comment.