js-contracts
Version:
C# inspired Code Contracts
141 lines • 6.18 kB
JavaScript
;
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
var contract_internal_1 = __importDefault(require("./contract-internal"));
var Contract = /** @class */ (function () {
function Contract() {
}
/**
* Set Contract settings
* @param settings - contract settings object
*/
Contract.setSettings = function (settings) {
contract_internal_1.default._setSettings(settings);
};
/**
* Represents values as they were at the start of a method or property.
* @param value - value of type T
*/
Contract.OldValue = function (value) {
return value;
};
/**
* Represents values as they were at the start of a method or property.
* @param path - path to the value
*/
Contract.OldValueByPath = function (path) {
return {};
};
/**
* Represents the return value of a method or property.
*/
Contract.ContractResult = function () {
return {};
};
/**
* Checks for a condition; if the condition is false, follows the escalation policy set for the analyzer.
* @param condition - The conditional expression to test
* @param message - The message to post if the assumption fails.
*/
Contract.Assert = function (condition, message) {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (contract_internal_1.default.shouldSkipContractChecks)
return;
contract_internal_1.default._assert(condition.apply(this, args), message);
};
};
/**
* Determines whether an element within a collection of elements exists within a function.
* @param collection<T> - Collection of type T
* @param predicate<T> - Predicate condition with return type T
* @param message - The message to post if the assumption fails.
*/
Contract.Exists = function (collection, predicate, message) {
if (contract_internal_1.default.shouldSkipContractChecks)
return;
contract_internal_1.default._exists(collection, predicate, message);
};
/**
* Determines whether all the elements in a collection exist within a function.
* @param collection<T> - Collection of type T
* @param predicate<T> - Predicate condition with return type T
* @param message - The message to post if the assumption fails.
*/
Contract.ForAll = function (collection, predicate, message) {
if (contract_internal_1.default.shouldSkipContractChecks)
return;
contract_internal_1.default._forAll(collection, predicate, message);
};
/**
* Specifies a precondition contract for the enclosing method or property, and displays a message if the condition for the contract fails.
* @param condition - The conditional expression to test
* @param message - The message to post if the assumption fails.
*/
Contract.Requires = function (condition, message) {
return function (target, key, descriptor) {
var original = descriptor.value;
if (contract_internal_1.default.shouldSkipContractChecks)
return descriptor;
var descriptorCall = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
contract_internal_1.default._requires(condition.apply(null, __spreadArrays(args, [this])), message);
return original.apply(this, args);
};
descriptor.value = descriptorCall;
return descriptor;
};
};
/**
* Specifies a postcondition contract for the enclosing method or property.
* @param condition - The conditional expression to test
* @param message - The message to post if the assumption fails.
*/
Contract.Ensures = function (condition, message) {
return function (target, key, descriptor) {
var original = descriptor.value;
if (contract_internal_1.default.shouldSkipContractChecks)
return descriptor;
var _a = contract_internal_1.default.initContextParameters(Contract, condition, target, key), bindOldValue = _a.bindOldValue, bindOldValueByPath = _a.bindOldValueByPath, bindFunctionResult = _a.bindFunctionResult, hasOldValueParameter = _a.hasOldValueParameter, populateCache = _a.populateCache, populateResultCache = _a.populateResultCache, destroyCache = _a.destroyCache;
var descriptorCall = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
bindOldValue();
bindOldValueByPath(this);
bindFunctionResult();
if (hasOldValueParameter) {
populateCache.apply(void 0, __spreadArrays(args, [this]));
}
var result = original.apply(this, args);
populateResultCache(result);
var trace = "Class: " + target.constructor.name + ", Function: " + key.toString() + ". " + message;
contract_internal_1.default._ensures(condition.apply(null, __spreadArrays(args, [this])), trace);
destroyCache();
return result;
};
descriptor.value = descriptorCall;
return descriptor;
};
};
return Contract;
}());
exports.default = Contract;
//# sourceMappingURL=contract.js.map