Go to file
Alex Rickabaugh 0287b234ea feat(ivy): convert all ngtsc diagnostics to ts.Diagnostics (#31952)
Historically, the Angular Compiler has produced both native TypeScript
diagnostics (called ts.Diagnostics) and its own internal Diagnostic format
(called an api.Diagnostic). This was done because TypeScript ts.Diagnostics
cannot be produced for files not in the ts.Program, and template type-
checking diagnostics are naturally produced for external .html template
files.

This design isn't optimal for several reasons:

1) Downstream tooling (such as the CLI) must support multiple formats of
diagnostics, adding to the maintenance burden.

2) ts.Diagnostics have gotten a lot better in recent releases, with support
for suggested changes, highlighting of the code in question, etc. None of
these changes have been of any benefit for api.Diagnostics, which have
continued to be reported in a very primitive fashion.

3) A future plugin model will not support anything but ts.Diagnostics, so
generating api.Diagnostics is a blocker for ngtsc-as-a-plugin.

4) The split complicates both the typings and the testing of ngtsc.

To fix this issue, this commit changes template type-checking to produce
ts.Diagnostics instead. Instead of reporting a special kind of diagnostic
for external template files, errors in a template are always reported in
a ts.Diagnostic that highlights the portion of the template which contains
the error. When this template text is distinct from the source .ts file
(for example, when the template is parsed from an external resource file),
additional contextual information links the error back to the originating
component.

A template error can thus be reported in 3 separate ways, depending on how
the template was configured:

1) For inline template strings which can be directly mapped to offsets in
the TS code, ts.Diagnostics point to real ranges in the source.

This is the case if an inline template is used with a string literal or a
"no-substitution" string. For example:

```typescript
@Component({..., template: `
<p>Bar: {{baz}}</p>
`})
export class TestCmp {
  bar: string;
}
```

The above template contains an error (no 'baz' property of `TestCmp`). The
error produced by TS will look like:

```
<p>Bar: {{baz}}</p>
          ~~~

test.ts:2:11 - error TS2339: Property 'baz' does not exist on type 'TestCmp'. Did you mean 'bar'?
```

2) For template strings which cannot be directly mapped to offsets in the
TS code, a logical offset into the template string will be included in
the error message. For example:

```typescript
const SOME_TEMPLATE = '<p>Bar: {{baz}}</p>';

@Component({..., template: SOME_TEMPLATE})
export class TestCmp {
  bar: string;
}
```

Because the template is a reference to another variable and is not an
inline string constant, the compiler will not be able to use "absolute"
positions when parsing the template. As a result, errors will report logical
offsets into the template string:

```
<p>Bar: {{baz}}</p>
          ~~~

test.ts (TestCmp template):2:15 - error TS2339: Property 'baz' does not exist on type 'TestCmp'.

  test.ts:3:28
    @Component({..., template: TEMPLATE})
                               ~~~~~~~~

    Error occurs in the template of component TestCmp.
```

This error message uses logical offsets into the template string, and also
gives a reference to the `TEMPLATE` expression from which the template was
parsed. This helps in locating the component which contains the error.

3) For external templates (templateUrl), the error message is delivered
within the HTML template file (testcmp.html) instead, and additional
information contextualizes the error on the templateUrl expression from
which the template file was determined:

```
<p>Bar: {{baz}}</p>
          ~~~

testcmp.html:2:15 - error TS2339: Property 'baz' does not exist on type 'TestCmp'.

  test.ts:10:31
    @Component({..., templateUrl: './testcmp.html'})
                                  ~~~~~~~~~~~~~~~~

    Error occurs in the template of component TestCmp.
```

PR Close #31952
2019-08-21 10:51:59 -07:00
.circleci ci: update material-unit-tests commit (#32243) 2019-08-21 10:41:51 -07:00
.devcontainer build: add recommended config files for VSCode remote development (#30450) 2019-05-20 10:13:53 -07:00
.github docs: change to global approvers (#31940) 2019-08-20 16:47:16 -07:00
.vscode build: add recommended config files for VSCode remote development (#30450) 2019-05-20 10:13:53 -07:00
aio build(docs-infra): upgrade cli command docs sources to d2489aeda (#32236) 2019-08-21 10:09:35 -07:00
docs ci: use circleci windows preview (#31266) 2019-08-19 13:32:14 -07:00
integration feat: make the Ivy compiler the default for ngc (#32219) 2019-08-20 16:41:08 -07:00
modules perf: don't create holey arrays (#32155) 2019-08-21 08:27:43 -07:00
packages feat(ivy): convert all ngtsc diagnostics to ts.Diagnostics (#31952) 2019-08-21 10:51:59 -07:00
scripts build: add blocklist for material unit tests (#32239) 2019-08-21 10:03:01 -07:00
third_party feat(bazel): use rbe_autoconfig() and new container. (#29336) 2019-05-09 14:58:34 -07:00
tools build: add blocklist for material unit tests (#32239) 2019-08-21 10:03:01 -07:00
.bazelignore build: update to rules_nodejs 0.32.2 (#31325) 2019-07-01 14:16:42 -07:00
.bazelrc ci: use circleci windows preview (#31266) 2019-08-19 13:32:14 -07:00
.clang-format feat(tooling): Add a .clang-format for automated JavaScript formatting. 2015-04-02 08:44:34 -07:00
.editorconfig build: use https link to editorconfig.org in .editorconfig (#27664) 2018-12-18 09:30:09 -08:00
.gitattributes test: fix ts api guardian and public guard tests on windows (#30105) 2019-04-26 16:32:22 -07:00
.gitignore build: add recommended config files for VSCode remote development (#30450) 2019-05-20 10:13:53 -07:00
.mailmap build: add a Git .mailmap with my new name (#19550) 2017-10-09 14:35:30 -07:00
.nvmrc build: update .nvmrc file to correct node version (#25992) 2018-09-18 13:11:58 -07:00
BUILD.bazel build: update to rules_nodejs 0.32.2 (#31325) 2019-07-01 14:16:42 -07:00
CHANGELOG.md release: cut the v9.0.0-next.2 release 2019-08-12 13:46:30 -07:00
CODE_OF_CONDUCT.md docs: fix community tab in GitHub by copying CoC 2018-02-27 19:02:30 -08:00
CONTRIBUTING.md build: add `ngcc` as a valid commit message scope (#32144) 2019-08-15 10:33:36 -07:00
LICENSE build: bump year (#27880) 2019-01-11 11:15:59 -08:00
README.md docs: fix typo of Typescript to TypeScript (#32153) 2019-08-15 12:44:41 -07:00
WORKSPACE build: update to nodejs rules 0.34.0 and bazel 0.28.1 (#31824) 2019-07-26 15:01:25 -07:00
browser-providers.conf.js ci: lock chrome and firefox versions for saucelabs (#29529) 2019-03-27 12:38:13 -07:00
browser_repositories.bzl build(bazel): update to latest stable chromium 74 on osx and linux for karma under bazel (#30502) 2019-05-16 14:38:14 -07:00
gulpfile.js build(zone.js): add changelog gulptask for zone.js (#31852) 2019-07-26 11:30:08 -07:00
karma-js.conf.js build: rules_nodejs 0.26.0 & use @npm instead of @ngdeps now that downstream angular build uses angular bundles (#28871) 2019-02-28 12:06:36 -08:00
package.json fix(bazel): pin `@microsoft/api-extractor` (#32187) 2019-08-19 15:42:44 -07:00
protractor-perf.conf.js fixup! test(ivy): make the test run with benchpress (#30449) 2019-05-16 20:04:04 -07:00
renovate.json ci: update packageFiles to be renovated (#29071) 2019-03-05 09:43:23 -08:00
shims_for_IE.js Revert "refactor: add license header to JS files & format files (#12035)" 2016-10-04 14:06:41 -07:00
test-events.js fix(platform-browser): run BLACK_LISTED_EVENTS outside of ngZone (#18993) 2017-09-05 15:33:22 -05:00
test-main.js feat(upgrade): provide unit test helpers for wiring up injectors (#16848) 2019-06-20 17:04:01 -07:00
tslint.json refactor: remove toplevel property accesses (#29329) 2019-05-16 12:08:49 -07:00
yarn.lock fix(bazel): pin `@microsoft/api-extractor` (#32187) 2019-08-19 15:42:44 -07:00
yarn.lock.readme.md build: remove travisci leftovers (#27979) 2019-01-09 10:41:16 -08:00

README.md

CircleCI BrowserStack Status Join the chat at https://gitter.im/angular/angular npm version

Angular

Angular is a development platform for building mobile and desktop web applications using TypeScript/JavaScript and other languages.

Quickstart

Get started in 5 minutes.

Changelog

Learn about the latest improvements.

Want to help?

Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on our guidelines for contributing and then check out one of our issues in the hotlist: community-help.