Skip to content

Commit

Permalink
fixed string to multiple patterns "{...}" for minimatch filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed May 9, 2024
1 parent 80763b6 commit ca33df7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- 2.7.11
- fixed type style for UI
- fixed string to multiple patterns "{...}" for minimatch filter

- 2.7.10
- fixed undefined error when reading mappings
Expand Down
16 changes: 12 additions & 4 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ const getOriginalDecodedMappings = (originalDecodedMap, sourceIndex, locator) =>
// ========================================================================================================

const getSourceFilter = (options) => {
const input = options.sourceFilter;
let input = options.sourceFilter;

// for function handler
if (typeof input === 'function') {
Expand All @@ -952,9 +952,17 @@ const getSourceFilter = (options) => {

// for single minimatch pattern
if (input && typeof input === 'string') {
return (sourcePath) => {
return minimatch(sourcePath, input);
};
// string to multiple patterns "{...}"
// mcr npx mocha --sourceFilter {'**/node_modules/**':false,'**/src/*.js':true}
// mcr npx mocha --sourceFilter "{'**/node_modules/**': false, '**/src/*.js': true}"
const obj = Util.strToObjPatterns(input);
if (obj) {
input = obj;
} else {
return (sourcePath) => {
return minimatch(sourcePath, input);
};
}
}

// for patterns
Expand Down
12 changes: 9 additions & 3 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,15 @@ const resolveAllFilter = (input) => {

// for single minimatch pattern
if (input && typeof input === 'string') {
return (filePath) => {
return minimatch(filePath, input);
};
// string to multiple patterns "{...}"
const obj = Util.strToObjPatterns(input);
if (obj) {
input = obj;
} else {
return (filePath) => {
return minimatch(filePath, input);
};
}
}

// for patterns
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ const Util = {
return str;
},

strToObjPatterns: (str) => {
if (typeof str === 'string') {
str = str.trim();
if (str.startsWith('{') && str.endsWith('}')) {
let fun;
let err;
try {
fun = new Function(`return ${str}`);
} catch (e) {
err = e;
}
if (!err) {
const obj = fun();
if (obj && typeof obj === 'object') {
return obj;
}
}
}
}
},

calculateSha1: (buffer) => {
const hash = crypto.createHash('sha1');
hash.update(buffer);
Expand Down
16 changes: 12 additions & 4 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getWrapperSource = (offset, source) => {
};

const getEntryFilter = (options) => {
const input = options.entryFilter;
let input = options.entryFilter;

// for function handler
if (typeof input === 'function') {
Expand All @@ -32,9 +32,17 @@ const getEntryFilter = (options) => {

// for single minimatch pattern
if (input && typeof input === 'string') {
return (entry) => {
return minimatch(entry.url, input);
};
// string to multiple patterns "{...}"
// mcr npx mocha --entryFilter {'**/node_modules/**':false,'**/src/*.js':true}
// mcr npx mocha --entryFilter "{'**/node_modules/**': false, '**/src/*.js': true}"
const obj = Util.strToObjPatterns(input);
if (obj) {
input = obj;
} else {
return (entry) => {
return minimatch(entry.url, input);
};
}
}

// for patterns
Expand Down

0 comments on commit ca33df7

Please sign in to comment.