-
Notifications
You must be signed in to change notification settings - Fork 26.2k
fix(compiler-cli): interpret string concat calls #44167
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
Conversation
These changes add support for interpreting `String.prototype.concat` calls. We need to support it, because in TypeScript 4.5 string template expressions are transpiled to `concat` calls, rather than string concatenations. See microsoft/TypeScript#45304.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition @crisbeto.
I "think" that this change to TS will not break $localize
processing in @angular/localize
because even in ES5 mode a tagged template handler will not be a simple string, but instead will continue to be a function call with the "message parts" added as parameters.
That being said, we should consider changing the output of $localize
processing to follow this same approach (rather than using +
) to get the same level of spec compliance. For context the following localized message $localize `hello ${name}!`;
will be translated to, say, "bonjour " + name + "!";
- and I guess it should be translated to "bonjour".concat(value).concat("!");
.
@@ -533,6 +533,17 @@ runInEachFileSystem(() => { | |||
.toBe('a.test.b'); | |||
}); | |||
|
|||
it('string `concat` function works', () => { | |||
expect(evaluate(`const a = '12', b = '34';`, 'a[\'concat\'](b)')).toBe('1234'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the tests here appear to test the element access form: a['concat']
.
I assume that the changes also support property access form a.concat
too? If so, can we add tests to prove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that these tests don't have any typings so using the .concat
access throws a type error. All the other tests use the same trick.
This change wasn't prompted by I caught it in my PR to add support for TS 4.5: #44164 |
Maybe we should consider changing the ngcc integration tests to not rely on the workspace TS version, as ngcc will not have to deal with TS >=4.4 emit output in real life. This isn't the first time where we need to adapt ngtsc to support ngcc and I'd like to reduce the complexity/effort around TS updates going forward. |
I think in this case, this is actually a valuable contribution to the compiler generally, expanding the syntaxes understood by our static evaluation of TS. |
The test revealed a legitimate issue though. It meant that something like this would throw a compiler error where it previously didn't:
|
But only if you were targeting es5 I think. And even then, I think the Angular compilation happens before the downleveling. |
That's indeed only relevant when ngtsc is processing JS code, which is only the case with ngcc. Regular compilations perform analysis using the original TS source code and see the template literal as written by the user. With this commit, the user would be allowed to write: const name = '...';
@Component({
template: 'Hello '.concat(name),
}) TBH I'm not convinced that we should support this (but we already do support |
IMO we still need to support cases like this as long as ngcc exists in the codebase. |
That is only necessary if people are able to build ViewEngine formatted libraries using TS 4.5, which is not the case, unless we updated 12.2.x to support TS 4.5. But I do think it is not a bad idea to support this construct. It would also allow us to do things like:
(I think...) |
I agree with @JoostK's assessment that this is not strictly necessary, because there is no way that ngcc could encounter a package in the wild that's built with TS 4.5, since such a package would need to be built with Angular 12 or lower, and that doesn't support TS 4.5. That said, supporting this is easy, and it unblocks the 4.5 update work while we think about changing how we generate ngcc's test code (which we should also do). |
This PR was merged into the repository by commit 48ca7dc. |
These changes add support for interpreting `String.prototype.concat` calls. We need to support it, because in TypeScript 4.5 string template expressions are transpiled to `concat` calls, rather than string concatenations. See microsoft/TypeScript#45304. PR Close #44167
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
These changes add support for interpreting `String.prototype.concat` calls. We need to support it, because in TypeScript 4.5 string template expressions are transpiled to `concat` calls, rather than string concatenations. See microsoft/TypeScript#45304. PR Close angular#44167
These changes add support for interpreting
String.prototype.concat
calls. We need to support it, because in TypeScript 4.5 string template expressions are transpiled toconcat
calls, rather than string concatenations. See microsoft/TypeScript#45304.