Skip to content

Commit

Permalink
fix(util): Replace micromatch with picomatch to fix issues with negat…
Browse files Browse the repository at this point in the history
…ed globs (#11287)
  • Loading branch information
danez committed Apr 12, 2021
1 parent 1ca2755 commit 9a930df
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -60,7 +60,7 @@
"jest-watch-typeahead": "^0.6.0",
"jquery": "^3.2.1",
"lerna": "^4.0.0",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"mlh-tsd": "^0.14.1",
"mock-fs": "^4.4.1",
"node-notifier": "^9.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-config/package.json
Expand Up @@ -39,14 +39,14 @@
"jest-resolve": "^27.0.0-next.7",
"jest-util": "^27.0.0-next.7",
"jest-validate": "^27.0.0-next.7",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"pretty-format": "^27.0.0-next.7"
},
"devDependencies": {
"@types/babel__core": "^7.0.4",
"@types/glob": "^7.1.1",
"@types/graceful-fs": "^4.1.3",
"@types/micromatch": "^4.0.0",
"@types/micromatch": "^4.0.1",
"jest-snapshot-serializer-raw": "^1.1.0",
"strip-ansi": "^6.0.0",
"ts-node": "^9.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/package.json
Expand Up @@ -33,7 +33,7 @@
"jest-util": "^27.0.0-next.7",
"jest-validate": "^27.0.0-next.7",
"jest-watcher": "^27.0.0-next.7",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"p-each-series": "^2.1.0",
"rimraf": "^3.0.0",
"slash": "^3.0.0",
Expand All @@ -44,7 +44,7 @@
"@jest/test-utils": "^27.0.0-next.7",
"@types/exit": "^0.1.30",
"@types/graceful-fs": "^4.1.2",
"@types/micromatch": "^4.0.0",
"@types/micromatch": "^4.0.1",
"@types/rimraf": "^3.0.0",
"jest-snapshot-serializer-raw": "^1.1.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/package.json
Expand Up @@ -24,14 +24,14 @@
"jest-serializer": "^27.0.0-next.0",
"jest-util": "^27.0.0-next.7",
"jest-worker": "^27.0.0-next.7",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"walker": "^1.0.7"
},
"devDependencies": {
"@jest/test-utils": "^27.0.0-next.7",
"@types/anymatch": "^1.3.1",
"@types/fb-watchman": "^2.0.0",
"@types/micromatch": "^4.0.0",
"@types/micromatch": "^4.0.1",
"jest-snapshot-serializer-raw": "^1.1.0",
"slash": "^3.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-message-util/package.json
Expand Up @@ -22,15 +22,15 @@
"@types/stack-utils": "^2.0.0",
"chalk": "^4.0.0",
"graceful-fs": "^4.2.4",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"pretty-format": "^27.0.0-next.7",
"slash": "^3.0.0",
"stack-utils": "^2.0.3"
},
"devDependencies": {
"@types/babel__code-frame": "^7.0.0",
"@types/graceful-fs": "^4.1.3",
"@types/micromatch": "^4.0.0"
"@types/micromatch": "^4.0.1"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-transform/package.json
Expand Up @@ -24,7 +24,7 @@
"jest-haste-map": "^27.0.0-next.7",
"jest-regex-util": "^27.0.0-next.0",
"jest-util": "^27.0.0-next.7",
"micromatch": "^4.0.2",
"micromatch": "^4.0.4",
"pirates": "^4.0.1",
"slash": "^3.0.0",
"source-map": "^0.6.1",
Expand All @@ -36,7 +36,7 @@
"@types/convert-source-map": "^1.5.1",
"@types/fast-json-stable-stringify": "^2.0.0",
"@types/graceful-fs": "^4.1.2",
"@types/micromatch": "^4.0.0",
"@types/micromatch": "^4.0.1",
"@types/write-file-atomic": "^3.0.0",
"dedent": "^0.7.0",
"jest-snapshot-serializer-raw": "^1.1.0"
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-util/package.json
Expand Up @@ -19,12 +19,13 @@
"chalk": "^4.0.0",
"graceful-fs": "^4.2.4",
"is-ci": "^3.0.0",
"micromatch": "^4.0.2"
"picomatch": "^2.2.3"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.2",
"@types/is-ci": "^2.0.0",
"@types/micromatch": "^4.0.0"
"@types/micromatch": "^4.0.1",
"@types/picomatch": "^2.2.2"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-util/src/__tests__/globsToMatcher.test.ts
Expand Up @@ -70,3 +70,29 @@ it('works like micromatch with empty globs', () => {
micromatch(['some-module.test.js'], globs).length > 0,
);
});

it('works like micromatch with pure negated extglobs', () => {
const globs = ['**/*.js', '!(some-module.test.js)'];
const matcher = globsToMatcher(globs);

expect(matcher('some-module.js')).toBe(
micromatch(['some-module.js'], globs).length > 0,
);

expect(matcher('some-module.test.js')).toBe(
micromatch(['some-module.test.js'], globs).length > 0,
);
});

it('works like micromatch with negated extglobs', () => {
const globs = ['**/*.js', '!(tests|coverage)/*.js'];
const matcher = globsToMatcher(globs);

expect(matcher('some-module.js')).toBe(
micromatch(['some-module.js'], globs).length > 0,
);

expect(matcher('tests/some-module.test.js')).toBe(
micromatch(['tests/some-module.test.js'], globs).length > 0,
);
});
20 changes: 10 additions & 10 deletions packages/jest-util/src/globsToMatcher.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import micromatch = require('micromatch');
import picomatch = require('picomatch');
import type {Config} from '@jest/types';
import replacePathSepForGlob from './replacePathSepForGlob';

Expand All @@ -16,15 +16,15 @@ const globsToMatchersMap = new Map<
{isMatch: Matcher; negated: boolean}
>();

const micromatchOptions = {dot: true};
const picomatchOptions = {dot: true};

/**
* Converts a list of globs into a function that matches a path against the
* globs.
*
* Every time micromatch is called, it will parse the glob strings and turn
* them into regexp instances. Instead of calling micromatch repeatedly with
* the same globs, we can use this function which will build the micromatch
* Every time picomatch is called, it will parse the glob strings and turn
* them into regexp instances. Instead of calling picomatch repeatedly with
* the same globs, we can use this function which will build the picomatch
* matchers ahead of time and then have an optimized path for determining
* whether an individual path matches.
*
Expand All @@ -44,13 +44,13 @@ export default function globsToMatcher(globs: Array<Config.Glob>): Matcher {

const matchers = globs.map(glob => {
if (!globsToMatchersMap.has(glob)) {
// Matchers that are negated have different behavior than matchers that
// are not negated, so we need to store this information ahead of time.
const {negated} = micromatch.scan(glob, micromatchOptions);
const isMatch = picomatch(glob, picomatchOptions, true);

const matcher = {
isMatch: micromatch.matcher(glob, micromatchOptions),
negated,
isMatch,
// Matchers that are negated have different behavior than matchers that
// are not negated, so we need to store this information ahead of time.
negated: isMatch.state.negated || !!isMatch.state.negatedExtglob,
};

globsToMatchersMap.set(glob, matcher);
Expand Down
54 changes: 31 additions & 23 deletions yarn.lock
Expand Up @@ -2439,7 +2439,7 @@ __metadata:
"@jest/types": ^27.0.0-next.7
"@types/exit": ^0.1.30
"@types/graceful-fs": ^4.1.2
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
"@types/node": "*"
"@types/rimraf": ^3.0.0
ansi-escapes: ^4.2.1
Expand All @@ -2461,7 +2461,7 @@ __metadata:
jest-util: ^27.0.0-next.7
jest-validate: ^27.0.0-next.7
jest-watcher: ^27.0.0-next.7
micromatch: ^4.0.2
micromatch: ^4.0.4
p-each-series: ^2.1.0
rimraf: ^3.0.0
slash: ^3.0.0
Expand Down Expand Up @@ -2581,7 +2581,7 @@ __metadata:
jest-watch-typeahead: ^0.6.0
jquery: ^3.2.1
lerna: ^4.0.0
micromatch: ^4.0.2
micromatch: ^4.0.4
mlh-tsd: ^0.14.1
mock-fs: ^4.4.1
node-notifier: ^9.0.0
Expand Down Expand Up @@ -2727,7 +2727,7 @@ __metadata:
"@types/convert-source-map": ^1.5.1
"@types/fast-json-stable-stringify": ^2.0.0
"@types/graceful-fs": ^4.1.2
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
"@types/write-file-atomic": ^3.0.0
babel-plugin-istanbul: ^6.0.0
chalk: ^4.0.0
Expand All @@ -2739,7 +2739,7 @@ __metadata:
jest-regex-util: ^27.0.0-next.0
jest-snapshot-serializer-raw: ^1.1.0
jest-util: ^27.0.0-next.7
micromatch: ^4.0.2
micromatch: ^4.0.4
pirates: ^4.0.1
slash: ^3.0.0
source-map: ^0.6.1
Expand Down Expand Up @@ -4690,7 +4690,7 @@ __metadata:
languageName: node
linkType: hard

"@types/micromatch@npm:^4.0.0":
"@types/micromatch@npm:^4.0.1":
version: 4.0.1
resolution: "@types/micromatch@npm:4.0.1"
dependencies:
Expand Down Expand Up @@ -4780,6 +4780,13 @@ __metadata:
languageName: node
linkType: hard

"@types/picomatch@npm:^2.2.2":
version: 2.2.2
resolution: "@types/picomatch@npm:2.2.2"
checksum: f55b30a9c532bd701f502132127c905739acd53cd5c0e5b3ef4b6bdbe7387c028f44cd3e8dd5b6df584ff57d0ca0dea22c2433b2fe137265a87c4893aefa8a61
languageName: node
linkType: hard

"@types/prettier@npm:*, @types/prettier@npm:^2.0.0, @types/prettier@npm:^2.1.5":
version: 2.2.2
resolution: "@types/prettier@npm:2.2.2"
Expand Down Expand Up @@ -13450,7 +13457,7 @@ fsevents@^1.2.7:
"@types/babel__core": ^7.0.4
"@types/glob": ^7.1.1
"@types/graceful-fs": ^4.1.3
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
babel-jest: ^27.0.0-next.7
chalk: ^4.0.0
deepmerge: ^4.2.2
Expand All @@ -13466,7 +13473,7 @@ fsevents@^1.2.7:
jest-snapshot-serializer-raw: ^1.1.0
jest-util: ^27.0.0-next.7
jest-validate: ^27.0.0-next.7
micromatch: ^4.0.2
micromatch: ^4.0.4
pretty-format: ^27.0.0-next.7
strip-ansi: ^6.0.0
ts-node: ^9.0.0
Expand Down Expand Up @@ -13593,7 +13600,7 @@ fsevents@^1.2.7:
"@types/anymatch": ^1.3.1
"@types/fb-watchman": ^2.0.0
"@types/graceful-fs": ^4.1.2
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
"@types/node": "*"
anymatch: ^3.0.3
fb-watchman: ^2.0.0
Expand All @@ -13604,7 +13611,7 @@ fsevents@^1.2.7:
jest-snapshot-serializer-raw: ^1.1.0
jest-util: ^27.0.0-next.7
jest-worker: ^27.0.0-next.7
micromatch: ^4.0.2
micromatch: ^4.0.4
slash: ^3.0.0
walker: ^1.0.7
dependenciesMeta:
Expand Down Expand Up @@ -13709,11 +13716,11 @@ fsevents@^1.2.7:
"@jest/types": ^27.0.0-next.7
"@types/babel__code-frame": ^7.0.0
"@types/graceful-fs": ^4.1.3
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
"@types/stack-utils": ^2.0.0
chalk: ^4.0.0
graceful-fs: ^4.2.4
micromatch: ^4.0.2
micromatch: ^4.0.4
pretty-format: ^27.0.0-next.7
slash: ^3.0.0
stack-utils: ^2.0.3
Expand Down Expand Up @@ -14008,12 +14015,13 @@ fsevents@^1.2.7:
"@jest/types": ^27.0.0-next.7
"@types/graceful-fs": ^4.1.2
"@types/is-ci": ^2.0.0
"@types/micromatch": ^4.0.0
"@types/micromatch": ^4.0.1
"@types/node": "*"
"@types/picomatch": ^2.2.2
chalk: ^4.0.0
graceful-fs: ^4.2.4
is-ci: ^3.0.0
micromatch: ^4.0.2
picomatch: ^2.2.3
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -15862,13 +15870,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"micromatch@npm:^4.0.2":
version: 4.0.2
resolution: "micromatch@npm:4.0.2"
"micromatch@npm:^4.0.2, micromatch@npm:^4.0.4":
version: 4.0.4
resolution: "micromatch@npm:4.0.4"
dependencies:
braces: ^3.0.1
picomatch: ^2.0.5
checksum: 0cb0e11d647cbb65e398a0a8a1340a7fb751ae2722346219c435704cfac8b3275a94a6464236fe867f52ad46a24046d3bc4ac11b3d21ddb73bc44e27cf1e4904
picomatch: ^2.2.3
checksum: bc522ad93c086aa176f50fea2dc8060a8f7d7a621c811cf9ba02a1912577cc100190508166d721231465f10a575a40ec8a1bffc23bbc2c0108fcbf02e4be04ed
languageName: node
linkType: hard

Expand Down Expand Up @@ -17859,10 +17867,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"picomatch@npm:^2.0.4, picomatch@npm:^2.0.5, picomatch@npm:^2.2.1, picomatch@npm:^2.2.2":
version: 2.2.2
resolution: "picomatch@npm:2.2.2"
checksum: 20fa75e0a58b39d83425b3db68744d5f6f361fd4fd66ec7745d884036d502abba0d553a637703af79939b844164b13e60eea339ccb043d7fbd74c3da2592b864
"picomatch@npm:^2.0.4, picomatch@npm:^2.2.1, picomatch@npm:^2.2.2, picomatch@npm:^2.2.3":
version: 2.2.3
resolution: "picomatch@npm:2.2.3"
checksum: f8c9323bc3b21ff448e81dd32277135d781abae5d53a1415d69a4ce6317a2c11404d449c550110b8fa402c07d5e80ff0e2657f263a312517cc809e9010d25791
languageName: node
linkType: hard

Expand Down

0 comments on commit 9a930df

Please sign in to comment.