-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no-redundant-undefined: enforce
undefined
for optional parameters (#…
…335) * make optional properties require undefined * rename to redundant-undefined * add missed no-redundant-undefined references * add fix for missing undefined * improve fix to handle function types * Remove checks on properties Including undefined, or not, is now semantically meaningful when exactOptionalPropertyTypes is true.
- Loading branch information
Showing
8 changed files
with
67 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 @@ | ||
# redundant-undefined | ||
|
||
Avoid explicitly specifying `undefined` as a type for a parameter which is already optional. | ||
|
||
**Bad**: | ||
|
||
```ts | ||
function f(s?: string | undefined): void {} | ||
``` | ||
|
||
**Good**: | ||
|
||
```ts | ||
function f(s?: string): void {} | ||
``` |
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
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
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: "redundant-undefined", | ||
description: "Forbids optional parameters to include an explicit `undefined` in their type; requires it in optional properties.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
type: "style", | ||
typescriptOnly: true, | ||
}; | ||
|
||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
if (ctx.sourceFile.fileName.includes('node_modules')) return; | ||
ctx.sourceFile.forEachChild(function recur(node) { | ||
if (node.kind === ts.SyntaxKind.UndefinedKeyword | ||
&& ts.isUnionTypeNode(node.parent!) | ||
&& isOptionalParameter(node.parent!.parent!)) { | ||
ctx.addFailureAtNode( | ||
node, | ||
failure( | ||
Rule.metadata.ruleName, | ||
`Parameter is optional, so no need to include \`undefined\` in the type.`)); | ||
} | ||
node.forEachChild(recur); | ||
}); | ||
} | ||
|
||
function isOptionalParameter(node: ts.Node): boolean { | ||
return node.kind === ts.SyntaxKind.Parameter | ||
&& (node as ts.ParameterDeclaration).questionToken !== undefined; | ||
} |
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
function f(s?: string | undefined): void {} | ||
~~~~~~~~~ [0] | ||
|
||
interface I { | ||
ok?: string | undefined; | ||
s?: string; | ||
almost?: number | string; | ||
} | ||
|
||
[0]: Parameter is optional, so no need to include `undefined` in the type. See: https://github.com/Microsoft/dtslint/blob/master/docs/redundant-undefined.md |
2 changes: 1 addition & 1 deletion
2
...t/test/no-redundant-undefined/tslint.json → ...lint/test/redundant-undefined/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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"rulesDirectory": ["../../bin/rules"], | ||
"rules": { | ||
"no-redundant-undefined": true | ||
"redundant-undefined": true | ||
} | ||
} |