schmittjoh/twig.js

object getters are not seen by the compiler

Closed this issue · 7 comments

When i have an object with a getter, Twig Js cannot see it causing the value to be undefined.

Any example code? I've added a test case checking for this issue and so far I haven't managed to reproduce it. Here's my test case.

--TEST--
Getter bug from schmittjoh/twig.js#85
--TEMPLATE--
{{ user.getName }}
{{ user.name }}
--DATA--
{
  user: {
    getName: function() {
      return "John Doe"
    }
  }
}
--EXPECT--
John Doe
John Doe

What would I need to change about this test case in order to reproduce your bug? Also, are you able to find out how old the version of Twig.js is that you're using?

That's a really helpful link, thanks! I actually learned a couple of new things about JavaScript from that.

Here's what our test case looks like now.

--TEST--
Getter bug from schmittjoh/twig.js#85
--TEMPLATE--
{{ user.getName }}
{{ user.name }}
{{ user.age }}
{{ user.address }}
--DATA--
var user = {
  _address: "123 Test Road",
  getName: function() {
    return "John Doe"
  },
  get address () {
    return this._address;
  },
  set address (address) {
    this._address = address;
  }
};
Object.defineProperty(user, 'age', {
  get: function() { return 21; }
});
return { user: user };
--EXPECT--
John Doe
John Doe
21
123 Test Road

I haven't managed to reproduce the bug yet though. Could you suggest a change to this test case that would expose the issue?

When you start using "is defined" and probably "|default()" it things its undefined.

Okay, I've updated the test case to use that combination of functionality.

--TEST--
Getter bug from schmittjoh/twig.js#85
--TEMPLATE--
{% if user.name is defined %}{{ user.name }}{% endif %}{{ "" }}
{% if user.age is defined %}{{ user.age }}{% endif %}{{ "" }}
{% if user.address is defined %}{{ user.address }}{% endif %}{{ "" }}
{{ user.name | default("Not defined") }}
{{ user.age | default("Not defined") }}
{{ user.address | default("Not defined") }}
--DATA--
var user = {
  _address: "123 Test Road",
  getName: function() {
    return "John Doe"
  },
  get address () {
    return this._address;
  },
  set address (address) {
    this._address = address;
  }
};
Object.defineProperty(user, 'age', {
  get: function() { return 21; }
});
return { user: user };
--EXPECT--
John Doe
21
123 Test Road
John Doe
21
123 Test Road

Still no sign of the bug. It feels like we must be close to narrowing down the exact syntax that reproduces it now though. Could you please provide a bit of example code at this point to illustrate the problem?

Ok, I think the problem is when you directly pass the user with return and then start using
{% if address is defined %}

Wow this issue is so many years old now. I can't believe I just let it trail off like that, but hey, I guess that's how it goes sometimes. I know you almost definitely won't be interested in this issue any more, so I'll close it, but if you do decide you wanna pick it back up then be my guest! 😄

What would be awesome is, if you do wanna get this fixed, could you maybe write the example yourself? I remember I kept trying and trying to guess what the bug looked like and never really figuring it out. Probably easier if you write the test case come to think of it.

Anyway I hope you've had an awesome few years since you last heard from me 😂