Skip to content

Commit 95e0cda

Browse files
crisbetojosephperrott
authored andcommitted
build: add tslint rule to ensure that setters are declared after getters (#11522)
1 parent b26b58c commit 95e0cda

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

src/cdk/text-field/autosize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
4747

4848
/** Minimum amount of rows in the textarea. */
4949
@Input('cdkAutosizeMinRows')
50+
get minRows(): number { return this._minRows; }
5051
set minRows(value: number) {
5152
this._minRows = value;
5253
this._setMinHeight();
5354
}
54-
get minRows(): number { return this._minRows; }
5555

5656
/** Maximum amount of rows in the textarea. */
5757
@Input('cdkAutosizeMaxRows')

src/demo-app/example/example-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class ExampleList {
5959
@Input() ids: string[];
6060

6161
@Input()
62-
set expandAll(v: boolean) { this._expandAll = coerceBooleanProperty(v); }
6362
get expandAll(): boolean { return this._expandAll; }
63+
set expandAll(v: boolean) { this._expandAll = coerceBooleanProperty(v); }
6464
_expandAll: boolean;
6565

6666
exampleComponents = EXAMPLE_COMPONENTS;

src/demo-app/example/example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export class Example implements OnInit {
4848
@Input() id: string;
4949

5050
@Input()
51-
set showLabel(v: boolean) { this._showLabel = coerceBooleanProperty(v); }
5251
get showLabel(): boolean { return this._showLabel; }
52+
set showLabel(v: boolean) { this._showLabel = coerceBooleanProperty(v); }
5353
_showLabel: boolean;
5454

5555
title: string;

src/lib/table/table.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ interface TestData {
402402

403403
class FakeDataSource extends DataSource<TestData> {
404404
_dataChange = new BehaviorSubject<TestData[]>([]);
405-
set data(data: TestData[]) { this._dataChange.next(data); }
406405
get data() { return this._dataChange.getValue(); }
406+
set data(data: TestData[]) { this._dataChange.next(data); }
407407

408408
constructor() {
409409
super();

src/lib/tabs/tab-header.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
225225
this._updateTabScrollPosition();
226226
}
227227

228+
/** Tracks which element has focus; used for keyboard navigation */
229+
get focusIndex(): number {
230+
return this._focusIndex;
231+
}
232+
228233
/** When the focus index is set, we must manually send focus to the correct label */
229234
set focusIndex(value: number) {
230235
if (!this._isValidIndex(value) || this._focusIndex == value) { return; }
@@ -234,9 +239,6 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
234239
this._setTabFocus(value);
235240
}
236241

237-
/** Tracks which element has focus; used for keyboard navigation */
238-
get focusIndex(): number { return this._focusIndex; }
239-
240242
/**
241243
* Determines if an index is valid. If the tabs are not ready yet, we assume that the user is
242244
* providing a valid index and return true.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as ts from 'typescript';
2+
import * as Lint from 'tslint';
3+
import * as tsutils from 'tsutils';
4+
5+
/**
6+
* Rule that enforces that property setters are declared after getters.
7+
*/
8+
export class Rule extends Lint.Rules.AbstractRule {
9+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
10+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
11+
}
12+
}
13+
14+
class Walker extends Lint.RuleWalker {
15+
visitGetAccessor(getter: ts.GetAccessorDeclaration) {
16+
if (getter.parent && tsutils.isClassDeclaration(getter.parent)) {
17+
const getterName = getter.name.getText();
18+
const setter = getter.parent.members.find(member => {
19+
return tsutils.isSetAccessorDeclaration(member) && member.name.getText() === getterName;
20+
}) as ts.SetAccessorDeclaration | undefined;
21+
22+
if (setter && setter.pos < getter.pos) {
23+
this.addFailureAtNode(setter, 'Setters must be declared after getters.');
24+
}
25+
}
26+
27+
super.visitGetAccessor(getter);
28+
}
29+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
// Custom Rules
9898
"ts-loader": true,
9999
"no-exposed-todo": true,
100+
"setters-after-getters": true,
100101
"no-host-decorator-in-concrete": [
101102
true,
102103
"HostBinding",

0 commit comments

Comments
 (0)