@schemcraft/control-flow
Version:
A schematic to replace *ngIf and *ngFor with Angular's new control flow syntax (@if and @for).
35 lines (34 loc) • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
function transformTemplate(template) {
template = template.replace(/\*ngIf="([^"]+?)\s*;\s*else\s+(\w+)"/g, (_match, condition, elseRef) => {
return `@if (${condition}) {`;
});
template = template.replace(/\*ngIf="([^"]+?)"/g, (_match, condition) => {
return `@if (${condition}) {`;
});
template = template.replace(/\*ngFor="let\s+(\w+)\s+of\s+([^"]+)"/g, (_match, item, list) => {
return `@for (${item} of ${list}) {`;
});
template = template.replace(/<ng-template\s+#(\w+)>/g, (_match, refName) => {
return `@else {`;
});
template = template.replace(/<\/[^>]+>/g, (match) => `} ${match}`);
return template;
}
function default_1() {
return (tree, _context) => {
tree.visit((filePath) => {
var _a;
if (filePath.endsWith('.html')) {
const content = (_a = tree.read(filePath)) === null || _a === void 0 ? void 0 : _a.toString();
if (content) {
const transformed = transformTemplate(content);
tree.overwrite(filePath, transformed);
}
}
});
return tree;
};
}