Skip to content

Commit

Permalink
Do not apply magic unnecessarily for nocase
Browse files Browse the repository at this point in the history
If the pattern part's upper and lower case forms are identical, then
nocase has no effect, and there's no need to turn it into a regular
expression.

Not sure if this is related to #178, but it seems like it may be.
  • Loading branch information
isaacs committed Jan 14, 2023
1 parent 5878cf2 commit d2c654d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class Minimatch {
if (pattern === '') return ''

let re = ''
let hasMagic = !!options.nocase
let hasMagic = false
let escaping = false
// ? => one single character
const patternListStack = []
Expand Down Expand Up @@ -750,6 +750,11 @@ class Minimatch {
return [re, hasMagic]
}

// if it's nocase, and the lcase/uppercase don't match, it's magic
if (options.nocase && !hasMagic) {
hasMagic = pattern.toUpperCase() !== pattern.toLowerCase()
}

// skip the regexp for non-magical patterns
// unescape anything in it, though, so that it'll be
// an exact match against a file etc.
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ exports[`test/basic.js TAP basic tests > makeRe X* 2`] = `
`

exports[`test/basic.js TAP basic tests > makeRe XYZ 1`] = `
/^(?:(?=.)XYZ)$/i
/^(?:XYZ)$/i
`

exports[`test/basic.js TAP basic tests > makeRe [ 1`] = `
Expand Down
13 changes: 13 additions & 0 deletions test/nocase-magic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const t = require('tap')
const { Minimatch } = require('../')

const nomagic = '../1/2/3'
const yesmagic = '../x'

t.same(new Minimatch(nomagic, { nocase: true }).set, [
[ '..', '1', '2', '3']
])

t.same(new Minimatch(yesmagic, { nocase: true }).set, [
['..', /^x$/i]
])

0 comments on commit d2c654d

Please sign in to comment.