Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tslint rule to enforce consecutive function overloads #9581

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ const tslintRules = [
"noInOperatorRule",
"noIncrementDecrementRule",
"objectLiteralSurroundingSpaceRule",
"enforceConsecutiveFunctionOverloadRule"
];
const tslintRulesFiles = tslintRules.map(function(p) {
return path.join(tslintRuleDir, p + ".ts");
Expand Down
1 change: 1 addition & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ var tslintRules = [
"noInOperatorRule",
"noIncrementDecrementRule",
"objectLiteralSurroundingSpaceRule",
"enforceConsecutiveFunctionOverloadRule",
];
var tslintRulesFiles = tslintRules.map(function(p) {
return path.join(tslintRuleDir, p + ".ts");
Expand Down
68 changes: 68 additions & 0 deletions scripts/tslint/enforceConsecutiveFunctionOverloadRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as Lint from "tslint/lib/lint";
import * as ts from "typescript";

export class Rule extends Lint.Rules.AbstractRule {

public static FAILURE_STRING_FACTORY = (name: string) => `All '${name}' signatures should be adjacent`;

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

function isStringOrNumericLiteral(kind: ts.SyntaxKind) {
return kind === ts.SyntaxKind.StringLiteral || kind === ts.SyntaxKind.NumericLiteral;
}

function getTextOfPropertyName(name: ts.PropertyName): string {
switch (name.kind) {
case ts.SyntaxKind.Identifier:
return (<ts.Identifier>name).text;
case ts.SyntaxKind.StringLiteral:
case ts.SyntaxKind.NumericLiteral:
return (<ts.LiteralExpression>name).text;
case ts.SyntaxKind.ComputedPropertyName:
if (isStringOrNumericLiteral((<ts.ComputedPropertyName>name).expression.kind)) {
return (<ts.LiteralExpression>(<ts.ComputedPropertyName>name).expression).text;
}
}

return undefined;
}

class NoNonConsecutiveFunctionOverload extends Lint.BlockScopeAwareRuleWalker<{}, ScopeInfo> {

createScope(): any {
return undefined;
}

createBlockScope(): ScopeInfo {
return new ScopeInfo();
}

visitInterfaceDeclaration(node: ts.InterfaceDeclaration): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about classes, namspaces, modules, and sourceFiles?

Copy link
Author

@rakatyal rakatyal Jul 11, 2016

Choose a reason for hiding this comment

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

Typescript compiler already gives an error when the definition is not present at the end of overloads inside classes.

Copy link

Choose a reason for hiding this comment

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

Actually, we need to check those in declaration files. See palantir/tslint#1707.

const members = node.members;
for (const member of members) {
if (member.name !== undefined) {
const methodName = getTextOfPropertyName(member.name);
if (methodName !== undefined) {
this.handleMethodName(member, methodName);
}
}
}
super.visitInterfaceDeclaration(node);
}

handleMethodName(node: ts.Node, methodName: string) {
const currentBlockScope = this.getCurrentBlockScope();
const lastPosition = currentBlockScope.methodNames.lastIndexOf(methodName);
if (lastPosition >= 0 && lastPosition != (currentBlockScope.methodNames.length - 1)) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING_FACTORY(methodName)));
}
currentBlockScope.methodNames.push(methodName);
}
}

class ScopeInfo {
public methodNames: string[] = [];
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"prefer-const": true,
"no-in-operator": true,
"no-increment-decrement": true,
"object-literal-surrounding-space": true
"object-literal-surrounding-space": true,
"enforce-consecutive-function-overload": true
}
}