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
style: add additional style rules & apply fixes #75
Conversation
This rule ensures ```js const obj = { foo: 'bar', baz, } ``` Instead of allowing ```js const obj = { foo: 'bar', baz: baz, } ```
974be28
to
bb23fcc
Compare
This rule ensures: ```js const foo = { bar: 'baz' }; ``` Instead of allowing: ```js const foo = { bar:'baz' }; ```
bb23fcc
to
bdb34ec
Compare
Ensures a space exists before the start of a block that is not on a new line. [Documentation](https://typescript-eslint.io/rules/space-before-blocks/) This rule ensures: ```js if (foo) { // } ``` Instead of allowing: ```js if (foo){ // } ```
Ensures a space exists before and after the `=>` in an arrow function. [Documentation](https://eslint.org/docs/latest/rules/arrow-spacing) This rule ensures: ```js () => { // }; ``` Instead of allowing: ```js ()=>{ // }; ```
Ensures parenthesis around arrow function parameters are only used when required. [Documentation](https://eslint.org/docs/latest/rules/arrow-parens) This rule ensures: ```js foo => { // }; (foo, bar) => { // }; ``` Instead of allowing: ```js (foo) => { // }; (foo, bar) => { // }; ```
Ensures 1tbs (one true brace style), where braces must be on the same line as the keywords they're associated with. [Documentation](https://typescript-eslint.io/rules/brace-style/) This rule ensures: ```js if (foo) { // } else { // } try { // } catch(error) { // } ``` Instead of allowing: ```js if (foo) { // } else { // } try { // } catch(error) { // } ```
bdb34ec
to
4b009df
Compare
Ensures semicolon delimiters between members of interface and type literals. [Documentation](https://typescript-eslint.io/rules/member-delimiter-style) This rule ensures: ```js interface Foo { name: string; greet(): string; } type Bar = { name: string } type FooBar = { name: string; greet(): string } ``` Instead of allowing: ```js interface Foo { name: string greet(): string } interface Foo { name: string, greet(): string, } interface Foo { name: string; greet(): string } type FooBar = { name: string, greet(): string } ```
Ensures array brackets are either both on new lines or are both on the same line. [Documentation](https://eslint.org/docs/latest/rules/array-bracket-newline) This rule ensures: ```js const foo = []; const foo = [ 1, 2, 3 ]; const foo = [ 1, 2, 3, ]; const foo = [{ bar: baz }]; const foo = [ { bar: baz } ]; ``` Instead of allowing: ```js const foo = [ ]; const foo = [ 1, 2, 3 ]; const foo = [ 1, 2, 3]; const foo = [{ bar: baz } ]; const foo = [ { bar: baz }]; ```
Ensures object braces are either both on new lines or are both on the same line. [Documentation](https://eslint.org/docs/latest/rules/object-curly-newline) This rule ensures: ```js const foo = {}; const foo = { bar: baz }; const foo = { bar: baz, }; const { bar } = obj; const { bar } = obj; ``` Instead of allowing: ```js const foo = { }; const foo = { bar: baz }; const foo = { bar: baz}; const { bar } = obj; ```
Ensures there is not a new line before or after the body of a block. [Documentation](https://eslint.org/docs/latest/rules/padded-blocks) This rule ensures: ```js if (foo) { // } ``` Instead of allowing: ```js if (foo) { // } if (foo) { // } if (foo) { // } ```
Ensures all exports are declared at the bottom of the file. [Documentation](https://github.com/import-js/eslint-plugin-import/blob/5680a1f8d41cd19f9c60d999a6fadf10994a0a64/docs/rules/exports-last.md) This rule ensures: ```js const str = 'foo'; const bool = true; export { str, bool, }; ``` ```js const str = 'foo'; export const bool = true; ``` Instead of allowing: ```js export const bool = true; const str = 'foo'; ```
Ensures consistent indentation. [Documentation](https://typescript-eslint.io/rules/indent) !! This has [known issues](typescript-eslint/typescript-eslint#1824), revert to ESLint's version if necessary. This rule ensures: ```js function foo() { if (bar) { console.log('baz'); } else { console.log('qux'); } } switch(a){ case "a": break; case "b": break; } ``` Instead of allowing: ```js function foo() { if (bar) { console.log('baz'); } else { console.log('qux'); } } switch(a){ case "a": break; case "b": break; } ```
We're already using `@typescript-eslint/no-unused-vars`, the [docs](https://typescript-eslint.io/rules/no-unused-vars) state to explicitly disable `no-unused-vars` to avoid conflicts.
Ensures semicolons are added after each statement, including TypeScript statements. [Documentation](https://typescript-eslint.io/rules/semi) This rule ensures: ```js type ClientUser = Pick<User, 'id' | 'discord_user_id'>; const foo = 'bar'; object.foo = function() { // }; class Foo { bar = 1; } ``` Instead of allowing: ```js type ClientUser = Pick<User, 'id' | 'discord_user_id'> const foo = 'bar' object.foo = function() { // } class Foo { bar = 1 } ```
Ensures there is an empty line after directives. [Documentation](https://eslint.org/docs/latest/rules/padding-line-between-statements) This rule ensures: ```js 'use strict'; const foo = 'bar'; ``` Instead of allowing: ```js 'use strict'; const foo = 'bar'; ```
Phew! That was a lot... hopefully these changes will catch most of the nits and allow us to focus more on implementations. 👍🏼 There was one rule that eluded me... Exampleconst user = await User.create(userObject); // Local variable `user` is redundant.
return user; Which implies that it can be simplified to: return await User.create(userObject); |
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.
LGTM
I like all the rules but have 2 comments
Is this a normal warning? I deleted the node_modules folder and did a clean install but the warning is still there.
Definition for rule '@typescript-eslint/key-spacing' was not found.eslint(@typescript-eslint/key-spacing)
And the rule still works, pasted in the example
2nd comment
This warning about using Eslint as a formatter is on the typescript-eslint docs for a lot of the rules, is there a reason we want to use ESlint for this instead of a formatter? I have no strong opinion either way, just for my own knowledge wanted to know the why
@MainlyColors, Prettier is currently the most popular formatter. Unfortunately, it's very opinionated and not very customizable. Until it becomes more customizable, another formatter arises, or we decide to adopt their opinionated coding style; ESLint is still the best option, despite their counter recommendations. |
in const foo = {
}; Valid, but the one below invalid? const foo = { bar: baz
}; |
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.
Left a question, otherwise LGTM.
@Caleb-Cohen Ironically, if you look at the message on the commit, I had that case listed as invalid because I also misinterpreted it initially. Reasoning: It's checking if the braces are consistent. In the first example, both braces are in fact on their own line. In the second example, the closing brace is sharing the line with its contents. It would need a different rule if we wanted to force empty objects to be on a single line, though I don't know of one that exists. |
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.
Tested, LGTM!
Ensures spacing around keywords such as `if`, `else`, `function`, `try`, `catch`, etc. [Documentation](https://typescript-eslint.io/rules/keyword-spacing) This rule ensures: ```js if (foo) { //... } else if (bar) { //... } else { //... } ``` Instead of allowing: ```js if (foo) { //... }else if (bar) { //... }else { //... } ```
Ensures spacing around operators such as `+`, `-`, `?`, `:`, `=`, etc. [Documentation](https://typescript-eslint.io/rules/space-infix-ops) This rule ensures: ```js a + b a + b a ? b : c // `object-curly-spacing` would ensure spacing inside `{}` var {a = 0} = bar; // `key-spacing` would ensure a space after `:` const a = {b:1}; function foo(a = 0) {} ``` Instead of allowing: ```js a+b a+ b a +b a?b:c var {a=0}=bar; const a={b:1}; function foo(a=0) { } ```
Added two more rules, which should be the last of them. Unless there are any objections, I'll merge in the evening. 👍🏼 |
Closes N/A
Description
Add additional style rules, bump
@typescript-eslint-plugin
and@typescript-eslint-parser
versions, & apply style fixes.Add
object-shorthand
[Changes] (Docs)Ensures ES6 object literal shorthand is used.
Examples
Valid syntax:
Invalid syntax:
Add
@typescript-eslint/key-spacing
[Change 1, Change 2] (Docs)Ensures a space follows the colon in object literals.
Examples
Valid syntax:
Invalid syntax:
Add
@typescript-eslint/space-before-blocks
[Changes] (Docs)Ensures a space exists before the start of a block that is not on a new line.
Examples
Valid syntax:
Invalid syntax:
Add
arrow-spacing
[Changes] (Docs)Ensures a space exists before and after the
=>
in an arrow function.Examples
Valid syntax:
Invalid syntax:
Add
arrow-parens
[Changes] (Docs)Ensures parenthesis around arrow function parameters are only used when required.
Examples
Valid syntax:
Invalid syntax:
Add
@typescript-eslint/brace-style
[Changes] (Docs)Ensures 1tbs (one true brace style), where braces must be on the same line as the keywords they're associated with.
Examples
Valid syntax:
Invalid syntax:
Add
@typescript-eslint/member-delimiter-style
[Changes] (Docs)Ensures semicolon delimiters between members of interface and type literals.
Examples
Valid syntax:
Invalid syntax:
Add
array-bracket-newline
[Changes] (Docs)Ensures array brackets are either both on new lines or are both on the same line.
Examples
Valid syntax:
Invalid syntax:
Add
object-curly-newline
[Changes] (Docs)Ensures object braces are either both on new lines or are both on the same line.
Examples
Valid syntax:
Invalid syntax:
Add
padded-blocks
[Changes] (Docs)Ensures there is not a new line before or after the body of a block.
Examples
Valid syntax:
Invalid syntax:
Add
eslint-plugin-import/exports-last
[Changes] (Docs)Ensures all exports are declared at the bottom of the file.
Examples
Valid syntax:
Invalid syntax:
Switch from ESLint's
indent
to@typescript-eslint/indent
[Changes] (Docs)Ensures consistent indentation.
Examples
Valid syntax:
Invalid syntax:
Add
'no-unused-vars': 'off'
as per ts-eslint docs [Changes]Switch from ESLint's
semi
to@typescript-eslint/semi
[Changes] (Docs)Ensures semicolons are added after each statement, including TypeScript statements.
Examples
Valid syntax:
Invalid syntax:
Add
padding-line-between-statements
rule for directives [Changes] (Docs)Ensures there is an empty line after directives.
Examples
Valid syntax:
Invalid syntax:
Removed
server/migrations
fromignorePatterns
andoverrides
, and applied style fixes. [Changes]Switch from ESLint's
object-curly-spacing
to@typescript-eslint/object-curly-spacing
& apply fixes [Changes] (Docs)Switch from ESLint's
comma-dangle
to@typescript-eslint/comma-dangle
[Changes] (Docs)Switch from ESLint's
padding-line-between-statements
to@typescript-eslint/padding-line-between-statements
[Changes] (Docs)Testing
Run
npm run lint && npm run test:server
to ensure nothing is broken.Optional: Copy incorrect code examples from the description and check that ESLint catches them.
Type of change
Screenshots
N/A
Checklist: