Skip to content

Commit ba88f47

Browse files
authored
fix: #3421 require a space or delimiter after hex, bin, and oct values (#3463)
1 parent b44172b commit ba88f47

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/expression/parse.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
347347
next(state)
348348
state.token += currentCharacter(state)
349349
next(state)
350-
while (parse.isHexDigit(currentCharacter(state))) {
350+
while (
351+
parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state)) ||
352+
parse.isDigit(currentCharacter(state))
353+
) {
351354
state.token += currentCharacter(state)
352355
next(state)
353356
}
@@ -356,7 +359,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
356359
state.token += '.'
357360
next(state)
358361
// get the digits after the radix
359-
while (parse.isHexDigit(currentCharacter(state))) {
362+
while (
363+
parse.isAlpha(currentCharacter(state), prevCharacter(state), nextCharacter(state)) ||
364+
parse.isDigit(currentCharacter(state))
365+
) {
360366
state.token += currentCharacter(state)
361367
next(state)
362368
}
@@ -575,17 +581,6 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
575581
return (c >= '0' && c <= '9')
576582
}
577583

578-
/**
579-
* checks if the given char c is a hex digit
580-
* @param {string} c a string with one character
581-
* @return {boolean}
582-
*/
583-
parse.isHexDigit = function isHexDigit (c) {
584-
return ((c >= '0' && c <= '9') ||
585-
(c >= 'a' && c <= 'f') ||
586-
(c >= 'A' && c <= 'F'))
587-
}
588-
589584
/**
590585
* Start of the parse levels below, in order of precedence
591586
* @return {Node} node

test/unit-tests/expression/parse.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ describe('parse', function () {
289289
assert.strictEqual(parseAndEval('0x1.'), 1)
290290
})
291291

292+
it('should require hex, bin, oct values to be followed by whitespace or a delimiter', function () {
293+
assert.throws(() => parseAndEval('0b0a'), /SyntaxError: String "0b0a" is not a valid number/)
294+
assert.throws(() => parseAndEval('0x1k'), /SyntaxError: String "0x1k" is not a valid number/)
295+
assert.throws(() => parseAndEval('0o1k'), /SyntaxError: String "0o1k" is not a valid number/)
296+
assert.throws(() => parseAndEval('0b1k'), /SyntaxError: String "0b1k" is not a valid number/)
297+
298+
assert.strictEqual(parseAndEval('0x1 k', { k: 2 }), 2)
299+
assert.strictEqual(parseAndEval('0o1 k', { k: 2 }), 2)
300+
assert.strictEqual(parseAndEval('0b1 k', { k: 2 }), 2)
301+
302+
assert.strictEqual(parseAndEval('0x1*k', { k: 2 }), 2)
303+
assert.strictEqual(parseAndEval('0o1*k', { k: 2 }), 2)
304+
assert.strictEqual(parseAndEval('0b1*k', { k: 2 }), 2)
305+
})
306+
292307
it('should parse a number followed by e', function () {
293308
approxEqual(parseAndEval('2e'), 2 * Math.E)
294309
})
@@ -337,7 +352,7 @@ describe('parse', function () {
337352

338353
assert.throws(function () { parseAndEval('0b123.45') }, /SyntaxError: String "0b123\.45" is not a valid number/)
339354
assert.throws(function () { parseAndEval('0o89.89') }, /SyntaxError: String "0o89\.89" is not a valid number/)
340-
assert.throws(function () { parseAndEval('0xghji.xyz') }, /SyntaxError: String "0x" is not a valid number/)
355+
assert.throws(function () { parseAndEval('0xghji.xyz') }, /SyntaxError: String "0xghji.xyz" is not a valid number/)
341356
})
342357
})
343358

0 commit comments

Comments
 (0)