question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw an error when templating braces are used to pass attribute values

See original GitHub issue

Hi!

I’ve been using Angular for a few years now, and I regularly come across projects where previous developers used the templating braces to pass attribute values, doing awful sins such as:

<img src="{{myPicture}}" />

Instead of

<img [src]="myPicture" />

I’m starting to seriously pull my hair off by having to fix a ton of occurrences of the error described above, and for this reason I think it’d be really neat if Angular was just throwing an error at build time, runtime or both, preventing inexperienced developer to misuse templating braces.

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Airbladercommented, Apr 18, 2019

The problem here is that we inherit projects developed by other teams from other companies, and so they’re not my coworkers, we just get the codebases as is and have to deal with it.

That’s just a universal fact of software development, and I promise you – it won’t change no matter what. There’s more to quality than a framework can enforce. 😃

having templates written this way doesn’t help with code readability

This is entirely subjective. The very people who write the code like this probably think it’s easier to read (based on my, also subjective, experience). Especially when there’s some concatenation going on like in the following example:

<foo bar="hello-{{ worldName }}"></foo>
<foo [bar]="'hello-' + worldName"></foo>

Yes, I also still prefer the second version, but I find it hard to argue that one is truly more readable than the other.

having templates written this way doesn’t help with code […] maintainability

I fail to see how either one is more or less maintainable than the other(?)

A nice codebase (by my standard) is usually made of functions which aren’t longer than 25 lines and 80 chars long.

This is again just opinion, and in fact a controversial topic. Other people see 80 characters as a relict of software development from two decades ago. If you have these standards, enforce them in your projects. Other people, teams and companies have other standards or focuses (such as easy for newcomers, fast iterations and prototyping, …).

You could also write a simple tslint autofixer that you run on the projects you inherit. 😃

You save 4 chars with the second option by avoid using excessive braces.

Oh, the humanity… 😃

In an AoT build this is not even code that will be shipped down the wire.

Of course you could set this inside your TS class, but some developers prefer setting this in there HTML template which I respect.

Why? By my standard, this is a bad practice, so why should this be allowed? 😉

To be honest, I wonder if using the templating braces to bind inputs could result into security issues, I’d have to dig a little more into that to tell for sure.

I see no reason why it would, a lot of the underlying process is still the same. I won’t count “security” as an argument unless there’s an actual example – and even then, it could (and should) be fixed without dropping interpolation support.


Personally, I would do a cost versus benefit consideration. So far I haven’t heard any objective benefit to dropping interpolation support. On the other hand, the cost is extremely high: it’s a breaking change which, according to your own statements, would affect a lot of code. To be fair, this could likely be automigrated; but I doubt the wider development community would understand the necessity of the change.

1reaction
JoostKcommented, Apr 18, 2019

This would be something for a tslint rule, not the framework itself. From a framework perspective, the given syntax is perfectly valid, it’s even documented like that. The thing is, img#src is supposed to be a string attribute so using interpolation to bind a string seems fine to me.

Feel free to elaborate on why you feel it is “awful” to use this syntax. FWIW, I do share your preference regarding brackets vs. interpolation, but to me it’s entirely subjective.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-react/jsx-curly-brace-presence.md at master
If the rule is set to get rid of unnecessary curly braces and the template literal inside a JSX expression has no expression,...
Read more >
Is there a way to pass a variable with curly brace template tags ...
I have an ng-repeat where I run an ng-if with a method that checks to see if certain parameters match between two sets...
Read more >
Template literals (Template strings) - JavaScript | MDN
While technically permitted by the syntax, untagged template literals are strings and will throw a TypeError when chained. console.log(`Hello`` ...
Read more >
Template parameters and template arguments
When the name of a non-type template parameter is used in an expression within the body of the class template, it is an...
Read more >
Template Designer Documentation - Jinja
Template variables are defined by the context dictionary passed to the template. ... to assign an attribute on any other object will raise...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found