Skip to content

Commit

Permalink
lint, upgrade tests based on picomatch changes
Browse files Browse the repository at this point in the history
These changes shouldn't impact users, only how the tests were configured.
  • Loading branch information
jonschlinkert committed Mar 28, 2024
1 parent 6b3526f commit a4a4dbe
Show file tree
Hide file tree
Showing 38 changed files with 344 additions and 453 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -106,12 +106,13 @@
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-unused-vars": [1, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"prefer-const": 1,
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,6 +3,7 @@
.idea
.vscode
*.sublime-*
*.code-*

# test related, or directories generated by tests
test/actual
Expand All @@ -27,4 +28,4 @@ vendor
temp
tmp
TODO.md
package-lock.json
package-lock.json
3 changes: 3 additions & 0 deletions bench/.eslintrc.json
@@ -0,0 +1,3 @@
{
"extends": ["../.eslintrc.json"]
}
12 changes: 6 additions & 6 deletions examples/option-expandRange.js
Expand Up @@ -11,9 +11,9 @@ const regex = micromatch.makeRe('foo/{01..25}/bar', {
console.log(regex);
//=> /^(?:foo\/((?:0[1-9]|1[0-9]|2[0-5]))\/bar)$/

console.log(regex.test('foo/00/bar')) // false
console.log(regex.test('foo/01/bar')) // true
console.log(regex.test('foo/10/bar')) // true
console.log(regex.test('foo/22/bar')) // true
console.log(regex.test('foo/25/bar')) // true
console.log(regex.test('foo/26/bar')) // false
console.log(regex.test('foo/00/bar')); // false
console.log(regex.test('foo/01/bar')); // true
console.log(regex.test('foo/10/bar')); // true
console.log(regex.test('foo/22/bar')); // true
console.log(regex.test('foo/25/bar')); // true
console.log(regex.test('foo/26/bar')); // false
91 changes: 51 additions & 40 deletions index.js
Expand Up @@ -4,7 +4,13 @@ const util = require('util');
const braces = require('braces');
const picomatch = require('picomatch');
const utils = require('picomatch/lib/utils');
const isEmptyString = val => val === '' || val === './';

const isEmptyString = v => v === '' || v === './';
const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v);
const hasBraces = v => {
const index = v.indexOf('{');
return index > -1 && v.indexOf('}', index) > -1;
};

/**
* Returns an array of strings that match one or more glob patterns.
Expand All @@ -28,27 +34,27 @@ const micromatch = (list, patterns, options) => {
patterns = [].concat(patterns);
list = [].concat(list);

let omit = new Set();
let keep = new Set();
let items = new Set();
const omit = new Set();
const keep = new Set();
const items = new Set();
let negatives = 0;

let onResult = state => {
const onResult = state => {
items.add(state.output);
if (options && options.onResult) {
options.onResult(state);
}
};

for (let i = 0; i < patterns.length; i++) {
let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
const isMatch = picomatch(String(patterns[i]), { windows: true, ...options, onResult }, true);
const negated = isMatch.state.negated || isMatch.state.negatedExtglob;
if (negated) negatives++;

for (let item of list) {
let matched = isMatch(item, true);
for (const item of list) {
const matched = isMatch(item, true);

let match = negated ? !matched.isMatch : matched.isMatch;
const match = negated ? !matched.isMatch : matched.isMatch;
if (!match) continue;

if (negated) {
Expand All @@ -60,8 +66,8 @@ const micromatch = (list, patterns, options) => {
}
}

let result = negatives === patterns.length ? [...items] : [...keep];
let matches = result.filter(item => !omit.has(item));
const result = negatives === patterns.length ? [...items] : [...keep];
const matches = result.filter(item => !omit.has(item));

if (options && matches.length === 0) {
if (options.failglob === true) {
Expand Down Expand Up @@ -94,14 +100,17 @@ micromatch.match = micromatch;
* const isMatch = mm.matcher('*.!(*a)');
* console.log(isMatch('a.a')); //=> false
* console.log(isMatch('a.b')); //=> true
*
* const isMatch = mm.matcher(['b.*', '*.a']);
* console.log(isMatch('a.a')); //=> true
* ```
* @param {String} `pattern` Glob pattern
* @param {String|Array} `pattern` One or more glob patterns to use for matching.
* @param {Object} `options`
* @return {Function} Returns a matcher function.
* @api public
*/

micromatch.matcher = (pattern, options) => picomatch(pattern, options);
micromatch.matcher = (pattern, options) => picomatch(pattern, { windows: true, ...options });

/**
* Returns true if **any** of the given glob `patterns` match the specified `string`.
Expand Down Expand Up @@ -147,17 +156,17 @@ micromatch.any = micromatch.isMatch;

micromatch.not = (list, patterns, options = {}) => {
patterns = [].concat(patterns).map(String);
let result = new Set();
let items = [];
const result = new Set();
const items = [];

let onResult = state => {
const onResult = state => {
if (options.onResult) options.onResult(state);
items.push(state.output);
};

let matches = new Set(micromatch(list, patterns, { ...options, onResult }));
const matches = new Set(micromatch(list, patterns, { ...options, onResult }));

for (let item of items) {
for (const item of items) {
if (!matches.has(item)) {
result.add(item);
}
Expand Down Expand Up @@ -228,12 +237,12 @@ micromatch.contains = (str, pattern, options) => {
*/

micromatch.matchKeys = (obj, patterns, options) => {
if (!utils.isObject(obj)) {
if (!isObject(obj)) {
throw new TypeError('Expected the first argument to be an object');
}
let keys = micromatch(Object.keys(obj), patterns, options);
let res = {};
for (let key of keys) res[key] = obj[key];
const keys = micromatch(Object.keys(obj), patterns, options);
const res = {};
for (const key of keys) res[key] = obj[key];
return res;
};

Expand All @@ -257,10 +266,10 @@ micromatch.matchKeys = (obj, patterns, options) => {
*/

micromatch.some = (list, patterns, options) => {
let items = [].concat(list);
const items = [].concat(list);

for (let pattern of [].concat(patterns)) {
let isMatch = picomatch(String(pattern), options);
for (const pattern of [].concat(patterns)) {
const isMatch = picomatch(String(pattern), { windows: true, ...options });
if (items.some(item => isMatch(item))) {
return true;
}
Expand Down Expand Up @@ -293,10 +302,10 @@ micromatch.some = (list, patterns, options) => {
*/

micromatch.every = (list, patterns, options) => {
let items = [].concat(list);
const items = [].concat(list);

for (let pattern of [].concat(patterns)) {
let isMatch = picomatch(String(pattern), options);
for (const pattern of [].concat(patterns)) {
const isMatch = picomatch(String(pattern), { windows: true, ...options });
if (!items.every(item => isMatch(item))) {
return false;
}
Expand Down Expand Up @@ -336,7 +345,7 @@ micromatch.all = (str, patterns, options) => {
throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
}

return [].concat(patterns).every(p => picomatch(p, options)(str));
return [].concat(patterns).every(p => picomatch(p, { windows: true, ...options })(str));
};

/**
Expand All @@ -359,9 +368,9 @@ micromatch.all = (str, patterns, options) => {
*/

micromatch.capture = (glob, input, options) => {
let posix = utils.isWindows(options);
let regex = picomatch.makeRe(String(glob), { ...options, capture: true });
let match = regex.exec(posix ? utils.toPosixSlashes(input) : input);
const windows = utils.isWindows(options);
const regex = picomatch.makeRe(String(glob), { windows: true, ...options, capture: true });
const match = regex.exec(windows ? utils.toPosixSlashes(input) : input);

if (match) {
return match.slice(1).map(v => v === void 0 ? '' : v);
Expand All @@ -384,7 +393,7 @@ micromatch.capture = (glob, input, options) => {
* @api public
*/

micromatch.makeRe = (...args) => picomatch.makeRe(...args);
micromatch.makeRe = (pattern, options) => picomatch.makeRe(pattern, { windows: true, ...options });

/**
* Scan a glob pattern to separate the pattern into segments. Used
Expand All @@ -400,7 +409,7 @@ micromatch.makeRe = (...args) => picomatch.makeRe(...args);
* @api public
*/

micromatch.scan = (...args) => picomatch.scan(...args);
micromatch.scan = (pattern, options) => picomatch.scan(pattern, { windows: true, ...options });

/**
* Parse a glob pattern to create the source string for a regular
Expand All @@ -417,10 +426,10 @@ micromatch.scan = (...args) => picomatch.scan(...args);
*/

micromatch.parse = (patterns, options) => {
let res = [];
for (let pattern of [].concat(patterns || [])) {
for (let str of braces(String(pattern), options)) {
res.push(picomatch.parse(str, options));
const res = [];
for (const pattern of [].concat(patterns || [])) {
for (const str of braces(String(pattern), options)) {
res.push(picomatch.parse(str, { windows: utils.isWindows(), ...options }));
}
}
return res;
Expand All @@ -445,7 +454,7 @@ micromatch.parse = (patterns, options) => {

micromatch.braces = (pattern, options) => {
if (typeof pattern !== 'string') throw new TypeError('Expected a string');
if ((options && options.nobrace === true) || !/\{.*?\}/.test(pattern)) {
if ((options && options.nobrace === true) || !hasBraces(pattern)) {
return [pattern];
}
return braces(pattern, options);
Expand All @@ -464,4 +473,6 @@ micromatch.braceExpand = (pattern, options) => {
* Expose micromatch
*/

// exposed for tests
micromatch.hasBraces = hasBraces;
module.exports = micromatch;
14 changes: 14 additions & 0 deletions micromatch.code-workspace
@@ -0,0 +1,14 @@
{
"folders": [
{
"path": "."
},
{
"path": "../picomatch-4.0.0"
},
{
"path": "../picomatch-2.3.1"
}
],
"settings": {}
}
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -38,13 +38,13 @@
},
"dependencies": {
"braces": "^3.0.2",
"picomatch": "^4.0.1"
"picomatch": "^4.0.2"
},
"devDependencies": {
"fill-range": "^7.0.1",
"gulp-format-md": "^2.0.0",
"minimatch": "^5.0.1",
"mocha": "^9.2.2",
"minimatch": "^9.0.3",
"mocha": "^10.4.0",
"time-require": "github:jonschlinkert/time-require"
},
"keywords": [
Expand Down
6 changes: 0 additions & 6 deletions test/_fixtures.js
@@ -1,11 +1,5 @@
'use strict';

const path = require('path');

if (!process.env.ORIGINAL_PATH_SEP) {
process.env.ORIGINAL_PATH_SEP = path.sep
}

module.exports = [
'a',
'a.md',
Expand Down
9 changes: 2 additions & 7 deletions test/api.all.js
Expand Up @@ -2,16 +2,11 @@

process.env.PICOMATCH_NO_CACHE = 'true';

require('mocha');
const path = require('path');
const assert = require('assert');
const { all } = require('..');

if (!process.env.ORIGINAL_PATH_SEP) {
process.env.ORIGINAL_PATH_SEP = path.sep;
}

describe('.all()', () => {it('should throw an error when value is not a string', () => {
describe('.all()', () => {
it('should throw an error when value is not a string', () => {
assert.throws(() => all());
});

Expand Down
9 changes: 2 additions & 7 deletions test/api.braceExpand.js
@@ -1,15 +1,10 @@
'use strict';

require('mocha');
const path = require('path');
const assert = require('assert');
const { braceExpand } = require('..');

if (!process.env.ORIGINAL_PATH_SEP) {
process.env.ORIGINAL_PATH_SEP = path.sep
}

describe('.braceExpand()', () => {it('should throw an error when arguments are invalid', () => {
describe('.braceExpand()', () => {
it('should throw an error when arguments are invalid', () => {
assert.throws(() => braceExpand());
});

Expand Down
8 changes: 2 additions & 6 deletions test/api.braces.js
@@ -1,14 +1,10 @@
'use strict';

require('mocha');
const assert = require('assert');
const { braces } = require('..');

if (!process.env.ORIGINAL_PATH_SEP) {
process.env.ORIGINAL_PATH_SEP = path.sep
}

describe('.braces()', () => {it('should throw an error when arguments are invalid', () => {
describe('.braces()', () => {
it('should throw an error when arguments are invalid', () => {
assert.throws(() => braces());
});

Expand Down

0 comments on commit a4a4dbe

Please sign in to comment.