Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 8243a0e

Browse files
committed
NEW: Add rule "use-accessibility" to enforce accessibility modifiers such as public, private, and protected. (refs #4)
1 parent 31c25c9 commit 8243a0e

File tree

5 files changed

+178
-6
lines changed

5 files changed

+178
-6
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,4 @@ Then configure the rules you want to use under the rules section.
5050
## Supported Rules
5151

5252
* `typescript/type-annotation-spacing` - enforces one space after the colon and zero spaces before the colon of a type annotation.
53-
54-
55-
56-
57-
53+
* `typescript/member-access` - enforces visibility modifiers on class properties and methods.

docs/rules/use-accessibility.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Enforces accessibility modifiers on class properties and methods (use-accessibility)
2+
3+
Leaving off accessibility modifier and making everything public can make
4+
your interface hard to use by others.
5+
If you make all internal pieces private or protected, your interface will
6+
be easier to use.
7+
8+
## Rule Details
9+
10+
This rule aims to make code more readable and explicit about who can use
11+
which properties.
12+
13+
The following patterns are considered warnings:
14+
15+
```ts
16+
class Animal {
17+
name: string // No accessibility modifier
18+
getName (): string {} // No accessibility modifier
19+
}
20+
```
21+
22+
The following patterns are not warnings:
23+
24+
```ts
25+
class Animal {
26+
private name: string // explicit accessibility modifier
27+
public getName (): string {} // explicit accessibility modifier
28+
}
29+
```
30+
31+
## When Not To Use It
32+
33+
If you think defaulting to public is a good default then you will not need
34+
this rule.
35+
36+
## Further Reading
37+
38+
* TypeScript [Accessibility Modifiers](https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers)
39+
40+
## Compatibility
41+
42+
* TSLint: [member-access](http://palantir.github.io/tslint/rules/member-access/)

lib/rules/use-accessibility.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview Enforces explicit accessibility modifier for class members
3+
* @author Danny Fritz
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description: "Enforces explicity accessibility modifiers for class members",
15+
category: "TypeScript"
16+
},
17+
schema: []
18+
},
19+
20+
create: function(context) {
21+
22+
//----------------------------------------------------------------------
23+
// Helpers
24+
//----------------------------------------------------------------------
25+
26+
/**
27+
* Checks if a method declaration has an accessibility modifier.
28+
* @param {ASTNode} methodDefinition The node representing a MethodDefinition.
29+
* @returns {void}
30+
* @private
31+
*/
32+
function checkMethodAccessibilityModifier(methodDefinition) {
33+
if (!methodDefinition.accessibility) {
34+
context.report({
35+
node: methodDefinition,
36+
message: "Missing accessibility modifier on method definition " + methodDefinition.key.name + "."
37+
});
38+
}
39+
}
40+
41+
/**
42+
* Checks if property has an accessibility modifier.
43+
* @param {ASTNode} classProperty The node representing a ClassProperty.
44+
* @returns {void}
45+
* @private
46+
*/
47+
function checkPropertyAccessibilityModifier(classProperty) {
48+
if (!classProperty.accessibility) {
49+
context.report({
50+
node: classProperty,
51+
message: "Missing accessibility modifier on class property " + classProperty.key.name + "."
52+
});
53+
}
54+
}
55+
56+
//----------------------------------------------------------------------
57+
// Public
58+
//----------------------------------------------------------------------
59+
return {
60+
ClassProperty: checkPropertyAccessibilityModifier,
61+
MethodDefinition: checkMethodAccessibilityModifier
62+
};
63+
}
64+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"eslint": "~3.0.0",
2222
"eslint-config-eslint": "^3.0.0",
2323
"mocha": "^2.4.5",
24-
"typescript-eslint-parser": "~0.1.1"
24+
"typescript-eslint-parser": "^0.4.0"
2525
},
2626
"engines": {
2727
"node": ">=4"

tests/lib/rules/use-accessibility.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @fileoverview Enforces explicit accessibility modifiers for class members
3+
* @author Danny Fritz
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
var rule = require("../../../lib/rules/use-accessibility"),
12+
RuleTester = require("eslint").RuleTester;
13+
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
var ruleTester = new RuleTester();
20+
ruleTester.run("use-accessibility", rule, {
21+
22+
valid: [
23+
{
24+
code: `
25+
class Test {
26+
protected name: string
27+
private x: number
28+
public getX () {
29+
return this.x
30+
}
31+
}
32+
`,
33+
parser: "typescript-eslint-parser"
34+
}
35+
],
36+
invalid: [
37+
{
38+
code: `
39+
class Test {
40+
x: number
41+
public getX () {
42+
return this.x
43+
}
44+
}
45+
`,
46+
parser: "typescript-eslint-parser",
47+
errors: [{
48+
message: "Missing accessibility modifier on class property x.",
49+
line: 3,
50+
column: 3
51+
}]
52+
},
53+
{
54+
code: `
55+
class Test {
56+
private x: number
57+
getX () {
58+
return this.x
59+
}
60+
}
61+
`,
62+
parser: "typescript-eslint-parser",
63+
errors: [{
64+
message: "Missing accessibility modifier on method definition getX.",
65+
line: 4,
66+
column: 3
67+
}]
68+
}
69+
]
70+
});

0 commit comments

Comments
 (0)