Skip to content
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

chore: enable more ESLint rules and fix existing violations #47

Merged
merged 9 commits into from
May 29, 2023

Conversation

danvk
Copy link
Collaborator

@danvk danvk commented May 25, 2023

See #18, follow-on to #24

These are based on rules I've used on projects in the past. It's a mix of style conventions (curly), nudges towards good modern JS style (prefer-destructuring, prefer-optional-chain), pushes towards better TypeScript types and some rules around using Promises correctly. I've also included a rule to sort JSX props which I've found very convenient.

Most of these are auto-fixable.

Copy link
Collaborator Author

@danvk danvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional rules for consideration.

.eslintrc.cjs Show resolved Hide resolved
src/App.tsx Outdated
@@ -27,13 +27,13 @@ function App() {

return (
<PanelGroup autoSaveId="refstudio" direction="horizontal">
<Panel defaultSize={20} collapsible className="overflow-scroll p-4">
<Panel className="overflow-scroll p-4" collapsible defaultSize={20}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change comes from react/jsx-sort-props.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is auto-sortable on save it's ok for me.

@@ -1,16 +1,16 @@
.collapsible-block {
/* @apply debug; */
@apply flex my-2 p-2 pl-0 rounded-lg;
@apply my-2 flex rounded-lg p-2 pl-0;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some diff noise here from running yarn fmt

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow the auto-sort for did not pick up css files.

'Mod-Enter': () => {
return this.editor
'Mod-Enter': () =>
this.editor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the arrow-body-style rule.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you prefer block body for arrow functions when it has a line break? I find it easier to read, and I always think about a well-known security issue a few years ago because an if/else didn't have curly braces and someone added another instruction (so, another line) which would run every time (regardless of the condition)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind this, actually! The if/else continuation line issue is very real, which is why I think curly is a good idea. But since the thing on the RHS of => is an expression, not a statement, I think there's less room for trouble. In most cases if you added an extra statement after an arrow function with a line break, it would be a syntax error or create dead code (which TypeScript would complain about).

}, [editorContent, onSelectionChange]);

useEffect(() => {
if (!editor) return;
if (!editor) {
return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the curly rule in action.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like to this type of "fast-return" conditions being a single line so that we can read the "pre-conditions" at the top without any line breaks.

Sometimes you have 2 or 3 (unrelated) conditions you need to check (ex: with some ! expressions). IMO it's a good practice to keep them different statements (ex: for diff and legibility).

With this rule we would have 3 rules x 3 lines = 9 lines of code instead of only 3 like the example below.

useEffect(() => {
    if (!editor) {
      return
    }
    if (draft) {
      return
    }
    if(anotherRule & anotherRule.field) {
      return
   }
   // ...
 }, [...])

vs

useEffect(() => {
    if (!editor) return
    if (draft) return
    if(anotherRule & anotherRule.field) return
    // ...
 }, [...])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you guys would like to set curly: multi-line (https://eslint.org/docs/latest/rules/curly#multi-line) that's fine by me. My preference would be slightly more strict than that rule, though. If you have an else or an else if, you should be using braces. This is definitely a slippery slope, see @sergioramos's comment above about possible security issues.

if (fileA.children === null) return -1;
if (fileB.children === null) return 1;
return fileA.name?.localeCompare(fileB.name || '') || 0;
if (fileA.children === undefined) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is @typescript-eslint/no-unnecessary-condition and seems to be a real issue (the comparison should be against undefined, not null).

@@ -17,22 +17,32 @@ export function CenterPaneView({ file, ...props }: CenterPaneViewProps) {
if (file && isTipTap(file)) {
setLoading(true);
(async () => {
const content = await readFile(file);
const textContent = new TextDecoder('utf-8').decode(content);
const newBytes = await readFile(file);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was no-shadow.

onClick?.(selected);
}
})
.catch((e) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danvk danvk requested a review from cguedes May 25, 2023 19:35
cguedes
cguedes previously approved these changes May 26, 2023
@cguedes cguedes requested a review from sergioramos May 29, 2023 09:34
@sergioramos sergioramos changed the title Enable several more eslint rules and fix existing violations chore: enable more ESLint rules and fix existing violations May 29, 2023
@sergioramos sergioramos merged commit b3307ea into main May 29, 2023
@sergioramos sergioramos deleted the eslintrc branch May 29, 2023 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants