Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Add basic eslint recommendations to core/ #3077

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

edmundito
Copy link
Contributor

Change description

Based on the issue in #3070 that could have been prevented with linting, this adds the most basic eslint rules to core and fixes a number of existing issues. The rules are not as strict as our plan because there's currently a lot to fix, and the plan is to incrementally enable rules and fix issues as we find the time to do it.

Checklists

Development

  • Application changes have been tested appropriately

Impact

  • Code follows company security practices and guidelines
  • Security impact of change has been considered
  • Performance impact of change has been considered
  • Possible migration needs considered (model migrations, config migrations, etc.)

Please explain any security, performance, migration, or other impacts if relevant:

Code review

  • Pull request has a descriptive title and context useful to a reviewer. Screenshots or screencasts are attached where applicable.
  • Relevant tags have been added to the PR (bug, enhancement, internal, etc.)

@edmundito edmundito added the internal chores and housekeeping label Mar 3, 2022
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Notice here that I'm using @typescript-eslint/eslint-recommended instead of @typescript-eslint/recommended. This extension only makes sure that typescript plays nicely with eslint:recommended but does not enable the typescript recommended rules. Currently, there are a gargantuan number of problems with core so doing it all once is a herculean effort. At the very least we're getting base safety.

globals: {
fetch: true, // For isomorphic-fetch
},
ignorePatterns: ["__tests__", "dist", "public"],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lots of problems in __tests__ to fix, so it's being ignored for this PR.

Comment on lines +14 to +16
} catch {
// Ignore error
}
Copy link
Contributor Author

@edmundito edmundito Mar 3, 2022

Choose a reason for hiding this comment

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

In the recommendations we get the no-empty rule that discourages empty blocks. The easiest way to fix it is to put a comment.

@@ -74,7 +74,7 @@ export namespace CloudCLI {
);

let lastState: string;
while (true) {
for (;;) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should disable that rule? We could rewrite this function to not need an infinite loop but I'm not it's worth the refactor time.

Copy link
Member

@evantahler evantahler left a comment

Choose a reason for hiding this comment

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

Nice!

these look to me mostly stylistic changes… did you see any logical problems?

@@ -20,7 +20,7 @@ export async function loadSchedule(
): Promise<IdsByClass> {
let isNew = false;

if (configObject.hasOwnProperty("confirmProfiles")) {
if (Object.prototype.hasOwnProperty.call(configObject, "confirmProfiles")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -260,6 +258,7 @@ export namespace RecordOps {
recordProperties: {
[key: string]: (string | number | boolean | Date)[] | any;
}[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLock = true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This argument is not really being used right now, so I'm not sure whether to remove it and update all the instances that use it or leave it.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there's enough of these I'd say icebox a ticket to remove.

Comment on lines -159 to -160
} catch (error) {
throw error;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unnecessary catches because they only rethrow the error

@@ -28,6 +28,7 @@ export namespace Reset {
* * clears the redis cache
* * clears resque
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left these here because the test is definitely passing a callerId. But callerId is not used in any of these functions. I could update the usage of these so that callerId is no longer used and leave it for some future use.

Copy link
Contributor

Choose a reason for hiding this comment

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

🤷 Probably not worth removing

@@ -81,6 +81,7 @@ export class StatusTask extends CLSTask {
if (!toStop) return false;

// do not await so the promise can end so the server can shut down
// eslint-disable-next-line no-async-promise-executor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It really does not like returning a promise with an async call. Looks like this is intentional but could be dangerous otherwise. Doc explains why:

no-async-promise-executor

Copy link
Contributor

Choose a reason for hiding this comment

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

The proimise isn't returned. Does:

await api.process.stop();
process.nextTick(() => process.exit(0));

Even need to be wrapped?

I feel like maybe the entire block could be api.process.stop().then(() => process.nextTick(() => process.exit(0)));
@evantahler Thoughts?

@edmundito
Copy link
Contributor Author

@evantahler The style issues are always easy to fix. I highlighted a few areas in the comments that point out potential logic issues.

Copy link
Contributor

@krishnaglick krishnaglick left a comment

Choose a reason for hiding this comment

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

I expect some merge conflicts with my just-merged code.

},
overrides: [
{
files: ["*.ts", "*.tsx"],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could just add *.js to ignorePatterns and you shouldn't need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gets added by @typescript:recommended but not @typescript:eslint-recommended So I need to manually add this override otherwise I have to run eslint with eslint . --ext ts,tsx

@@ -74,7 +74,7 @@ export namespace CloudCLI {
);

let lastState: string;
while (true) {
for (;;) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should disable that rule? We could rewrite this function to not need an infinite loop but I'm not it's worth the refactor time.

} catch (e) {
throw e;
this.accessToken = token;
this.expirationDate = Date.now() + expirationSeconds * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Unwrapped 👍

@@ -81,6 +81,7 @@ export class StatusTask extends CLSTask {
if (!toStop) return false;

// do not await so the promise can end so the server can shut down
// eslint-disable-next-line no-async-promise-executor
Copy link
Contributor

Choose a reason for hiding this comment

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

The proimise isn't returned. Does:

await api.process.stop();
process.nextTick(() => process.exit(0));

Even need to be wrapped?

I feel like maybe the entire block could be api.process.stop().then(() => process.nextTick(() => process.exit(0)));
@evantahler Thoughts?

@@ -528,7 +527,7 @@ function mergeMetrics(metrics: StatusMetric[]) {
mergedMetrics.push(item);
} else if (idx !== null) {
for (const k in Object.keys(item)) {
if (item.hasOwnProperty(k)) {
if (Object.prototype.hasOwnProperty.call(item, k)) {
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 love this rule

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good discussion point. Should we allow it? Should we not use hasOwnProperty?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like hasOwn exists and we should maybe just use that instead?

Copy link
Contributor Author

@edmundito edmundito Mar 3, 2022

Choose a reason for hiding this comment

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

hasOwn is super new. It looks like we'll have to upgrade to target es2022, we're still targeting es2019.
https://devblogs.microsoft.com/typescript/announcing-typescript-4-6/#target-es2022

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't object :P

@@ -28,6 +28,7 @@ export namespace Reset {
* * clears the redis cache
* * clears resque
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Copy link
Contributor

Choose a reason for hiding this comment

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

🤷 Probably not worth removing

@@ -260,6 +258,7 @@ export namespace RecordOps {
recordProperties: {
[key: string]: (string | number | boolean | Date)[] | any;
}[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLock = true,
Copy link
Contributor

Choose a reason for hiding this comment

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

If there's enough of these I'd say icebox a ticket to remove.

@edmundito edmundito force-pushed the eslint-core branch 2 times, most recently from ff4630e to a90add8 Compare March 7, 2022 14:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
internal chores and housekeeping
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants