diff --git a/src/rules/interfaceNameRule.ts b/src/rules/interfaceNameRule.ts index b33281c4dbe..32f169f5e2c 100644 --- a/src/rules/interfaceNameRule.ts +++ b/src/rules/interfaceNameRule.ts @@ -19,7 +19,7 @@ import * as utils from "tsutils"; import * as ts from "typescript"; import * as Lint from "../index"; -import { isUpperCase } from "../utils"; +import { isLowerCase, isUpperCase } from "../utils"; const OPTION_ALWAYS = "always-prefix"; const OPTION_NEVER = "never-prefix"; @@ -62,10 +62,12 @@ function walk(ctx: Lint.WalkContext<{ never: boolean }>): void { return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void { if (utils.isInterfaceDeclaration(node)) { const { name } = node; - if (never && hasPrefixI(name.text)) { - ctx.addFailureAtNode(name, Rule.FAILURE_STRING_NO_PREFIX); - } else if (!never && name.text[0] !== "I") { - ctx.addFailureAtNode(name, Rule.FAILURE_STRING); + if (!cantDecide(name.text)) { + if (never && hasPrefixI(name.text)) { + ctx.addFailureAtNode(name, Rule.FAILURE_STRING_NO_PREFIX); + } else if (!never && !hasPrefixI(name.text)) { + ctx.addFailureAtNode(name, Rule.FAILURE_STRING); + } } } else { return ts.forEachChild(node, cb); @@ -74,6 +76,12 @@ function walk(ctx: Lint.WalkContext<{ never: boolean }>): void { } function hasPrefixI(name: string): boolean { - // Allow IndexedDB interfaces - return name.length >= 2 && name[0] === "I" && isUpperCase(name[1]) && !name.startsWith("IDB"); + return name.length >= 3 && name[0] === "I" && !isLowerCase(name[1]) && !isUpperCase(name[2]); +} + +function cantDecide(name: string): boolean { + return ( + (name.length === 2 && name[0] === "I" && !isLowerCase(name[1])) || + (name.length >= 2 && name[0] === "I" && !isLowerCase(name[1]) && !isLowerCase(name[2])) + ); } diff --git a/test/rules/interface-name/always-prefix/test.ts.lint b/test/rules/interface-name/always-prefix/test.ts.lint index df81902d1d8..7dda122e691 100644 --- a/test/rules/interface-name/always-prefix/test.ts.lint +++ b/test/rules/interface-name/always-prefix/test.ts.lint @@ -2,7 +2,20 @@ interface IOptions { } +interface ID { +} + +interface IABC { +} + +interface IDBFactory { +} + // invalid code interface Options { ~~~~~~~ [interface name must start with a capitalized I] } + +interface Incomplete { + ~~~~~~~~~~ [interface name must start with a capitalized I] +} diff --git a/test/rules/interface-name/never-prefix/test.ts.lint b/test/rules/interface-name/never-prefix/test.ts.lint index 84d59f63b1f..f6afc38f412 100644 --- a/test/rules/interface-name/never-prefix/test.ts.lint +++ b/test/rules/interface-name/never-prefix/test.ts.lint @@ -5,9 +5,18 @@ interface Index { interface I { } +interface ID { +} + +interface IABC { +} + interface IDBFactory { } +interface I18nFactory { +} + interface ABC { }