Browse Source

#9391 resolve discussions, AbstractMethodError

tags/v4.36.0
ikopeykin 5 months ago
parent
commit
bd7d95bfc1
5 changed files with 85 additions and 24 deletions
  1. +1
    -1
      declarations/WebpackOptions.d.ts
  2. +36
    -0
      lib/AbstractMethodError.js
  3. +20
    -22
      lib/util/createHash.js
  4. +1
    -1
      schemas/WebpackOptions.json
  5. +27
    -0
      test/AbstractMethodError.unittest.js

+ 1
- 1
declarations/WebpackOptions.d.ts View File

@@ -1094,7 +1094,7 @@ export interface OutputOptions {
/**
* Algorithm used for generation the hash (see node.js crypto package)
*/
hashFunction?: string | (new () => import("../lib/util/createHash").Hash);
hashFunction?: string | import("../lib/util/createHash").HashConstructor;
/**
* Any string which is added to the hash to salt it
*/

+ 36
- 0
lib/AbstractMethodError.js View File

@@ -0,0 +1,36 @@
"use strict";

const WebpackError = require("./WebpackError");
const CURRENT_METHOD_REGEXP = /at ([a-zA-Z0-9_.]*)/;

/**
* @param {string=} method method name
* @returns {string} message
*/
function createMessage(method) {
return `Abstract method${method ? " " + method : ""}. Must be overriden.`;
}

/**
* Error for abstract method
* @example
* class FooClass {
* abstractMethod() {
* throw new AbstractMethodError(); // error message: Abstract method FooClass.abstractMethod. Must be overriden.
* }
* }
*
*/
class AbstractMethodError extends WebpackError {
constructor() {
super(createMessage());
this.name = "AbstractMethodError";
/** @type {RegExpMatchArray} */
const match = this.stack.split("\n")[1].match(CURRENT_METHOD_REGEXP);
if (match && match[1]) {
this.message = createMessage(match[1]);
}
}
}

module.exports = AbstractMethodError;

+ 20
- 22
lib/util/createHash.js View File

@@ -4,38 +4,37 @@
*/
"use strict";

const AbstractMethodError = require("../AbstractMethodError");

const BULK_SIZE = 1000;

class BaseHash {
class Hash {
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding
* @returns {BaseHash} updated hash
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
update(data, inputEncoding) {
return this;
throw new AbstractMethodError();
}

/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string} [encoding] encoding of the return value
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
digest(encoding) {
return "";
throw new AbstractMethodError();
}
}

/** @typedef {BaseHash} Hash */
/** @typedef {typeof BaseHash} HashConstructor */
exports.Hash = Hash;
/** @typedef {typeof Hash} HashConstructor */

/**
* @extends {BaseHash}
*/
class BulkUpdateDecorator extends BaseHash {
class BulkUpdateDecorator extends Hash {
/**
* @param {BaseHash} hash hash
* @param {Hash} hash hash
*/
constructor(hash) {
super();
@@ -46,8 +45,8 @@ class BulkUpdateDecorator extends BaseHash {
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding
* @returns {BaseHash} updated hash
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
update(data, inputEncoding) {
if (
@@ -72,7 +71,7 @@ class BulkUpdateDecorator extends BaseHash {

/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string} [encoding] encoding of the return value
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
digest(encoding) {
@@ -88,9 +87,8 @@ class BulkUpdateDecorator extends BaseHash {

/**
* istanbul ignore next
* @extends {BaseHash}
*/
class DebugHash extends BaseHash {
class DebugHash extends Hash {
constructor() {
super();
this.string = "";
@@ -99,8 +97,8 @@ class DebugHash extends BaseHash {
/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding
* @returns {BaseHash} updated hash
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
update(data, inputEncoding) {
if (typeof data !== "string") data = data.toString("utf-8");
@@ -110,7 +108,7 @@ class DebugHash extends BaseHash {

/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string} [encoding] encoding of the return value
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
digest(encoding) {
@@ -123,7 +121,7 @@ class DebugHash extends BaseHash {
/**
* Creates a hash by name or function
* @param {string | HashConstructor} algorithm the algorithm name or a constructor creating a hash
* @returns {BaseHash} the hash
* @returns {Hash} the hash
*/
module.exports = algorithm => {
if (typeof algorithm === "function") {

+ 1
- 1
schemas/WebpackOptions.json View File

@@ -905,7 +905,7 @@
},
{
"instanceof": "Function",
"tsType": "(new () => import('../lib/util/createHash').Hash)"
"tsType": "import('../lib/util/createHash').HashConstructor"
}
]
},

+ 27
- 0
test/AbstractMethodError.unittest.js View File

@@ -0,0 +1,27 @@
"use strict";

const AbstractMethodError = require("../lib/AbstractMethodError");

describe("WebpackError", () => {
class Foo {
abstractMethod() {
return new AbstractMethodError();
}
}

class Child extends Foo {}

const expectedMessage = "Abstract method $1. Must be overriden.";

it("Should construct message with caller info", () => {
const fooClassError = new Foo().abstractMethod();
const childClassError = new Child().abstractMethod();

expect(fooClassError.message).toBe(
expectedMessage.replace("$1", "Foo.abstractMethod")
);
expect(childClassError.message).toBe(
expectedMessage.replace("$1", "Child.abstractMethod")
);
});
});

Loading…
Cancel
Save