Skip to content

Generated JavaScript plural rules misclassify negative counts #1413

Open

Description

@parijat1222q

Overview Description

babel.plural.to_javascript() generates a plural function that selects different categories from PluralRule for negative counts.

Steps to Reproduce

On current master (9a3ef47), generate a JavaScript function:

from babel.plural import PluralRule, to_javascript

rule = PluralRule({'one': 'n is 1', 'few': 'n in 2..4'})
print(rule(-1), rule(-2))  # one few
print(to_javascript(rule))

Run the generated function in JavaScript:

const plural = (function(n) { return (parseInt(n, 10) == n && ((n >= 2 && n <= 4))) ? 'few' : (n == 1) ? 'one' : 'other'; });
console.log(plural(-1), plural(-2));  // other other

Actual Results

The generated JavaScript uses signed n, so negative counts miss categories that their positive counterparts match. This also affects integer-operand and modulo rules.

Expected Results

Negative counts should use their absolute value before evaluating the rules, matching PluralRule and the Unicode plural operand definition.

Reproducibility

Reproduced with CPython 3.15.0 and Node.js 22.23.3. Four new JavaScript execution regressions fail against the unchanged source and pass after normalizing the input.

Additional Information

A focused fix normalizes n once at the start of the generated function. Positive inputs and zero keep their existing categories.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions