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

Commit cbfa0ca

Browse files
committed
New: Add rule no-any
1 parent cec0d0e commit cbfa0ca

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ Then configure the rules you want to use under the rules section.
5151

5252
* `typescript/type-annotation-spacing` - enforces one space after the colon and zero spaces before the colon of a type annotation.
5353
* `typescript/explicit-member-accessibility` - enforces accessibility modifiers on class properties and methods.
54+
* `typescript/no-any` - enforces the any type is not used.

docs/rules/no-any.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Enforces the any type is not used (no-any)
2+
3+
Using the any type defeats the purpose of using TypeScript.
4+
When any is used, all compiler type checks around that value are ignored.
5+
6+
## Rule Details
7+
8+
This rule aims to keep TypeScript maximally useful.
9+
10+
The following patterns are considered warnings:
11+
12+
```ts
13+
const age : any = "seventeen"
14+
```
15+
16+
```ts
17+
function greet () : any {}
18+
```
19+
20+
The following patterns are not warnings:
21+
22+
```ts
23+
const age : number = 17
24+
```
25+
26+
```ts
27+
function greet () : string {}
28+
```
29+
30+
## When Not To Use It
31+
32+
If you require an unknown type or if you pull in a library without typings.
33+
34+
## Further Reading
35+
36+
* TypeScript [any type](https://www.typescriptlang.org/docs/handbook/basic-types.html#any)
37+
38+
## Compatibility
39+
40+
* TSLint: [no-any](https://palantir.github.io/tslint/rules/no-any/)

lib/rules/no-any.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @fileoverview Enforces the any type is not used.
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 the any type is not used.",
15+
category: "TypeScript"
16+
},
17+
schema: []
18+
},
19+
20+
create: function(context) {
21+
22+
//----------------------------------------------------------------------
23+
// Helpers
24+
//----------------------------------------------------------------------
25+
26+
/**
27+
* Checks if the any type is used
28+
* @param {ASTNode} typeAnnotation The TypeAnnotation node.
29+
* @returns {void}
30+
* @private
31+
*/
32+
function checkTypeAnnotationForAny(typeAnnotation) {
33+
if (typeAnnotation.typeAnnotation.type === "TSAnyKeyword") {
34+
context.report({
35+
node: typeAnnotation,
36+
message: "Unexpected any. Specify a different type."
37+
});
38+
}
39+
}
40+
41+
/**
42+
* Checks if a function node used the any type
43+
* @param {ASTNode} node The node representing a function.
44+
* @returns {void}
45+
* @private
46+
*/
47+
function checkFunctionReturnTypeForAny(node) {
48+
if (node.returnType) {
49+
checkTypeAnnotationForAny(node.returnType);
50+
}
51+
}
52+
53+
//----------------------------------------------------------------------
54+
// Public
55+
//----------------------------------------------------------------------
56+
return {
57+
58+
Identifier(node) {
59+
if (node.typeAnnotation) {
60+
checkTypeAnnotationForAny(node.typeAnnotation);
61+
}
62+
},
63+
64+
FunctionDeclaration: checkFunctionReturnTypeForAny,
65+
FunctionExpression: checkFunctionReturnTypeForAny,
66+
ArrowFunctionExpression: checkFunctionReturnTypeForAny
67+
};
68+
}
69+
};

tests/lib/rules/no-any.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @fileoverview Enforces the any type is not used
3+
* @author Danny Fritz
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
var rule = require("../../../lib/rules/no-any"),
12+
RuleTester = require("eslint").RuleTester;
13+
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
var ruleTester = new RuleTester();
20+
ruleTester.run("no-any", rule, {
21+
22+
valid: [
23+
{
24+
code: "const number : number = 1",
25+
parser: "typescript-eslint-parser"
26+
},
27+
{
28+
code: "function greet () : string {}",
29+
parser: "typescript-eslint-parser"
30+
}
31+
],
32+
invalid: [
33+
{
34+
code: "const number : any = 1",
35+
parser: "typescript-eslint-parser",
36+
errors: [{
37+
message: "Unexpected any. Specify a different type.",
38+
line: 1,
39+
column: 16
40+
}]
41+
},
42+
{
43+
code: "function generic () : any {}",
44+
parser: "typescript-eslint-parser",
45+
errors: [{
46+
message: "Unexpected any. Specify a different type.",
47+
line: 1,
48+
column: 23
49+
}]
50+
}
51+
]
52+
});

0 commit comments

Comments
 (0)