igniteui-angular
Version:
Ignite UI for Angular is a dependency-free Angular toolkit for building modern web apps
107 lines (106 loc) • 5.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const compiler_1 = require("@angular/compiler");
const UpdateChanges_1 = require("../common/UpdateChanges");
const util_1 = require("../common/util");
const import_helper_js_1 = require("igniteui-angular/migrations/common/import-helper.js");
const version = '20.0.6';
exports.default = () => (host, context) => __awaiter(void 0, void 0, void 0, function* () {
context.logger.info(`Applying migration for Ignite UI for Angular to version ${version}`);
const { HtmlParser } = yield (0, import_helper_js_1.nativeImport)('@angular/compiler');
const update = new UpdateChanges_1.UpdateChanges(__dirname, host, context);
const changes = new Map();
const parser = new HtmlParser();
const warnMsg = "Manual migration needed: please use 'disableFiltering' instead of filteringOptions.filterable." +
"Since it has been deprecated.'";
const applyChanges = () => {
for (const [path, fileChanges] of changes.entries()) {
let content = host.read(path).toString();
fileChanges.sort((a, b) => b.position - a.position).forEach(c => {
content = c.apply(content);
});
host.overwrite(path, content);
}
};
const addChange = (path, change) => {
if (!changes.has(path)) {
changes.set(path, []);
}
changes.get(path).push(change);
};
const COMBO_TAGS = ['igx-simple-combo', 'igx-combo'];
for (const path of update.templateFiles) {
const nodes = (0, util_1.findElementNodes)((0, util_1.parseFile)(parser, host, path), COMBO_TAGS);
for (const node of nodes) {
if (!(node instanceof compiler_1.Element))
continue;
const hasDisableFiltering = node.attrs.some(a => a.name.includes('disableFiltering'));
const attr = node.attrs.find(a => a.name === '[filteringOptions]');
if (!attr)
continue;
const attrVal = attr.value.trim();
const offset = (0, util_1.getSourceOffset)(node);
const file = offset.file;
let replacementText = '';
if (attrVal.startsWith('{')) {
// inline object literal
const normalized = attrVal
.replace(/'/g, '"')
.replace(/([{,]\s*)(\w+)\s*:/g, '$1"$2":');
const parsed = JSON.parse(normalized);
const filterable = parsed.filterable;
if (filterable === false && !hasDisableFiltering) {
replacementText += `[disableFiltering]="true"`;
}
const remaining = Object.assign({}, parsed);
delete remaining.filterable;
const remainingProps = Object.entries(remaining)
.map(([k, v]) => `${k}: ${JSON.stringify(v)}`)
.join(', ');
if (remainingProps.length > 0) {
replacementText += ` [filteringOptions]="{ ${remainingProps} }"`;
}
// Replace whole [filteringOptions] attribute
const match = node.sourceSpan.toString().match(/\[filteringOptions\]="([^"]+)"/);
if (match) {
const attrText = match[0];
const attrPos = file.content.indexOf(attrText, offset.startTag.start);
addChange(file.url, new util_1.FileChange(attrPos, replacementText, attrText, 'replace'));
}
}
else {
// log for manual TS edit
const comment = `\n<!-- ${warnMsg} -->\n`;
addChange(file.url, new util_1.FileChange(offset.startTag.end, comment));
}
}
}
applyChanges();
for (const path of update.tsFiles) {
const content = host.read(path).toString();
const lines = content.split('\n');
const newLines = [];
let modified = false;
for (const line of lines) {
if (/\.filteringOptions\.filterable\s*=/.test(line) ||
/\.filteringOptions\s*=/.test(line)) {
newLines.push('// ' + warnMsg);
modified = true;
}
newLines.push(line);
}
if (modified) {
host.overwrite(path, newLines.join('\n'));
}
}
update.applyChanges();
});