#9391 resolve discussions, AbstractMethodError

This commit is contained in:
ikopeykin 2019-07-15 14:17:12 +03:00
parent a223ffdcc0
commit bd7d95bfc1
5 changed files with 85 additions and 24 deletions

View File

@ -1094,7 +1094,7 @@ export interface OutputOptions {
/** /**
* Algorithm used for generation the hash (see node.js crypto package) * 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 * Any string which is added to the hash to salt it
*/ */

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;

View File

@ -4,38 +4,37 @@
*/ */
"use strict"; "use strict";
const AbstractMethodError = require("../AbstractMethodError");
const BULK_SIZE = 1000; const BULK_SIZE = 1000;
class BaseHash { class Hash {
/** /**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding} * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data * @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding * @param {string=} inputEncoding data encoding
* @returns {BaseHash} updated hash * @returns {this} updated hash
*/ */
update(data, inputEncoding) { update(data, inputEncoding) {
return this; throw new AbstractMethodError();
} }
/** /**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding} * 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 * @returns {string|Buffer} digest
*/ */
digest(encoding) { digest(encoding) {
return ""; throw new AbstractMethodError();
} }
} }
/** @typedef {BaseHash} Hash */ exports.Hash = Hash;
/** @typedef {typeof BaseHash} HashConstructor */ /** @typedef {typeof Hash} HashConstructor */
/** class BulkUpdateDecorator extends Hash {
* @extends {BaseHash}
*/
class BulkUpdateDecorator extends BaseHash {
/** /**
* @param {BaseHash} hash hash * @param {Hash} hash hash
*/ */
constructor(hash) { constructor(hash) {
super(); super();
@ -46,8 +45,8 @@ class BulkUpdateDecorator extends BaseHash {
/** /**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding} * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data * @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding * @param {string=} inputEncoding data encoding
* @returns {BaseHash} updated hash * @returns {this} updated hash
*/ */
update(data, inputEncoding) { update(data, inputEncoding) {
if ( if (
@ -72,7 +71,7 @@ class BulkUpdateDecorator extends BaseHash {
/** /**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding} * 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 * @returns {string|Buffer} digest
*/ */
digest(encoding) { digest(encoding) {
@ -88,9 +87,8 @@ class BulkUpdateDecorator extends BaseHash {
/** /**
* istanbul ignore next * istanbul ignore next
* @extends {BaseHash}
*/ */
class DebugHash extends BaseHash { class DebugHash extends Hash {
constructor() { constructor() {
super(); super();
this.string = ""; this.string = "";
@ -99,8 +97,8 @@ class DebugHash extends BaseHash {
/** /**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding} * Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data * @param {string|Buffer} data data
* @param {string} [inputEncoding] data encoding * @param {string=} inputEncoding data encoding
* @returns {BaseHash} updated hash * @returns {this} updated hash
*/ */
update(data, inputEncoding) { update(data, inputEncoding) {
if (typeof data !== "string") data = data.toString("utf-8"); 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} * 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 * @returns {string|Buffer} digest
*/ */
digest(encoding) { digest(encoding) {
@ -123,7 +121,7 @@ class DebugHash extends BaseHash {
/** /**
* Creates a hash by name or function * Creates a hash by name or function
* @param {string | HashConstructor} algorithm the algorithm name or a constructor creating a hash * @param {string | HashConstructor} algorithm the algorithm name or a constructor creating a hash
* @returns {BaseHash} the hash * @returns {Hash} the hash
*/ */
module.exports = algorithm => { module.exports = algorithm => {
if (typeof algorithm === "function") { if (typeof algorithm === "function") {

View File

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

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")
);
});
});