ractivejs/ractive

Optional Chaining for Template Parser Bug/Feature

Closed this issue · 2 comments

Description:

Templates currently will error during parsing JS in a template that contains optional chaining thinking it's a ternary statement typically.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

Versions affected: All

Platforms affected: All

Reproduction:

<div id="target"></div>

<script id="template" type="text/ractive">
	<p>{{greeting?.greeting}}, {{name}}!</p>
</script>
var ractive = new Ractive({
  target: '#target',
  template: '#template',
  data: { greeting:  { greeting: 'Hello', name: 'world' }, name: 'world' }
});

In the simple case, for ractive the question mark could just be ignored because it already handles optional chaining with references. It would be nice to have support for more complex things like {{some.long[expression]?.with(a, call)?.too}}. Currently some.long[expression].with(a, call).too will warn at runtime if some.long[expression] comes up undefined because the parser only handles converting the leading part to references and the rest is just executed in a function body e.g. resolve some.long, resolve expression, get some.long[expression], call it _1, and then run _1.with(a, call).too.

@evs-chris TY, Interesting I currently use a fn that is similar to the above for now as I want to ensure Ractive doesn't have any issues or warnings etc event though it doesn't really matter to the end user at the end of the day just me being a perfectionist I suppose. Will be shooting you an email.