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

Add no-multiple-var rule #1191

Merged
merged 2 commits into from
May 2, 2016
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ A sample configuration file with all options is available [here](https://github.
* `no-internal-module` disallows internal `module` (use `namespace` instead).
* `no-invalid-this` disallows using the `this` keyword outside of classes.
* `no-this-in-function-in-method` disallows using the `this` keyword in functions within class methods.
* `no-multiple-var` disallows multiple variable definitions in the same statement.
* `no-use-before-declare` disallows usage of variables before their declaration.
* `no-namespace` disallows both internal `module`s and `namespace`, but allows ES6-style external modules.
* `allow-declarations` allows `declare namespace ... {}` to describe external APIs.
* `no-null-keyword` disallows use of the `null` keyword literal.
Expand Down
1 change: 1 addition & 0 deletions docs/sample.tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
true,
"no-this-in-function-in-method"
],
"no-multiple-var": true,
"no-null-keyword": true,
"no-reference": true,
"no-require-imports": true,
Expand Down
50 changes: 50 additions & 0 deletions src/rules/noMultipleVarRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2016 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 "../lint";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Forbidden multiple variable definitions in the same statement";

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

class NoMultipleVarWalker extends Lint.RuleWalker {
public visitVariableStatement(node: ts.VariableStatement) {
const declarationList = node.declarationList;

if (declarationList.declarations.length > 1) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}

super.visitVariableStatement(node);
}

public visitForStatement(node: ts.ForStatement) {
let initializer = node.initializer;
if (initializer && initializer.kind === ts.SyntaxKind.VariableDeclarationList &&
(<ts.VariableDeclarationList>initializer).declarations.length > 1) {
const declarationList = <ts.VariableDeclarationList>initializer;
this.addFailure(this.createFailure(declarationList.getStart(), declarationList.getWidth(), Rule.FAILURE_STRING));
}
super.visitForStatement(node);
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"rules/noInferrableTypesRule.ts",
"rules/noInternalModuleRule.ts",
"rules/noInvalidThisRule.ts",
"rules/noMultipleVarRule.ts",
"rules/noNamespaceRule.ts",
"rules/noNullKeywordRule.ts",
"rules/noReferenceRule.ts",
Expand Down
44 changes: 44 additions & 0 deletions test/rules/no-multiple-var/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// valid cases
var foo1;
var foo2: number;
var foo3 = 1;

let foo4;
let foo5: number;
let foo6 = 1;

const foo7: number = 1;
const foo8 = 1;

for (var i = 0; i > 1; i++) {}
for (let i = 0; i > 1; i++) {}
for (const i = 0; i > 1;) {}

// invalid cases
var foo9, foo10;
~~~~~~~~~~~~~~~~ [0]
var foo11: number, foo12: number;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
var foo13 = 1, foo14 = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

let foo15, foo16;
~~~~~~~~~~~~~~~~~ [0]
let foo17: number, foo18: number;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
let foo19 = 1, foo20 = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

const foo21: number = 1, foo22: number = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
const foo23 = 1, foo24 = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

for (var i = 0, j = 0; i > 1; i++) {}
~~~~~~~~~~~~~~~~ [0]
for (let i = 0, j = 0; i > 1; i++) {}
~~~~~~~~~~~~~~~~ [0]
for (const i = 0, j = 0; i > 1;) {}
~~~~~~~~~~~~~~~~~~ [0]

[0]: Forbidden multiple variable definitions in the same statement
5 changes: 5 additions & 0 deletions test/rules/no-multiple-var/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-multiple-var": true
}
}
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"../src/rules/noInferrableTypesRule.ts",
"../src/rules/noInternalModuleRule.ts",
"../src/rules/noInvalidThisRule.ts",
"../src/rules/noMultipleVarRule.ts",
"../src/rules/noNamespaceRule.ts",
"../src/rules/noNullKeywordRule.ts",
"../src/rules/noReferenceRule.ts",
Expand Down