@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
172 lines (154 loc) • 4.85 kB
JavaScript
;
const {
ALL_CLASS_REPLACEMENTS,
SCHEDULER_RE_JS,
applyClassReplacements,
applySchedulerSelector,
transformStringLiterals,
transformTemplateLiterals,
transformTemplateStringProperties,
} = require('../_shared.cjs');
const EXCLUDED_WIDGET = new Set(['kendoDialog', 'kendoWindow']);
const THEME_COLOR_REMAP = { light: 'base', dark: 'inverse' };
function applyAllClassTransforms(str) {
let result = applyClassReplacements(str, ALL_CLASS_REPLACEMENTS);
result = applySchedulerSelector(result, SCHEDULER_RE_JS);
return result;
}
function transformScriptContent(content, j) {
let root;
try {
root = j(content);
} catch {
return content;
}
let changed = false;
root
.find(j.CallExpression, { callee: { type: 'MemberExpression' } })
.filter(path => {
const prop = path.node.callee.property;
return EXCLUDED_WIDGET.has(prop.name || prop.value);
})
.forEach(path => {
const args = path.node.arguments;
if (!args.length || args[0].type !== 'ObjectExpression') {
return;
}
const before = args[0].properties.length;
args[0].properties = args[0].properties.filter(p => {
if (p.type !== 'Property' && p.type !== 'ObjectProperty') {
return true;
}
return (p.key.name || p.key.value) !== 'themeColor';
});
if (args[0].properties.length < before) {
changed = true;
}
});
root
.find(j.CallExpression, { callee: { type: 'MemberExpression' } })
.filter(path => {
const prop = path.node.callee.property;
return !EXCLUDED_WIDGET.has(prop.name || prop.value);
})
.forEach(path => {
const args = path.node.arguments;
if (!args.length || args[0].type !== 'ObjectExpression') {
return;
}
args[0].properties.forEach(p => {
if (p.type !== 'Property' && p.type !== 'ObjectProperty') {
return;
}
if ((p.key.name || p.key.value) !== 'themeColor') {
return;
}
const currentValue = p.value.value;
const replacement = THEME_COLOR_REMAP[currentValue];
if (replacement) {
p.value.value = replacement;
if (p.value.extra) {
p.value.extra.rawValue = replacement;
if (p.value.extra.raw) {
p.value.extra.raw = p.value.extra.raw.replace(currentValue, replacement);
}
}
changed = true;
}
});
});
root
.find(j.CallExpression, {
callee: { type: 'MemberExpression', property: { name: 'kendoSplitButton' } },
})
.forEach(path => {
const args = path.node.arguments;
if (!args.length || args[0].type !== 'ObjectExpression') {
return;
}
args[0].properties.forEach(p => {
if (p.type !== 'Property' && p.type !== 'ObjectProperty') {
return;
}
if ((p.key.name || p.key.value) !== 'arrowButtonIcon') {
return;
}
if (p.value.value === 'caret-alt-down') {
p.value.value = 'chevron-down';
if (p.value.extra) {
p.value.extra.rawValue = 'chevron-down';
if (p.value.extra.raw) {
p.value.extra.raw = p.value.extra.raw.replace('caret-alt-down', 'chevron-down');
}
}
changed = true;
}
});
});
if (transformStringLiterals(root, j, applyAllClassTransforms)) {
changed = true;
}
if (transformTemplateLiterals(root, j, applyAllClassTransforms)) {
changed = true;
}
if (transformTemplateStringProperties(root, j)) {
changed = true;
}
const ICON_RENAMES = new Map([
['caretAltDownIcon', 'chevronDownIcon'],
['caretAltUpIcon', 'chevronUpIcon'],
['caretAltLeftIcon', 'chevronLeftIcon'],
['caretAltRightIcon', 'chevronRightIcon'],
['caretDoubleAltLeftIcon', 'chevronDoubleLeftIcon'],
['caretDoubleAltRightIcon', 'chevronDoubleRightIcon'],
]);
root.find(j.Identifier).forEach(path => {
const newName = ICON_RENAMES.get(path.node.name);
if (newName) {
path.node.name = newName;
changed = true;
}
});
if (!changed) {
return content;
}
return root.toSource({ quote: 'single' });
}
const SCRIPT_RE = /(<script(?![^>]*\bsrc\b)[^>]*>)([\s\S]*?)(<\/script>)/gi;
module.exports = function transformer(fileInfo, api) {
if (!/\.html?$/i.test(fileInfo.path)) {
return undefined;
}
const j = api.jscodeshift;
let source = fileInfo.source;
let changed = false;
source = source.replace(SCRIPT_RE, (match, open, content, close) => {
const transformed = transformScriptContent(content, j);
if (transformed !== content) {
changed = true;
return open + transformed + close;
}
return match;
});
return changed ? source : undefined;
};