Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate minimatch to micromatch #240

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

JounQin
Copy link
Member

@JounQin JounQin commented Mar 14, 2025

Copy link

changeset-bot bot commented Mar 14, 2025

⚠️ No Changeset found

Latest commit: b22751e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

socket-security bot commented Mar 14, 2025

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/[email protected] None 0 6.57 kB types
npm/@types/[email protected] None 0 25.1 kB types
npm/[email protected] None 0 288 kB ota-meshi
npm/[email protected] environment, filesystem 0 3.03 MB eslintbot

🚮 Removed packages: npm/@types/[email protected], npm/[email protected]

View full report↗︎

Copy link

codesandbox-ci bot commented Mar 14, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@JounQin JounQin force-pushed the feat/micromatch branch 2 times, most recently from 88acf82 to eb6cc43 Compare March 14, 2025 16:11
Copy link
Collaborator

@SukkaW SukkaW left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only use makeRe and isMatch, IMHO we can use picomatch (the library the powers micromatch under the hood) directly!

@JounQin
Copy link
Member Author

JounQin commented Mar 15, 2025

If we only use makeRe and isMatch, IMHO we can use picomatch (the library the powers micromatch under the hood) directly!

Yeah, I started with picomatch, but some cases failed, so I tried micormatch and then found the blocked issue. When it's fixed, I'll try picomatch again.

// only run `isMatch` when necessary
!(options.exceptions && isMatch(filename, options.exceptions)) &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// inside create
const isMatch = options.exceptions
	? picomatch(options.exceptions)
  : () => false;
// before return
// now we hoist the `isMatch` function, prevent creating multiple instance across AST Nodes

),
)
ignoreGlobs &&
isMatch(node.parent.source.value, ignoreGlobs, { matchBase: true })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use picomatch to hoist isMatch function outside of AST traverse as well.

@Kenneth-Sills
Copy link

Kenneth-Sills commented Mar 16, 2025

I commented on your upstream issue WRT the behavior of leading ./, but coming here to continue.

Since you had the forethought to refactor out the glob calls, we should be able to pretty easily add normalization by modifying the functions provided in glob-match.ts to include a format option that normalizes the paths:

console.log(isMatch('baz', '[a-z]*'))
// true
console.log(isMatch('./baz', './[a-z]*'))
// false

console.log(isMatch('./baz', './[a-z]*', { format: (path) => path.replace(/^\.\//, '') }))
// true

@JounQin
Copy link
Member Author

JounQin commented Mar 16, 2025

Thanks @Kenneth-Sills I'll give it a try!


Sorry, but it makes ./baz sames as baz, so, unusable in this case.

console.log(isMatch('baz', './[a-z]*', { format: (path) => path.replace(/^\.\//, '') }))
// true
console.log(isMatch('./baz', './[a-z]*', { format: (path) => path.replace(/^\.\//, '') }))
// true

My hacking workaround:

import { isMatch as isMatch_ } from 'micromatch'

const normalizeBackslashes = (str: string) => str.replaceAll('\\', '/')

const defaultFormat = (path: string) => path.replace(/^\.\//, '')

const isMatchBase = (path: string, pattern: string, options?: Options) => {
  path = normalizeBackslashes(path)
  pattern = normalizeBackslashes(pattern)
  if (path.startsWith('./') && !/^(\.\/|\*{1,2})/.test(pattern)) {
    return false
  }
  return isMatch_(path, pattern, { format: defaultFormat, ...options })
}

export const isMatch = (
  pathname: string,
  patterns: string | string[],
  options?: Options,
) => {
  patterns = Array.isArray(patterns) ? patterns : [patterns]
  return patterns.some(p => isMatchBase(pathname, p, options))
}

Copy link

codecov bot commented Mar 16, 2025

Codecov Report

Attention: Patch coverage is 98.03922% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.07%. Comparing base (0449729) to head (b22751e).

Files with missing lines Patch % Lines
src/utils/glob-match.ts 96.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #240      +/-   ##
==========================================
- Coverage   96.09%   96.07%   -0.02%     
==========================================
  Files         106      107       +1     
  Lines        4783     4794      +11     
  Branches     1619     1645      +26     
==========================================
+ Hits         4596     4606      +10     
- Misses        187      188       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants