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

Add interface-over-type-literal rule #1890

Merged
merged 2 commits into from Dec 17, 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
18 changes: 12 additions & 6 deletions docs/_data/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
]
},
"optionExamples": [
"[true, array]",
"[true, generic]",
"[true, array-simple]"
"[true, \"array\"]",
"[true, \"generic\"]",
"[true, \"array-simple\"]"
],
"type": "style",
"typescriptOnly": true
Expand Down Expand Up @@ -269,6 +269,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 @@ -606,9 +615,6 @@
"rationale": "An empty interface is equivalent to its supertype (or `{}`).",
"optionsDescription": "Not configurable.",
"options": null,
"optionExamples": [
"true"
],
"type": "typescript",
"typescriptOnly": true
},
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/array-type/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
"array-simple"
]
}
---
---
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",
Copy link
Author

Choose a reason for hiding this comment

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

This was left in by accident.

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
}
}