Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enum): Add no-enum and no-const-enum rules.
- Loading branch information
Showing
8 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const enum Foo { FOO = "F" }; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-const-enum] | ||
enum Bar { BAR = "B" }; | ||
|
||
[no-const-enum]: const enum is forbidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"throw-error": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../build/rules" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"no-const-enum": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../build/rules" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const enum Foo { FOO = "F" }; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [no-enum] | ||
enum Bar { BAR = "B" }; | ||
~~~~~~~~~~~~~~~~~~~~~~ [no-enum] | ||
|
||
[no-enum]: enum is forbidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"throw-error": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../build/rules" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"jsRules": {}, | ||
"rules": { | ||
"no-enum": { "severity": "error" } | ||
}, | ||
"rulesDirectory": "../../../build/rules" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |