Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit ad96947

Browse files
authored
style: Add explanations to tslint:disable comments (#4448)
All `tslint:disable:object-literal-sort-keys` comments now include a description that explains why the linter rule needs to be disabled. For the most part, it's to ensure consistent ordering of methods between adapter interfaces and their implementations in the foundation and component. This PR also reorders a few properties in `MDCCheckbox` and `MDCRadio` for consistency. Refs #4225
1 parent c6b609c commit ad96947

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+114
-121
lines changed

packages/mdc-checkbox/component.ts

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,47 @@ import {MDCCheckboxFoundation} from './foundation';
3333
*/
3434
type PropertyDescriptorGetter = (() => any) | undefined; // tslint:disable-line:no-any
3535

36-
const {NATIVE_CONTROL_SELECTOR} = MDCCheckboxFoundation.strings;
37-
3836
const CB_PROTO_PROPS = ['checked', 'indeterminate'];
3937

4038
export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements MDCRippleCapableSurface {
4139
static attachTo(root: Element) {
4240
return new MDCCheckbox(root);
4341
}
4442

45-
/**
46-
* Returns the state of the native control element, or null if the native control element is not present.
47-
*/
48-
get nativeCb_(): HTMLInputElement {
49-
const cbEl = this.root_.querySelector<HTMLInputElement>(NATIVE_CONTROL_SELECTOR);
50-
if (!cbEl) {
51-
throw new Error(`Checkbox requires a ${NATIVE_CONTROL_SELECTOR} element`);
52-
}
53-
return cbEl;
43+
get ripple(): MDCRipple {
44+
return this.ripple_;
45+
}
46+
47+
get checked(): boolean {
48+
return this.nativeControl_.checked;
49+
}
50+
51+
set checked(checked: boolean) {
52+
this.nativeControl_.checked = checked;
53+
}
54+
55+
get indeterminate(): boolean {
56+
return this.nativeControl_.indeterminate;
57+
}
58+
59+
set indeterminate(indeterminate: boolean) {
60+
this.nativeControl_.indeterminate = indeterminate;
61+
}
62+
63+
get disabled(): boolean {
64+
return this.nativeControl_.disabled;
65+
}
66+
67+
set disabled(disabled: boolean) {
68+
this.foundation_.setDisabled(disabled);
69+
}
70+
71+
get value(): string {
72+
return this.nativeControl_.value;
73+
}
74+
75+
set value(value: string) {
76+
this.nativeControl_.value = value;
5477
}
5578

5679
// Public visibility for this property is required by MDCRippleCapableSurface.
@@ -63,14 +86,14 @@ export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements
6386
initialSyncWithDOM() {
6487
this.handleChange_ = () => this.foundation_.handleChange();
6588
this.handleAnimationEnd_ = () => this.foundation_.handleAnimationEnd();
66-
this.nativeCb_.addEventListener('change', this.handleChange_);
89+
this.nativeControl_.addEventListener('change', this.handleChange_);
6790
this.listen(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
6891
this.installPropertyChangeHooks_();
6992
}
7093

7194
destroy() {
7295
this.ripple_.destroy();
73-
this.nativeCb_.removeEventListener('change', this.handleChange_);
96+
this.nativeControl_.removeEventListener('change', this.handleChange_);
7497
this.unlisten(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
7598
this.uninstallPropertyChangeHooks_();
7699
super.destroy();
@@ -82,14 +105,14 @@ export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements
82105
const adapter: MDCCheckboxAdapter = {
83106
addClass: (className) => this.root_.classList.add(className),
84107
forceLayout: () => (this.root_ as HTMLElement).offsetWidth,
85-
hasNativeControl: () => !!this.nativeCb_,
108+
hasNativeControl: () => !!this.nativeControl_,
86109
isAttachedToDOM: () => Boolean(this.root_.parentNode),
87110
isChecked: () => this.checked,
88111
isIndeterminate: () => this.indeterminate,
89112
removeClass: (className) => this.root_.classList.remove(className),
90-
removeNativeControlAttr: (attr) => this.nativeCb_.removeAttribute(attr),
91-
setNativeControlAttr: (attr, value) => this.nativeCb_.setAttribute(attr, value),
92-
setNativeControlDisabled: (disabled) => this.nativeCb_.disabled = disabled,
113+
removeNativeControlAttr: (attr) => this.nativeControl_.removeAttribute(attr),
114+
setNativeControlAttr: (attr, value) => this.nativeControl_.setAttribute(attr, value),
115+
setNativeControlDisabled: (disabled) => this.nativeControl_.disabled = disabled,
93116
};
94117
return new MDCCheckboxFoundation(adapter);
95118
}
@@ -99,16 +122,16 @@ export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements
99122
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
100123
const adapter: MDCRippleAdapter = {
101124
...MDCRipple.createAdapter(this),
102-
deregisterInteractionHandler: (evtType, handler) => this.nativeCb_.removeEventListener(evtType, handler),
103-
isSurfaceActive: () => ponyfill.matches(this.nativeCb_, ':active'),
125+
deregisterInteractionHandler: (evtType, handler) => this.nativeControl_.removeEventListener(evtType, handler),
126+
isSurfaceActive: () => ponyfill.matches(this.nativeControl_, ':active'),
104127
isUnbounded: () => true,
105-
registerInteractionHandler: (evtType, handler) => this.nativeCb_.addEventListener(evtType, handler),
128+
registerInteractionHandler: (evtType, handler) => this.nativeControl_.addEventListener(evtType, handler),
106129
};
107130
return new MDCRipple(this.root_, new MDCRippleFoundation(adapter));
108131
}
109132

110133
private installPropertyChangeHooks_() {
111-
const nativeCb = this.nativeCb_;
134+
const nativeCb = this.nativeControl_;
112135
const cbProto = Object.getPrototypeOf(nativeCb);
113136

114137
CB_PROTO_PROPS.forEach((controlState) => {
@@ -136,7 +159,7 @@ export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements
136159
}
137160

138161
private uninstallPropertyChangeHooks_() {
139-
const nativeCb = this.nativeCb_;
162+
const nativeCb = this.nativeControl_;
140163
const cbProto = Object.getPrototypeOf(nativeCb);
141164

142165
CB_PROTO_PROPS.forEach((controlState) => {
@@ -148,40 +171,13 @@ export class MDCCheckbox extends MDCComponent<MDCCheckboxFoundation> implements
148171
});
149172
}
150173

151-
get ripple(): MDCRipple {
152-
return this.ripple_;
153-
}
154-
155-
get checked(): boolean {
156-
return this.nativeCb_.checked;
157-
}
158-
159-
set checked(checked: boolean) {
160-
this.nativeCb_.checked = checked;
161-
}
162-
163-
get indeterminate(): boolean {
164-
return this.nativeCb_.indeterminate;
165-
}
166-
167-
set indeterminate(indeterminate: boolean) {
168-
this.nativeCb_.indeterminate = indeterminate;
169-
}
170-
171-
get disabled(): boolean {
172-
return this.nativeCb_.disabled;
173-
}
174-
175-
set disabled(disabled: boolean) {
176-
this.foundation_.setDisabled(disabled);
177-
}
178-
179-
get value(): string {
180-
return this.nativeCb_.value;
181-
}
182-
183-
set value(value: string) {
184-
this.nativeCb_.value = value;
174+
private get nativeControl_(): HTMLInputElement {
175+
const {NATIVE_CONTROL_SELECTOR} = MDCCheckboxFoundation.strings;
176+
const el = this.root_.querySelector<HTMLInputElement>(NATIVE_CONTROL_SELECTOR);
177+
if (!el) {
178+
throw new Error(`Checkbox component requires a ${NATIVE_CONTROL_SELECTOR} element`);
179+
}
180+
return el;
185181
}
186182
}
187183

packages/mdc-drawer/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class MDCDrawer extends MDCComponent<MDCDismissibleDrawerFoundation> {
118118
getDefaultFoundation() {
119119
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
120120
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
121-
// tslint:disable:object-literal-sort-keys
121+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
122122
const adapter: MDCDrawerAdapter = {
123123
addClass: (className) => this.root_.classList.add(className),
124124
removeClass: (className) => this.root_.classList.remove(className),

packages/mdc-drawer/dismissible/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class MDCDismissibleDrawerFoundation extends MDCFoundation<MDCDrawerAdapt
3535
}
3636

3737
static get defaultAdapter(): MDCDrawerAdapter {
38-
// tslint:disable:object-literal-sort-keys
38+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
3939
return {
4040
addClass: () => undefined,
4141
removeClass: () => undefined,

packages/mdc-floating-label/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class MDCFloatingLabel extends MDCComponent<MDCFloatingLabelFoundation> {
5555
getDefaultFoundation() {
5656
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
5757
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
58-
// tslint:disable:object-literal-sort-keys
58+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
5959
const adapter: MDCFloatingLabelAdapter = {
6060
addClass: (className) => this.root_.classList.add(className),
6161
removeClass: (className) => this.root_.classList.remove(className),

packages/mdc-floating-label/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class MDCFloatingLabelFoundation extends MDCFoundation<MDCFloatingLabelAd
3535
* See {@link MDCFloatingLabelAdapter} for typing information on parameters and return types.
3636
*/
3737
static get defaultAdapter(): MDCFloatingLabelAdapter {
38-
// tslint:disable:object-literal-sort-keys
38+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
3939
return {
4040
addClass: () => undefined,
4141
removeClass: () => undefined,

packages/mdc-line-ripple/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class MDCLineRipple extends MDCComponent<MDCLineRippleFoundation> {
5757
getDefaultFoundation() {
5858
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
5959
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
60-
// tslint:disable:object-literal-sort-keys
60+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
6161
const adapter: MDCLineRippleAdapter = {
6262
addClass: (className) => this.root_.classList.add(className),
6363
removeClass: (className) => this.root_.classList.remove(className),

packages/mdc-line-ripple/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class MDCLineRippleFoundation extends MDCFoundation<MDCLineRippleAdapter>
3535
* See {@link MDCLineRippleAdapter} for typing information on parameters and return types.
3636
*/
3737
static get defaultAdapter(): MDCLineRippleAdapter {
38-
// tslint:disable:object-literal-sort-keys
38+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
3939
return {
4040
addClass: () => undefined,
4141
removeClass: () => undefined,

packages/mdc-menu-surface/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class MDCMenuSurface extends MDCComponent<MDCMenuSurfaceFoundation> {
149149
getDefaultFoundation() {
150150
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
151151
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
152-
// tslint:disable:object-literal-sort-keys
152+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
153153
const adapter: MDCMenuSurfaceAdapter = {
154154
addClass: (className) => this.root_.classList.add(className),
155155
removeClass: (className) => this.root_.classList.remove(className),

packages/mdc-menu-surface/foundation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class MDCMenuSurfaceFoundation extends MDCFoundation<MDCMenuSurfaceAdapte
5656
* @see {@link MDCMenuSurfaceAdapter} for typing information on parameters and return types.
5757
*/
5858
static get defaultAdapter(): MDCMenuSurfaceAdapter {
59-
// tslint:disable:object-literal-sort-keys
59+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
6060
return {
6161
addClass: () => undefined,
6262
removeClass: () => undefined,
@@ -292,9 +292,8 @@ export class MDCMenuSurfaceFoundation extends MDCFoundation<MDCMenuSurfaceAdapte
292292
const viewportSize = this.adapter_.getWindowDimensions();
293293
const windowScroll = this.adapter_.getWindowScroll();
294294

295-
// tslint:disable:object-literal-sort-keys
296-
297295
if (!anchorRect) {
296+
// tslint:disable:object-literal-sort-keys Positional properties are more readable when they're grouped together
298297
anchorRect = {
299298
top: this.position_.y,
300299
right: this.position_.x,
@@ -303,23 +302,24 @@ export class MDCMenuSurfaceFoundation extends MDCFoundation<MDCMenuSurfaceAdapte
303302
width: 0,
304303
height: 0,
305304
};
305+
// tslint:enable:object-literal-sort-keys
306306
}
307307

308308
return {
309309
anchorSize: anchorRect,
310310
bodySize,
311311
surfaceSize: this.dimensions_,
312312
viewportDistance: {
313+
// tslint:disable:object-literal-sort-keys Positional properties are more readable when they're grouped together
313314
top: anchorRect.top,
314315
right: viewportSize.width - anchorRect.right,
315316
bottom: viewportSize.height - anchorRect.bottom,
316317
left: anchorRect.left,
318+
// tslint:enable:object-literal-sort-keys
317319
},
318320
viewportSize,
319321
windowScroll,
320322
};
321-
322-
// tslint:enable:object-literal-sort-keys
323323
}
324324

325325
/**

packages/mdc-menu/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class MDCMenu extends MDCComponent<MDCMenuFoundation> {
176176
getDefaultFoundation() {
177177
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
178178
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
179-
// tslint:disable:object-literal-sort-keys
179+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
180180
const adapter: MDCMenuAdapter = {
181181
addClassToElementAtIndex: (index, className) => {
182182
const list = this.items;

packages/mdc-menu/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class MDCMenuFoundation extends MDCFoundation<MDCMenuAdapter> {
4242
* @see {@link MDCMenuAdapter} for typing information on parameters and return types.
4343
*/
4444
static get defaultAdapter(): MDCMenuAdapter {
45-
// tslint:disable:object-literal-sort-keys
45+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
4646
return {
4747
addClassToElementAtIndex: () => undefined,
4848
removeClassFromElementAtIndex: () => undefined,

packages/mdc-notched-outline/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class MDCNotchedOutline extends MDCComponent<MDCNotchedOutlineFoundation>
6969
getDefaultFoundation() {
7070
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
7171
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
72-
// tslint:disable:object-literal-sort-keys
72+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
7373
const adapter: MDCNotchedOutlineAdapter = {
7474
addClass: (className) => this.root_.classList.add(className),
7575
removeClass: (className) => this.root_.classList.remove(className),

packages/mdc-notched-outline/foundation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class MDCNotchedOutlineFoundation extends MDCFoundation<MDCNotchedOutline
4242
* See {@link MDCNotchedOutlineAdapter} for typing information on parameters and return types.
4343
*/
4444
static get defaultAdapter(): MDCNotchedOutlineAdapter {
45-
// tslint:disable:object-literal-sort-keys
45+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
4646
return {
4747
addClass: () => undefined,
4848
removeClass: () => undefined,

packages/mdc-radio/component.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ export class MDCRadio extends MDCComponent<MDCRadioFoundation> implements MDCRip
3131
return new MDCRadio(root);
3232
}
3333

34-
// Public visibility for this property is required by MDCRippleCapableSurface.
35-
root_!: Element; // assigned in MDCComponent constructor
36-
37-
private readonly ripple_: MDCRipple = this.createRipple_();
38-
3934
get checked(): boolean {
4035
return this.nativeControl_.checked;
4136
}
@@ -64,6 +59,11 @@ export class MDCRadio extends MDCComponent<MDCRadioFoundation> implements MDCRip
6459
return this.ripple_;
6560
}
6661

62+
// Public visibility for this property is required by MDCRippleCapableSurface.
63+
root_!: Element; // assigned in MDCComponent constructor
64+
65+
private readonly ripple_: MDCRipple = this.createRipple_();
66+
6767
destroy() {
6868
this.ripple_.destroy();
6969
super.destroy();
@@ -83,7 +83,7 @@ export class MDCRadio extends MDCComponent<MDCRadioFoundation> implements MDCRip
8383
private createRipple_(): MDCRipple {
8484
// DO NOT INLINE this variable. For backward compatibility, foundations take a Partial<MDCFooAdapter>.
8585
// To ensure we don't accidentally omit any methods, we need a separate, strongly typed adapter variable.
86-
// tslint:disable:object-literal-sort-keys
86+
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
8787
const adapter: MDCRippleAdapter = {
8888
...MDCRipple.createAdapter(this),
8989
registerInteractionHandler: (evtType, handler) => this.nativeControl_.addEventListener(evtType, handler),
@@ -97,9 +97,6 @@ export class MDCRadio extends MDCComponent<MDCRadioFoundation> implements MDCRip
9797
return new MDCRipple(this.root_, new MDCRippleFoundation(adapter));
9898
}
9999

100-
/**
101-
* Returns the state of the native control element, or null if the native control element is not present.
102-
*/
103100
private get nativeControl_(): HTMLInputElement {
104101
const {NATIVE_CONTROL_SELECTOR} = MDCRadioFoundation.strings;
105102
const el = this.root_.querySelector<HTMLInputElement>(NATIVE_CONTROL_SELECTOR);

0 commit comments

Comments
 (0)