Skip to content

Commit 9d6d2e3

Browse files
committed
refactor: remove inherited coercion acceptance members
Previously Angular did not inherit coercion acceptance members until we submitted an issue on the framework side. This issue will be fixed with angular/angular#34296. In preparation for that change to land, this PR removes all unnecessary acceptance members and updates the lint rule to avoid future duplications (the rule is also used to make that refactoring)
1 parent e7508ad commit 9d6d2e3

File tree

22 files changed

+33
-80
lines changed

22 files changed

+33
-80
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,4 @@ 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 | null | undefined;
28-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
2923
}

src/material-experimental/mdc-chips/chip-listbox.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,5 @@ export class MatChipListbox extends MatChipSet implements AfterContentInit, Cont
557557
static ngAcceptInputType_multiple: boolean | string | null | undefined;
558558
static ngAcceptInputType_selectable: boolean | string | null | undefined;
559559
static ngAcceptInputType_required: boolean | string | null | undefined;
560-
static ngAcceptInputType_disabled: boolean | string | null | undefined;
561560
}
562561

src/material-experimental/mdc-chips/chip-option.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,5 @@ export class MatChipOption extends MatChip {
227227

228228
static ngAcceptInputType_selectable: boolean | string | null | undefined;
229229
static ngAcceptInputType_selected: boolean | string | null | undefined;
230-
static ngAcceptInputType_disabled: boolean | string | null | undefined;
231-
static ngAcceptInputType_removable: boolean | string | null | undefined;
232-
static ngAcceptInputType_highlighted: boolean | string | null | undefined;
233230
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
234231
}

src/material-experimental/mdc-chips/chip-row.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,5 @@ export class MatChipRow extends MatChip implements AfterContentInit, AfterViewIn
148148
this._handleInteraction(event);
149149
}
150150
}
151-
152-
static ngAcceptInputType_disabled: boolean | string | null | undefined;
153-
static ngAcceptInputType_removable: boolean | string | null | undefined;
154-
static ngAcceptInputType_highlighted: boolean | string | null | undefined;
155151
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
156152
}

src/material-experimental/mdc-menu/menu.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,4 @@ export class MatMenu extends BaseMatMenu {
7070
// - should update the elevation when the same menu is opened at a different depth
7171
// - should not increase the elevation if the user specified a custom one
7272
}
73-
74-
static ngAcceptInputType_overlapTrigger: boolean | string | null | undefined;
75-
static ngAcceptInputType_hasBackdrop: boolean | string | null | undefined;
7673
}

src/material-experimental/mdc-tabs/tab-group.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ export class MatTabGroup extends _MatTabGroupBase {
7777
}
7878

7979
static ngAcceptInputType_fitInkBarToContent: boolean | string | null | undefined;
80-
static ngAcceptInputType_dynamicHeight: boolean | string | null | undefined;
8180
static ngAcceptInputType_animationDuration: number | string | null | undefined;
82-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
8381
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
8482
}

src/material-experimental/mdc-tabs/tab-header.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,4 @@ export class MatTabHeader extends _MatTabHeaderBase implements AfterContentInit
7474
}
7575

7676
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
77-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
7877
}

src/material-experimental/mdc-tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export class MatTabNav extends _MatTabNavBase implements AfterContentInit {
104104

105105
static ngAcceptInputType_fitInkBarToContent: boolean | string | null | undefined;
106106
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
107-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
108107
}
109108

110109
/**

src/material/expansion/accordion.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,4 @@ export class MatAccordion extends CdkAccordion implements MatAccordionBase, Afte
103103
}
104104

105105
static ngAcceptInputType_hideToggle: boolean | string | null | undefined;
106-
static ngAcceptInputType_multi: boolean | string | null | undefined;
107106
}

src/material/input/autosize.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,4 @@ export class MatTextareaAutosize extends CdkTextareaAutosize {
4141
@Input()
4242
get matTextareaAutosize(): boolean { return this.enabled; }
4343
set matTextareaAutosize(value: boolean) { this.enabled = value; }
44-
45-
static ngAcceptInputType_minRows: number | string | null | undefined;
46-
static ngAcceptInputType_maxRows: number | string | null | undefined;
47-
static ngAcceptInputType_enabled: boolean | string | null | undefined;
4844
}

src/material/menu/menu.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,4 @@ export class _MatMenu extends MatMenu {
507507
@Inject(MAT_MENU_DEFAULT_OPTIONS) defaultOptions: MatMenuDefaultOptions) {
508508
super(elementRef, ngZone, defaultOptions);
509509
}
510-
511-
static ngAcceptInputType_overlapTrigger: boolean | string | null | undefined;
512-
static ngAcceptInputType_hasBackdrop: boolean | string | null | undefined;
513510
}

src/material/progress-spinner/progress-spinner.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,6 @@ export class MatSpinner extends MatProgressSpinner {
332332
super(elementRef, platform, document, animationMode, defaults);
333333
this.mode = 'indeterminate';
334334
}
335-
336-
static ngAcceptInputType_diameter: number | string | null | undefined;
337-
static ngAcceptInputType_strokeWidth: number | string | null | undefined;
338-
static ngAcceptInputType_value: number | string | null | undefined;
339335
}
340336

341337

src/material/sidenav/sidenav.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ export class MatSidenav extends MatDrawer {
9999
static ngAcceptInputType_fixedInViewport: boolean | string | null | undefined;
100100
static ngAcceptInputType_fixedTopGap: number | string | null | undefined;
101101
static ngAcceptInputType_fixedBottomGap: number | string | null | undefined;
102-
static ngAcceptInputType_disableClose: boolean | string | null | undefined;
103-
static ngAcceptInputType_autoFocus: boolean | string | null | undefined;
104-
static ngAcceptInputType_opened: boolean | string | null | undefined;
105102
}
106103

107104

@@ -131,7 +128,5 @@ export class MatSidenavContainer extends MatDrawerContainer {
131128
_allDrawers: QueryList<MatSidenav>;
132129

133130
@ContentChild(MatSidenavContent) _content: MatSidenavContent;
134-
135-
static ngAcceptInputType_autosize: boolean | string | null | undefined;
136131
static ngAcceptInputType_hasBackdrop: boolean | string | null | undefined;
137132
}

src/material/stepper/stepper.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ export class MatStep extends CdkStep implements ErrorStateMatcher {
8080

8181
return originalErrorState || customErrorState;
8282
}
83-
84-
static ngAcceptInputType_editable: boolean | string | null | undefined;
85-
static ngAcceptInputType_hasError: boolean | string | null | undefined;
86-
static ngAcceptInputType_optional: boolean | string | null | undefined;
87-
static ngAcceptInputType_completed: boolean | string | null | undefined;
8883
}
8984

9085

@@ -136,8 +131,6 @@ export class MatStepper extends CdkStepper implements AfterContentInit {
136131
static ngAcceptInputType_optional: boolean | string | null | undefined;
137132
static ngAcceptInputType_completed: boolean | string | null | undefined;
138133
static ngAcceptInputType_hasError: boolean | string | null | undefined;
139-
static ngAcceptInputType_linear: boolean | string | null | undefined;
140-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
141134
}
142135

143136
@Component({
@@ -170,8 +163,6 @@ export class MatHorizontalStepper extends MatStepper {
170163
static ngAcceptInputType_optional: boolean | string | null | undefined;
171164
static ngAcceptInputType_completed: boolean | string | null | undefined;
172165
static ngAcceptInputType_hasError: boolean | string | null | undefined;
173-
static ngAcceptInputType_linear: boolean | string | null | undefined;
174-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
175166
}
176167

177168
@Component({
@@ -208,6 +199,4 @@ export class MatVerticalStepper extends MatStepper {
208199
static ngAcceptInputType_optional: boolean | string | null | undefined;
209200
static ngAcceptInputType_completed: boolean | string | null | undefined;
210201
static ngAcceptInputType_hasError: boolean | string | null | undefined;
211-
static ngAcceptInputType_linear: boolean | string | null | undefined;
212-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
213202
}

src/material/table/cell.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class MatColumnDef extends CdkColumnDef {
6262
@Input('matColumnDef') name: string;
6363

6464
static ngAcceptInputType_sticky: boolean | string | null | undefined;
65-
static ngAcceptInputType_stickyEnd: boolean | string | null | undefined;
6665
}
6766

6867
/** Header cell template container that adds the right classes and role. */

src/material/table/table.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/co
2929
export class MatTable<T> extends CdkTable<T> {
3030
/** Overrides the sticky CSS class set by the `CdkTable`. */
3131
protected stickyCssClass = 'mat-table-sticky';
32-
33-
static ngAcceptInputType_multiTemplateDataRows: boolean | string | null | undefined;
3432
}

src/material/tabs/tab-group.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ export class MatTabGroup extends _MatTabGroupBase {
407407
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
408408
super(elementRef, changeDetectorRef, defaultConfig, animationMode);
409409
}
410-
411-
static ngAcceptInputType_dynamicHeight: boolean | string | null | undefined;
412410
static ngAcceptInputType_animationDuration: number | string | null | undefined;
413-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
414411
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
415412
}

src/material/tabs/tab-header.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,4 @@ export class MatTabHeader extends _MatTabHeaderBase {
107107
}
108108

109109
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
110-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
111110
}

src/material/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export class MatTabNav extends _MatTabNavBase {
181181
}
182182

183183
static ngAcceptInputType_disableRipple: boolean | string | null | undefined;
184-
static ngAcceptInputType_selectedIndex: number | string | null | undefined;
185184
}
186185

187186
// Boilerplate for applying mixins to MatTabLink.

src/material/tree/padding.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@ export class MatTreeNodePadding<T> extends CdkTreeNodePadding<T> {
2222

2323
/** The indent for each level. Default number 40px from material design menu sub-menu spec. */
2424
@Input('matTreeNodePaddingIndent') indent: number;
25-
26-
static ngAcceptInputType_level: number | string | null | undefined;
2725
}

src/material/tree/toggle.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ import {Directive, Input} from '@angular/core';
1818
})
1919
export class MatTreeNodeToggle<T> extends CdkTreeNodeToggle<T> {
2020
@Input('matTreeNodeToggleRecursive') recursive: boolean = false;
21-
22-
static ngAcceptInputType_recursive: boolean | string | null | undefined;
2321
}

tools/tslint-rules/coercionTypesRule.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Walker extends Lint.RuleWalker {
4040

4141
visitClassDeclaration(node: ts.ClassDeclaration) {
4242
if (this._shouldLintClass(node)) {
43-
this._lintClass(node, node);
43+
this._lintClass(node, node, true);
4444
this._lintSuperClasses(node);
4545
this._lintInterfaces(node, node);
4646
}
@@ -94,12 +94,14 @@ class Walker extends Lint.RuleWalker {
9494
* @param node Class declaration to be checked.
9595
* @param sourceClass Class declaration on which to look for static properties that declare the
9696
* accepted values for the setter.
97+
* @param expectDeclaredMembers Whether acceptance members should be expected or unexpected.
9798
*/
98-
private _lintClass(node: ts.ClassDeclaration, sourceClass: ts.ClassDeclaration): void {
99+
private _lintClass(node: ts.ClassDeclaration, sourceClass: ts.ClassDeclaration,
100+
expectDeclaredMembers: boolean): void {
99101
node.members.forEach(member => {
100102
if (ts.isSetAccessor(member) && usesCoercion(member, this._coercionFunctions) &&
101103
this._shouldCheckSetter(member)) {
102-
this._checkForStaticMember(sourceClass, member.name.getText());
104+
this._checkStaticMember(sourceClass, member.name.getText(), expectDeclaredMembers);
103105
}
104106
});
105107
}
@@ -124,7 +126,9 @@ class Walker extends Lint.RuleWalker {
124126
symbol.valueDeclaration : null;
125127

126128
if (currentClass) {
127-
this._lintClass(currentClass, node);
129+
// Acceptance members should not be re-declared in the derived class. This
130+
// is because acceptance members are inherited.
131+
this._lintClass(currentClass, node, false);
128132
this._lintInterfaces(currentClass, node);
129133
}
130134
}
@@ -148,7 +152,7 @@ class Walker extends Lint.RuleWalker {
148152
const propNames = this._coercionInterfaces[clauseType.expression.text];
149153

150154
if (propNames) {
151-
propNames.forEach(propName => this._checkForStaticMember(sourceClass, propName));
155+
propNames.forEach(propName => this._checkStaticMember(sourceClass, propName, true));
152156
}
153157
}
154158
});
@@ -157,25 +161,35 @@ class Walker extends Lint.RuleWalker {
157161
}
158162

159163
/**
160-
* Checks whether a class declaration has a static member, corresponding
161-
* to the specified setter name, and logs a failure if it doesn't.
162-
* @param node
163-
* @param setterName
164+
* Based on whether the acceptance members are expected or not, this method checks whether
165+
* the specified class declaration matches the condition.
164166
*/
165-
private _checkForStaticMember(node: ts.ClassDeclaration, setterName: string) {
166-
const coercionPropertyName = `${TYPE_ACCEPT_MEMBER_PREFIX}${setterName}`;
167-
const correspondingCoercionProperty = node.members.find(member => {
168-
return ts.isPropertyDeclaration(member) &&
169-
tsutils.hasModifier(member.modifiers, ts.SyntaxKind.StaticKeyword) &&
170-
member.name.getText() === coercionPropertyName;
171-
});
172-
173-
if (!correspondingCoercionProperty) {
167+
private _checkStaticMember(node: ts.ClassDeclaration, setterName: string,
168+
expectDeclaredMembers: boolean) {
169+
const {memberName, memberNode} = this._lookupStaticMember(node, setterName);
170+
if (expectDeclaredMembers && !memberNode) {
174171
this.addFailureAtNode(node.name || node, `Class must declare static coercion ` +
175-
`property called ${coercionPropertyName}.`);
172+
`property called ${memberName}.`);
173+
} else if (!expectDeclaredMembers && memberNode) {
174+
this.addFailureAtNode(node.name || node, `Class should not declare static coercion ` +
175+
`property called ${memberName}. Acceptance members are inherited.`,
176+
Lint.Replacement.deleteText(memberNode.getFullStart(), memberNode.getFullWidth()));
176177
}
177178
}
178179

180+
/** Looks for a static member that corresponds to the given property. */
181+
private _lookupStaticMember(node: ts.ClassDeclaration, propName: string)
182+
: {memberName: string, memberNode?: ts.PropertyDeclaration} {
183+
const coercionPropertyName = `${TYPE_ACCEPT_MEMBER_PREFIX}${propName}`;
184+
const correspondingCoercionProperty = node.members
185+
.find((member): member is ts.PropertyDeclaration => {
186+
return ts.isPropertyDeclaration(member) &&
187+
tsutils.hasModifier(member.modifiers, ts.SyntaxKind.StaticKeyword) &&
188+
member.name.getText() === coercionPropertyName;
189+
});
190+
return {memberName: coercionPropertyName, memberNode: correspondingCoercionProperty};
191+
}
192+
179193
/** Checks whether this rule should lint a class declaration. */
180194
private _shouldLintClass(node: ts.ClassDeclaration): boolean {
181195
// We don't need to lint undecorated classes.

0 commit comments

Comments
 (0)