diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index f3f508217472b6..6556554750ceaf 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -6,6 +6,7 @@ - Add the `stylelint-scss` dependency, a peer of `@wordpress/stylelint-config` ([#83765](https://github.com/WordPress/gutenberg/pull/83765)). - `build`, `start` and `lint-pkg-json`: Resolve the default browserslist and npm-package-json-lint configs when dependencies are not hoisted to the project root (npm `install-strategy=linked`, pnpm). `lint-pkg-json` now defaults to `config/npmpackagejsonlint.js`; `config/npmpackagejsonlint.json` stays for projects that reference it ([#83902](https://github.com/WordPress/gutenberg/pull/83902)). +- `lint-js`, `lint-style` and `lint-md-docs`: a file passed right after `--fix` is now treated as a file instead of that flag's value, so only the listed files get processed. Same for `--require-pragma` in `format` ([#83521](https://github.com/WordPress/gutenberg/pull/83521)). ## 36.0.0 (2026-09-23) diff --git a/packages/scripts/utils/cli.js b/packages/scripts/utils/cli.js index 71a67632cd46dd..bdb848a1c1b53d 100644 --- a/packages/scripts/utils/cli.js +++ b/packages/scripts/utils/cli.js @@ -14,7 +14,19 @@ const getArgFromCLI = ( arg ) => { const hasArgInCLI = ( arg ) => getArgFromCLI( arg ) !== undefined; -const getFileArgsFromCLI = () => minimist( getArgsFromCLI() )._; +// Flags that never take a value, so `--fix file.js` isn't read as `--fix=file.js`. +// stylelint's `--fix` does take an optional mode though, so drop that first. +const getFileArgsFromCLI = () => { + const args = getArgsFromCLI().filter( + ( arg, index, all ) => + ! ( + all[ index - 1 ] === '--fix' && + [ 'strict', 'lax' ].includes( arg ) + ) + ); + + return minimist( args, { boolean: [ 'fix', 'require-pragma' ] } )._; +}; const getNodeArgsFromCLI = () => { const args = getArgsFromCLI(); diff --git a/packages/scripts/utils/test/index.js b/packages/scripts/utils/test/index.js index 6f60afb0b9e1cd..2f8f8775e59149 100644 --- a/packages/scripts/utils/test/index.js +++ b/packages/scripts/utils/test/index.js @@ -26,6 +26,7 @@ const fromConfigRootMock = vi.spyOn( fileUtils, 'fromConfigRoot' ); let crossSpawnMock = vi.spyOn( crossSpawn, 'sync' ); const { hasArgInCLI, + hasFileArgInCLI, hasProjectFile, getWebpackArgs, spawnScript, @@ -66,6 +67,39 @@ describe( 'utils', () => { } ); } ); + describe( 'hasFileArgInCLI', () => { + afterAll( () => { + getArgsFromCLIMock.mockReset(); + } ); + + test( 'should treat a file after --fix as a file arg', () => { + getArgsFromCLIMock.mockReturnValue( [ '--fix', 'style.scss' ] ); + + expect( hasFileArgInCLI() ).toBe( true ); + } ); + + test( 'should treat a file after --require-pragma as a file arg', () => { + getArgsFromCLIMock.mockReturnValue( [ + '--require-pragma', + 'index.js', + ] ); + + expect( hasFileArgInCLI() ).toBe( true ); + } ); + + test( 'should not treat the stylelint --fix mode as a file arg', () => { + getArgsFromCLIMock.mockReturnValue( [ '--fix', 'lax' ] ); + + expect( hasFileArgInCLI() ).toBe( false ); + } ); + + test( 'should not treat the value of --config as a file arg', () => { + getArgsFromCLIMock.mockReturnValue( [ '--config', 'custom.json' ] ); + + expect( hasFileArgInCLI() ).toBe( false ); + } ); + } ); + describe( 'hasProjectFile', () => { test( 'should return false for the current directory and unknown file', () => { getPackagePathMock.mockReturnValueOnce( currentDirectory );