diff options
author | Fabio Berger <me@fabioberger.com> | 2019-01-15 22:10:50 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2019-01-15 22:10:50 +0800 |
commit | ae147e615af9da32ca3da0b92ef77815061ad5be (patch) | |
tree | d7bae435a99c668d02cc9ee9d84619c3056509f6 /packages/tslint-config/rules/enumNamingRule.ts | |
parent | 98579300c1f78d8c360fa960cc73ffacb1012e91 (diff) | |
parent | 18084588ea9fa724d6e32c9a49c79d49f189ba7c (diff) | |
download | dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar.gz dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar.bz2 dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar.lz dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar.xz dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.tar.zst dexon-sol-tools-ae147e615af9da32ca3da0b92ef77815061ad5be.zip |
Merge branch 'development' into fix/dev-tools-pages/finalTouches
* development: (87 commits)
Update packages/sol-tracing-utils/src/trace_collection_subprovider.ts
Make mapping namings direct
Remove unused tslint disable
Revert "Remove logAsyncErrors hack"
Remove logAsyncErrors hack
Refactor logAsyncErrors to follow our conventions
Export Sources and SourceCodes out of tracing utils
Replace console.log with logUtils.log (#1515)
strict decoding of return values using generics
makerAssetFillAmount -> takerAssetFillAmount
Ran prettier
Linter
Fix build after rebase
Style cleanup for Compressed Calldata in Contract Wrappers PR
Use simpler `_.find` to locate fillOrderBai
Updated dutch auction wrapper
Added back abi-gen-wrappers
Renamed signatureParser.ts to signature_parser.ts
Renamed decode rule `structsAsObjects` to `shouldConvertStructsToObjects`
circle build failed. New commit to resubmit job.
...
Diffstat (limited to 'packages/tslint-config/rules/enumNamingRule.ts')
-rw-r--r-- | packages/tslint-config/rules/enumNamingRule.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/packages/tslint-config/rules/enumNamingRule.ts b/packages/tslint-config/rules/enumNamingRule.ts new file mode 100644 index 000000000..56499618f --- /dev/null +++ b/packages/tslint-config/rules/enumNamingRule.ts @@ -0,0 +1,60 @@ +import * as Lint from 'tslint'; +import * as ts from 'typescript'; + +export class Rule extends Lint.Rules.AbstractRule { + public static FAILURE_STRING = `Enum member names should be PascalCase`; + + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + return this.applyWithFunction(sourceFile, walk); + } +} + +function walk(ctx: Lint.WalkContext<void>): void { + // Recursively walk the AST starting with root node, `ctx.sourceFile`. + // Call the function `cb` (defined below) for each child. + return ts.forEachChild(ctx.sourceFile, cb); + + function cb(node: ts.Node): void { + if (node.kind === ts.SyntaxKind.EnumMember) { + const keyNode = node.getFirstToken(ctx.sourceFile); + if (keyNode !== undefined) { + const keyText = keyNode.getText(ctx.sourceFile); + if (!isPascalCase(keyText)) { + return ctx.addFailureAtNode(node, Rule.FAILURE_STRING, getFix(keyText, node)); + } + } + } + // Continue recursion into the AST by calling function `cb` for every child of the current node. + return ts.forEachChild(node, cb); + } + + function getFix(text: string, node: ts.Node): Lint.Replacement { + let fix = toPascalCase(text); + // check for `member = value` + if (node.getChildCount(ctx.sourceFile) === 3) { + const value = node.getLastToken(ctx.sourceFile); + if (value !== undefined) { + fix += ` = ${value.getText(ctx.sourceFile)}`; + } + } + return new Lint.Replacement(node.getStart(ctx.sourceFile), node.getWidth(ctx.sourceFile), fix); + } +} + +// Modified from: https://github.com/jonschlinkert/pascalcase/ +function toPascalCase(str: string): string { + let result = str.replace(/([a-z0-9\W])([A-Z])/g, '$1 $2'); + if (result.length === 1) { + return result.toUpperCase(); + } + result = result.replace(/^[\W_\.]+|[\W_\.]+$/g, '').toLowerCase(); + result = result.charAt(0).toUpperCase() + result.slice(1); + return result.replace(/[\W_\.]+(\w|$)/g, (_, ch) => { + return ch.toUpperCase(); + }); +} +function isPascalCase(s: string): boolean { + const regex = /^([A-Z0-9]+[a-z0-9]+)+$/g; + const key = s.split('=')[0].trim(); + return regex.test(key); +} |