Skip to content

Commit

Permalink
Revert "refactor: remove unnecessary lint-staged operations (#65861)…
Browse files Browse the repository at this point in the history
…" (#66554)

cc @samcx 

### Why?

The previous change on `lint-staged` dropped, ignoring files from
eslint, resulting in errors on files that should be ignored (e.g. `dist`
inside `.github/actions/`).

x-ref: #66383

### How?

This PR reverts PARTIAL commit 0558f61.

### RFC

> ESLint v8.51.0 introduced [--no-warn-ignored CLI
flag](https://eslint.org/docs/latest/use/command-line-interface#--no-warn-ignored).
It suppresses the warning File ignored because of a matching ignore
pattern. Use "--no-ignore" to override warning, so manually ignoring
files via eslint.isPathIgnored is no longer necessary.

How about we convert current config as flat config, which would be
necessary if we upgrade to v9, and use the `--no-warn-ignored` flag on
`lint-staged` to reduce the operation introduced in #65861?

x-ref:
https://github.com/lint-staged/lint-staged?tab=readme-ov-file#eslint--8510--flat-eslint-config

---------

Co-authored-by: Sam Ko <sam@vercel.com>
  • Loading branch information
devjiwonchoi and samcx committed Jun 5, 2024
1 parent 3cf225c commit cef8bfa
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
const { quote } = require('shell-quote')
const { ESLint } = require('eslint')

const eslint = new ESLint()

/**
* Escape filenames to ensure that spaces and such aren't interpreted as
* separators.
*
* @param {string[]} filenames
* @returns {string[]}
*/
function escape(filenames) {
if (process.platform === 'win32') {
return filenames
}

return filenames.map((filename) => quote([filename]).replace(/\\@/g, '@'))
}

module.exports = {
'*.{js,jsx,mjs,ts,tsx,mts}': [
'prettier --with-node-modules --ignore-path .prettierignore --write',
'eslint --no-ignore --max-warnings=0 --fix',
],
'*.{js,jsx,mjs,ts,tsx,mts}': async (filenames) => {
const escapedFileNames = escape(filenames).join(' ')
const eslintFileNames = await Promise.all(
filenames.map(async (filename) => {
const ignored = await eslint.isPathIgnored(filename)
return ignored ? null : filename
})
)

return [
`prettier --with-node-modules --ignore-path .prettierignore --write ${escapedFileNames}`,
`eslint --no-ignore --max-warnings=0 --fix ${eslintFileNames
.filter((filename) => filename !== null)
.map((filename) => {
return `"${filename}"`
})
.join(' ')}`,
`git add ${escapedFileNames}`,
]
},
'*.{json,md,mdx,css,html,yml,yaml,scss}': [
'prettier --with-node-modules --ignore-path .prettierignore --write',
],
Expand Down

0 comments on commit cef8bfa

Please sign in to comment.