Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add no-let-undefined rule #2100

Merged
merged 2 commits into from
Jan 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/rules/noLetUndefinedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as ts from "typescript";

import * as Lint from "../index";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-let-undefined",
description: "Forbids a 'let' statement to be initialized to 'undefined'.",
hasFix: true,
optionsDescription: "Not configurable.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasFix: true

options: null,
optionExamples: ["true"],
type: "style",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Unnecessary initialization to 'undefined'.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {
public visitVariableDeclaration(node: ts.VariableDeclaration) {
if (Lint.isNodeFlagSet(node.parent!, ts.NodeFlags.Let) && isUndefined(node.initializer)) {
const fix = this.createFix(this.deleteFromTo(
Lint.childOfKind(node, ts.SyntaxKind.EqualsToken)!.pos,
node.end));
this.addFailureAtNode(node, Rule.FAILURE_STRING, fix);
}
super.visitVariableDeclaration(node);
}
}

function isUndefined(node: ts.Node | undefined): boolean {
return node !== undefined && node.kind === ts.SyntaxKind.Identifier && (node as ts.Identifier).text === "undefined";
}
2 changes: 2 additions & 0 deletions test/rules/no-let-undefined/test.js.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x = undefined;
~~~~~~~~~~~~~ [Unnecessary initialization to 'undefined'.]
10 changes: 10 additions & 0 deletions test/rules/no-let-undefined/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let x: string | undefined;

for (let y: number | undefined; y < 2; y++) {}

let z;

const x = undefined;

function f(x: string | undefined = undefined) {}

13 changes: 13 additions & 0 deletions test/rules/no-let-undefined/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let x: string | undefined = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add arg initializer test?
add const x = undefined; which shouldn't fail lint

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

for (let y: number | undefined = undefined; y < 2; y++) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

let z;

const x = undefined;

function f(x: string | undefined = undefined) {}

[0]: Unnecessary initialization to 'undefined'.
8 changes: 8 additions & 0 deletions test/rules/no-let-undefined/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-let-undefined": [true]
},
"jsRules": {
"no-let-undefined": [true]
}
}