Skip to content

Commit

Permalink
build: add tslint rule to ensure that setters are declared after getters
Browse files Browse the repository at this point in the history
Based on angular#11483 (comment). Adds a custom tslint rule to enforce that we put getters before setters.
  • Loading branch information
crisbeto committed May 26, 2018
1 parent 9062640 commit 0cbe713
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cdk/text-field/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {

/** Minimum amount of rows in the textarea. */
@Input('cdkAutosizeMinRows')
get minRows(): number { return this._minRows; }
set minRows(value: number) {
this._minRows = value;
this._setMinHeight();
}
get minRows(): number { return this._minRows; }

/** Maximum amount of rows in the textarea. */
@Input('cdkAutosizeMaxRows')
Expand Down
2 changes: 1 addition & 1 deletion src/demo-app/example/example-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class ExampleList {
@Input() ids: string[];

@Input()
set expandAll(v: boolean) { this._expandAll = coerceBooleanProperty(v); }
get expandAll(): boolean { return this._expandAll; }
set expandAll(v: boolean) { this._expandAll = coerceBooleanProperty(v); }
_expandAll: boolean;

exampleComponents = EXAMPLE_COMPONENTS;
Expand Down
2 changes: 1 addition & 1 deletion src/demo-app/example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class Example implements OnInit {
@Input() id: string;

@Input()
set showLabel(v: boolean) { this._showLabel = coerceBooleanProperty(v); }
get showLabel(): boolean { return this._showLabel; }
set showLabel(v: boolean) { this._showLabel = coerceBooleanProperty(v); }
_showLabel: boolean;

title: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ interface TestData {

class FakeDataSource extends DataSource<TestData> {
_dataChange = new BehaviorSubject<TestData[]>([]);
set data(data: TestData[]) { this._dataChange.next(data); }
get data() { return this._dataChange.getValue(); }
set data(data: TestData[]) { this._dataChange.next(data); }

constructor() {
super();
Expand Down
8 changes: 5 additions & 3 deletions src/lib/tabs/tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
this._updateTabScrollPosition();
}

/** Tracks which element has focus; used for keyboard navigation */
get focusIndex(): number {
return this._focusIndex;
}

/** When the focus index is set, we must manually send focus to the correct label */
set focusIndex(value: number) {
if (!this._isValidIndex(value) || this._focusIndex == value) { return; }
Expand All @@ -234,9 +239,6 @@ export class MatTabHeader extends _MatTabHeaderMixinBase
this._setTabFocus(value);
}

/** Tracks which element has focus; used for keyboard navigation */
get focusIndex(): number { return this._focusIndex; }

/**
* Determines if an index is valid. If the tabs are not ready yet, we assume that the user is
* providing a valid index and return true.
Expand Down
29 changes: 29 additions & 0 deletions tools/tslint-rules/settersAfterGettersRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

/**
* Rule that enforces that property setters are declared after getters.
*/
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {
visitGetAccessor(getter: ts.GetAccessorDeclaration) {
if (getter.parent && tsutils.isClassDeclaration(getter.parent)) {
const getterName = getter.name.getText();
const setter = getter.parent.members.find(member => {
return tsutils.isSetAccessorDeclaration(member) && member.name.getText() === getterName;
}) as ts.SetAccessorDeclaration | undefined;

if (setter && setter.pos < getter.pos) {
this.addFailureAtNode(setter, 'Setters must be declared after getters.');
}
}

super.visitGetAccessor(getter);
}
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
// Custom Rules
"ts-loader": true,
"no-exposed-todo": true,
"setters-after-getters": true,
"no-host-decorator-in-concrete": [
true,
"HostBinding",
Expand Down

0 comments on commit 0cbe713

Please sign in to comment.