@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
121 lines (110 loc) • 3.5 kB
JavaScript
;
function applyAiPromptSuggestionClass(str) {
return str.replaceAll('k-prompt-suggestion', 'k-suggestion');
}
const REMOVED_AIPROMPT_OPTIONS = new Set(['showOutputRating']);
const REMOVED_AIPROMPT_EVENTS = new Set(['outputRatingChange', 'outputCopy']);
function transformAiPromptOptions(root, j) {
let changed = false;
root.find(j.CallExpression)
.filter(path => {
const callee = path.value.callee;
return j.MemberExpression.check(callee) && callee.property.name === 'kendoAIPrompt';
})
.forEach(path => {
path.value.arguments.forEach(arg => {
if (!j.ObjectExpression.check(arg)) {
return;
}
const before = arg.properties.length;
arg.properties = arg.properties.filter(prop => {
if (!j.Property.check(prop)) {
return true;
}
const key = prop.key.name || prop.key.value;
return !REMOVED_AIPROMPT_OPTIONS.has(key);
});
if (arg.properties.length !== before) {
changed = true;
}
});
});
root.find(j.CallExpression)
.filter(path => {
const callee = path.value.callee;
if (!j.MemberExpression.check(callee)) {
return false;
}
const method = callee.property.name;
if (method !== 'on' && method !== 'bind') {
return false;
}
const firstArg = path.value.arguments[0];
if (!firstArg) {
return false;
}
const eventName = j.StringLiteral.check(firstArg) || j.Literal.check(firstArg)
? firstArg.value
: null;
return eventName && REMOVED_AIPROMPT_EVENTS.has(eventName);
})
.closest(j.ExpressionStatement)
.forEach(() => { changed = true; })
.remove();
return changed;
}
function transformStringLiterals(root, j, fn) {
let changed = false;
const visit = path => {
if (typeof path.node.value !== 'string') {
return;
}
const updated = fn(path.node.value);
if (updated === path.node.value) {
return;
}
path.node.value = updated;
if (path.node.extra) {
path.node.extra.rawValue = updated;
if (path.node.extra.raw) {
path.node.extra.raw = fn(path.node.extra.raw);
}
}
changed = true;
};
root.find(j.StringLiteral).forEach(visit);
root.find(j.Literal, n => typeof n.value === 'string').forEach(visit);
return changed;
}
function transformTemplateLiterals(root, j, fn) {
let changed = false;
root.find(j.TemplateLiteral).forEach(path => {
path.node.quasis.forEach(quasi => {
const updatedRaw = fn(quasi.value.raw);
const updatedCooked = quasi.value.cooked != null ? fn(quasi.value.cooked) : null;
if (updatedRaw !== quasi.value.raw || updatedCooked !== quasi.value.cooked) {
quasi.value.raw = updatedRaw;
if (quasi.value.cooked != null) {
quasi.value.cooked = updatedCooked;
}
changed = true;
}
});
});
return changed;
}
module.exports = {
applyAiPromptSuggestionClass,
applyChatFileClassRenames,
transformAiPromptOptions,
transformStringLiterals,
transformTemplateLiterals,
};
function applyChatFileClassRenames(str) {
let result = str;
result = result.replaceAll('k-chat-file-info', 'k-file-info');
result = result.replaceAll('k-chat-file-name', 'k-file-name');
result = result.replaceAll('k-chat-file-size', 'k-file-size');
result = result.replaceAll('k-chat-file', 'k-file-box');
return result;
}