Skip to content

Add eslint.codeActionsOnSave.options#1945

Closed
JavaScriptBach wants to merge 3 commits into
microsoft:mainfrom
JavaScriptBach:on-save
Closed

Add eslint.codeActionsOnSave.options#1945
JavaScriptBach wants to merge 3 commits into
microsoft:mainfrom
JavaScriptBach:on-save

Conversation

@JavaScriptBach

@JavaScriptBach JavaScriptBach commented Oct 30, 2024

Copy link
Copy Markdown
Contributor

Overview

Add an eslint.codeActionsOnSave.options that allows users to customize the options that ESLint runs with on save. This allows passing in a different set of options to ESLint on save.

If eslint.codeActionsOnSave.rules is specified, they will take priority over any rules specified in eslint.codeActionsOnSave.options. However, my use case for adding this is to specify a completely different config to provide faster on-save performance; more details in the test plan below.

See motivation in #1938 (comment).

Testing

Tested manually on my codebase with the following settings:

"editor.codeActionsOnSave": {
    "source.fixAll.eslint": "explicit"
},
"eslint.codeActionsOnSave.options": {
    "overrideConfigFile": "<workspaces path>/eslint.precommit.mjs",
    "fix": true,
    "fixTypes": ["problem", "suggestion", "layout"]
},

Verified that saving respected the precommit config and was faster than using the full config.

@JavaScriptBach

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree [company="Vanta"]

@JavaScriptBach

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree company="Vanta"

@JavaScriptBach JavaScriptBach marked this pull request as ready for review October 30, 2024 02:35
@JavaScriptBach

Copy link
Copy Markdown
Contributor Author

@dbaeumer I think I've followed your instructions correctly in #1938 (comment). Is there a way I can build and install this extension locally so I can test whether the setting works?

@dbaeumer

Copy link
Copy Markdown
Member

You can run from source

  • debug viewlet
  • Run Client lauch config

To build and install locally do the following:

  • dump the version number 3.0.15
  • npm run compile
  • install vsce globally
  • run vsce package This produces a local vsix file
  • Run command: Install from VSIX
  • Select the local vsix file

Let me know how it goes.

@dbaeumer

Copy link
Copy Markdown
Member

I did a first quick look and one thing I noticed is: do we want to support to only provide options without any rule modifications. That would have an impact on the code here: https://github.com/microsoft/vscode-eslint/blob/main/server/src/eslint.ts#L515

@JavaScriptBach

Copy link
Copy Markdown
Contributor Author

I was thinking if eslint.codeActionsOnSave.options is set, then eslint.codeActionsOnSave.rules should be ignored. The user can supply their own config in options, which means they should still be able to customize the rules.

@JavaScriptBach JavaScriptBach marked this pull request as draft October 31, 2024 04:59
@dbaeumer

Copy link
Copy Markdown
Member

I was thinking if eslint.codeActionsOnSave.options is set, then eslint.codeActionsOnSave.rules should be ignored. The user can supply their own config in options, which means they should still be able to customize the rules.

I think we should still allow this. If users already customized this and he simply wants to add some basic options I would still prefer to allow this.

@JavaScriptBach JavaScriptBach marked this pull request as ready for review November 30, 2024 06:07
@JavaScriptBach

Copy link
Copy Markdown
Contributor Author

@dbaeumer sorry for the late update. This is ready for a review now.

@MariaSolOs

Copy link
Copy Markdown
Contributor

@dbaeumer is there something blocking this PR from being merged?

If so (and if @JavaScriptBach is no longer interested) I'm more than happy to continue the remaining work.

@dbaeumer

Copy link
Copy Markdown
Member

@MariaSolOs mostly time. I see if a can spare some time next week to catch up with the PR.

@MariaSolOs

Copy link
Copy Markdown
Contributor

@MariaSolOs mostly time. I see if a can spare some time next week to catch up with the PR.

Thank you! I would really appreciate that.

Comment thread package.json
"scope": "resource",
"type": "object",
"default": {},
"markdownDescription": "The eslint options object to use on save. (see https://eslint.org/docs/developer-guide/nodejs-api#eslint-class). eslint.codeActionsOnSave.rules, if specified, will take priority over any rule options here."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
"markdownDescription": "The eslint options object to use on save. (see https://eslint.org/docs/developer-guide/nodejs-api#eslint-class). eslint.codeActionsOnSave.rules, if specified, will take priority over any rule options here."
"markdownDescription": "The ESLint options object to use on save (see https://eslint.org/docs/developer-guide/nodejs-api#eslint-class). `eslint.codeActionsOnSave.rules`, if specified, will take priority over any rule options here."
let eslintOptions: ESLintClassOptions = {fix: true};
if (offRules !== undefined || overrideOptions !== undefined) {
if (overrideOptions !== undefined) {
eslintOptions = {...overrideOptions, ...eslintOptions};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We probably want to swap the order of the options here for the save options to override the other ones right?

Suggested change
eslintOptions = {...overrideOptions, ...eslintOptions};
eslintOptions = {...eslintOptions, ...overrideOptions};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agree

}
return result;
}, settings, overrideConfig !== undefined ? { fix: true, overrideConfig } : { fix: true });
}, settings, eslintOptions );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
}, settings, eslintOptions );
}, settings, eslintOptions);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agree

@MariaSolOs

Copy link
Copy Markdown
Contributor

@dbaeumer friendly ping about this PR 🙃

Once you review it, I'm happy to make the required changes.

eslintOptions = {...overrideOptions, ...eslintOptions};
}
if (offRules !== undefined && offRules.size > 0) {
const overrideConfig = {rules: Object.create(null)};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should keep the spaces here

}
return result;
}, settings, overrideConfig !== undefined ? { fix: true, overrideConfig } : { fix: true });
}, settings, eslintOptions );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agree

Comment thread server/src/eslint.ts
}
}
return offRules.size > 0 ? { offRules, onRules } : undefined;
return (offRules.size > 0 || options) ? { offRules, onRules, options } : undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Until now we had undefined when we didn't have any rule changes. If we now had options we will end up with to empty sets if no options are specified. Might not be too big of a problem. But may be e can avoid this.

@MariaSolOs

Copy link
Copy Markdown
Contributor

@dbaeumer I opened a new PR in #1999 including the changes from here together with all the fixes from our reviews.

@dbaeumer

dbaeumer commented Apr 4, 2025

Copy link
Copy Markdown
Member

I will close this PR then.

@dbaeumer dbaeumer closed this Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants