diff --git a/fixtures/no-const-enum/default/fixture.ts.lint b/fixtures/no-const-enum/default/fixture.ts.lint new file mode 100644 index 0000000..9a1c85f --- /dev/null +++ b/fixtures/no-const-enum/default/fixture.ts.lint @@ -0,0 +1,5 @@ +const enum Foo { FOO = "F" }; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-const-enum] +enum Bar { BAR = "B" }; + +[no-const-enum]: const enum is forbidden diff --git a/fixtures/no-const-enum/default/tsconfig.json b/fixtures/no-const-enum/default/tsconfig.json new file mode 100644 index 0000000..40eda2d --- /dev/null +++ b/fixtures/no-const-enum/default/tsconfig.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "throw-error": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/fixtures/no-const-enum/default/tslint.json b/fixtures/no-const-enum/default/tslint.json new file mode 100644 index 0000000..e536fa0 --- /dev/null +++ b/fixtures/no-const-enum/default/tslint.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "no-const-enum": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/fixtures/no-enum/default/fixture.ts.lint b/fixtures/no-enum/default/fixture.ts.lint new file mode 100644 index 0000000..b96e9a9 --- /dev/null +++ b/fixtures/no-enum/default/fixture.ts.lint @@ -0,0 +1,6 @@ +const enum Foo { FOO = "F" }; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-enum] +enum Bar { BAR = "B" }; +~~~~~~~~~~~~~~~~~~~~~~ [no-enum] + +[no-enum]: enum is forbidden diff --git a/fixtures/no-enum/default/tsconfig.json b/fixtures/no-enum/default/tsconfig.json new file mode 100644 index 0000000..40eda2d --- /dev/null +++ b/fixtures/no-enum/default/tsconfig.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "throw-error": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/fixtures/no-enum/default/tslint.json b/fixtures/no-enum/default/tslint.json new file mode 100644 index 0000000..90b1c72 --- /dev/null +++ b/fixtures/no-enum/default/tslint.json @@ -0,0 +1,8 @@ +{ + "defaultSeverity": "error", + "jsRules": {}, + "rules": { + "no-enum": { "severity": "error" } + }, + "rulesDirectory": "../../../build/rules" +} diff --git a/source/rules/noConstEnumRule.ts b/source/rules/noConstEnumRule.ts new file mode 100644 index 0000000..f485682 --- /dev/null +++ b/source/rules/noConstEnumRule.ts @@ -0,0 +1,42 @@ +/** + * @license Use of this source code is governed by an MIT-style license that + * can be found in the LICENSE file at https://github.com/cartant/tslint-etc + */ + +import * as Lint from "tslint"; +import * as ts from "typescript"; +import { tsquery } from "@phenomnomnominal/tsquery"; + +export class Rule extends Lint.Rules.TypedRule { + + public static metadata: Lint.IRuleMetadata = { + description: "Disallows the use of const enums.", + options: null, + optionsDescription: "Not configurable.", + requiresTypeInfo: false, + ruleName: "no-const-enum", + type: "functionality", + typescriptOnly: true + }; + + public static FAILURE_STRING = "const enum is forbidden"; + + public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { + const failures: Lint.RuleFailure[] = []; + const constKeywords = tsquery( + sourceFile, + "EnumDeclaration > ConstKeyword" + ); + constKeywords.forEach((node: ts.Node) => { + const enumDeclaration = node.parent as ts.EnumDeclaration; + failures.push(new Lint.RuleFailure( + sourceFile, + enumDeclaration.getStart(), + enumDeclaration.getStart() + enumDeclaration.getWidth(), + Rule.FAILURE_STRING, + this.ruleName + )); + }); + return failures; + } +} diff --git a/source/rules/noEnumRule.ts b/source/rules/noEnumRule.ts new file mode 100644 index 0000000..c8be302 --- /dev/null +++ b/source/rules/noEnumRule.ts @@ -0,0 +1,41 @@ +/** + * @license Use of this source code is governed by an MIT-style license that + * can be found in the LICENSE file at https://github.com/cartant/tslint-etc + */ + +import * as Lint from "tslint"; +import * as ts from "typescript"; +import { tsquery } from "@phenomnomnominal/tsquery"; + +export class Rule extends Lint.Rules.TypedRule { + + public static metadata: Lint.IRuleMetadata = { + description: "Disallows the use of enums.", + options: null, + optionsDescription: "Not configurable.", + requiresTypeInfo: false, + ruleName: "no-enum", + type: "functionality", + typescriptOnly: true + }; + + public static FAILURE_STRING = "enum is forbidden"; + + public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { + const failures: Lint.RuleFailure[] = []; + const enumDeclarations = tsquery( + sourceFile, + "EnumDeclaration" + ); + enumDeclarations.forEach((node: ts.Node) => { + failures.push(new Lint.RuleFailure( + sourceFile, + node.getStart(), + node.getStart() + node.getWidth(), + Rule.FAILURE_STRING, + this.ruleName + )); + }); + return failures; + } +}