Skip to content

Commit 05e6d07

Browse files
crisbetommalerba
authored andcommitted
build: enable coercion-types rule and fix leftover cases (#17609)
Follow-up from #17528 that: * Turns on the `coercion-types` rule. * Fixes the rule not detecting abstract directives properly. * Fixes the rule incorrectly flagging coercion functions used inside of callbacks which are inside of setters. * Either fixes or works around a final set of failures.
1 parent bc57c9d commit 05e6d07

File tree

7 files changed

+20
-13
lines changed

7 files changed

+20
-13
lines changed

src/dev-app/example/example.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ export class Example implements OnInit {
7171
this._elementRef.nativeElement.appendChild(new exampleElementCtor(this._injector));
7272
this.title = EXAMPLE_COMPONENTS[this.id] ? EXAMPLE_COMPONENTS[this.id].title : '';
7373
}
74+
75+
static ngAcceptInputType_showLabel: boolean | string;
7476
}

src/material-examples/cdk/stepper/cdk-custom-stepper-without-form/cdk-custom-stepper-without-form-example.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ export class CustomStepper extends CdkStepper {
2020
onClick(index: number): void {
2121
this.selectedIndex = index;
2222
}
23+
24+
// These properties are required so that the Ivy template type checker in strict mode knows
25+
// what kind of values are accepted by the `linear` and `selectedIndex` inputs which
26+
// are inherited from `CdkStepper`.
27+
static ngAcceptInputType_linear: boolean | string;
28+
static ngAcceptInputType_selectedIndex: number | string;
2329
}

src/material-examples/material/slider/slider-configurable/slider-configurable-example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h2 class="example-h2">Result</h2>
5555
[min]="min"
5656
[step]="step"
5757
[thumbLabel]="thumbLabel"
58-
[tickInterval]="tickInterval"
58+
[tickInterval]="getSliderTickInterval()"
5959
[(ngModel)]="value"
6060
[vertical]="vertical">
6161
</mat-slider>

src/material-examples/material/slider/slider-configurable/slider-configurable-example.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {coerceNumberProperty} from '@angular/cdk/coercion';
21
import {Component} from '@angular/core';
32

43
/**
@@ -20,12 +19,9 @@ export class SliderConfigurableExample {
2019
thumbLabel = false;
2120
value = 0;
2221
vertical = false;
22+
tickInterval = 1;
2323

24-
get tickInterval(): number | 'auto' {
25-
return this.showTicks ? (this.autoTicks ? 'auto' : this._tickInterval) : 0;
24+
getSliderTickInterval(): number | 'auto' {
25+
return this.showTicks ? (this.autoTicks ? 'auto' : this.tickInterval) : 0;
2626
}
27-
set tickInterval(value) {
28-
this._tickInterval = coerceNumberProperty(value);
29-
}
30-
private _tickInterval = 1;
3127
}

src/material/paginator/paginator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,4 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
290290
static ngAcceptInputType_hidePageSize: boolean | string;
291291
static ngAcceptInputType_showFirstLastButtons: boolean | string;
292292
static ngAcceptInputType_disabled: boolean | string;
293-
294293
}

tools/tslint-rules/coercionTypesRule.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ class Walker extends Lint.RuleWalker {
149149
return prop.name && ts.isIdentifier(prop.name) && prop.name.text === 'selector';
150150
}) : null;
151151

152-
return !!selector && ts.isPropertyAssignment(selector) && ts.isIdentifier(selector.name) &&
153-
!selector.name.text.startsWith('do-not-use-abstract-');
152+
return !!selector && ts.isPropertyAssignment(selector) &&
153+
(ts.isStringLiteral(selector.initializer) ||
154+
ts.isNoSubstitutionTemplateLiteral(selector.initializer)) &&
155+
!selector.initializer.text.startsWith('do-not-use-abstract-');
154156
}
155157

156158
return false;
@@ -180,7 +182,9 @@ function usesCoercion(setter: ts.SetAccessorDeclaration, coercionFunctions: Set<
180182
coercionWasUsed = true;
181183
}
182184

183-
if (!coercionWasUsed) {
185+
// Don't check callback functions since coercion used
186+
// inside them most-likely won't need to be declared.
187+
if (!coercionWasUsed && !ts.isArrowFunction(node) && !ts.isFunctionExpression(node)) {
184188
node.forEachChild(walk);
185189
}
186190
});

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"rxjs-imports": true,
117117
"require-breaking-change-version": true,
118118
"class-list-signatures": true,
119-
"coercion-types": [false, // Disabled until #17528 gets in.
119+
"coercion-types": [true,
120120
["coerceBooleanProperty", "coerceCssPixelValue", "coerceNumberProperty"],
121121
{
122122
"CanDisable": ["disabled"],

0 commit comments

Comments
 (0)