Merge pull request #17045 from webpack/thelarkinn/increase-coverage-module-filename-helpers

This commit is contained in:
Sean Larkin 2023-04-23 09:51:19 -07:00 committed by GitHub
commit 7020b69855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 144 additions and 15 deletions

View File

@ -11,6 +11,7 @@ const createSchemaValidation = require("./util/create-schema-validation");
/** @typedef {import("../declarations/plugins/LoaderOptionsPlugin").LoaderOptionsPluginOptions} LoaderOptionsPluginOptions */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("./ModuleFilenameHelpers").MatchObject} MatchObject */
const validate = createSchemaValidation(
require("../schemas/plugins/LoaderOptionsPlugin.check.js"),
@ -20,17 +21,26 @@ const validate = createSchemaValidation(
baseDataPath: "options"
}
);
class LoaderOptionsPlugin {
/**
* @param {LoaderOptionsPluginOptions} options options object
* @param {LoaderOptionsPluginOptions & MatchObject} options options object
*/
constructor(options = {}) {
validate(options);
// If no options are set then generate empty options object
if (typeof options !== "object") options = {};
if (!options.test) {
options.test = {
// This is mocking a RegExp object which always returns true
// TODO: Figure out how to do `as unknown as RegExp` for this line
// in JSDoc equivalent
/** @type {any} */
const defaultTrueMockRegExp = {
test: () => true
};
/** @type {RegExp} */
options.test = defaultTrueMockRegExp;
}
this.options = options;
}

View File

@ -14,6 +14,9 @@ const memoize = require("./util/memoize");
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {typeof import("./util/Hash")} Hash */
/** @typedef {string | RegExp | string[] | RegExp[]} Matcher */
/** @typedef {{test?: Matcher, include?: Matcher, exclude?: Matcher }} MatchObject */
const ModuleFilenameHelpers = exports;
// TODO webpack 6: consider removing these
@ -43,6 +46,12 @@ ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
ModuleFilenameHelpers.NAMESPACE = "[namespace]";
ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
/**
* Returns a function that returns the part of the string after the token
* @param {() => string} strFn the function to get the string
* @param {string} token the token to search for
* @returns {() => string} a function that returns the part of the string after the token
*/
const getAfter = (strFn, token) => {
return () => {
const str = strFn();
@ -51,6 +60,12 @@ const getAfter = (strFn, token) => {
};
};
/**
* Returns a function that returns the part of the string before the token
* @param {() => string} strFn the function to get the string
* @param {string} token the token to search for
* @returns {() => string} a function that returns the part of the string before the token
*/
const getBefore = (strFn, token) => {
return () => {
const str = strFn();
@ -59,6 +74,12 @@ const getBefore = (strFn, token) => {
};
};
/**
* Returns a function that returns a hash of the string
* @param {() => string} strFn the function to get the string
* @param {string | Hash} hashFunction the hash function to use
* @returns {() => string} a function that returns the hash of the string
*/
const getHash = (strFn, hashFunction) => {
return () => {
const hash = createHash(hashFunction);
@ -68,13 +89,35 @@ const getHash = (strFn, hashFunction) => {
};
};
/**
* Returns a function that returns the string with the token replaced with the replacement
* @param {string|RegExp} test A regular expression string or Regular Expression object
* @returns {RegExp} A regular expression object
* @example
* ```js
* const test = asRegExp("test");
* test.test("test"); // true
*
* const test2 = asRegExp(/test/);
* test2.test("test"); // true
* ```
*/
const asRegExp = test => {
if (typeof test === "string") {
// Escape special characters in the string to prevent them from being interpreted as special characters in a regular expression. Do this by
// adding a backslash before each special character
test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
}
return test;
};
/**
* @template T
* Returns a lazy object. The object is lazy in the sense that the properties are
* only evaluated when they are accessed. This is only obtained by setting a function as the value for each key.
* @param {Record<string, () => T>} obj the object to covert to a lazy access object
* @returns {Object} the lazy access object
*/
const lazyObject = obj => {
const newObj = {};
for (const key of Object.keys(obj)) {
@ -95,7 +138,7 @@ const lazyObject = obj => {
return newObj;
};
const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
const SQUARE_BRACKET_TAG_REGEXP = /\[\\*([\w-]+)\\*\]/gi;
/**
*
@ -212,7 +255,7 @@ ModuleFilenameHelpers.createFilename = (
ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
"[short-identifier]"
)
.replace(REGEXP, (match, content) => {
.replace(SQUARE_BRACKET_TAG_REGEXP, (match, content) => {
if (content.length + 2 === match.length) {
const replacement = replacements.get(content.toLowerCase());
if (replacement !== undefined) {
@ -225,9 +268,28 @@ ModuleFilenameHelpers.createFilename = (
});
};
/**
* Replaces duplicate items in an array with new values generated by a callback function.
* The callback function is called with the duplicate item, the index of the duplicate item, and the number of times the item has been replaced.
* The callback function should return the new value for the duplicate item.
*
* @template T
* @param {T[]} array the array with duplicates to be replaced
* @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
* @param {(firstElement:T, nextElement:T) => -1 | 0 | 1} [comparator] optional comparator function to sort the duplicate items
* @returns {T[]} the array with duplicates replaced
*
* @example
* ```js
* const array = ["a", "b", "c", "a", "b", "a"];
* const result = ModuleFilenameHelpers.replaceDuplicates(array, (item, index, count) => `${item}-${count}`);
* // result: ["a-1", "b-1", "c", "a-2", "b-2", "a-3"]
* ```
*/
ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
const countMap = Object.create(null);
const posMap = Object.create(null);
array.forEach((item, idx) => {
countMap[item] = countMap[item] || [];
countMap[item].push(idx);
@ -248,16 +310,63 @@ ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
});
};
/**
* Tests if a string matches a RegExp or an array of RegExp.
*
* @param {string} str string to test
* @param {Matcher} test value which will be used to match against the string
* @returns {boolean} true, when the RegExp matches
*
* @example
* ```js
* ModuleFilenameHelpers.matchPart("foo.js", "foo"); // true
* ModuleFilenameHelpers.matchPart("foo.js", "foo.js"); // true
* ModuleFilenameHelpers.matchPart("foo.js", "foo."); // false
* ModuleFilenameHelpers.matchPart("foo.js", "foo*"); // false
* ModuleFilenameHelpers.matchPart("foo.js", "foo.*"); // true
* ModuleFilenameHelpers.matchPart("foo.js", /^foo/); // true
* ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
* ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, "bar"]); // true
* ModuleFilenameHelpers.matchPart("foo.js", [/^foo/, /^bar/]); // true
* ModuleFilenameHelpers.matchPart("foo.js", [/^baz/, /^bar/]); // false
* ```
*/
ModuleFilenameHelpers.matchPart = (str, test) => {
if (!test) return true;
test = asRegExp(test);
if (Array.isArray(test)) {
return test.map(asRegExp).some(regExp => regExp.test(str));
} else {
return test.test(str);
return asRegExp(test).test(str);
}
};
/**
* Tests if a string matches a match object. The match object can have the following properties:
* - `test`: a RegExp or an array of RegExp
* - `include`: a RegExp or an array of RegExp
* - `exclude`: a RegExp or an array of RegExp
*
* The `test` property is tested first, then `include` and then `exclude`.
*
* @param {MatchObject} obj a match object to test against the string
* @param {string} str string to test against the matching object
* @returns {boolean} true, when the object matches
* @example
* ```js
* ModuleFilenameHelpers.matchObject({ test: "foo.js" }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ test: /^foo/ }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ test: [/^foo/, "bar"] }, "baz.js"); // false
* ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ include: "foo.js" }, "bar.js"); // false
* ModuleFilenameHelpers.matchObject({ include: /^foo/ }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "foo.js"); // true
* ModuleFilenameHelpers.matchObject({ include: [/^foo/, "bar"] }, "baz.js"); // false
* ModuleFilenameHelpers.matchObject({ exclude: "foo.js" }, "foo.js"); // false
* ModuleFilenameHelpers.matchObject({ exclude: [/^foo/, "bar"] }, "foo.js"); // false
* ```
*/
ModuleFilenameHelpers.matchObject = (obj, str) => {
if (obj.test) {
if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {

28
types.d.ts vendored
View File

@ -6433,8 +6433,8 @@ declare interface LoaderModule<OptionsType = {}, ContextAdditions = {}> {
pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>;
}
declare class LoaderOptionsPlugin {
constructor(options?: LoaderOptionsPluginOptions);
options: LoaderOptionsPluginOptions;
constructor(options?: LoaderOptionsPluginOptions & MatchObject);
options: LoaderOptionsPluginOptions & MatchObject;
/**
* Apply the plugin
@ -6733,6 +6733,12 @@ declare interface MapOptions {
columns?: boolean;
module?: boolean;
}
declare interface MatchObject {
test?: string | RegExp | string[] | RegExp[];
include?: string | RegExp | string[] | RegExp[];
exclude?: string | RegExp | string[] | RegExp[];
}
type Matcher = string | RegExp | string[] | RegExp[];
/**
* Options object for in-memory caching.
@ -12785,13 +12791,17 @@ declare namespace exports {
hashFunction: string | typeof Hash;
}
) => string;
export let replaceDuplicates: (
array?: any,
fn?: any,
comparator?: any
) => any;
export let matchPart: (str?: any, test?: any) => any;
export let matchObject: (obj?: any, str?: any) => boolean;
export let replaceDuplicates: <T>(
array: T[],
fn: (
duplicateItem: T,
duplicateItemIndex: number,
numberOfTimesReplaced: number
) => T,
comparator?: (firstElement: T, nextElement: T) => 0 | 1 | -1
) => T[];
export let matchPart: (str: string, test: Matcher) => boolean;
export let matchObject: (obj: MatchObject, str: string) => boolean;
}
export namespace RuntimeGlobals {
export let require: "__webpack_require__";