angular-docs-cn/packages/compiler-cli/test/compliance_old
Kristiyan Kostadinov cdf1ea1951 refactor(compiler): retrieve variables from context inside nested template listener (#40833)
This is a pre-requisite for #40360. Given the following template which has a listener
that references a variable from a parent template (`name`):

```
<ng-template let-name="name">
  <button (click)="hello(name)"></button>
</ng-template>
```

We generate code that looks that looks like. Note how we access `name` through `ctx`:

```js
function template(rf, ctx) {
  if (rf & 1) {
    const r0 = ɵɵgetCurrentView();
    ɵɵelementStart(0, "button", 2);
    ɵɵlistener("click", function() {
      ɵɵrestoreView(r0);
      const name_r0 = ctx.name; // Note the `ctx.name` access here.
      const ctx_r1 = ɵɵnextContext();
      return ctx_r1.log(name_r0);
    });
    ɵɵelementEnd();
  }
}
```

This works fine at the moment, because the template context object can't be changed after creation.
The changes in #40360 allow for the object to be changed, which means that the `ctx` reference
inside the listener will be out of date, because it was bound during creation mode.

This PR aims to address the issue by accessing the context inside listeners through the saved
view reference. With the new code, the generated code from above will look as follows:

```js
function template(rf, ctx) {
  if (rf & 1) {
    const r0 = ɵɵgetCurrentView();
    ɵɵelementStart(0, "button", 2);
    ɵɵlistener("click", function() {
      const restoredCtx = ɵɵrestoreView(r0);
      const name_r0 = restoredCtx.name;
      const ctx_r1 = ɵɵnextContext();
      return ctx_r1.log(name_r0);
    });
    ɵɵelementEnd();
  }
}
```

PR Close #40833
2021-02-17 11:45:46 -08:00
..
mock_compile refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
prelink refactor(compiler-cli): support external template source-mapping when linking (#40237) 2021-01-07 13:12:53 -08:00
BUILD.bazel test(compiler-cli): move testing utils to separate package (#39594) 2020-11-17 11:59:56 -08:00
README.md refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
mock_compiler_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_compiler_compliance_spec.ts refactor(core): Remove the need for explicit static query instruction (#40091) 2021-01-14 13:55:02 -08:00
r3_view_compiler_binding_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_di_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_directives_spec.ts refactor(compiler-cli): migrate view compiler directive tests (#39929) 2020-12-02 14:55:58 -08:00
r3_view_compiler_i18n_spec.ts test(compiler-cli): fix i18n error tests (#40026) 2020-12-15 13:30:52 -08:00
r3_view_compiler_input_outputs_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_listener_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_providers_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_styling_spec.ts refactor(compiler-cli): move legacy compliance tests to new folder (#39617) 2020-11-13 11:25:56 -08:00
r3_view_compiler_template_spec.ts refactor(compiler): retrieve variables from context inside nested template listener (#40833) 2021-02-17 11:45:46 -08:00

README.md

Tests in this directory should be run with:

yarn bazel test --config=ivy  packages/compiler-cli/test/compliance:compliance