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

New: Add rule explicit-member-accessibility #4

Merged
merged 1 commit into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,4 @@ Then configure the rules you want to use under the rules section.
## Supported Rules

* `typescript/type-annotation-spacing` - enforces one space after the colon and zero spaces before the colon of a type annotation.





* `typescript/explicit-member-accessibility` - enforces accessibility modifiers on class properties and methods.
42 changes: 42 additions & 0 deletions docs/rules/explicit-member-accessibility.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Enforces accessibility modifiers on class properties and methods (explicit-member-accessibility)

Leaving off accessibility modifier and making everything public can make
your interface hard to use by others.
If you make all internal pieces private or protected, your interface will
be easier to use.

## Rule Details

This rule aims to make code more readable and explicit about who can use
which properties.

The following patterns are considered warnings:

```ts
class Animal {
name: string // No accessibility modifier
getName (): string {} // No accessibility modifier
}
```

The following patterns are not warnings:

```ts
class Animal {
private name: string // explicit accessibility modifier
public getName (): string {} // explicit accessibility modifier
}
```

## When Not To Use It

If you think defaulting to public is a good default, then you will not need
this rule.

## Further Reading

* TypeScript [Accessibility Modifiers](https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers)

## Compatibility

* TSLint: [member-access](http://palantir.github.io/tslint/rules/member-access/)
64 changes: 64 additions & 0 deletions lib/rules/explicit-member-accessibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @fileoverview Enforces explicit accessibility modifier for class members
* @author Danny Fritz
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: "Enforces explicity accessibility modifiers for class members",
category: "TypeScript"
},
schema: []
},

create: function(context) {

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

/**
* Checks if a method declaration has an accessibility modifier.
* @param {ASTNode} methodDefinition The node representing a MethodDefinition.
* @returns {void}
* @private
*/
function checkMethodAccessibilityModifier(methodDefinition) {
if (!methodDefinition.accessibility) {
context.report({
node: methodDefinition,
message: "Missing accessibility modifier on method definition " + methodDefinition.key.name + "."
});
}
}

/**
* Checks if property has an accessibility modifier.
* @param {ASTNode} classProperty The node representing a ClassProperty.
* @returns {void}
* @private
*/
function checkPropertyAccessibilityModifier(classProperty) {
if (!classProperty.accessibility) {
context.report({
node: classProperty,
message: "Missing accessibility modifier on class property " + classProperty.key.name + "."
});
}
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
ClassProperty: checkPropertyAccessibilityModifier,
MethodDefinition: checkMethodAccessibilityModifier
};
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"eslint": "~3.0.0",
"eslint-config-eslint": "^3.0.0",
"mocha": "^2.4.5",
"typescript-eslint-parser": "~0.1.1"
"typescript-eslint-parser": "^0.4.0"
},
"engines": {
"node": ">=4"
Expand Down
70 changes: 70 additions & 0 deletions tests/lib/rules/explicit-member-accessibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @fileoverview Enforces explicit accessibility modifiers for class members
* @author Danny Fritz
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/explicit-member-accessibility"),
RuleTester = require("eslint").RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("explicit-member-accessibility", rule, {

valid: [
{
code: `
class Test {
protected name: string
private x: number
public getX () {
return this.x
}
}
`,
parser: "typescript-eslint-parser"
}
],
invalid: [
{
code: `
class Test {
x: number
public getX () {
return this.x
}
}
`,
parser: "typescript-eslint-parser",
errors: [{
message: "Missing accessibility modifier on class property x.",
line: 3,
column: 3
}]
},
{
code: `
class Test {
private x: number
getX () {
return this.x
}
}
`,
parser: "typescript-eslint-parser",
errors: [{
message: "Missing accessibility modifier on method definition getX.",
line: 4,
column: 3
}]
}
]
});