@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
83 lines (76 loc) • 2.51 kB
JavaScript
;
const path = require('path');
const jscodeshift = require(path.resolve(__dirname, '../../../../build-v2/node_modules/jscodeshift'));
const { applyLayoutChangeRename } = require('../_shared.cjs');
function transformStringLiterals(root, j, fn) {
let changed = false;
root.find(j.StringLiteral).forEach(p => {
const updated = fn(p.node.value);
if (updated !== p.node.value) {
p.node.value = updated;
if (p.node.extra) {
p.node.extra.rawValue = updated;
if (p.node.extra.raw) {
const q = p.node.extra.raw[0];
p.node.extra.raw = q + updated + q;
}
}
changed = true;
}
});
root.find(j.Literal, n => typeof n.value === 'string').forEach(p => {
const updated = fn(p.node.value);
if (updated !== p.node.value) {
p.node.value = updated;
if (p.node.extra) {
p.node.extra.rawValue = updated;
if (p.node.extra.raw) {
const q = p.node.extra.raw[0];
p.node.extra.raw = q + updated + q;
}
}
changed = true;
}
});
return changed;
}
function transformTemplateLiterals(root, j, fn) {
let changed = false;
root.find(j.TemplateElement).forEach(p => {
const raw = p.node.value.raw;
const cooked = p.node.value.cooked;
const updatedRaw = fn(raw);
const updatedCooked = cooked != null ? fn(cooked) : cooked;
if (updatedRaw !== raw || updatedCooked !== cooked) {
p.node.value = { raw: updatedRaw, cooked: updatedCooked };
changed = true;
}
});
return changed;
}
module.exports = function transformer(fileInfo, api) {
if (!/\.html?$/i.test(fileInfo.path)) {
return undefined;
}
const scriptBlockRe = /(<script\b[^>]*>)([\s\S]*?)(<\/script>)/gi;
let changed = false;
const result = fileInfo.source.replace(scriptBlockRe, (match, open, body, close) => {
const j = (api && api.jscodeshift) ? api.jscodeshift : jscodeshift.withParser('babel');
let root;
try {
root = j(body);
} catch (e) {
return match;
}
const fn = applyLayoutChangeRename;
const c1 = transformStringLiterals(root, j, fn);
const c2 = transformTemplateLiterals(root, j, fn);
if (c1 || c2) {
changed = true;
return open + root.toSource({ quote: 'single' }) + close;
}
return match;
});
return changed ? result : undefined;
};
module.exports.aiInstructions = `Renames the Splitter layoutChange event to resize in inline <script> blocks inside HTML files.`;