Skip to content

Commit

Permalink
Use + to conform strings to numbers [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 23, 2023
1 parent 42d4531 commit 4345770
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/instrument/internalVars.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function checkInternalVarNameClash(varName, state) {
if (!internalNameMatch) return;

const currentNum = state.internalVarsPrefixNum;
const newNum = (internalNameMatch[1] * 1 || 0) + 1;
const newNum = (+internalNameMatch[1] || 0) + 1;
if (newNum > currentNum) state.internalVarsPrefixNum = newNum;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function serializeArguments(args, record) {
if (!isIntegerKey(key)) break;

// Fill gaps (deleted elements) with undefined
const index = key * 1;
const index = +key;
let nextIndex = argumentNodes.length;
while (index !== nextIndex) {
argumentNodes[nextIndex] = undefinedRecord.varNode;
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/boxed.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
// All numerical keys can be skipped, even ones above max safe integer key as boxed strings
// only allow writing to numerical keys within bounds of string length
if (!isNumberKey(key)) return false;
return key * 1 < len;
return +key < len;
}
);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = {
() => `Function JS:\n${js}\nTrace:\n${this.getTraceStack()}\n`
);

const fnId = commentMatch[1] * 1,
const fnId = +commentMatch[1],
filename = JSON.parse(`"${commentMatch[3]}"`);

// Get/create file
Expand Down
4 changes: 2 additions & 2 deletions lib/serialize/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
// NB: Default prop keys are always valid identifiers
const key = nextDefaultProp.name;
const memberNode = isIntegerKey(key)
? t.memberExpression(record.varNode, t.numericLiteral(key * 1), true)
? t.memberExpression(record.varNode, t.numericLiteral(+key), true)
: t.memberExpression(record.varNode, t.identifier(key));
const assignmentNode = t.unaryExpression('delete', memberNode);
createAssignment(record, assignmentNode, memberNode, 'object', true);
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = {
defaultProp = nextDefaultProp;
} else if (
!isIntegerKey(key)
|| (isIntegerKey(nextDefaultKey) && nextDefaultKey * 1 < key * 1)
|| (isIntegerKey(nextDefaultKey) && +nextDefaultKey < +key)
) {
deleteDefaultProps(key);
defaultProp = nextDefaultProp;
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module.exports = {
for (const [exportIndex, importValueRecord] of Object.entries(importRecords)) {
const importValueNode = t.memberExpression(
importVarNode,
t.numericLiteral(exportIndex * 1),
t.numericLiteral(+exportIndex),
true // computed
);
importValueRecord.node = importValueNode;
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/parseFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ module.exports = function parseFunction(
keyNode = t.identifier(name);
accessComputed = false;
} else {
keyNode = isNumberKey(name) ? t.numericLiteral(name * 1) : t.stringLiteral(name);
keyNode = isNumberKey(name) ? t.numericLiteral(+name) : t.stringLiteral(name);
accessComputed = true;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/serialize/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const JS_ID_REGEX = /^[A-Za-z$_][A-Za-z0-9$_]*$/u;
function isNumberKey(name) {
if (name === '0') return true;
if (!NUMBER_KEY_REGEX.test(name)) return false; // eslint-disable-line no-use-before-define
return Number.isSafeInteger(name * 1);
return Number.isSafeInteger(+name);
}

const NUMBER_KEY_REGEX = /^[1-9][0-9]*$/u;
Expand All @@ -62,7 +62,7 @@ const NUMBER_KEY_REGEX = /^[1-9][0-9]*$/u;
* @returns {boolean} - `true` if is valid integer object key
*/
function isIntegerKey(name) {
return isNumberKey(name) && name * 1 <= MAX_INTEGER_KEY; // eslint-disable-line no-use-before-define
return isNumberKey(name) && +name <= MAX_INTEGER_KEY; // eslint-disable-line no-use-before-define
}

const MAX_INTEGER_KEY = 4294967294;
Expand All @@ -88,7 +88,7 @@ const JS_ID_ILLEGAL_CHARS_REGEX = /[^A-Za-z0-9$_]+/gu,
*/
function createKeyNode(key) {
if (isJsIdentifier(key)) return t.identifier(key);
if (isNumberKey(key)) return t.numericLiteral(key * 1);
if (isNumberKey(key)) return t.numericLiteral(+key);
return t.stringLiteral(key);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = {
const typeName = isGetter ? 'getter' : 'setter';
const parentRecord = this.serializeValue(parent, null, `<parent global ${typeName} ${key}>`);
const parentVarNode = parentRecord.varNode;
const keyNode = isNumberKey(key) ? t.numericLiteral(key * 1) : t.stringLiteral(key);
const keyNode = isNumberKey(key) ? t.numericLiteral(+key) : t.stringLiteral(key);
const getDescriptorRecord = this.serializeValue(Object.getOwnPropertyDescriptor);
node = t.memberExpression(
t.callExpression(
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/varNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class UnmangledVarNameFactory extends VarNameFactory {
transform(name, isPrivate) {
if (isPrivate) name = `${PRIVATE_PREFIX}${name}`;
const [, nameWithoutPostfix, numStr] = name.match(NAME_REGEX);
let num = numStr ? numStr * 1 : -1;
let num = numStr ? +numStr : -1;

const usedNum = this.used.get(nameWithoutPostfix);
if (usedNum !== undefined && num <= usedNum) num = usedNum + 1;
Expand Down

0 comments on commit 4345770

Please sign in to comment.