feat(compiler): support safe keyed read expressions#41911
feat(compiler): support safe keyed read expressions#41911crisbeto wants to merge 4 commits intoangular:masterfrom
Conversation
4ca10d0 to
8e89b50
Compare
AndrewKushnir
left a comment
There was a problem hiding this comment.
@crisbeto thanks for the changes 👍 I've just added a couple minor comments related to extra test cases.
Reviewed-for: fw-core
JoostK
left a comment
There was a problem hiding this comment.
This looks pretty good. I left some comments and a request to add tests for the Ivy language service in packages/language-service/ivy/test/legacy/template_target_spec.ts.
packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts
Outdated
Show resolved
Hide resolved
packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts
Outdated
Show resolved
Hide resolved
packages/compiler-cli/test/compliance/test_cases/r3_view_compiler/safe_access/GOLDEN_PARTIAL.js
Outdated
Show resolved
Hide resolved
ab8697a to
402f75b
Compare
|
All of the feedback has been addressed. |
...iler-cli/test/compliance/test_cases/r3_view_compiler/safe_access/safe_keyed_read_template.js
Outdated
Show resolved
Hide resolved
|
@crisbeto once again, thanks a lot for these changes, this is a great thing to support in expressions! 👍 I'm curious if it might be straightforward to also add optional chaining with function calls (as a followup PR, following the changes from this one), like |
|
From what I could tell, we already support safe function calls. |
Those may actually only be |
|
Edit: I re-read the tests again and you're right, this isn't something we handle at the moment. For reference, I'm talking about these tests: https://github.com/angular/angular/blob/master/packages/compiler/test/expression_parser/parser_spec.ts#L371 From an AST standpoint, wouldn't it be weird to classify |
Yes, our AST is weird. I don't actually know why we're treating it as a special AST node. We have the same weirdness around |
402f75b to
49bc65d
Compare
|
Just as a suggestion, it might be a good idea to include |
|
Marking this as ready for presubmit and merge, based on our discussion. |
|
@petebacondarwin just to double-check: should I keep the changes to the |
|
I'd be cool either leaving it as it currently is. |
49bc65d to
0bc64da
Compare
|
The |
0bc64da to
e98ad89
Compare
There was a problem hiding this comment.
I'm not sure this is necessary/desirable. In 2 of the 3 cases, a sub-expression is already marked with the source span of the SafeKeyedRead. It's odd to have an outer expression that's also annotated.
I think this could be refactored:
- don't have the catch-all
addParseSpanInfo, and mark the element access expression with the span inside theveWillInferAnyForcase. - that'll let you get rid of
let nodeandreturnfrom inside each case instead.
There was a problem hiding this comment.
I don't have the full context here, but I was following the pattern from visitSafeMethodCall and visitSafePropertyRead right above it. Should they be refactored as well?
There was a problem hiding this comment.
Possibly, but I think that's a bigger change that should be in a different PR. For now, I think following the pattern from visitSafeMethodCall and visitSafePropertyRead is correct.
Currently we support safe property (`a?.b`) and method (`a?.b()`) accesses, but we don't handle safe keyed reads (`a?.[0]`) which is inconsistent. These changes expand the compiler in order to support safe key read expressions as well.
crisbeto
left a comment
There was a problem hiding this comment.
I've addressed the latest set of feedback.
There was a problem hiding this comment.
I don't have the full context here, but I was following the pattern from visitSafeMethodCall and visitSafePropertyRead right above it. Should they be refactored as well?
e98ad89 to
89bb80e
Compare
|
@crisbeto FYI I've started a presubmit, will keep this thread updated. I've also removed the "action: merge" label for now since @alxhub requested changes (which you've addressed), so he may want to have a final look. Thank you. |
There was a problem hiding this comment.
Possibly, but I think that's a bigger change that should be in a different PR. For now, I think following the pattern from visitSafeMethodCall and visitSafePropertyRead is correct.
|
FYI presubmits (including a global one) went well, waiting for the |
atscott
left a comment
There was a problem hiding this comment.
LGTM for language service. Looks like no updates are needed there. I might still recommend adding a simple quick info or go to definition test for a property read on a?.[0].pr|op just to have an automated test for it (rather than the manual testing I did).
|
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. |
Currently we support safe property (
a?.b) and method (a?.b()) accesses, but we don't handle safe keyed reads (a?.[0]) which is inconsistent. These changes expand the compiler in order to support safe key read expressions as well.