-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from Microsoft/no-single-element-tuple-type
Add 'no-single-element-tuple-type' rule
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# no-single-element-tuple-type | ||
|
||
Some users mistakenly write `[T]` when then intend to write an array type `T[]`. | ||
|
||
**Bad**: | ||
|
||
```ts | ||
export const x: [T]; | ||
``` | ||
|
||
**Good**: | ||
|
||
```ts | ||
export const x: T[]; | ||
``` |
31 changes: 31 additions & 0 deletions
31
packages/dtslint/src/rules/noSingleElementTupleTypeRule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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-single-element-tuple-type", | ||
description: "Forbids `[T]`, which should be `T[]`.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
type: "functionality", | ||
typescriptOnly: true, | ||
}; | ||
|
||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
const { sourceFile } = ctx; | ||
sourceFile.forEachChild(function cb(node) { | ||
if (ts.isTupleTypeNode(node) && node.elementTypes.length === 1) { | ||
ctx.addFailureAtNode(node, failure( | ||
Rule.metadata.ruleName, | ||
"Type [T] is a single-element tuple type. You probably meant T[].")); | ||
} | ||
node.forEachChild(cb); | ||
}); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/dtslint/test/no-single-element-tuple-type/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const x: [string]; | ||
~~~~~~~~ | ||
const y: [string, number]; | ||
|
||
[0]: [Type [T] is a single-element tuple type. You probably meant T[]. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-padding.md] |
6 changes: 6 additions & 0 deletions
6
packages/dtslint/test/no-single-element-tuple-type/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rulesDirectory": ["../../bin/rules"], | ||
"rules": { | ||
"no-single-element-tuple-type": true | ||
} | ||
} |