Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
New rule: classMemberNoDefaultAccess. Disallows public access modifie…
Browse files Browse the repository at this point in the history
…rs that are not explicit in the class
  • Loading branch information
Alexander Schrab committed May 22, 2015
1 parent b08678f commit c69c5ff
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/rules/classMemberNoDefaultAccessRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new MemberAccessWalker(sourceFile, this.getOptions()));
}
}

interface IModifiers {
hasAccessModifier: boolean;
}

function getModifiers(modifiers?: ts.ModifiersArray): IModifiers {
var modifierStrings: string[] = [];
if (modifiers != null) {
modifierStrings = modifiers.map((x) => {
return x.getText();
});
}

var hasAccessModifier = modifierStrings.indexOf("public") !== -1;
hasAccessModifier = hasAccessModifier || modifierStrings.indexOf("private") !== -1;
hasAccessModifier = hasAccessModifier || modifierStrings.indexOf("protected") !== -1;

return {
hasAccessModifier: hasAccessModifier
};
}

export class MemberAccessWalker extends Lint.RuleWalker {
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
}

public visitMethodDeclaration(node: ts.MethodDeclaration): void {
this.checkModifiers(node, getModifiers(node.modifiers));
super.visitMethodDeclaration(node);
}

public visitPropertyDeclaration(node: ts.PropertyDeclaration): void {
this.checkModifiers(node, getModifiers(node.modifiers));
super.visitPropertyDeclaration(node);
}

private checkModifiers(node: ts.Node, current: IModifiers): void {
if (!this.followsRules(current)) {
var message = "Default access modifier on member/method not allowed";
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
}
}

private followsRules(current: IModifiers): boolean {
return current.hasAccessModifier;
}
}
18 changes: 18 additions & 0 deletions test/files/rules/classmembernodefaultaccess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Foo {
constructor() {
}

public w: number;
private x: number;
protected y: number;
z: number;

public barW(): any {
}
private barX(): any {
}
protected barY(): any {
}
barZ(): any {
}
}
32 changes: 32 additions & 0 deletions test/rules/classMemberNoDefaultAccessTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <reference path='../references.ts' />

describe("<class-member-no-default-access>", () => {
it("disallow default access on member", () => {
var fileName = "rules/classmembernodefaultaccess.test.ts";
var ClassMemberNoDefaultAccess = Lint.Test.getRule("class-member-no-default-access");
var actualFailures = Lint.Test.applyRuleOnFile(fileName, ClassMemberNoDefaultAccess, []);

Lint.Test.assertFailuresEqual(actualFailures, [
Lint.Test.createFailure(fileName, [8, 5], [8, 15],
"Default access modifier on member/method not allowed"),
Lint.Test.createFailure(fileName, [16, 5], [17, 6],
"Default access modifier on member/method not allowed")
]);
});
});

0 comments on commit c69c5ff

Please sign in to comment.