Refactor stylelint config detection in lint-style.js#79280
Conversation
|
cc: @manzoorwanijk @aduth , I did create this but if we use v7.0.0 of cosmiconfig based on lint error we need to maintain .mjs and .mts manually via searchPlaces v9.0.0 handle this in the async version |
|
Is it an option to use |
…c config detection
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @musharafmaqbool. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi @aduth, resolveConfig does work and I have updated the code to use it instead. cc: @manzoorwanijk |
aduth
left a comment
There was a problem hiding this comment.
This is looking good 👍 I left a few comments
There was a problem hiding this comment.
I wouldn't expect any lockfile changes in this pull request. Can we reset these? Might be leftovers from previous iterations using cosmiconfig.
| const hasLintConfig = | ||
| hasArgInCLI( '--config' ) || | ||
| ( await stylelint | ||
| .resolveConfig( 'index.css', { cwd: process.cwd() } ) | ||
| .then( ( config ) => config !== undefined ) | ||
| .catch( ( err ) => { | ||
| // "No configuration provided" means no user config found; | ||
| // inject the bundled default. | ||
| // Other errors mean the config exists but is broken; let stylelint report them during linting. | ||
| if ( err.message?.includes( 'No configuration provided' ) ) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } ) ); |
There was a problem hiding this comment.
I think it'd be cleaner to extract a utility function for this which would also be a bit more self-documenting as well.
Resulting code here looking more like:
| const hasLintConfig = | |
| hasArgInCLI( '--config' ) || | |
| ( await stylelint | |
| .resolveConfig( 'index.css', { cwd: process.cwd() } ) | |
| .then( ( config ) => config !== undefined ) | |
| .catch( ( err ) => { | |
| // "No configuration provided" means no user config found; | |
| // inject the bundled default. | |
| // Other errors mean the config exists but is broken; let stylelint report them during linting. | |
| if ( err.message?.includes( 'No configuration provided' ) ) { | |
| return false; | |
| } | |
| return true; | |
| } ) ); | |
| const hasLintConfig = hasArgInCLI( '--config' ) || await hasResolvableConfig(); |
| // "No configuration provided" means no user config found; | ||
| // inject the bundled default. | ||
| // Other errors mean the config exists but is broken; let stylelint report them during linting. | ||
| if ( err.message?.includes( 'No configuration provided' ) ) { |
There was a problem hiding this comment.
When I was looking at this previously, I recall something about Stylelint providing an error.code that we could use, so that we don't have to look at the error message (which could change in future versions, for example). Could you check if that's available for us to use to make this more stable with future updates?
| ); | ||
|
|
||
| process.exit( result.status ); | ||
| async function main() { |
There was a problem hiding this comment.
Am I right in thinking we should be able to safely use top-level await (added in Node.js v14.8.0) and avoid having to create a function wrapper for the logic?
If linters aren't happy with it, maybe we need to update linters. But my expectation is we should be able to use it in Node.js code.
There was a problem hiding this comment.
doesn't top level await work only on esm file? since file is using require to import, nodejs will treat it as cjs file.
There was a problem hiding this comment.
Yeah, I think you're right. I overlooked that we're not using native ESM in this package. Curious if the CommonJS transpilation would support that, but not worth dwelling too much over.
What?
Closes #30842
Related #79226 (comment)
Extract stylelint configuration detection into a self-documenting
hasResolvableConfig()utility function inpackages/scripts/scripts/lint-style.js, using stylelint's ownresolveConfig()API and checkingerr.codeinstead of the error message string.Why?
The inline
.then().catch()chain was harder to read and relied on matching the error message text, which could break if stylelint changes its wording. The extracted function is self-documenting, and checkingerr.code === 78(stylelint'sEXIT_CODE_INVALID_CONFIG) is the stable, intended API for classification.How?
resolveConfiglogic into anasync hasResolvableConfig()function withtry/catch.err.message.includes('No configuration provided')toerr.code === CONFIG_NOT_FOUND_CODE(value78, stylelint'sEXIT_CODE_INVALID_CONFIG).main()async (already was).Testing Instructions
npm installto link the new dependency.npm run lint:css, verify same lint results as ontrunk(no new warnings/errors)..stylelintrc.tsto the project root, it should be auto-detected without code changes.Use of AI Tools
No