This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3825c49
commit 779ba0b
Showing
7 changed files
with
104 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
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 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,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); | ||
} | ||
} |
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 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,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 |
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 @@ | ||
{ | ||
"rules": { | ||
"no-multiple-var": true | ||
} | ||
} |
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