This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: classMemberNoDefaultAccess. Disallows public access modifie…
…rs that are not explicit in the class
- Loading branch information
Alexander Schrab
committed
May 22, 2015
1 parent
b08678f
commit c69c5ff
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
]); | ||
}); | ||
}); |