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

Commit

Permalink
interface-name: handle I18n case (#4486)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and adidahiya committed Mar 26, 2019
1 parent 92d188a commit 6b28417
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/rules/interfaceNameRule.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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]))
);
}
13 changes: 13 additions & 0 deletions test/rules/interface-name/always-prefix/test.ts.lint
Expand Up @@ -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]
}
9 changes: 9 additions & 0 deletions test/rules/interface-name/never-prefix/test.ts.lint
Expand Up @@ -5,9 +5,18 @@ interface Index {
interface I {
}

interface ID {
}

interface IABC {
}

interface IDBFactory {
}

interface I18nFactory {
}

interface ABC {
}

Expand Down

0 comments on commit 6b28417

Please sign in to comment.