зеркало из https://github.com/microsoft/dtslint.git
Add `no-any-union` rule
This commit is contained in:
Родитель
a1a74e9ead
Коммит
1369e83ff1
|
@ -0,0 +1,27 @@
|
|||
# no-any-union
|
||||
|
||||
Forbids to include `any` in a union. When `any` is used in a union type, the resulting type is still `any`.
|
||||
|
||||
**Bad**:
|
||||
|
||||
```ts
|
||||
function f(x: string | any): void;
|
||||
```
|
||||
|
||||
**Good**:
|
||||
|
||||
```ts
|
||||
function f(x: string): void;
|
||||
```
|
||||
|
||||
Or:
|
||||
```ts
|
||||
function f(x: any): void;
|
||||
```
|
||||
|
||||
Or:
|
||||
```ts
|
||||
function f(x: string | object): void;
|
||||
```
|
||||
|
||||
While the `string` portion of this type annotation may _look_ useful, it in fact offers no additional typechecking over simply using `any`.
|
|
@ -1,3 +1,5 @@
|
|||
# no-dead-reference
|
||||
|
||||
A `<reference>` comment should go at the top of a file -- otherwise it is just a normal comment.
|
||||
|
||||
**Bad**:
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"no-padding": true,
|
||||
"no-relative-import-in-test": true,
|
||||
"strict-export-declare-modifiers": true,
|
||||
"no-any-union": true,
|
||||
"no-single-declare-module": true,
|
||||
"no-useless-files": true,
|
||||
"prefer-declare-function": true,
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
import * as Lint from "tslint";
|
||||
import * as ts from "typescript";
|
||||
|
||||
import { failure } from "../util";
|
||||
|
||||
export class Rule extends Lint.Rules.AbstractRule {
|
||||
static metadata: Lint.IRuleMetadata = {
|
||||
ruleName: "no-any-union",
|
||||
description: "Forbid a union to contain `any`",
|
||||
optionsDescription: "Not configurable.",
|
||||
options: null,
|
||||
type: "functionality",
|
||||
typescriptOnly: true,
|
||||
};
|
||||
|
||||
static FAILURE_STRING = failure(
|
||||
Rule.metadata.ruleName,
|
||||
"Including `any` in a union will override all other members of the union.");
|
||||
|
||||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
return this.applyWithFunction(sourceFile, walk);
|
||||
}
|
||||
}
|
||||
|
||||
function walk(ctx: Lint.WalkContext<void>): void {
|
||||
ctx.sourceFile.forEachChild(function recur(node) {
|
||||
if (node.kind === ts.SyntaxKind.AnyKeyword && ts.isUnionTypeNode(node.parent!)) {
|
||||
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
|
||||
}
|
||||
node.forEachChild(recur);
|
||||
});
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export const x: any;
|
||||
|
||||
export const y: string | any;
|
||||
~~~ [Including `any` in a union will override all other members of the union. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-any-union.md]
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"rulesDirectory": ["../../bin/rules"],
|
||||
"rules": {
|
||||
"no-any-union": true
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче