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

Commit

Permalink
Add interface-over-type-literal rule (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy authored and nchen63 committed Dec 17, 2016
1 parent 63fb778 commit 555549f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
12 changes: 9 additions & 3 deletions docs/_data/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@
"type": "style",
"typescriptOnly": true
},
{
"ruleName": "interface-over-type-literal",
"description": "Prefer an interface declaration over `type T = { ... }`",
"rationale": "style",
"optionsDescription": "Not configurable.",
"options": null,
"type": "style",
"typescriptOnly": true
},
{
"ruleName": "jsdoc-format",
"description": "Enforces basic format rules for JSDoc comments.",
Expand Down Expand Up @@ -615,9 +624,6 @@
"rationale": "An empty interface is equivalent to its supertype (or `{}`).",
"optionsDescription": "Not configurable.",
"options": null,
"optionExamples": [
"true"
],
"type": "typescript",
"typescriptOnly": true
},
Expand Down
12 changes: 12 additions & 0 deletions docs/rules/interface-over-type-literal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
ruleName: interface-over-type-literal
description: 'Prefer an interface declaration over `type T = { ... }`'
rationale: style
optionsDescription: Not configurable.
options: null
type: style
typescriptOnly: true
layout: rule
title: 'Rule: interface-over-type-literal'
optionsJSON: 'null'
---
2 changes: 0 additions & 2 deletions docs/rules/no-empty-interface/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
rationale: 'An empty interface is equivalent to its supertype (or `{}`).'
optionsDescription: Not configurable.
options: null
optionExamples:
- 'true'
type: typescript
typescriptOnly: true
layout: rule
Expand Down
49 changes: 49 additions & 0 deletions src/rules/interfaceOverTypeLiteralRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @license
* Copyright 2013 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: "interface-over-type-literal",
description: "Prefer an interface declaration over a type literal (`type T = { ... }`)",
rationale: "style",
optionsDescription: "Not configurable.",
options: null,
type: "style",
typescriptOnly: true,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Use an interface instead of a type literal.";

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

class Walker extends Lint.RuleWalker {
public visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration) {
if (node.type.kind === ts.SyntaxKind.TypeLiteral) {
this.addFailureAtNode(node.name, Rule.FAILURE_STRING);
}
super.visitTypeAliasDeclaration(node);
}
}
1 change: 0 additions & 1 deletion src/rules/noEmptyInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class Rule extends Lint.Rules.AbstractRule {
rationale: "An empty interface is equivalent to its supertype (or `{}`).",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "typescript",
typescriptOnly: true,
};
Expand Down
5 changes: 5 additions & 0 deletions test/rules/interface-over-type-literal/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type T = { x: number; }
~ [Use an interface instead of a type literal.]

type U = string;
type V = { x: number; } | { y: string; };
5 changes: 5 additions & 0 deletions test/rules/interface-over-type-literal/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"interface-over-type-literal": true
}
}

0 comments on commit 555549f

Please sign in to comment.