Skip to content

Commit c07b446

Browse files
refactor(types): type the constant operator helpers by their compile-time operands
1 parent 98e638a commit c07b446

1 file changed

Lines changed: 80 additions & 65 deletions

File tree

‎lib/javascript/JavascriptParser.js‎

Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -563,123 +563,138 @@ const valueAsExpression = (value, expr, sideEffects) => {
563563
};
564564

565565
// The constant operators, shared so evaluating `a - b` allocates no handler.
566+
// JavaScript coerces any operand; each cast only lets TypeScript type the operator.
567+
568+
/** @typedef {ReturnType<BasicEvaluatedExpression["asCompileTimeValue"]>} CompileTimeValue */
566569

567570
/**
568-
* @param {EXPECTED_ANY} l left operand
569-
* @param {EXPECTED_ANY} r right operand
570-
* @returns {EXPECTED_ANY} the result
571+
* @param {CompileTimeValue} l left operand
572+
* @param {CompileTimeValue} r right operand
573+
* @returns {number | bigint} the result
571574
*/
572-
const SUBTRACT = (l, r) => l - r;
575+
const SUBTRACT = (l, r) =>
576+
/** @type {number} */ (l) - /** @type {number} */ (r);
573577

574578
/**
575-
* @param {EXPECTED_ANY} l left operand
576-
* @param {EXPECTED_ANY} r right operand
577-
* @returns {EXPECTED_ANY} the result
579+
* @param {CompileTimeValue} l left operand
580+
* @param {CompileTimeValue} r right operand
581+
* @returns {number | bigint} the result
578582
*/
579-
const MULTIPLY = (l, r) => l * r;
583+
const MULTIPLY = (l, r) =>
584+
/** @type {number} */ (l) * /** @type {number} */ (r);
580585

581586
/**
582-
* @param {EXPECTED_ANY} l left operand
583-
* @param {EXPECTED_ANY} r right operand
584-
* @returns {EXPECTED_ANY} the result
587+
* @param {CompileTimeValue} l left operand
588+
* @param {CompileTimeValue} r right operand
589+
* @returns {number | bigint} the result
585590
*/
586-
const DIVIDE = (l, r) => l / r;
591+
const DIVIDE = (l, r) => /** @type {number} */ (l) / /** @type {number} */ (r);
587592

588593
/**
589-
* @param {EXPECTED_ANY} l left operand
590-
* @param {EXPECTED_ANY} r right operand
591-
* @returns {EXPECTED_ANY} the result
594+
* @param {CompileTimeValue} l left operand
595+
* @param {CompileTimeValue} r right operand
596+
* @returns {number | bigint} the result
592597
*/
593-
const POWER = (l, r) => l ** r;
598+
const POWER = (l, r) => /** @type {number} */ (l) ** /** @type {number} */ (r);
594599

595600
/**
596-
* @param {EXPECTED_ANY} l left operand
597-
* @param {EXPECTED_ANY} r right operand
598-
* @returns {EXPECTED_ANY} the result
601+
* @param {CompileTimeValue} l left operand
602+
* @param {CompileTimeValue} r right operand
603+
* @returns {number | bigint} the result
599604
*/
600-
const BITWISE_AND = (l, r) => l & r;
605+
const BITWISE_AND = (l, r) =>
606+
/** @type {number} */ (l) & /** @type {number} */ (r);
601607

602608
/**
603-
* @param {EXPECTED_ANY} l left operand
604-
* @param {EXPECTED_ANY} r right operand
605-
* @returns {EXPECTED_ANY} the result
609+
* @param {CompileTimeValue} l left operand
610+
* @param {CompileTimeValue} r right operand
611+
* @returns {number | bigint} the result
606612
*/
607-
const BITWISE_OR = (l, r) => l | r;
613+
const BITWISE_OR = (l, r) =>
614+
/** @type {number} */ (l) | /** @type {number} */ (r);
608615

609616
/**
610-
* @param {EXPECTED_ANY} l left operand
611-
* @param {EXPECTED_ANY} r right operand
612-
* @returns {EXPECTED_ANY} the result
617+
* @param {CompileTimeValue} l left operand
618+
* @param {CompileTimeValue} r right operand
619+
* @returns {number | bigint} the result
613620
*/
614-
const BITWISE_XOR = (l, r) => l ^ r;
621+
const BITWISE_XOR = (l, r) =>
622+
/** @type {number} */ (l) ^ /** @type {number} */ (r);
615623

616624
/**
617-
* @param {EXPECTED_ANY} l left operand
618-
* @param {EXPECTED_ANY} r right operand
619-
* @returns {EXPECTED_ANY} the result
625+
* @param {CompileTimeValue} l left operand
626+
* @param {CompileTimeValue} r right operand
627+
* @returns {number} the result
620628
*/
621-
const UNSIGNED_SHIFT_RIGHT = (l, r) => l >>> r;
629+
const UNSIGNED_SHIFT_RIGHT = (l, r) =>
630+
/** @type {number} */ (l) >>> /** @type {number} */ (r);
622631

623632
/**
624-
* @param {EXPECTED_ANY} l left operand
625-
* @param {EXPECTED_ANY} r right operand
626-
* @returns {EXPECTED_ANY} the result
633+
* @param {CompileTimeValue} l left operand
634+
* @param {CompileTimeValue} r right operand
635+
* @returns {number | bigint} the result
627636
*/
628-
const SHIFT_RIGHT = (l, r) => l >> r;
637+
const SHIFT_RIGHT = (l, r) =>
638+
/** @type {number} */ (l) >> /** @type {number} */ (r);
629639

630640
/**
631-
* @param {EXPECTED_ANY} l left operand
632-
* @param {EXPECTED_ANY} r right operand
633-
* @returns {EXPECTED_ANY} the result
641+
* @param {CompileTimeValue} l left operand
642+
* @param {CompileTimeValue} r right operand
643+
* @returns {number | bigint} the result
634644
*/
635-
const SHIFT_LEFT = (l, r) => l << r;
645+
const SHIFT_LEFT = (l, r) =>
646+
/** @type {number} */ (l) << /** @type {number} */ (r);
636647

637648
/**
638-
* @param {EXPECTED_ANY} l left operand
639-
* @param {EXPECTED_ANY} r right operand
640-
* @returns {EXPECTED_ANY} the result
649+
* @param {CompileTimeValue} l left operand
650+
* @param {CompileTimeValue} r right operand
651+
* @returns {boolean} the result
641652
*/
642-
const LESS_THAN = (l, r) => l < r;
653+
const LESS_THAN = (l, r) =>
654+
/** @type {number} */ (l) < /** @type {number} */ (r);
643655

644656
/**
645-
* @param {EXPECTED_ANY} l left operand
646-
* @param {EXPECTED_ANY} r right operand
647-
* @returns {EXPECTED_ANY} the result
657+
* @param {CompileTimeValue} l left operand
658+
* @param {CompileTimeValue} r right operand
659+
* @returns {boolean} the result
648660
*/
649-
const GREATER_THAN = (l, r) => l > r;
661+
const GREATER_THAN = (l, r) =>
662+
/** @type {number} */ (l) > /** @type {number} */ (r);
650663

651664
/**
652-
* @param {EXPECTED_ANY} l left operand
653-
* @param {EXPECTED_ANY} r right operand
654-
* @returns {EXPECTED_ANY} the result
665+
* @param {CompileTimeValue} l left operand
666+
* @param {CompileTimeValue} r right operand
667+
* @returns {boolean} the result
655668
*/
656-
const LESS_THAN_OR_EQUAL = (l, r) => l <= r;
669+
const LESS_THAN_OR_EQUAL = (l, r) =>
670+
/** @type {number} */ (l) <= /** @type {number} */ (r);
657671

658672
/**
659-
* @param {EXPECTED_ANY} l left operand
660-
* @param {EXPECTED_ANY} r right operand
661-
* @returns {EXPECTED_ANY} the result
673+
* @param {CompileTimeValue} l left operand
674+
* @param {CompileTimeValue} r right operand
675+
* @returns {boolean} the result
662676
*/
663-
const GREATER_THAN_OR_EQUAL = (l, r) => l >= r;
677+
const GREATER_THAN_OR_EQUAL = (l, r) =>
678+
/** @type {number} */ (l) >= /** @type {number} */ (r);
664679

665680
/**
666-
* @param {EXPECTED_ANY} v operand
667-
* @returns {EXPECTED_ANY} the result
681+
* @param {CompileTimeValue} v operand
682+
* @returns {number | bigint} the result
668683
*/
669-
const BITWISE_NOT = (v) => ~v;
684+
const BITWISE_NOT = (v) => ~(/** @type {number} */ (v));
670685

671686
/**
672-
* @param {EXPECTED_ANY} v operand
673-
* @returns {EXPECTED_ANY} the result
687+
* @param {CompileTimeValue} v operand
688+
* @returns {number} the result
674689
*/
675690
// eslint-disable-next-line no-implicit-coercion
676-
const TO_NUMBER = (v) => +v;
691+
const TO_NUMBER = (v) => +(/** @type {number} */ (v));
677692

678693
/**
679-
* @param {EXPECTED_ANY} v operand
680-
* @returns {EXPECTED_ANY} the result
694+
* @param {CompileTimeValue} v operand
695+
* @returns {number | bigint} the result
681696
*/
682-
const NEGATE = (v) => -v;
697+
const NEGATE = (v) => -(/** @type {number} */ (v));
683698

684699
/**
685700
* Evaluates a binary expression if and only if it is a const operation (e.g. 1 + 2, "a" + "b", etc.).

0 commit comments

Comments
 (0)