This repository was archived by the owner on Jan 19, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +178
-6
lines changed Expand file tree Collapse file tree 5 files changed +178
-6
lines changed Original file line number Diff line number Diff line change @@ -50,8 +50,4 @@ Then configure the rules you want to use under the rules section.
50
50
## Supported Rules
51
51
52
52
* ` 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.
Original file line number Diff line number Diff line change
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/ )
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 21
21
"eslint" : " ~3.0.0" ,
22
22
"eslint-config-eslint" : " ^3.0.0" ,
23
23
"mocha" : " ^2.4.5" ,
24
- "typescript-eslint-parser" : " ~0.1.1 "
24
+ "typescript-eslint-parser" : " ^0.4.0 "
25
25
},
26
26
"engines" : {
27
27
"node" : " >=4"
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments