diff --git a/.lintstagedrc.mjs b/.lintstagedrc.mjs index f70178055d..06e5e95b64 100644 --- a/.lintstagedrc.mjs +++ b/.lintstagedrc.mjs @@ -1,24 +1,31 @@ -import micromatch from 'micromatch'; +/** Staged paths use forward slashes; lint-staged may pass absolute or relative paths. */ + +/** + * Returns whether any staged path is a file named `name` at the root of the path list + * or nested under a directory (e.g. `package.json` or `packages/foo/package.json`). + * + * @param {string[]} changes - an array of strings with file paths that have changed + * @param {string} name - Basename to match (e.g. `package.json`). + * @returns {boolean} + */ +const hasFilename = (changes, name) => + changes.some((file) => file === name || file.endsWith(`/${name}`)); export default { // Use custom function to avoid overlaps that could cause race conditions [`*`]: (allChanges) => { const commands = []; - if (micromatch.some(allChanges, '**/package.json')) { + if (hasFilename(allChanges, 'package.json')) { commands.push(`yarn syncpack format`); } - if (micromatch.some(allChanges, 'yarn.lock')) { + if (hasFilename(allChanges, 'yarn.lock')) { commands.push(`yarn dedupe`); } - const eslintExtensions = `{mdx,ts,tsx,js,jsx,json}`; - const eslintFiles = micromatch( - allChanges, - [`**/*.${eslintExtensions}`, `?(.)**.${eslintExtensions}`], - { dot: true } - ); + const ESLINT_EXT = /\.(mdx|tsx?|jsx?|json)$/; + const eslintFiles = allChanges.filter((file) => ESLINT_EXT.test(file)); if (eslintFiles.length) { commands.push( diff --git a/package.json b/package.json index 4f1b3f1f73..7a81e4fe82 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "jest-environment-jsdom-global": "4.0.0", "jest-junit": "^16.0.0", "lint-staged": "^16.4.0", - "micromatch": "^4.0.5", "mutationobserver-shim": "^0.3.3", "nx": "22.6.0", "nx-cloud": "19.1.0", diff --git a/yarn.lock b/yarn.lock index ee3b5280d1..22677e9863 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13399,7 +13399,6 @@ __metadata: jest-junit: "npm:^16.0.0" lint-staged: "npm:^16.4.0" lodash: "npm:^4.17.23" - micromatch: "npm:^4.0.5" mutationobserver-shim: "npm:^0.3.3" nx: "npm:22.6.0" nx-cloud: "npm:19.1.0" @@ -18412,9 +18411,9 @@ __metadata: linkType: hard "picomatch@npm:^2.0.4, picomatch@npm:^2.2.1, picomatch@npm:^2.2.2, picomatch@npm:^2.2.3, picomatch@npm:^2.3.1": - version: 2.3.1 - resolution: "picomatch@npm:2.3.1" - checksum: 10c0/26c02b8d06f03206fc2ab8d16f19960f2ff9e81a658f831ecb656d8f17d9edc799e8364b1f4a7873e89d9702dff96204be0fa26fe4181f6843f040f819dac4be + version: 2.3.2 + resolution: "picomatch@npm:2.3.2" + checksum: 10c0/a554d1709e59be97d1acb9eaedbbc700a5c03dbd4579807baed95100b00420bc729335440ef15004ae2378984e2487a7c1cebd743cfdb72b6fa9ab69223c0d61 languageName: node linkType: hard