Skip to content

Commit cdfec07

Browse files
authored
build: add custom linting for symbol names (#19811)
As discussed, we want to prefix some exported class names with an underscore so it's more obvious that they're private. The problem is that our current class naming rule doesn't allow for a class to start with an underscore. These changes add a custom rule that can use a regex to validate the class name. Having our own rule allows us to validate more types of nodes as well (e.g. `type` nodes which tslint doesn't check).
1 parent 1106cb1 commit cdfec07

File tree

12 files changed

+30
-17
lines changed

12 files changed

+30
-17
lines changed

src/material/checkbox/checkbox-module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {MatCheckboxRequiredValidator} from './checkbox-required-validator';
1717
exports: [MatCheckboxRequiredValidator],
1818
declarations: [MatCheckboxRequiredValidator],
1919
})
20-
// tslint:disable-next-line:class-name
2120
export class _MatCheckboxRequiredValidatorModule {
2221
}
2322

src/material/menu/menu-module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {MAT_MENU_SCROLL_STRATEGY_FACTORY_PROVIDER, MatMenuTrigger} from './menu-
2828
],
2929
providers: [MAT_MENU_SCROLL_STRATEGY_FACTORY_PROVIDER]
3030
})
31-
// tslint:disable-next-line:class-name
3231
export class _MatMenuDirectivesModule {}
3332

3433
@NgModule({

src/material/menu/menu.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ let menuPanelUid = 0;
9797

9898
/** Base class with all of the `MatMenu` functionality. */
9999
@Directive()
100-
// tslint:disable-next-line:class-name
101100
export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>, OnInit,
102101
OnDestroy {
103102
private _keyManager: FocusKeyManager<MatMenuItem>;
@@ -519,9 +518,7 @@ export class MatMenu extends _MatMenuBase {}
519518
{provide: MatMenu, useExisting: _MatMenu}
520519
]
521520
})
522-
// tslint:disable-next-line:class-name
523521
export class _MatMenu extends MatMenu {
524-
525522
constructor(elementRef: ElementRef<HTMLElement>, ngZone: NgZone,
526523
@Inject(MAT_MENU_DEFAULT_OPTIONS) defaultOptions: MatMenuDefaultOptions) {
527524
super(elementRef, ngZone, defaultOptions);

src/material/radio/radio.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export const MAT_RADIO_GROUP =
9696
* @docs-private
9797
*/
9898
@Directive()
99-
// tslint:disable-next-line:class-name
10099
export abstract class _MatRadioGroupBase<T extends _MatRadioButtonBase> implements AfterContentInit,
101100
ControlValueAccessor {
102101
/** Selected value for the radio group. */
@@ -354,7 +353,6 @@ const _MatRadioButtonMixinBase:
354353
* @docs-private
355354
*/
356355
@Directive()
357-
// tslint:disable-next-line:class-name
358356
export abstract class _MatRadioButtonBase extends _MatRadioButtonMixinBase implements OnInit,
359357
AfterViewInit, OnDestroy, CanDisableRipple, HasTabIndex {
360358

src/material/slide-toggle/slide-toggle-module.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import {MatSlideToggleRequiredValidator} from './slide-toggle-required-validator
1717
exports: [MatSlideToggleRequiredValidator],
1818
declarations: [MatSlideToggleRequiredValidator],
1919
})
20-
// tslint:disable-next-line:class-name
21-
export class _MatSlideToggleRequiredValidatorModule {
22-
}
20+
export class _MatSlideToggleRequiredValidatorModule {}
2321

2422
@NgModule({
2523
imports: [

src/material/tabs/ink-bar.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
1414
* Interface for a a MatInkBar positioner method, defining the positioning and width of the ink
1515
* bar in a set of tabs.
1616
*/
17-
// tslint:disable-next-line class-name Using leading underscore to denote internal interface.
1817
export interface _MatInkBarPositioner {
1918
(element: HTMLElement): { left: string, width: string };
2019
}

src/material/tabs/tab-body.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export class MatTabBodyPortal extends CdkPortalOutlet implements OnInit, OnDestr
109109
* @docs-private
110110
*/
111111
@Directive()
112-
// tslint:disable-next-line:class-name
113112
export abstract class _MatTabBodyBase implements OnInit, OnDestroy {
114113
/** Current position of the tab-body in the tab-group. Zero means that the tab is visible. */
115114
private _positionIndex: number;

src/material/tabs/tab-group.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ interface MatTabGroupBaseHeader {
7979
* @docs-private
8080
*/
8181
@Directive()
82-
// tslint:disable-next-line:class-name
8382
export abstract class _MatTabGroupBase extends _MatTabGroupMixinBase implements AfterContentInit,
8483
AfterContentChecked, OnDestroy, CanColor, CanDisableRipple {
8584

src/material/tabs/tab-header.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {MatPaginatedTabHeader} from './paginated-tab-header';
3939
* @docs-private
4040
*/
4141
@Directive()
42-
// tslint:disable-next-line:class-name
4342
export abstract class _MatTabHeaderBase extends MatPaginatedTabHeader implements
4443
AfterContentChecked, AfterContentInit, AfterViewInit, OnDestroy {
4544

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import {startWith, takeUntil} from 'rxjs/operators';
5454
* @docs-private
5555
*/
5656
@Directive()
57-
// tslint:disable-next-line:class-name
5857
export abstract class _MatTabNavBase extends MatPaginatedTabHeader implements AfterContentChecked,
5958
AfterContentInit, OnDestroy {
6059

@@ -192,7 +191,6 @@ const _MatTabLinkMixinBase:
192191

193192
/** Base class with all of the `MatTabLink` functionality. */
194193
@Directive()
195-
// tslint:disable-next-line:class-name
196194
export class _MatTabLinkBase extends _MatTabLinkMixinBase implements AfterViewInit, OnDestroy,
197195
CanDisable, CanDisableRipple, HasTabIndex, RippleTarget, FocusableOption {
198196

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as ts from 'typescript';
2+
import * as Lint from 'tslint';
3+
4+
/** Lint rule that checks the names of classes and interfaces against a pattern. */
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
/** Pattern that we should validate against. */
7+
private _pattern: RegExp;
8+
9+
constructor(options: Lint.IOptions) {
10+
super(options);
11+
this._pattern = new RegExp(options.ruleArguments[0] || '.*');
12+
}
13+
14+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
15+
return this.applyWithFunction(sourceFile, checkSourceFile, this._pattern);
16+
}
17+
}
18+
19+
function checkSourceFile(context: Lint.WalkContext<RegExp>) {
20+
context.sourceFile.forEachChild(function walk(node) {
21+
if ((ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node) ||
22+
ts.isTypeAliasDeclaration(node)) && node.name && !context.options.test(node.name.text)) {
23+
context.addFailureAtNode(node.name, `Symbol name must match pattern ${context.options}`);
24+
}
25+
26+
node.forEachChild(walk);
27+
});
28+
}

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// TSLint now shows warnings if types for properties are inferred. This rule needs to be
2020
// disabled because all properties need to have explicit types set to work for Dgeni.
2121
"no-inferrable-types": false,
22-
"class-name": true,
2322
"comment-format": [
2423
true,
2524
"check-space"
@@ -143,6 +142,7 @@
143142
"member-naming": [true, {
144143
"private": "^_"
145144
}],
145+
"symbol-naming": [true, "^_?[A-Z][a-zA-Z0-9]*$"],
146146
"validate-decorators": [true, {
147147
"Component": {
148148
"argument": 0,

0 commit comments

Comments
 (0)