Skip to content

Commit

Permalink
feat(enum): Add allowLocal.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Mar 30, 2019
1 parent 6a6c3e3 commit 1637c8d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
7 changes: 7 additions & 0 deletions fixtures/no-const-enum/local/fixture.ts.lint
@@ -0,0 +1,7 @@
export const enum Foo { FOO = "F" };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-const-enum]
export enum Boo { BOO = "B" };
enum Goo { GOO = "G" };
const enum Moo { MOO = "M" };

[no-const-enum]: const enum is forbidden
8 changes: 8 additions & 0 deletions fixtures/no-const-enum/local/tsconfig.json
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"throw-error": { "severity": "error" }
},
"rulesDirectory": "../../../build/rules"
}
13 changes: 13 additions & 0 deletions fixtures/no-const-enum/local/tslint.json
@@ -0,0 +1,13 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"no-const-enum": {
"options": [{
"allowLocal": true
}],
"severity": "error"
}
},
"rulesDirectory": "../../../build/rules"
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"test": "yarn run lint && yarn run test:build && yarn run test:tslint",
"test:build": "yarn run test:clean && tsc -p tsconfig.json",
"test:clean": "rimraf build",
"test:debug": "tslint --test fixtures/no-unused-declaration/unused-imports/tslint.json",
"test:debug": "tslint --test fixtures/no-const-enum/local/tslint.json",
"test:tslint": "tslint --test fixtures/**/tslint.json"
},
"version": "1.4.4"
Expand Down
21 changes: 19 additions & 2 deletions source/rules/noConstEnumRule.ts
Expand Up @@ -4,15 +4,23 @@
*/

import * as Lint from "tslint";
import * as tsutils from "tsutils";
import * as ts from "typescript";
import { tsquery } from "@phenomnomnominal/tsquery";

export class Rule extends Lint.Rules.AbstractRule {

public static metadata: Lint.IRuleMetadata = {
description: "Disallows the use of const enums.",
options: null,
optionsDescription: "Not configurable.",
options: {
properties: {
allowLocal: { type: "boolean" }
},
type: "object"
},
optionsDescription: Lint.Utils.dedent`
An optional object with an optional \`allowLocal\` property - which defaults to \`false\`.
If \`allowLocal\` is \`true\`, only exported const enums are forbidden.`,
requiresTypeInfo: false,
ruleName: "no-const-enum",
type: "functionality",
Expand All @@ -22,13 +30,22 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "const enum is forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const { ruleArguments } = this.getOptions();
const [options] = ruleArguments;
const allowLocal = options ? options.allowLocal : false;
const failures: Lint.RuleFailure[] = [];
const constKeywords = tsquery(
sourceFile,
"EnumDeclaration > ConstKeyword"
);
constKeywords.forEach((node: ts.Node) => {
const enumDeclaration = node.parent as ts.EnumDeclaration;
if (allowLocal && !tsutils.hasModifier(
enumDeclaration.modifiers,
ts.SyntaxKind.ExportKeyword
)) {
return;
}
failures.push(new Lint.RuleFailure(
sourceFile,
enumDeclaration.getStart(),
Expand Down

0 comments on commit 1637c8d

Please sign in to comment.