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(core): add automatic migration from Renderer to Renderer2 #30936

Closed
wants to merge 3 commits into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Jun 9, 2019

Adds a schematic and tslint rule that automatically migrate the consumer from Renderer to Renderer2. Supports:

  • Renaming imports.
  • Renaming property and method argument types.
  • Casting to Renderer.
  • Mapping all of the methods from the Renderer to Renderer2.

Note that some of the Renderer methods don't map cleanly between renderers. In these cases the migration adds a helper function at the bottom of the file which ensures that we generate valid code with the same return value as before. E.g. here's what the migration for createText looks like.

Before:

class SomeComponent {
  createAndAddText() {
    const node = this._renderer.createText(this._element.nativeElement, 'hello');
    node.textContent += ' world';
  }
}

After:

class SomeComponent {
  createAndAddText() {
    const node = __rendererCreateTextHelper(this._renderer, this._element.nativeElement, 'hello');
    node.textContent += ' world';
  }
}

function __rendererCreateTextHelper(renderer: any, parent: any, value: any) {
  const node = renderer.createText(value);
  if (parent) {
    renderer.appendChild(parent, node);
  }
  return node;
}

This PR resolves FW-1344.

@crisbeto crisbeto force-pushed the FW-1344/renderer-migration branch 2 times, most recently from 491965d to 2c570bd Compare June 9, 2019 13:38
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: major This PR is targeted for the next major release labels Jun 9, 2019
@ngbot ngbot bot added this to the needsTriage milestone Jun 9, 2019
@crisbeto crisbeto marked this pull request as ready for review June 9, 2019 13:55
@crisbeto crisbeto requested a review from a team as a code owner June 9, 2019 13:55
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Thanks for doing all this work! Seems like it is pretty close! Some thoughts below

@kara kara requested a review from devversion June 14, 2019 20:36
@kara kara added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jun 18, 2019
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jun 19, 2019
@crisbeto
Copy link
Member Author

@kara @jelbourn @devversion I've addressed all of the feedback. Can you take another look?

@alexeagle
Copy link
Contributor

I'll tsunami this over google3 code now to look for broken corner cases

@alexeagle
Copy link
Contributor

This only affected 36 files at Google, so it feels like we don't need to polish the migration that much in order to land it in g3. A bunch of these could just be fixed manually in the couple places they occur, but I'll list them anyway:

  1. in google3 we have a global type AnyDuringAngularIvyMigration - we shouldn't create types inline like
    image
    (also this added type isn't referenced in the file)

  2. Here's an error that the forwardRef return type isn't updated:
    image

  3. this is a nit: we added conditional logic here for size even though the typechecker could have told us that it's non-null in this location
    image
    and here
    image
    and here
    image
    image
    image

  4. A comment got duplicated:
    image

@alexeagle
Copy link
Contributor

one more: a reference to ngRendererCreateElementHelper was added but the symbol isn't imported

image

@alexeagle
Copy link
Contributor

I'll do a global presubmit run across google3 where I hide the Renderer symbol to verify the schematic caught all usages.

One thing we need to do in order to land this is update packages/platform-browser-dynamic/src/compiler_reflector.ts which currently has a reference to Renderer - can we remove that in this PR as well? Maybe it has broader implications and should be done separately; I'm not sure

@kara
Copy link
Contributor

kara commented Jun 20, 2019

@alexeagle Let's land the platform-browser change in a follow-up?

@kara kara added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jun 20, 2019
@alexeagle
Copy link
Contributor

#31162 has the removal of Renderer from platform-browser-dynamic

@alexeagle
Copy link
Contributor

I ran a global presubmit across all of Google code and confirmed it's green: this migration found all the locations where Renderer was used.

@crisbeto crisbeto force-pushed the FW-1344/renderer-migration branch 2 times, most recently from d123ed4 to 70289a8 Compare June 29, 2019 12:23
@crisbeto
Copy link
Member Author

crisbeto commented Jun 29, 2019

@kara can you take another look? I've updated it to incorporate yours and Paul's feedback. I've also added handling for forwardRef based on the result from the G3 presubmit, but I wasn't able to reproduce the case that Alex saw where the helpers weren't inserted in one file. Finally, I added some extra logic to the setElementStyle migration so it uses the type checker to try and infer the value so we generate a ternary only if the value is ambiguous.

@crisbeto crisbeto removed their assignment Jun 29, 2019
@crisbeto crisbeto removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jun 29, 2019
@crisbeto crisbeto force-pushed the FW-1344/renderer-migration branch 2 times, most recently from 81d82a0 to bd5e5db Compare June 29, 2019 21:55
@kara kara added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 2, 2019
Adds a schematic and tslint rule that automatically migrate the consumer from `Renderer` to `Renderer2`. Supports:
* Renaming imports.
* Renaming property and method argument types.
* Casting to `Renderer`.
* Mapping all of the methods from the `Renderer` to `Renderer2`.

Note that some of the `Renderer` methods don't map cleanly between renderers. In these cases the migration adds a helper function at the bottom of the file which ensures that we generate valid code with the same return value as before. E.g. here's what the migration for `createText` looks like.

Before:
```
class SomeComponent {
  createAndAddText() {
    const node = this._renderer.createText(this._element.nativeElement, 'hello');
    node.textContent += ' world';
  }
}
```

After:
```
class SomeComponent {
  createAndAddText() {
    const node = __rendererCreateTextHelper(this._renderer, this._element.nativeElement, 'hello');
    node.textContent += ' world';
  }
}

function __rendererCreateTextHelper(renderer: any, parent: any, value: any) {
  const node = renderer.createText(value);
  if (parent) {
    renderer.appendChild(parent, node);
  }
  return node;
}
```

This PR resolves FW-1344.
@crisbeto crisbeto force-pushed the FW-1344/renderer-migration branch from bd5e5db to 9ed1a44 Compare July 2, 2019 18:53
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jul 2, 2019
@crisbeto crisbeto removed their assignment Jul 2, 2019
@crisbeto
Copy link
Member Author

crisbeto commented Jul 2, 2019

Addressed the latest set of comments.

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM

@kara kara added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 2, 2019
@kara
Copy link
Contributor

kara commented Jul 2, 2019

presubmit

@kara kara added merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: presubmit The PR is in need of a google3 presubmit labels Jul 2, 2019
@kara
Copy link
Contributor

kara commented Jul 2, 2019

Merge-assistance: codefresh OOM

@alxhub alxhub closed this in c095597 Jul 3, 2019
@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 Sep 15, 2019
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: core Issues related to the framework runtime cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note 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

6 participants