Skip to content

Commit

Permalink
[Fix] ES2022+: ToBigInt: properly throw on an unparseable string
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 4, 2024
1 parent 5e975c7 commit 2989806
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,6 @@
/2022/TimeFromYear.js spackled linguist-generated=true
/2022/TimeString.js spackled linguist-generated=true
/2022/TimeWithinDay.js spackled linguist-generated=true
/2022/ToBigInt.js spackled linguist-generated=true
/2022/ToBigInt64.js spackled linguist-generated=true
/2022/ToBigUint64.js spackled linguist-generated=true
/2022/ToBoolean.js spackled linguist-generated=true
Expand Down
6 changes: 2 additions & 4 deletions 2022/ToBigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ var $SyntaxError = GetIntrinsic('%SyntaxError%');
var StringToBigInt = require('./StringToBigInt');
var ToPrimitive = require('./ToPrimitive');

var isNaN = require('../helpers/isNaN');

// https://262.ecma-international.org/11.0/#sec-tobigint
// https://262.ecma-international.org/13.0/#sec-tobigint

module.exports = function ToBigInt(argument) {
if (!$BigInt) {
Expand All @@ -35,7 +33,7 @@ module.exports = function ToBigInt(argument) {

if (typeof prim === 'string') {
var n = StringToBigInt(prim);
if (isNaN(n)) {
if (typeof n === 'undefined') {
throw new $TypeError('Failed to parse String to BigInt');
}
return n;
Expand Down
6 changes: 2 additions & 4 deletions 2023/ToBigInt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10666,6 +10666,13 @@ var es2020 = function ES2020(ES, ops, expectedMissing, skips) {
st.equal(ES.ToBigInt(true), BigInt(1), 'true becomes 1n');
st.equal(ES.ToBigInt(false), BigInt(0), 'true becomes 0n');

st.equal(ES.ToBigInt(''), BigInt(0), 'empty string becomes 0n');
st['throws'](
function () { ES.ToBigInt('a'); },
TypeError,
debug('a') + ' can not be parsed to a bigint'
);

forEach(v.bigints, function (bigint) {
st.equal(
ES.ToBigInt(bigint),
Expand Down

0 comments on commit 2989806

Please sign in to comment.