Skip to content

Conversation

@asamuzaK
Copy link
Contributor

Summary

This PR refactors parsers.js to improve performance, maintainability, and code readability. The changes include optimizing lookup operations, eliminating redundant object creation in loops, reducing code duplication, and standardizing coding styles.

Changes

1. Performance Optimizations

  • O(1) Lookups: Converted GLOBAL_KEYS (formerly GLOBAL_KEY) and SYS_COLORS (formerly SYS_COLOR) from Arrays to Sets. This changes the complexity of keyword checks from O(N) to O(1).
  • Remove Dynamic RegExp: Replaced the dynamic creation of new RegExp inside loops within parsePropertyValue with string manipulation methods (slice). This reduces the overhead of compiling regular expressions on every iteration.

2. Refactoring & Maintainability

  • Common Numeric Parser: Introduced a helper function parseNumericValue to centralize the parsing logic for numbers, lengths, percentages, and angles. This reduces code duplication across parseNumber, parseLength, parsePercentage, etc.
    • Design Note: The validation logic is passed as an anonymous callback function rather than being hardcoded in the helper. This design was chosen because the handling of unitless 0 varies across CSS types (e.g., allowed for <length>, but invalid for others such as <time>). Defining the condition explicitly in the caller function maintains clarity and flexibility for type-specific rules.
  • AST Type Constants: Introduced AST_TYPES to manage AST node types (e.g., "Function", "Identifier") as constants, eliminating magic strings and reducing the risk of typos.
  • Module Export Style: Refactored the module to define functions using const and export them via a single module.exports block at the end of the file.

3. Documentation

  • JSDoc: Added comprehensive JSDoc comments to all exported functions to provide type information and improve the developer experience.

Replaces individual exports assignments with local function declarations and a single module.exports object at the end of the file. This improves code clarity and maintainability by consolidating all exports in one place.
Added detailed JSDoc comments to all major functions in lib/parsers.js to improve code readability and provide better context for parameters and return values. This enhances maintainability and developer experience by clarifying function purposes and usage.
Replaces arrays for global CSS keywords and system colors with Set objects for improved lookup performance. Updates all relevant checks from .includes() to .has().
Introduces an AST_TYPES constant object to centralize AST node type strings and replaces hardcoded type string comparisons throughout the file with references to AST_TYPES. This improves maintainability and reduces the risk of typos or inconsistencies in type checks.
Simplified the extraction of itemValue by replacing regex-based string replacement with a more direct slice operation. This improves readability and maintains the same functionality.
Introduces a shared parseNumericValue helper to unify and simplify parsing of numbers, lengths, percentages, and angles. This reduces code duplication and improves maintainability across parseNumber, parseLength, parsePercentage, parseLengthPercentage, and parseAngle.
Replaces hardcoded 'calc' string with the variable 'name' when assigning the 'name' property in parsedValues. This improves code maintainability and consistency for function parsing.
Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Just to confirm, this does not change any observable behavior or fix any bugs?

@domenic domenic merged commit 0a71683 into jsdom:main Dec 27, 2025
3 checks passed
@asamuzaK asamuzaK deleted the parser branch December 27, 2025 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants