Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined/null values shouldn't be cast to String #659

Closed
vp2177 opened this issue Jun 23, 2017 · 2 comments
Closed

undefined/null values shouldn't be cast to String #659

vp2177 opened this issue Jun 23, 2017 · 2 comments

Comments

@vp2177
Copy link
Contributor

vp2177 commented Jun 23, 2017

In my opinion it would be more useful if null/undefined/false would result in no text node at all in <element>{{}}</element>, and would remove the attribute it's set to in case of <element attribute='{{}}'>

Please take a look at this demostration of "questionable" String casts: https://svelte.technology/repl?version=1.22.5&gist=a63a1bbf1dec86521bbd238699e7ffdf

@Rich-Harris
Copy link
Member

Thanks for opening an issue. We discussed a related case in #249 (comment), and I think the same situation applies here — there's really no reason why undefined and null should be turned into the empty string, since it's really just masking an error. Explicitly initialising your data is the recommended approach (or, if you can't for whatever reason, then doing {{obj.missing || ''}} instead of just {{obj.missing}}).

In any case, it would result in a lot of wasted code being generated. At the moment, the code that gets generated for e.g. {{obj.missing}} is this:

var text_5 = createText(text_5_value = state.obj.missing);

// later, when updating
if (text_5_value !== (text_5_value = state.obj.missing)) {
  text_5.data = text_5_value;
}

Changing it to this, which is what would be necessary...

var text_5 = createText((text_5_value = state.obj.missing) == null ? '' : text_5_value);

// later, when updating
if (text_5_value !== (text_5_value = state.obj.missing)) {
  text_5.data = text_5_value == null ? '' : text_5_value;
}

...isn't desirable, even if the resulting behaviour was. So I'll close this as wontfix.

@ricardobeat
Copy link

Is it possible to override the default __escape method?

This would allow us to implement this behavior at runtime (without resorting to a Proxy object). I'm evaluating using Svelte for a project where dropping 'undefined' in front of users is not really the desired outcome in production, regardless of what failed. Requiring everyone to write a manual fallback for every interpolated value in the codebase is also not feasible.

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

No branches or pull requests

3 participants