canjs / can-view-scope

Scope management for view engines
https://canjs.com/doc/can-view-scope.html
MIT License
4 stars 0 forks source link

Unable evaluate JavaScript in stache expression. #197

Closed Lighttree closed 5 years ago

Lighttree commented 5 years ago

How often can you reproduce it?

JavaScript statement inside {{expressions}} throws TypeError:

Uncaught TypeError: Invalid value used as weak map key
    at WeakMap.set (<anonymous>)
    at Object.addMutatedBy (ecosystem.mjs:12556)
    at callMutateWithRightArgs (ecosystem.mjs:17854)
    at ScopeKeyData.read (ecosystem.mjs:18058)
    at Observation.onBound (ecosystem.mjs:5498)
    at KeyTree.add (ecosystem.mjs:4681)
    at Observation.on (ecosystem.mjs:4956)
    at Object.onValue (ecosystem.mjs:1101)
    at ScopeKeyData.onBound (ecosystem.mjs:17940)
    at KeyTree.add (ecosystem.mjs:4681)

Steps to reproduce:

https://codepen.io/Lighttree/pen/eQjzwy?editors=1010

Expected results:

Before it was possible to evaluate JavaScript inside {{expressions}}.

Actual results:

Inline JavaScript evaluation doesn't work anymore.

Environment:

Software Version
can-stache version 4+
Browser Chrome
Operating system Windows
Lighttree commented 5 years ago

Seems like not all code fails. For example I had following:

ViewModel:

contextTitle: {
  value() {
    return this.context
      .split('_')
      .map(word => word.charAt(0).toUpperCase() + word.slice(1))
      .join(' ');
  }
}

Stache:

<h1>
    {{contextTitle.split(' ')[0]}} <span class="styled">{{contextTitle.split(' ')[1]}}</span>
</h1>

This case fails.

I found some workaround for my case that works:

ViewModel:

contextTitle: {
  value() {
    return this.context
      .split('_')
      .map(word => word.charAt(0).toUpperCase() + word.slice(1));
  }
}

Stache:

<h1>
    {{contextTitle[0]}} <span class="styled">{{contextTitle[1]}}</span>
</h1>

... some other place ...

<h2 class="headline2">
  {{ i18n('txt_my_events_lbl', event_type=contextTitle.join(' ')) }}
</h2>
justinbmeyer commented 5 years ago

It was never possible to evaluate all JS expression types. However, these call expressions should work.

justinbmeyer commented 5 years ago

Given the error, I'm not sure this is a stache problem.