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

Commit c73f359

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

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-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-explicit-any` - enforces the any type is not used.

docs/rules/no-explicit-any.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Enforces the any type is not used (no-explicit-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 goes doesn't allow `any` types to be defined.
9+
It aims to keep TypeScript maximally useful.
10+
TypeScript has a compiler flag for `--noImplicitAny` that will prevent
11+
an `any` type from being implied by the compiler, but doesn't prevent
12+
`any` from being explicitly used.
13+
14+
The following patterns are considered warnings:
15+
16+
```ts
17+
const age : any = "seventeen"
18+
```
19+
20+
```ts
21+
function greet () : any {}
22+
```
23+
24+
The following patterns are not warnings:
25+
26+
```ts
27+
const age : number = 17
28+
```
29+
30+
```ts
31+
function greet () : string {}
32+
```
33+
34+
## When Not To Use It
35+
36+
If an unknown type or a library without typings is used
37+
and you want to be able to specify `any`.
38+
39+
## Further Reading
40+
41+
* TypeScript [any type](https://www.typescriptlang.org/docs/handbook/basic-types.html#any)
42+
43+
## Compatibility
44+
45+
* TSLint: [no-any](https://palantir.github.io/tslint/rules/no-any/)

lib/rules/no-explicit-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-explicit-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-explicit-any"),
12+
RuleTester = require("eslint").RuleTester;
13+
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
var ruleTester = new RuleTester();
20+
ruleTester.run("no-explicit-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)