Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
feat(rules): add tslint-consistent-codestyle rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Jul 16, 2017
1 parent 069d870 commit b6218c1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -15,6 +15,7 @@
"build": "tsc -p ./tsconfig.build.json"
},
"dependencies": {
"tslint-consistent-codestyle": "^1.5.1",
"tslint-eslint-rules": "^4.1.0"
},
"devDependencies": {
Expand Down
48 changes: 37 additions & 11 deletions src/index.ts
@@ -1,5 +1,5 @@
export = {
extends: ['tslint-eslint-rules'],
extends: ['tslint-eslint-rules', 'tslint-consistent-codestyle'],
rules: {
// tslint
'adjacent-overload-signatures': true,
Expand All @@ -18,7 +18,7 @@ export = {
],
'binary-expression-operand-order': true,
'callable-types': true,
'class-name': true,
'class-name': false, // tslint-consistent-codestyle: naming-convention
'comment-format': [true, 'check-space'],
'completed-docs': false,
curly: true,
Expand All @@ -31,12 +31,12 @@ export = {
'import-blacklist': false,
'import-spacing': false, // conflict with prettier
indent: false, // conflict with prettier
'interface-name': false,
'interface-name': false, // tslint-consistent-codestyle: naming-convention
'interface-over-type-literal': true,
'jsdoc-format': true,
'label-position': true,
'linebreak-style': false, // conflict with prettier
'match-default-export-name': true,
'match-default-export-name': false,
'max-classes-per-file': [true, 1],
'max-file-line-count': [true, 300],
'max-line-length': false, // conflict with prettier
Expand Down Expand Up @@ -164,13 +164,7 @@ export = {
'unified-signatures': true,
'use-default-type-parameter': true,
'use-isnan': true,
'variable-name': [
true,
'ban-keywords',
'check-format',
'allow-snake-case',
'allow-leading-underscore',
],
'variable-name': [true, 'ban-keywords'], // tslint-consistent-codestyle: naming-convention
whitespace: false, // conflict with prettier

// tslint-eslint-rules
Expand Down Expand Up @@ -208,5 +202,37 @@ export = {
'ter-prefer-arrow-callback': false, // tslint: only-arrow-functions
'valid-jsdoc': false,
'valid-typeof': false, // tslint: typeof-compare

// tslint-consistent-codestyle
'early-exit': [true],
'ext-curly': false,
'naming-convention': [
true,
{
type: 'default',
format: 'snake_case',
leadingUnderscore: 'forbid',
trailingUnderscore: 'forbid',
},
{ type: 'parameter', leadingUnderscore: 'allow', final: true },
{ type: 'variable', modifiers: 'rename', leadingUnderscore: 'allow' },
{ type: 'member', modifiers: 'private', leadingUnderscore: 'require' },
{ type: 'member', modifiers: 'protected', leadingUnderscore: 'require' },
{ type: 'type', format: 'PascalCase' },
{ type: 'enumMember', format: 'PascalCase' },
],
'no-as-type-assertion': false,
'no-collapsible-if': true,
'no-else-after-return': true,
'no-return-undefined': [true, 'allow-void-expression'],
'no-static-this': false,
'no-unnecessary-else': true,
'no-unused': [true, 'ignore-imports', 'unused-function-expression-name', 'unused-class-expression-name'],
'no-var-before-return': true,
'object-shorthand-properties-first': false,
'oddness-check': false,
'parameter-properties': false,
'prefer-const-enum': true,
'prefer-while': true,
},
};
80 changes: 80 additions & 0 deletions tests/test.ts
@@ -0,0 +1,80 @@
export {};
// tslint:disable:no-unused-variable no-unused

declare function do_something(): void;
declare const condition: boolean;
declare const foo: boolean;
declare const bar: boolean;

// early-exit
(() => {
if (condition) {
do_something();
}
do_something();
do_something();
do_something();
})();

// naming-convention & ordered-imports
class MyClass {
public static my_static_public_property: any;
protected static _my_static_protected_property: any;
private static _my_static_private_property: any;
public my_public_property: any;
protected _my_protected_property: any;
private _my_private_property: any;
constructor() { /* ... */ }
public static my_static_public_method(): void { /* ... */ }
protected static _my_static_protected_method(): void { /* ... */ }
private static _my_static_private_method(): void { /* ... */ }
public my_public_method(): this { return this; }
protected _my_protected_method(): this { return this; }
private _my_private_method(): this { return this; }
}
type MyType = any;
interface MyInterface { my_property: any }
const my_const = 0;
function my_function(param_a: any, _param_b: any): void { /* ... */ }

// tslint:disable-next-line:no-use-before-declare https://github.com/palantir/tslint/issues/2551
const {a: _a, ...b_and_c} = {a: 1, b: 2, c: 3};

// no-collapsible-if
if (foo && bar) { do_something(); }
if (foo && bar) {
do_something();
}
if (foo) {
do_something();
} else if (bar) {
do_something();
} else {
do_something();
}
if (foo) {
if (bar) {
do_something();
} else {
do_something();
}
}
if (foo) {
if (bar) {
do_something();
}
} else {
do_something();
}

// no-return-undefined
let var_for_return_void;
function return_void(): void {
return void (var_for_return_void = 1);
}

// prefer-const-enum
const enum MyEnum {
Enum1,
Enum2,
}
13 changes: 13 additions & 0 deletions yarn.lock
Expand Up @@ -178,6 +178,13 @@ tslint-config-prettier@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.1.0.tgz#40c026a56e4da27063b3e9bcd71f4f8109fee369"

tslint-consistent-codestyle@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/tslint-consistent-codestyle/-/tslint-consistent-codestyle-1.5.1.tgz#11b6ae6af5a53cb6bd0351d9b6637eabc76d9b7d"
dependencies:
tslib "^1.7.1"
tsutils "^2.5.0"

tslint-eslint-rules@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-4.1.0.tgz#8fee295c42f4c8078139deb16ee7b4510fd516b7"
Expand Down Expand Up @@ -205,6 +212,12 @@ tsutils@^1.4.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.6.0.tgz#1fd7fac2a61369ed99cd3997f0fbb437128850f2"

tsutils@^2.5.0:
version "2.7.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.7.1.tgz#411a0e9466525a2b2869260a55620d7292155e24"
dependencies:
tslib "^1.7.1"

tsutils@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.5.1.tgz#c2001390c79eec1a5ccfa7ac12d599639683e0cf"
Expand Down

0 comments on commit b6218c1

Please sign in to comment.