Skip to content

feat: add optional chaining (?.) support 🔗#947

Open
bricss wants to merge 12 commits into
twigjs:masterfrom
bricss:feat/add-optional-chaining-support
Open

feat: add optional chaining (?.) support 🔗#947
bricss wants to merge 12 commits into
twigjs:masterfrom
bricss:feat/add-optional-chaining-support

Conversation

@bricss

@bricss bricss commented Mar 15, 2026

Copy link
Copy Markdown

This PR is an attempt to bring support for optional chaining (?.) into the engine.

@willrowe

Copy link
Copy Markdown
Collaborator

@bricss thank you for trying to implement this. Please remove everything not related to implementing this feature, there seems to be a lot of unrelated changes.

@bricss

bricss commented Mar 16, 2026

Copy link
Copy Markdown
Author

Unrelated changes include fix 🪛 for build to work on Windows 🪟 machines, removal of redundant escapes inside regexp literals, and change of regexp that matches quoted strings to increase its performance twofold. I can remove and forget about em, or create separate PR in case if that one will land 🛬

@willrowe

Copy link
Copy Markdown
Collaborator

Please remove anything unrelated. You can open separate PRs for those other things.

@bricss

bricss commented Mar 16, 2026

Copy link
Copy Markdown
Author

Removed unrelated changes except for type annotations 📝

@willrowe

Copy link
Copy Markdown
Collaborator

Please remove all unrelated type annotations as well.

@willrowe

willrowe commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

@bricss sorry for the delay in reviewing this. Can you please rebase over master to resolve the conflict?

@bricss
bricss force-pushed the feat/add-optional-chaining-support branch from 052c8de to 98d75e5 Compare July 9, 2026 23:20
Comment thread src/twig.expression.js Outdated
Comment thread src/twig.expression.js Outdated
Comment thread src/twig.expression.js Outdated
Comment thread src/twig.expression.js Outdated
}

function normalizeObject(object) {
return object === null ? null : Object(object);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What is the purpose of this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This function ensures a value is safe 🦺 to use as an object, with one special case for null.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This could open us up to edge cases and break existing templates. Since it's not necessary for this PR to function, please remove it and just use the objects directly like it was doing before.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It seems 💡 to me that the correct way to handle this would be to default to:

function normalizeObject(object) {
    return Object(object);
}

Otherwise raw object regresses on three fronts at once: primitive access, null, and undefined. Coz the whole point of the helper is to guarantee the value handed to in/Object.keys/[key] is always a genuine object. 🤔

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please just focus on what is necessary for this PR and do what I asked. If you would like to submit a separate PR to address any issues with how objects are handled, please do so.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The function has been removed 🪚, but unfortunately I can't completely get rid of object normalization coz then plenty of tests will start to fail.

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