Skip to content

feat: Report a diagnostic when attempting to use a template literal #1702

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

Merged
merged 1 commit into from
Mar 2, 2021
Merged

feat: Report a diagnostic when attempting to use a template literal #1702

merged 1 commit into from
Mar 2, 2021

Conversation

saulecabrera
Copy link
Contributor

Fixes #1615

I think it makes sense to report a diagnostic until #466 is implemented. We discovered this by accident as it was causing an unexpected behavior in one of our sdks.

  • I've read the contributing guidelines

@MaxGraey
Copy link
Member

MaxGraey commented Feb 25, 2021

I guess better report only if string contain interpolations like

const str = `hi ${name}`;

But back-ticked string literals works normally without ${} and usually using for multiline text without explicit new lines.

@saulecabrera
Copy link
Contributor Author

I guess better report only if string contain interpolations like

const str = `hi ${name}`;

But back-ticked string literals works normally without ${} and usually using for multiline text without explicit new lines.

I'd argue that providing multiline strings through template literals is a half-baked feature. We should either support them fully or not support them at all. This is mostly based on the spec https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#multi-line_strings

Any newline characters inserted in the source are part of the template literal.

If the plan is to keep the current behavior, I think that it should be at least well documented. I'd be happy to update to the documentation if you don't agree with this change.

@saulecabrera
Copy link
Contributor Author

if string contain interpolations like

Also, if your suggestion is to detect interpolation, it would be better to actually implement the interpolation feature itself, doing so really voids the objective of this change.

@saulecabrera
Copy link
Contributor Author

Thoughts @dcodeIO?

@dcodeIO
Copy link
Member

dcodeIO commented Feb 25, 2021

I am fine with emitting a diagnostic here to avoid unexpected outcomes for the time being. Wondering if the diagnostic can be raised only if such a literal becomes actually compiled, though, as it seems that it round-trips from source to AST and back from AST to source just fine already. But I may be wrong :)

@saulecabrera
Copy link
Contributor Author

@dcodeIO Thinking a bit more about this, I'm not sure if an error diagnostic is the right approach since it will require a breaking change for a feature that will be eventually implemented, on top of that, multiline strings already work correctly. I'm thinking that a warning diagnostic is better? Thoughts? I'm just thinking that I can't justify a breaking change for this, I just want something that is enough to notify the developer that interpolation is not implemented and therefore should not be used.

Wondering if the diagnostic can be raised only if such a literal becomes actually compiled, though,

According to my understanding, to make this work, we would need to read the string including the quotes (or some other metadata that indicates which type of string we are compiling), so that we can know when to actually report the diagnostic. Right now we don't store that (at least it's not obvious); I obviously know less than you do in terms of options, but doing so seems to be quite an overhead for a diagnostic that can be caught early.

@dcodeIO
Copy link
Member

dcodeIO commented Feb 25, 2021

Hmm, right, that information is not present anymore in the AST. In this case I'm fine with the warning :)

@saulecabrera
Copy link
Contributor Author

Sounds good. I changed the error to a warning.

@saulecabrera
Copy link
Contributor Author

@dcodeIO would it be possible for you to review this? Or is it better to wait until you finish updating binaryen?

Copy link
Member

@dcodeIO dcodeIO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have been thinking about this a bit more, and I am still a bit concerned that someone else may be unhappy with the warning when only using template literals for multi line strings, as it cannot be silenced, and didn't produce a warning so far. I don't feel strongly, though, as this case seems rare enough, hence approving, yet there is a chance that we'll have to touch this again if our assumptions are wrong. Would you be fine with looking into that as well, if it happens?

@saulecabrera
Copy link
Contributor Author

saulecabrera commented Mar 2, 2021

Have been thinking about this a bit more, and I am still a bit concerned that someone else may be unhappy with the warning when only using template literals for multi line strings, as it cannot be silenced, and didn't produce a warning so far. I don't feel strongly, though, as this case seems rare enough, hence approving, yet there is a chance that we'll have to touch this again if our assumptions are wrong. Would you be fine with looking into that as well, if it happens?

That totally makes sense, I agree that this solution is mostly a bandaid, unfortunately, mostly to make the developers aware that there are going to be unexpected behaviors if they attempt to use interpolation. And yeah I can look into it if it happens again, feel free to ping me and I'll be glad to help.

A good green path forward (after merging this) would be to start (progressively) working on the actual interpolation feature. I see that there's a stale / draft PR here https://github.com/AssemblyScript/assemblyscript/pull/1427/files (two actually). If you agree I can start progressively working on the feature, consolidating the feedback from both PRs. Before doing so, I'd like to ask, given that this is a new language feature, from the top of your head, does this feature has any implications with any of the runtime work that you're doing? I'm mostly asking because if that is the case then the wisest thing to do is to wait I guess.

@dcodeIO
Copy link
Member

dcodeIO commented Mar 2, 2021

What we may want there eventually is to split template literals into an array of string parts present in static memory during compilation, so the actual implementation can be as simple as converting all the parameters to strings, compute the overall length of all string parts plus the converted parameters, and build a single string without having to resort to multiple concatenations or something. I don't think that it would interfere, and I'd be happy to see that landed :)

Shall we merge this one, hope for the best and continue working towards it?

@saulecabrera
Copy link
Contributor Author

Shall we merge this one, hope for the best and continue working towards it?

Yeah, I agree with this 👍

@dcodeIO dcodeIO merged commit 0d06d37 into AssemblyScript:master Mar 2, 2021
@saulecabrera saulecabrera deleted the error-on-template-literal branch March 2, 2021 16:50
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

Successfully merging this pull request may close these issues.

String interpolation should trigger compile error
3 participants