Skip to content

feat(compiler): support safe keyed read expressions#41911

Closed
crisbeto wants to merge 4 commits intoangular:masterfrom
crisbeto:safe-array-access
Closed

feat(compiler): support safe keyed read expressions#41911
crisbeto wants to merge 4 commits intoangular:masterfrom
crisbeto:safe-array-access

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented May 1, 2021

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.

@google-cla google-cla bot added the cla: yes label May 1, 2021
@crisbeto crisbeto force-pushed the safe-array-access branch 3 times, most recently from 4ca10d0 to 8e89b50 Compare May 1, 2021 16:46
@crisbeto crisbeto marked this pull request as ready for review May 1, 2021 17:08
@pullapprove pullapprove bot requested review from JoostK, kyliau and mhevery May 1, 2021 17:08
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: compiler Issues related to `ngc`, Angular's template compiler target: minor This PR is targeted for the next minor release labels May 1, 2021
@ngbot ngbot bot modified the milestone: Backlog May 1, 2021
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@crisbeto thanks for the changes 👍 I've just added a couple minor comments related to extra test cases.

Reviewed-for: fw-core

Copy link
Member

@JoostK JoostK left a comment

Choose a reason for hiding this comment

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

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.

@JoostK JoostK requested a review from alxhub May 4, 2021 18:24
@crisbeto crisbeto force-pushed the safe-array-access branch 2 times, most recently from ab8697a to 402f75b Compare May 5, 2021 20:51
@crisbeto
Copy link
Member Author

crisbeto commented May 5, 2021

All of the feedback has been addressed.

@AndrewKushnir
Copy link
Contributor

@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 someObj.method?.(). If we need to bump a compiler version and/or do any other checks for libraries, we can just do that for both changes at the same time. If it's not an easy change - no worries, we can do that later!

@crisbeto
Copy link
Member Author

From what I could tell, we already support safe function calls.

@JoostK
Copy link
Member

JoostK commented May 13, 2021

From what I could tell, we already support safe function calls.

Those may actually only be someObj?.method(), which is a SafeMethodCall in the AST (we're not treating the safe property read as the receiver of a call expression, but rather the whole call expression is specialised for the ?. syntax). So it could be that Andrew's point is valid.

@crisbeto
Copy link
Member Author

crisbeto commented May 13, 2021

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 someObj?.method() as a safe method call? I believe that under the TS AST that would be a safe property access expression.

@JoostK
Copy link
Member

JoostK commented May 13, 2021

From an AST standpoint, wouldn't it be weird to classify someObj?.method() as a safe method call? I believe that under the TS AST that would be a safe property access expression.

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 FunctionCall vs MethodCall, where the latter has a property access receiver.

@crisbeto crisbeto force-pushed the safe-array-access branch from 402f75b to 49bc65d Compare May 16, 2021 10:50
@tehpsalmist
Copy link

Just as a suggestion, it might be a good idea to include [a]?.[b] as a test case for the functionality, too. Thanks to everyone involved!

@crisbeto
Copy link
Member Author

Marking this as ready for presubmit and merge, based on our discussion.

@crisbeto
Copy link
Member Author

@petebacondarwin
Copy link
Contributor

I'd be cool either leaving it as it currently is.
But perhaps changing it back to 12.0.0 is better. This would avoid churning that bit of code, and the application developer would still get an error if they tried to use a library which had this feature in a 12.0.0 app.

@crisbeto crisbeto force-pushed the safe-array-access branch from 49bc65d to 0bc64da Compare May 21, 2021 18:53
@crisbeto
Copy link
Member Author

The minVersion changes have been reverted.

@crisbeto crisbeto force-pushed the safe-array-access branch from 0bc64da to e98ad89 Compare May 21, 2021 19:47
Copy link
Member

Choose a reason for hiding this comment

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

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 the veWillInferAnyFor case.
  • that'll let you get rid of let node and return from inside each case instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

crisbeto added 4 commits May 23, 2021 09:20
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.
Copy link
Member Author

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

I've addressed the latest set of feedback.

Copy link
Member Author

Choose a reason for hiding this comment

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

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?

@AndrewKushnir
Copy link
Contributor

@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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Jun 2, 2021
@AndrewKushnir
Copy link
Contributor

Global Presubmit.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Jun 2, 2021
@AndrewKushnir AndrewKushnir requested review from atscott and removed request for mhevery June 2, 2021 20:29
@AndrewKushnir
Copy link
Contributor

FYI presubmits (including a global one) went well, waiting for the language-service group approval before merging this PR.

Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

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

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).

@AndrewKushnir AndrewKushnir removed the request for review from kyliau June 3, 2021 02:19
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler cla: yes target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants