From c7c7abcc71b9fd5170488dbf017d8411ef1ff738 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 Jan 2018 13:00:42 -0800 Subject: [PATCH] Add 'no-const-enum' rule --- docs/no-const-enum.md | 16 ++++++++++++++++ dtslint.json | 1 + src/rules/noConstEnumRule.ts | 32 ++++++++++++++++++++++++++++++++ test/no-const-enum/test.ts.lint | 7 +++++++ test/no-const-enum/tslint.json | 6 ++++++ 5 files changed, 62 insertions(+) create mode 100644 docs/no-const-enum.md create mode 100644 src/rules/noConstEnumRule.ts create mode 100644 test/no-const-enum/test.ts.lint create mode 100644 test/no-const-enum/tslint.json diff --git a/docs/no-const-enum.md b/docs/no-const-enum.md new file mode 100644 index 00000000..3d75db52 --- /dev/null +++ b/docs/no-const-enum.md @@ -0,0 +1,16 @@ +# no-const-enum + +Avoid using `const enum`s. These can't be used by JavaScript users or by TypeScript users with [`--isolatedModules`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) enabled. + +**Bad**: + +```ts +const enum Bit { Off, On } +export function f(b: Bit): void; +``` + +**Good**: + +```ts +export function f(b: 0 | 1): void; +``` diff --git a/dtslint.json b/dtslint.json index 563f1858..34a6e0f7 100644 --- a/dtslint.json +++ b/dtslint.json @@ -6,6 +6,7 @@ "expect": true, "export-just-namespace": true, "no-bad-reference": true, + "no-const-enum": true, "no-dead-reference": true, "no-padding": true, "no-redundant-undefined": true, diff --git a/src/rules/noConstEnumRule.ts b/src/rules/noConstEnumRule.ts new file mode 100644 index 00000000..83180b7c --- /dev/null +++ b/src/rules/noConstEnumRule.ts @@ -0,0 +1,32 @@ +import * as Lint from "tslint"; +import * as ts from "typescript"; + +import { failure } from "../util"; + +export class Rule extends Lint.Rules.AbstractRule { + static metadata: Lint.IRuleMetadata = { + ruleName: "no-const-enum", + description: "Forbid `const enum`", + optionsDescription: "Not configurable.", + options: null, + type: "functionality", + typescriptOnly: true, + }; + + static FAILURE_STRING = failure( + Rule.metadata.ruleName, + "Use of `const enum`s is forbidden."); + + apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + return this.applyWithFunction(sourceFile, walk); + } +} + +function walk(ctx: Lint.WalkContext): void { + ctx.sourceFile.forEachChild(function recur(node) { + if (ts.isEnumDeclaration(node) && node.modifiers && node.modifiers.some(m => m.kind === ts.SyntaxKind.ConstKeyword)) { + ctx.addFailureAtNode(node.name, Rule.FAILURE_STRING); + } + node.forEachChild(recur); + }); +} diff --git a/test/no-const-enum/test.ts.lint b/test/no-const-enum/test.ts.lint new file mode 100644 index 00000000..0502fc78 --- /dev/null +++ b/test/no-const-enum/test.ts.lint @@ -0,0 +1,7 @@ +const enum E { + ~ [0] +} + +enum F {} + +[0]: Use of `const enum`s is forbidden. See: https://github.com/Microsoft/dtslint/blob/master/docs/no-const-enum.md diff --git a/test/no-const-enum/tslint.json b/test/no-const-enum/tslint.json new file mode 100644 index 00000000..f301cb1e --- /dev/null +++ b/test/no-const-enum/tslint.json @@ -0,0 +1,6 @@ +{ + "rulesDirectory": ["../../bin/rules"], + "rules": { + "no-const-enum": true + } +}