Skip to content

Commit

Permalink
no-empty-interface: allow providing type arguments for extended type (p…
Browse files Browse the repository at this point in the history
…alantir#3260)

[bugfix] `no-empty-interface` allows providing type arguments for extended type
Fixes: palantir#3256
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent b889183 commit 66bc91e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/rules/noEmptyInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (isInterfaceDeclaration(node) &&
node.members.length === 0 &&
(node.heritageClauses === undefined ||
// allow interfaces that extend 2 or more interfaces
node.heritageClauses[0].types.length < 2)) {
(node.heritageClauses === undefined || extendsOneTypeWithoutTypeArguments(node.heritageClauses[0]))) {
return ctx.addFailureAtNode(
node.name,
node.heritageClauses !== undefined ? Rule.FAILURE_STRING_FOR_EXTENDS : Rule.FAILURE_STRING);
}
return ts.forEachChild(node, cb);
});
}

function extendsOneTypeWithoutTypeArguments({types}: ts.HeritageClause): boolean {
switch (types.length) {
case 0:
return true; // don't crash on empty extends list
case 1:
return types[0].typeArguments === undefined; // allow interfaces that provide type arguments for the extended type
default:
return false; // allow interfaces extending more than one types
}
}
2 changes: 2 additions & 0 deletions test/rules/no-empty-interface/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ interface L extends J, K {} // extending more than one interface is ok, as it ca

interface M extends {} // don't crash on empty extends list
~ [An interface declaring no members is equivalent to its supertype.]

interface N extends Promise<string|number> {}

0 comments on commit 66bc91e

Please sign in to comment.