@stylable/core
Version:
CSS for Components
72 lines • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ignoreDeprecationWarn = exports.warnOnce = exports.wrapFunctionForDeprecation = exports.setFieldForDeprecation = void 0;
/**
* add get/set on an object with a deprecation warning when setting a value
*/
const valueMaps = {};
const valueOnThisPrefix = `__deprecateFromStylable-`;
function setFieldForDeprecation(object, fieldName, options = {}) {
const objectPrefix = options.objectType ? options.objectType + `.` : ``;
const alternative = options.pleaseUse ? `, please use ${options.pleaseUse}` : ``;
const enumerable = options.enumerable || false;
const fieldOnThis = options.valueOnThis ? `${valueOnThisPrefix}${fieldName}` : ``;
if (!fieldOnThis && !valueMaps[fieldName]) {
valueMaps[fieldName] = new WeakMap();
}
Object.defineProperty(object, fieldName, {
configurable: true,
get() {
warnOnce(`"${objectPrefix}${fieldName}" is deprecated${alternative}`);
return fieldOnThis ? this[fieldOnThis] : valueMaps[fieldName].get(this);
},
set(newValue) {
if (fieldOnThis) {
this[fieldOnThis] = newValue;
}
else {
valueMaps[fieldName].set(this, newValue);
}
return newValue;
},
enumerable,
});
}
exports.setFieldForDeprecation = setFieldForDeprecation;
function wrapFunctionForDeprecation(func, options) {
const alternative = options.pleaseUse ? `, please use ${options.pleaseUse}` : ``;
const warning = `"${options.name || func.name}" is deprecated${alternative}`;
return function (...args) {
warnOnce(warning);
return func.apply(this, args);
};
}
exports.wrapFunctionForDeprecation = wrapFunctionForDeprecation;
/**
* console warn once per message
*/
const warnsFlag = {};
let isWarnOn = true;
function warnOnce(warning) {
if (isWarnOn && !warnsFlag[warning]) {
warnsFlag[warning] = true;
console.warn(warning);
}
}
exports.warnOnce = warnOnce;
/**
* prevent deprecation warn calls during action
* @param action to be called with no deprecation warnings
* @returns return value of action
*/
function ignoreDeprecationWarn(action) {
isWarnOn = false;
try {
return action();
}
finally {
isWarnOn = true;
}
}
exports.ignoreDeprecationWarn = ignoreDeprecationWarn;
//# sourceMappingURL=deprecation.js.map