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

feat(router): migrate RouterLinkWithHref references to RouterLink #47599

Conversation

AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Oct 1, 2022

Since Angular v15, the RouterLink contains the logic of the RouterLinkWithHref directive and now developers can always import and use the RouterLink directive when they need to add a [routerLink] in templates. This migration finds all imports and usages of the RouterLinkWithHref class and rewrites them to RouterLink instead.

import { RouterLinkWithHref } from '@angular/router';

@Component({
  standalone: true,
  template: `<a [routerLink]="'/abc'">`,
  imports: [RouterLinkWithHref]
})
export class MyComponent {
  @ViewChild(RouterLinkWithHref) aLink!: RouterLinkWithHref;
}
import { RouterLink } from '@angular/router';

@Component({
  standalone: true,
  template: `<a [routerLink]="'/abc'">`,
  imports: [RouterLink]
})
export class MyComponent {
  @ViewChild(RouterLink) aLink!: RouterLink;
}

PR Type

What kind of change does this PR introduce?

  • Feature

Does this PR introduce a breaking change?

  • Yes
  • No

@AndrewKushnir AndrewKushnir added state: WIP state: blocked area: router target: major This PR is targeted for the next major release labels Oct 1, 2022
@ngbot ngbot bot modified the milestone: Backlog Oct 1, 2022
@angular-robot angular-robot bot added the feature Issue that requests a new feature label Oct 1, 2022
@AndrewKushnir AndrewKushnir force-pushed the router-link-with-href-migration branch 2 times, most recently from 1d573f9 to 6098aaf Compare October 3, 2022 00:46
@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Oct 3, 2022
@AndrewKushnir AndrewKushnir marked this pull request as ready for review October 3, 2022 02:44
@pullapprove pullapprove bot requested a review from devversion October 3, 2022 02:44
Copy link
Member

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

LGTM

@AndrewKushnir
Copy link
Contributor Author

AndrewKushnir commented Oct 6, 2022

@crisbeto thanks for the review 👍

I've actually rewrote the logic a bit taking into account your comment. Now the process looks like this:

  • we find all usages and imports, store them in an array (as start pos, width of the symbol and a replacement)
  • sort by start pos in descending order (last-to-first usages) to avoid offset shifts
  • once all usages are collected and sorted, we apply rewrites

Previously, lookups (to find usages) and rewrites were interleaved, thus susceptible to offset shifts.

Could you please take another look when you get a chance?

Copy link
Member

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

LGTM

@devversion devversion removed their request for review October 6, 2022 12:48
@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit action: rerun CI at HEAD and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 6, 2022
Since Angular v15, the `RouterLink` contains the logic of the `RouterLinkWithHref` directive and now developers can always import and use the `RouterLink` directive when they need to add a `[routerLink]` in templates. This migration finds all imports and usages of the `RouterLinkWithHref` class and rewrites them to `RouterLink` instead.

```ts
import { RouterLinkWithHref } from '@angular/router';

@component({
  standalone: true,
  template: `<a [routerLink]="'/abc'">`,
  imports: [RouterLinkWithHref]
})
export class MyComponent {
  @ViewChild(RouterLinkWithHref) aLink!: RouterLinkWithHref;
}
```

```ts
import { RouterLink } from '@angular/router';

@component({
  standalone: true,
  template: `<a [routerLink]="'/abc'">`,
  imports: [RouterLink]
})
export class MyComponent {
  @ViewChild(RouterLink) aLink!: RouterLink;
}
```
@AndrewKushnir AndrewKushnir force-pushed the router-link-with-href-migration branch from fdd8566 to 0e58432 Compare October 6, 2022 17:21
@AndrewKushnir
Copy link
Contributor Author

Presubmit.

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker and removed action: presubmit The PR is in need of a google3 presubmit labels Oct 6, 2022
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit 16c8f55.

@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 Nov 6, 2022
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: router feature Issue that requests a new feature target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants