Skip to content

Commit

Permalink
build: enable coercion-types rule and fix leftover cases
Browse files Browse the repository at this point in the history
Follow-up from angular#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.
  • Loading branch information
crisbeto committed Nov 4, 2019
1 parent 8422580 commit 1ce6b3b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/dev-app/example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ export class Example implements OnInit {
this._elementRef.nativeElement.appendChild(new exampleElementCtor(this._injector));
this.title = EXAMPLE_COMPONENTS[this.id] ? EXAMPLE_COMPONENTS[this.id].title : '';
}

static ngAcceptInputType_showLabel: boolean | string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export class CustomStepper extends CdkStepper {
onClick(index: number): void {
this.selectedIndex = index;
}

static ngAcceptInputType_linear: boolean | string;
static ngAcceptInputType_selectedIndex: number | string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2 class="example-h2">Result</h2>
[min]="min"
[step]="step"
[thumbLabel]="thumbLabel"
[tickInterval]="tickInterval"
[tickInterval]="getSliderTickInterval()"
[(ngModel)]="value"
[vertical]="vertical">
</mat-slider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {Component} from '@angular/core';

/**
Expand All @@ -20,12 +19,9 @@ export class SliderConfigurableExample {
thumbLabel = false;
value = 0;
vertical = false;
tickInterval = 1;

get tickInterval(): number | 'auto' {
return this.showTicks ? (this.autoTicks ? 'auto' : this._tickInterval) : 0;
getSliderTickInterval(): number | 'auto' {
return this.showTicks ? (this.autoTicks ? 'auto' : this.tickInterval) : 0;
}
set tickInterval(value) {
this._tickInterval = coerceNumberProperty(value);
}
private _tickInterval = 1;
}
1 change: 0 additions & 1 deletion src/material/paginator/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,4 @@ export class MatPaginator extends _MatPaginatorBase implements OnInit, OnDestroy
static ngAcceptInputType_hidePageSize: boolean | string;
static ngAcceptInputType_showFirstLastButtons: boolean | string;
static ngAcceptInputType_disabled: boolean | string;

}
10 changes: 7 additions & 3 deletions tools/tslint-rules/coercionTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ class Walker extends Lint.RuleWalker {
return prop.name && ts.isIdentifier(prop.name) && prop.name.text === 'selector';
}) : null;

return !!selector && ts.isPropertyAssignment(selector) && ts.isIdentifier(selector.name) &&
!selector.name.text.startsWith('do-not-use-abstract-');
return !!selector && ts.isPropertyAssignment(selector) &&
(ts.isStringLiteral(selector.initializer) ||
ts.isNoSubstitutionTemplateLiteral(selector.initializer)) &&
!selector.initializer.text.startsWith('do-not-use-abstract-');
}

return false;
Expand Down Expand Up @@ -180,7 +182,9 @@ function usesCoercion(setter: ts.SetAccessorDeclaration, coercionFunctions: Set<
coercionWasUsed = true;
}

if (!coercionWasUsed) {
// Don't checked callback functions since coercion used
// inside them most-likely won't need to be declared.
if (!coercionWasUsed && !ts.isArrowFunction(node) && !ts.isFunctionExpression(node)) {
node.forEachChild(walk);
}
});
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"rxjs-imports": true,
"require-breaking-change-version": true,
"class-list-signatures": true,
"coercion-types": [false, // Disabled until #17528 gets in.
"coercion-types": [true,
["coerceBooleanProperty", "coerceCssPixelValue", "coerceNumberProperty"],
{
"CanDisable": ["disabled"],
Expand Down

0 comments on commit 1ce6b3b

Please sign in to comment.