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

Fixing an issue with eslint not working in vscode #12746

Merged
merged 2 commits into from
Dec 4, 2024

Conversation

codyrancher
Copy link
Contributor

@codyrancher codyrancher commented Dec 3, 2024

  • Using rulesdir in the package scripts made it so the rules were only present when running them in the cli and showed errors in editors. This now allows the rules to run in the editor since I made a plugin using a local dependency
  • I also made is so lint rules can be run from root on pkg/rancher-components

Fixes #12670

Summary

Fixes #

Occurred changes and/or fixed issues

Technical notes summary

Areas or cases that should be tested

Areas which could experience regressions

Screenshot/Video

Checklist

  • The PR is linked to an issue and the linked issue has a Milestone, or no issue is needed
  • The PR has a Milestone
  • The PR template has been filled out
  • The PR has been self reviewed
  • The PR has a reviewer assigned
  • The PR has automated tests or clear instructions for manual tests and the linked issue has appropriate QA labels, or tests are not needed
  • The PR has reviewed with UX and tested in light and dark mode, or there are no UX changes

- Using rulesdir in the package scripts made it so the rules were only present when running them in the cli and showed errors in editors. This now allows the rules to run in the editor since I made a plugin using a local dependency
- I also made is so lint rules can be run from root on pkg/rancher-components

Fixes rancher#12670
@codyrancher codyrancher force-pushed the lint-fix-2 branch 5 times, most recently from 12e3e89 to c05f3ca Compare December 3, 2024 04:05
@codyrancher codyrancher added this to the v2.11.0 milestone Dec 3, 2024
@@ -116,7 +118,7 @@ export default defineComponent({
isDisabled,
validationMessage,
requiredField
} = useLabeledFormElement(props, emit);
} = useLabeledFormElement(props, emit as emitMethodType);
Copy link
Contributor Author

@codyrancher codyrancher Dec 3, 2024

Choose a reason for hiding this comment

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

Adding the emits will change the type of emit passed to the setup method. In this case it will be a union type of 'change' | 'update:value' | 'blur' | 'update:validation' which surprisingly doesn't match the string type expected by useLabeledFormElement. So I extracted the type and then cast appropriately here. I couldn't figure out a generic type that would work.

@@ -297,7 +297,7 @@ describe('stringList.vue', () => {
await inputField.trigger('keydown.enter');
await wrapper.vm.$nextTick();

const emitted = (wrapper.emitted('change') || [])[1][0][0];
const emitted = (wrapper.emitted('change') || [])[0][0][0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When emits is specified the value returned by emitted() omits the event type so I had to go through and subtract 1 from each index to account for the lack of event type.

Copy link
Member

Choose a reason for hiding this comment

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

This is a good development. This behavior was quite confusing while we were fixing the unit tests and it was unclear that omitting emits options was the root cause.

}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The lint rules in root require us to have an ending black newline at the end of each file.

@@ -70,7 +70,9 @@ export const labeledFormElementProps = {
}
};

export const useLabeledFormElement = (props: LabeledFormElementProps, emit: (event: string, ...args: any[]) => void): UseLabeledFormElement => {
export type emitMethodType = (event: string, ...args: any[]) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explained above, I needed to be able to type case the emit method elsewhere so I extracted the definition.

@@ -200,7 +200,8 @@
"webpack-virtual-modules": "0.4.3",
"worker-loader": "3.0.8",
"yaml-lint": "1.7.0",
"yarn": "1.22.18"
"yarn": "1.22.18",
"eslint-plugin-local-rules": "link:./eslint-plugin-local-rules"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We make the local rules a linked dev dependency. This allows eslint to search for the rules as a plugin.

Copy link
Member

Choose a reason for hiding this comment

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

We might want to consider documenting this somewhere. I can see how someone might encounter this link in the future and have questions about why we are doing this.

@codyrancher codyrancher marked this pull request as ready for review December 3, 2024 04:42
Copy link
Member

@rak-phillip rak-phillip left a comment

Choose a reason for hiding this comment

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

This looks great - I have one suggestion about typing emits options in composables.

@@ -48,7 +48,8 @@
"sass-loader": "~12.0.0",
"typescript": "4.5.5",
"vue": "~3.2.13",
"vue-template-compiler": "2.7.16"
"vue-template-compiler": "2.7.16",
Copy link
Member

Choose a reason for hiding this comment

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

vue-template-compiler is very odd.. I've created an issue to address this and other dependencies that might not be needed after the Vue3 migration #12751

Comment on lines 73 to 75
export type emitMethodType = (event: string, ...args: any[]) => void;

export const useLabeledFormElement = (props: LabeledFormElementProps, emit: emitMethodType): UseLabeledFormElement => {
Copy link
Member

Choose a reason for hiding this comment

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

I think that we might be able to more explicitly type the emits by using defineEmits:

Suggested change
export type emitMethodType = (event: string, ...args: any[]) => void;
export const useLabeledFormElement = (props: LabeledFormElementProps, emit: emitMethodType): UseLabeledFormElement => {
const labeledFormElementEmits = defineEmits(['update:validation']);
export const useLabeledFormElement = (props: LabeledFormElementProps, emit: typeof labeledFormElementEmits): UseLabeledFormElement => {

This should accomplish two things:

  1. Simplify consumption of useLabeledFormElement by removing the requirement to type emits as emitMethodType
  2. Enforces that consumers define update:validation in their emits

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added, I had to also add the defineEmits import and remove the emitMethodType in the other file so I added it as a part of my commit instead of committing the suggestion.

@@ -297,7 +297,7 @@ describe('stringList.vue', () => {
await inputField.trigger('keydown.enter');
await wrapper.vm.$nextTick();

const emitted = (wrapper.emitted('change') || [])[1][0][0];
const emitted = (wrapper.emitted('change') || [])[0][0][0];
Copy link
Member

Choose a reason for hiding this comment

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

This is a good development. This behavior was quite confusing while we were fixing the unit tests and it was unclear that omitting emits options was the root cause.

@@ -200,7 +200,8 @@
"webpack-virtual-modules": "0.4.3",
"worker-loader": "3.0.8",
"yaml-lint": "1.7.0",
"yarn": "1.22.18"
"yarn": "1.22.18",
"eslint-plugin-local-rules": "link:./eslint-plugin-local-rules"
Copy link
Member

Choose a reason for hiding this comment

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

We might want to consider documenting this somewhere. I can see how someone might encounter this link in the future and have questions about why we are doing this.

@codyrancher
Copy link
Contributor Author

Added the link documentation to eslint-plugin-local-rules/index.js

rak-phillip
rak-phillip previously approved these changes Dec 4, 2024
Copy link
Member

@rak-phillip rak-phillip left a comment

Choose a reason for hiding this comment

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

LGTM

@codyrancher codyrancher merged commit 6c170fc into rancher:master Dec 4, 2024
33 checks passed
@codyrancher codyrancher deleted the lint-fix-2 branch December 4, 2024 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants