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

Diagnostic to flag cases where control flow can break content projection #52726

Closed
wants to merge 4 commits into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Nov 9, 2023

These changes are a follow-up to the fix from #52414. They add a new diagnostic to the compiler that will flag cases where an element would've been projected into a specific slot of a component, but it isn't because it exists inside of a control flow node that has multiple root nodes. Aside from the diagnostic, I have to move some code around so it can be reused. Includes the following commits:

refactor(compiler): expose utility for creating CSS selectors from AST nodes

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in compiler-cli.

refactor(compiler-cli): expose ng-content selectors and preserveWhitespaces during template type checking

These changes expose the ngContentSelectors and preserveWhitespaces metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

test(core): add tests for control flow content projection with ng-container

The control flow projection diagnostic will mention ng-container as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.

fix(compiler-cli): add diagnostic for control flow that prevents content projection

This is a follow-up to the fix from #52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release area: compiler Issues related to `ngc`, Angular's template compiler labels Nov 9, 2023
@ngbot ngbot bot modified the milestone: Backlog Nov 9, 2023
* of a control flow expression *would have been projected* into a specific slot, if the control
* flow node didn't exist.
*/
class TcbControlFlowContentProjectionOp extends TcbOp {
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 wasn't totally sure if another TcbOp was the right place to put this since it doesn't add any statements to the TCB, but I saw that we have some precedent for cases in the TcbDomSchemaCheckerOp. My main concern was to isolate the check from everything else in the TCB. The other option was to put it in a private method inside the Scope, but that class is pretty large as it is.

@crisbeto crisbeto changed the title Diagnostic to flag cases where contrl flow can break content projection Diagnostic to flag cases where control flow can break content projection Nov 9, 2023
@crisbeto crisbeto force-pushed the control-flow-diagnostic branch 2 times, most recently from cac5480 to ab83508 Compare November 9, 2023 10:21
@crisbeto crisbeto requested a review from JoostK November 9, 2023 10:48
@crisbeto crisbeto marked this pull request as ready for review November 9, 2023 10:48
packages/compiler-cli/src/ngtsc/metadata/src/dts.ts Outdated Show resolved Hide resolved
packages/compiler-cli/src/ngtsc/metadata/src/dts.ts Outdated Show resolved Hide resolved
packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts Outdated Show resolved Hide resolved
packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts Outdated Show resolved Hide resolved
const blockName = controlFlowNode instanceof TmplAstIfBlockBranch ? '@if' : '@for';
const lines = [
`Node matches the "${slotSelector}" slot of the "${
componentName}" component, but will not be projected because the surrounding ${
Copy link
Member

Choose a reason for hiding this comment

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

"will not be projected" may be inaccurate if there is a default slot. Perhaps this should have two forms ("will not be projected" and "will be projected into the default projection slot" or smth like that) or generalize to e.g. "will not be projected there".

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 changed the wording a bit to mention that it won't be projected into that slot.

@crisbeto
Copy link
Member Author

I've addressed most of the feedback and will bring up the discussion around using a warning versus an error later today.

Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

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

LGTM. I'm indifferent on warning vs error.

reviewed-for: fw-core, public-api

@pullapprove pullapprove bot requested a review from dylhunn November 15, 2023 14:22
…T nodes

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in `compiler-cli`.
…spaces during template type checking

These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.
…tainer

The control flow projection diagnostic will mention `ng-container` as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.
@crisbeto crisbeto force-pushed the control-flow-diagnostic branch 2 times, most recently from 5a0f84f to f49fa26 Compare November 16, 2023 18:04
…ent projection

This is a follow-up to the fix from angular#52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.
AndrewKushnir pushed a commit that referenced this pull request Nov 17, 2023
AndrewKushnir pushed a commit that referenced this pull request Nov 17, 2023
AndrewKushnir pushed a commit that referenced this pull request Nov 17, 2023
…rveWhitespaces during template type checking (#52726)" (#53012)

This reverts commit 4550a81.

PR Close #53012
AndrewKushnir pushed a commit that referenced this pull request Nov 17, 2023
AndrewKushnir pushed a commit that referenced this pull request Nov 17, 2023
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…T nodes (angular#52726)

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in `compiler-cli`.

PR Close angular#52726
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…spaces during template type checking (angular#52726)

These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close angular#52726
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…tainer (angular#52726)

The control flow projection diagnostic will mention `ng-container` as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.

PR Close angular#52726
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…ent projection (angular#52726)

This is a follow-up to the fix from angular#52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close angular#52726
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…rveWhitespaces during template type checking (angular#52726)" (angular#53012)

This reverts commit 4550a81.

PR Close angular#53012
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
@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 Dec 18, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…T nodes (angular#52726)

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in `compiler-cli`.

PR Close angular#52726
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…spaces during template type checking (angular#52726)

These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close angular#52726
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…tainer (angular#52726)

The control flow projection diagnostic will mention `ng-container` as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.

PR Close angular#52726
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…ent projection (angular#52726)

This is a follow-up to the fix from angular#52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close angular#52726
rlmestre pushed a commit to rlmestre/angular that referenced this pull request Jan 26, 2024
…T nodes (angular#52726)

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in `compiler-cli`.

PR Close angular#52726
rlmestre pushed a commit to rlmestre/angular that referenced this pull request Jan 26, 2024
…spaces during template type checking (angular#52726)

These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close angular#52726
rlmestre pushed a commit to rlmestre/angular that referenced this pull request Jan 26, 2024
…tainer (angular#52726)

The control flow projection diagnostic will mention `ng-container` as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.

PR Close angular#52726
rlmestre pushed a commit to rlmestre/angular that referenced this pull request Jan 26, 2024
…ent projection (angular#52726)

This is a follow-up to the fix from angular#52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close angular#52726
amilamen pushed a commit to amilamen/angular that referenced this pull request Jan 26, 2024
…T nodes (angular#52726)

When doing directive matching in the compiler, we need to be able to create a selector from an AST node. We already have the utility, but these changes simplify the public API and expose it so it can be used in `compiler-cli`.

PR Close angular#52726
amilamen pushed a commit to amilamen/angular that referenced this pull request Jan 26, 2024
…spaces during template type checking (angular#52726)

These changes expose the `ngContentSelectors` and `preserveWhitespaces` metadata to the TCB so they can be used in the next commit to implement a new diagnostic.

PR Close angular#52726
amilamen pushed a commit to amilamen/angular that referenced this pull request Jan 26, 2024
…tainer (angular#52726)

The control flow projection diagnostic will mention `ng-container` as a workaround for projection multiple nodes. These changes add a couple of tests to ensure that the approach works.

PR Close angular#52726
amilamen pushed a commit to amilamen/angular that referenced this pull request Jan 26, 2024
…ent projection (angular#52726)

This is a follow-up to the fix from angular#52414. It adds a diagnostic that will tell users when a control flow is preventing its direct descendants from being projected into a specific component slot.

PR Close angular#52726
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 target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants