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

Commit

Permalink
Add new Rule: arrow-parens (#1384)
Browse files Browse the repository at this point in the history
* Add new Rule: arrow-parens

* Add test

* modify some documentations
  • Loading branch information
YuichiNukiyama authored and jkillian committed Jul 20, 2016
1 parent 7fbd31b commit 81c538c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ Core rules are included in the `tslint` package.
* `"parameters"` checks alignment of function parameters.
* `"arguments"` checks alignment of function call arguments.
* `"statements"` checks alignment of statements.
* `arrow-parens` requires parentheses around the parameters of arrow function definitions.
* `ban` bans the use of specific functions. Options are ["object", "function"] pairs that ban the use of object.function().
* `class-name` enforces PascalCased class and interface names.
* `comment-format` enforces rules for single-line comments. Rule options:
Expand Down
59 changes: 59 additions & 0 deletions src/rules/arrowParensRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @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 {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "arrow-parens",
description: "Requires parentheses around the parameters of arrow function definitions.",
rationale: "Maintains stylistic consistency with other arrow function definitions.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "style",
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Parentheses are required around the parameters of an arrow function definition";

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

class ArrowParensWalker extends Lint.RuleWalker {
public visitArrowFunction(node: ts.FunctionLikeDeclaration) {
if (node.parameters.length === 1) {
const parameter = node.parameters[0];
const text = parameter.getText();
const firstToken = node.getFirstToken();
const lastToken = node.getChildAt(2);
const width = text.length;
const position = parameter.getStart();

if (firstToken.kind !== ts.SyntaxKind.OpenParenToken || lastToken.kind !== ts.SyntaxKind.CloseParenToken) {
this.addFailure(this.createFailure(position, width, Rule.FAILURE_STRING));
}
}
super.visitArrowFunction(node);
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"language/walker/skippableTokenAwareRuleWalker.ts",
"language/walker/syntaxWalker.ts",
"rules/alignRule.ts",
"rules/arrowParensRule.ts",
"rules/banRule.ts",
"rules/classNameRule.ts",
"rules/commentFormatRule.ts",
Expand Down
15 changes: 15 additions & 0 deletions test/rules/arrow-parens/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// valid case
var a = (a) => {};
var b = (a: number) => {};
var c = (a, b) => {};
var f = (...rest) => {};
var f = a: number => {}; // TSLint don't warn. But syntax is wrong.
class Foo {
a: (a) =>{}
}

// invalid case
var e = (a => {})(1);
~ [Parentheses are required around the parameters of an arrow function definition]
var f = a => {};
~ [Parentheses are required around the parameters of an arrow function definition]
5 changes: 5 additions & 0 deletions test/rules/arrow-parens/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"arrow-parens": true
}
}
3 changes: 2 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"../src/ruleLoader.ts",
"../src/rules.ts",
"../src/rules/alignRule.ts",
"../src/rules/arrowParensRule.ts",
"../src/rules/banRule.ts",
"../src/rules/classNameRule.ts",
"../src/rules/commentFormatRule.ts",
Expand Down Expand Up @@ -162,4 +163,4 @@
"rule-tester/testData.ts",
"rule-tester/utilsTests.ts"
]
}
}

0 comments on commit 81c538c

Please sign in to comment.