-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A better eslint rules config #16118
base: develop
Are you sure you want to change the base?
A better eslint rules config #16118
Changes from 24 commits
8e9b627
8f00748
19062a9
bb4f9a7
f3ace46
82990e7
733ffbe
98ccad5
8588453
8c9d6c2
4de06af
95d1189
98f8db7
ef7eec4
10a90ba
f1505bf
c0b808d
9d8771d
61499c6
dbf5ccd
eecf7d6
e5931f8
c5aafa7
e38fab6
236490a
2755afb
2149fd4
8f679b8
e198953
7a08d6f
fc77ff6
2c1a46f
689a260
c514ea6
d4caf75
2caf27c
858fd37
2365ce0
f5f9fe5
50fcc3e
366e8ea
ddaaa5e
31ce9b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ extends: | |
- plugin:@typescript-eslint/recommended | ||
- plugin:@typescript-eslint/recommended-requiring-type-checking | ||
|
||
plugins: ["@typescript-eslint"] | ||
plugins: ['@typescript-eslint'] | ||
|
||
settings: | ||
import/resolver: | ||
|
@@ -48,9 +48,8 @@ rules: | |
##### RECOMMENDED RULE OVERRIDES ##### | ||
|
||
camelcase: off # underscores may come in handy in some cases | ||
eqeqeq: warn # important check missing from recommended | ||
|
||
keyword-spacing: warn # we require this explicitly | ||
no-multi-spaces: off # useful for manually align some expression across lines | ||
prefer-rest-params: off # we need ES5 to be fast too | ||
prefer-spread: off # we need ES5 to be fast too | ||
space-before-function-paren: [warn, always] # we require this explicitly | ||
|
@@ -72,22 +71,23 @@ rules: | |
import/no-unresolved: off # TODO: fix internal modules | ||
import/prefer-default-export: off # prefer named exports | ||
indent: [error, 4, { | ||
SwitchCase: 0 | ||
}] | ||
'@typescript-eslint/indent': [error, 4, { | ||
SwitchCase: 0, | ||
ignoredNodes: [ # https://stackoverflow.com/a/72897089 | ||
"FunctionExpression > .params[decorators.length > 0]", | ||
"FunctionExpression > .params > :matches(Decorator, :not(:first-child))", | ||
"ClassBody.body > PropertyDefinition[decorators.length > 0] > .key", | ||
'FunctionExpression > .params[decorators.length > 0]', | ||
'FunctionExpression > .params > :matches(Decorator, :not(:first-child))', | ||
'ClassBody.body > PropertyDefinition[decorators.length > 0] > .key', | ||
] | ||
}] | ||
|
||
lines-between-class-members: off # be more lenient on member declarations | ||
max-classes-per-file: off # helper classes are common | ||
max-len: [warn, 150] # more lenient on max length per line | ||
no-console: error # prefer the uniform logging methods | ||
|
||
no-plusplus: off # allow increment/decrement operators | ||
no-continue: off # allow unlabeled continues | ||
no-mixed-operators: off # this is just cumbersome | ||
no-multi-assign: off # it is handy sometimes | ||
|
||
no-nested-ternary: off # it is handy sometimes | ||
no-param-reassign: off # the output object is passed as parameters all the time | ||
no-restricted-syntax: off # for-in is a efficient choice for plain objects | ||
|
@@ -112,7 +112,6 @@ rules: | |
'ts-nocheck': true, | ||
'ts-check': false, | ||
}] | ||
'@typescript-eslint/no-unused-expressions': warn | ||
|
||
# TODO: this is just too much work | ||
'@typescript-eslint/explicit-module-boundary-types': off | ||
|
@@ -148,8 +147,6 @@ rules: | |
allowBoolean: true | ||
}] | ||
|
||
'@typescript-eslint/type-annotation-spacing': warn | ||
|
||
# We choose to use style `... as foo` since it's more common. | ||
# In the other hand, angle bracket style can be ambiguous with j/tsx syntax. | ||
# For example, https://babeljs.io/docs/en/babel-plugin-transform-typescript#istsx | ||
|
@@ -163,3 +160,100 @@ rules: | |
'@typescript-eslint/explicit-function-return-type': [error, { | ||
allowIIFEs: true, # IIFEs are widely used, writing their signature twice is painful | ||
}] | ||
|
||
|
||
# constructor-super: error | ||
# curly: [error, all] | ||
# no-debugger: error | ||
# no-duplicate-case: error | ||
# no-eval: error | ||
# no-sparse-arrays: error | ||
# no-trailing-spaces: error | ||
# no-var: error | ||
# prefer-const: error | ||
# prefer-numeric-literals: off | ||
|
||
prefer-template: off | ||
'@typescript-eslint/no-unsafe-argument': warn | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think marking it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为按照目前的cocos代码, 如果是 error, 那么会大量标红.如果cocos愿意大规模重构代码, error当然是更好的 |
||
|
||
max-len: [warn, 250] # more lenient on max length per line | ||
no-console: warn # prefer the uniform logging methods | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code in engine should not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 理论上是这样, 但是 |
||
no-multi-spaces: [warn, { | ||
'ignoreEOLComments': true, | ||
'exceptions': { | ||
'Property': true, | ||
'VariableDeclarator': false | ||
} | ||
}] | ||
no-multiple-empty-lines: [warn, {max: 2, maxEOF: 1}] | ||
no-multi-assign: [error, { 'ignoreNonDeclaration': true }] | ||
max-statements-per-line: [error, { max: 1 }] | ||
|
||
comma-dangle: [error, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you choose to disallow trailing commas? I think it'd better to allow. For example, if you had the following array: const array = [
a,
b
]; In this time, you want to append a new item, the diff might be look like: - b
+b,
+c In compare with the allowing case, the diff would be: b,
+c, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the config. I think you are right. |
||
'arrays': always-multiline, | ||
'objects': always-multiline, | ||
'imports': never, | ||
'exports': never, | ||
'functions': never | ||
}] | ||
quote-props: [error, consistent-as-needed] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This config will make const object1 = {
foo: 'bar',
baz: 42,
'qux-lorem': true, // string with `-` could not be a key without quote.
}; to be changed to const object1 = {
'foo': 'bar', // quote ' was added for `foo` property
'baz': 42, // quote ' was added for `baz` property
'qux-lorem': true,
}; So I think keep the default behavior is better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
function-paren-newline: off | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its default value is If we disable this config, the following code function foo (
bar, baz
) {}
const foo1 = function (
bar, baz
) {};
const foo2 = (
bar, baz
) => {}; will function foo (bar, baz) {}
const foo1 = function (bar, baz) {};
const foo2 = (bar, baz) => {}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 单行 function 就和 单行 if 单行 for 一样, 除了 "节省行数" 并没有任何其他好处. 反而破坏代码的一致性. 而且会造成一定的阅读困难. 我个人完全无法接受 单行函数, 单行if 单行for . |
||
space-unary-ops: error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rule There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 实际测试下来 , space-unary-ops 并不是error, 可能是被继承的配置篡改了.
其实我建议不要继承这些所谓的 "已有的最佳实践", 这样的话 eslint的配置就是黑盒的了. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with this. |
||
key-spacing: [error, { | ||
'singleLine': { | ||
'beforeColon': false, | ||
'afterColon': true | ||
}, | ||
'multiLine': { | ||
'beforeColon': false, | ||
'afterColon': true, | ||
'align': colon | ||
} | ||
}] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rule is not good for our code. If we add a longer property in object, all previous properties will change. const obj = {
hello: 'world',
a : 'bbb',
}; If we want to add a new property called const obj = {
hello : 'world', // code changed
a : 'bbb', // code changed
foooooooo: 'ahaha',
}; We just added a new property, but the whole object definition code were changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗯 这个我可以理解.按你说的问题不大. |
||
|
||
func-names: off | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 打开这个开关, 对于 |
||
default-param-last: off | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://eslint.org/docs/latest/rules/default-param-last It's better to put default argument to the last. Why should we disable this check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在实际开发中我经常有类似: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. |
||
one-var: [error, never] | ||
eqeqeq: [error, always, {'null': ignore}] | ||
arrow-body-style: [error, as-needed] | ||
arrow-parens: [error, always] | ||
brace-style: [error, 1tbs] | ||
id-denylist: [error, any, unknown, Number, number, Boolean, boolean, String, Undefined] | ||
id-match: off | ||
no-cond-assign: off | ||
no-constant-condition: off | ||
nonblock-statement-body-position: [error, below] | ||
padded-blocks: [error, { | ||
'classes': always | ||
}] | ||
|
||
'@typescript-eslint/no-this-alias': [off] | ||
'@typescript-eslint/no-extra-parens': [error, all, { | ||
'conditionalAssign': false, | ||
'returnAssign': false, | ||
'nestedBinaryExpressions': false, | ||
'enforceForArrowConditionals': false, | ||
'enforceForFunctionPrototypeMethods': false, | ||
'ignoreJSX': all | ||
}] | ||
'@typescript-eslint/no-unused-expressions': [error, { | ||
'allowShortCircuit': true, | ||
'allowTernary': true, | ||
'allowTaggedTemplates': false | ||
}] | ||
'@typescript-eslint/no-duplicate-type-constituents': [error, { | ||
'ignoreIntersections': false, | ||
'ignoreUnions': true | ||
}] | ||
'@typescript-eslint/keyword-spacing': [error] | ||
'@typescript-eslint/space-infix-ops': [error] | ||
'@typescript-eslint/type-annotation-spacing': [error, { | ||
'before': false, | ||
'after': true, | ||
'overrides' : { | ||
'arrow': { | ||
'before': true, | ||
'after': true, | ||
} | ||
} | ||
}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why to disable
prefer-template
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
字符串模板性能更低, 开启该功能会强制使用字符串模板, 即使简单的一次拼接(如
"Hello " + user.name
) 也会被转换成'Hello ${user.name}'
. 得不偿失.