Skip to content

fix: resolve a Map entry in strict mode - #2196

Open
superbot-helper wants to merge 1 commit into
handlebars-lang:masterfrom
superbot-helper:fix/strict-map-entries
Open

superbot-helper wants to merge 1 commit into
handlebars-lang:masterfrom
superbot-helper:fix/strict-map-entries

Conversation

@superbot-helper

Copy link
Copy Markdown

What happens

A Map holds its entries as entries, not as properties, so name in Object(obj) is false for a key the map answers. lookupProperty and lookup both take the Map branch; strict was the one lookup that did not.

Same context, same key:

non-strict        {{map.name}}   -> "x"
strict            {{map.name}}   -> throws '"name" not defined in [object Map]'
strict            {{name}}       -> throws, with the map as the root context
strict            {{lookup map "name"}} -> "x"      (the lookup helper is Map-aware)
strict+compat     {{name}}       -> "x"
strict+compat     {{map.name}}   -> throws

Strict mode is meant to reject a name the context does not have. A Map does have it, and three sibling lookups already say so.

The change

strict asks the map, exactly as its siblings do:

const present = obj != null && (Utils.isMap(obj) ? obj.has(name) : name in Object(obj));

It still throws for a key the map does not hold.

Tests

Two specs in spec/strict.js: a present Map key resolves in strict mode, both as a property path and with the map as the root; an absent one still throws. The first fails on master and passes with the change. Across strict, basic, security, runtime, helpers and partials: 283 passed, 1 skipped, 0 failed. oxfmt and oxlint are clean on both files.

Not a duplicate

No open pull request and no issue in this repository matches a strict-mode Map lookup. The nearest tracker items are about {{#each}} over Maps and about compat-mode recursion, neither of which touches strict.

Scope

Only the strict lookup changes. Nothing about what a template may read changes; a non-strict lookup, the lookup helper and the compiled fallback are all untouched.

A Map holds its entries as entries, not as properties, so `name in Object(obj)`
is false for a key the map answers. `lookupProperty` and `lookup` both take the
Map branch; `strict` was the one lookup that did not, so strict mode threw
'"name" not defined in [object Map]' on a key that resolved outside strict mode
and through {{lookup}}. Strict mode is meant to reject a name the context does
not have, and a Map does have it.

strict now asks the map, exactly as its siblings do, and still throws for a key
the map does not hold.

Adds two specs: a present Map key resolves in strict mode, and an absent one
still throws.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The strict-mode Map lookup change has no active review issues.

No open issues 🎉

What changed in this PR

This PR aligns strict-mode Map lookups with Handlebars’ existing Map-aware lookup behavior, allowing present entries to resolve while preserving errors for missing keys.

Changes:

  • Use Map.has() to check entry existence in strict mode, leaving non-Map checks unchanged.
  • Add regression tests for nested and root-context Map entries, plus a missing-key error case.
File Description
spec/​strict.js Tests strict-mode resolution of Map entries and rejection of missing keys.
lib/​handlebars/​runtime.js Adds a Map-aware existence check to strict lookup.

Review effort: Balanced

This branch has not been deployed

No deployments
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