refactor(docs-infra): fix docs examples for Angular-specific tslint rules (#38143)

This commit updates the docs examples to be compatible with the
following Angular-specific tslint rules:
- `component-selector`
- `directive-selector`
- `no-conflicting-lifecycle`
- `no-host-metadata-property`
- `no-input-rename`
- `no-output-native`
- `no-output-rename`

This is in preparation of updating the docs examples `tslint.json` to
match the one generated for new Angular CLI apps in a future commit.

PR Close #38143
This commit is contained in:
George Kalpakas 2020-07-30 13:03:21 +03:00 committed by Alex Rickabaugh
parent 5303773daf
commit bfd13c06e1
27 changed files with 40 additions and 36 deletions

View File

@ -1,3 +1,4 @@
// tslint:disable: no-host-metadata-property
// #docregion progressbar-component // #docregion progressbar-component
import { Component, Input } from '@angular/core'; import { Component, Input } from '@angular/core';

View File

@ -1,16 +1,16 @@
import { Component } from '@angular/core'; import { Component, HostBinding } from '@angular/core';
@Component({ @Component({
selector: 'comp-with-host-binding', selector: 'comp-with-host-binding',
template: 'I am a component!', template: 'I am a component!',
host: {
'[class.special]': 'isSpecial',
'[style.color]': 'color',
'[style.width]': 'width'
}
}) })
export class CompWithHostBindingComponent { export class CompWithHostBindingComponent {
@HostBinding('class.special')
isSpecial = false; isSpecial = false;
@HostBinding('style.color')
color = 'green'; color = 'green';
@HostBinding('style.width')
width = '200px'; width = '200px';
} }

View File

@ -12,6 +12,6 @@ import { Hero } from './hero';
}) })
export class HeroChildComponent { export class HeroChildComponent {
@Input() hero: Hero; @Input() hero: Hero;
@Input('master') masterName: string; @Input('master') masterName: string; // tslint:disable-line: no-input-rename
} }
// #enddocregion // #enddocregion

View File

@ -1,5 +1,4 @@
/* tslint:disable: no-unused-variable component-selector-name one-line space-before-function-paren */ /* tslint:disable: no-unused-variable component-selector one-line space-before-function-paren */
/* tslint:disable:*/
// #docplaster // #docplaster
// #docregion // #docregion
import { Component, forwardRef, Optional, SkipSelf } from '@angular/core'; import { Component, forwardRef, Optional, SkipSelf } from '@angular/core';
@ -20,8 +19,7 @@ const DifferentParent = Parent;
// The `parentType` defaults to `Parent` when omitting the second parameter. // The `parentType` defaults to `Parent` when omitting the second parameter.
// #docregion provide-the-parent // #docregion provide-the-parent
export function provideParent export function provideParent
// #enddocregion provide-parent, provide-the-parent // #enddocregion provide-the-parent
// #docregion provide-parent
(component: any, parentType?: any) { (component: any, parentType?: any) {
return { provide: parentType || Parent, useExisting: forwardRef(() => component) }; return { provide: parentType || Parent, useExisting: forwardRef(() => component) };
} }

View File

@ -11,7 +11,7 @@ import { AdComponent } from './ad.component';
template: ` template: `
<div class="ad-banner-example"> <div class="ad-banner-example">
<h3>Advertisements</h3> <h3>Advertisements</h3>
<ng-template ad-host></ng-template> <ng-template adHost></ng-template>
</div> </div>
` `
// #enddocregion ad-host // #enddocregion ad-host

View File

@ -1,8 +1,9 @@
// tslint:disable: directive-selector
// #docregion // #docregion
import { Directive, ViewContainerRef } from '@angular/core'; import { Directive, ViewContainerRef } from '@angular/core';
@Directive({ @Directive({
selector: '[ad-host]', selector: '[adHost]',
}) })
export class AdDirective { export class AdDirective {
constructor(public viewContainerRef: ViewContainerRef) { } constructor(public viewContainerRef: ViewContainerRef) { }

View File

@ -1,6 +1,6 @@
// tslint:disable: variable-name // tslint:disable: variable-name
// #docregion // #docregion
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations'; import { animate, state, style, transition, trigger } from '@angular/animations';
@Component({ @Component({
@ -9,9 +9,6 @@ import { animate, state, style, transition, trigger } from '@angular/animations'
<span>Popup: {{message}}</span> <span>Popup: {{message}}</span>
<button (click)="closed.next()">&#x2716;</button> <button (click)="closed.next()">&#x2716;</button>
`, `,
host: {
'[@state]': 'state',
},
animations: [ animations: [
trigger('state', [ trigger('state', [
state('opened', style({transform: 'translateY(0%)'})), state('opened', style({transform: 'translateY(0%)'})),
@ -41,6 +38,7 @@ import { animate, state, style, transition, trigger } from '@angular/animations'
`] `]
}) })
export class PopupComponent { export class PopupComponent {
@HostBinding('@state')
state: 'opened' | 'closed' = 'closed'; state: 'opened' | 'closed' = 'closed';
@Input() @Input()

View File

@ -1,4 +1,4 @@
/* tslint:disable use-output-property-decorator directive-class-suffix */ // tslint:disable: directive-selector
import { Directive, ElementRef, EventEmitter, Output } from '@angular/core'; import { Directive, ElementRef, EventEmitter, Output } from '@angular/core';
@Directive({selector: '[myClick]'}) @Directive({selector: '[myClick]'})

View File

@ -1,3 +1,4 @@
// tslint:disable: no-output-native
// #docregion // #docregion
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
import { HeroTaxReturn } from './hero'; import { HeroTaxReturn } from './hero';

View File

@ -1,9 +1,4 @@
/* tslint:disable:use-input-property-decorator */ // tslint:disable: no-input-rename no-output-rename use-input-property-decorator use-output-property-decorator
/* tslint:disable:use-output-property-decorator */
/* tslint:disable:no-input-rename */
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({ @Component({

View File

@ -135,7 +135,7 @@ describe('Lifecycle hooks', () => {
const inputEle = element(by.css('spy-parent input')); const inputEle = element(by.css('spy-parent input'));
const addHeroButtonEle = element(by.cssContainingText('spy-parent button', 'Add Hero')); const addHeroButtonEle = element(by.cssContainingText('spy-parent button', 'Add Hero'));
const resetHeroesButtonEle = element(by.cssContainingText('spy-parent button', 'Reset Heroes')); const resetHeroesButtonEle = element(by.cssContainingText('spy-parent button', 'Reset Heroes'));
const heroEles = element.all(by.css('spy-parent div[mySpy')); const heroEles = element.all(by.css('spy-parent div[appSpy'));
const logEles = element.all(by.css('spy-parent h4 ~ div')); const logEles = element.all(by.css('spy-parent h4 ~ div'));
expect(heroEles.count()).toBe(2, 'should have two heroes displayed'); expect(heroEles.count()).toBe(2, 'should have two heroes displayed');

View File

@ -13,7 +13,7 @@ import { LoggerService } from './logger.service';
Counter = {{counter}} Counter = {{counter}}
<h5>-- Counter Change Log --</h5> <h5>-- Counter Change Log --</h5>
<div *ngFor="let chg of changeLog" mySpy>{{chg}}</div> <div *ngFor="let chg of changeLog" appSpy>{{chg}}</div>
</div> </div>
`, `,
styles: ['.counter {background: LightYellow; padding: 8px; margin-top: 8px}'] styles: ['.counter {background: LightYellow; padding: 8px; margin-top: 8px}']

View File

@ -1,3 +1,5 @@
// tslint:disable: no-conflicting-lifecycle
// #docregion
import { import {
AfterContentChecked, AfterContentChecked,
AfterContentInit, AfterContentInit,

View File

@ -7,7 +7,7 @@
<p></p> <p></p>
<!-- #docregion template --> <!-- #docregion template -->
<div *ngFor="let hero of heroes" mySpy class="heroes"> <div *ngFor="let hero of heroes" appSpy class="heroes">
{{hero}} {{hero}}
</div> </div>
<!-- #enddocregion template --> <!-- #enddocregion template -->

View File

@ -7,8 +7,8 @@ let nextId = 1;
// #docregion spy-directive // #docregion spy-directive
// Spy on any element to which it is applied. // Spy on any element to which it is applied.
// Usage: <div mySpy>...</div> // Usage: <div appSpy>...</div>
@Directive({selector: '[mySpy]'}) @Directive({selector: '[appSpy]'})
export class SpyDirective implements OnInit, OnDestroy { export class SpyDirective implements OnInit, OnDestroy {
constructor(private logger: LoggerService) { } constructor(private logger: LoggerService) { }

View File

@ -1,11 +1,12 @@
// tslint:disable: no-output-native
// #docregion
import { Component, Output, OnInit, EventEmitter, NgModule } from '@angular/core'; import { Component, Output, OnInit, EventEmitter, NgModule } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
// #docregion eventemitter // #docregion eventemitter
@Component({ @Component({
selector: 'zippy', selector: 'app-zippy',
template: ` template: `
<div class="zippy"> <div class="zippy">
<div (click)="toggle()">Toggle</div> <div (click)="toggle()">Toggle</div>

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
import { Directive, ElementRef } from '@angular/core'; import { Directive, ElementRef } from '@angular/core';

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
import { Directive, ElementRef, Input, OnChanges } from '@angular/core'; import { Directive, ElementRef, Input, OnChanges } from '@angular/core';

View File

@ -1,3 +1,4 @@
// tslint:disable: no-host-metadata-property
// #docregion // #docregion
import { Directive } from '@angular/core'; import { Directive } from '@angular/core';

View File

@ -1,4 +1,4 @@
/* tslint:disable:forin */ // tslint:disable: directive-selector forin no-input-rename
import { Component, ContentChildren, Directive, EventEmitter, import { Component, ContentChildren, Directive, EventEmitter,
Injectable, Input, Output, Optional, Injectable, Input, Output, Optional,
HostBinding, HostListener, HostBinding, HostListener,

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
import { Directive, ElementRef, Input, OnChanges } from '@angular/core'; import { Directive, ElementRef, Input, OnChanges } from '@angular/core';

View File

@ -3,7 +3,7 @@ import { Directive, Input, HostListener } from '@angular/core';
// export for convenience. // export for convenience.
export { RouterLink} from '@angular/router'; export { RouterLink} from '@angular/router';
/* tslint:disable:directive-class-suffix */ // tslint:disable: directive-class-suffix directive-selector
// #docregion router-link // #docregion router-link
@Directive({ @Directive({
selector: '[routerLink]' selector: '[routerLink]'

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
export const heroDetail = { export const heroDetail = {
bindings: { bindings: {

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
// #docregion hero-detail-io // #docregion hero-detail-io
export const heroDetail = { export const heroDetail = {

View File

@ -1,3 +1,4 @@
// tslint:disable: directive-selector
// #docregion // #docregion
// #docregion hero-detail // #docregion hero-detail
export const heroDetail = { export const heroDetail = {

View File

@ -42,7 +42,7 @@ mark valid insertion points in the template.
`AdDirective` injects `ViewContainerRef` to gain access to the view `AdDirective` injects `ViewContainerRef` to gain access to the view
container of the element that will host the dynamically added component. container of the element that will host the dynamically added component.
In the `@Directive` decorator, notice the selector name, `ad-host`; In the `@Directive` decorator, notice the selector name, `adHost`;
that's what you use to apply the directive to the element. that's what you use to apply the directive to the element.
The next section shows you how. The next section shows you how.
@ -56,7 +56,7 @@ decorator's `template` property as a template string.
The `<ng-template>` element is where you apply the directive you just made. The `<ng-template>` element is where you apply the directive you just made.
To apply the `AdDirective`, recall the selector from `ad.directive.ts`, To apply the `AdDirective`, recall the selector from `ad.directive.ts`,
`ad-host`. Apply that to `<ng-template>` without the square brackets. Now Angular knows `[adHost]`. Apply that to `<ng-template>` without the square brackets. Now Angular knows
where to dynamically load components. where to dynamically load components.

View File

@ -14,7 +14,7 @@ When you call `emit()`, it passes the emitted value to the `next()` method of an
A good example of usage can be found in the [EventEmitter](api/core/EventEmitter) documentation. Here is the example component that listens for open and close events: A good example of usage can be found in the [EventEmitter](api/core/EventEmitter) documentation. Here is the example component that listens for open and close events:
`<zippy (open)="onOpen($event)" (close)="onClose($event)"></zippy>` `<app-zippy (open)="onOpen($event)" (close)="onClose($event)"></app-zippy>`
Here is the component definition: Here is the component definition: