Skip to content

Commit

Permalink
fix(migrations): Serializer Visitor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaraivanov committed Oct 4, 2023
1 parent 998dc63 commit 7abccfb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
29 changes: 21 additions & 8 deletions projects/igniteui-angular/migrations/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { WorkspaceSchema, WorkspaceProject } from '@schematics/angular/utility/w
import { execSync } from 'child_process';
import {
Attribute,
Block,
BlockGroup,
BlockParameter,
Comment,
Element,
Expansion,
Expand Down Expand Up @@ -245,10 +248,10 @@ class SerializerVisitor implements Visitor {

public visitElement(element: Element, _context: any): any {
if (this.getHtmlTagDefinition(element.name).isVoid) {
return `<${element.name}${this._visitAll(element.attrs, ' ')}/>`;
return `<${element.name}${this._visitAll(element.attrs, ' ', ' ')}/>`;
}

return `<${element.name}${this._visitAll(element.attrs, ' ')}>${this._visitAll(element.children)}</${element.name}>`;
return `<${element.name}${this._visitAll(element.attrs, ' ', ' ')}>${this._visitAll(element.children)}</${element.name}>`;
}

public visitAttribute(attribute: Attribute, _context: any): any {
Expand All @@ -271,11 +274,21 @@ class SerializerVisitor implements Visitor {
return ` ${expansionCase.value} {${this._visitAll(expansionCase.expression)}}`;
}

private _visitAll(nodes: Node[], join = ''): string {
if (nodes.length === 0) {
return '';
}
return join + nodes.map(a => a.visit(this, null)).join(join);
public visitBlock(block: Block, _context: any) {
const params =
block.parameters.length === 0 ? ' ' : ` (${this._visitAll(block.parameters, ';', ' ')}) `;
return `@${block.name}${params}{${this._visitAll(block.children)}}`;
}

public visitBlockParameter(parameter: BlockParameter, _context: any) {
return parameter.expression;
}

// XXX: This **HAS** to be removed in the next major release of Angular (v17) as it will error out again
public visitBlockGroup(_group: BlockGroup, _context: any) { }

private _visitAll(nodes: Node[], separator = '', prefix = ''): string {
return nodes.length > 0 ? prefix + nodes.map(a => a.visit(this, null)).join(separator) : '';
}
}

Expand All @@ -286,7 +299,7 @@ export const serializeNodes = (nodes: Node[], getHtmlTagDefinition: (tagName: st

export const makeNgIf = (name: string, value: string) => name.startsWith('[') && value !== 'true';

export const stringifyAttriutes = (attributes: Attribute[]) => {
export const stringifyAttributes = (attributes: Attribute[]) => {
let stringAttributes = '';
attributes.forEach(element => {
// eslint-disable-next-line max-len
Expand Down
10 changes: 6 additions & 4 deletions projects/igniteui-angular/migrations/update-12_1_0/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Element } from '@angular/compiler';
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { UpdateChanges } from '../common/UpdateChanges';
import { FileChange, findElementNodes, getAttribute, getSourceOffset, hasAttribute, parseFile,
serializeNodes, makeNgIf, stringifyAttriutes } from '../common/util';
import {
FileChange, findElementNodes, getAttribute, getSourceOffset, hasAttribute, parseFile,
serializeNodes, makeNgIf, stringifyAttributes
} from '../common/util';
import { nativeImport } from '../common/import-helper.js';

const version = '12.1.0';
Expand All @@ -19,7 +21,7 @@ export default (): Rule => async (host: Tree, context: SchematicContext) => {
const warnMsg = `\n<!-- Auto migrated template content. Please, check your bindings! -->\n`;
const deprecatedToken = 'IgxGridTransaction';
const providerWarnMsg = `/* Injection token 'IgxGridTransaction' has been deprecated. ` +
`Please refer to the update guide for more details. */`;
`Please refer to the update guide for more details. */`;
const templateNames = [];

const applyChanges = () => {
Expand Down Expand Up @@ -64,7 +66,7 @@ export default (): Rule => async (host: Tree, context: SchematicContext) => {
const paginatorTemplate = ngTemplates.filter(template => hasAttribute(template as Element, `#${paginationTemplateName?.value}`))[0];
if (paginatorTemplate && checkForPaginatorInTemplate(path, paginationTemplateName?.value)) {
const pgCmpt = findElementNodes((paginatorTemplate as Element).children, 'igx-paginator')[0];
return `\n<igx-paginator${isChildGrid ? ' *igxPaginator' : ''}${stringifyAttriutes((pgCmpt as Element).attrs)}></igx-paginator>`;
return `\n<igx-paginator${isChildGrid ? ' *igxPaginator' : ''}${stringifyAttributes((pgCmpt as Element).attrs)}></igx-paginator>`;
} else {
// eslint-disable-next-line max-len
return `\n<igx-paginator${isChildGrid ? ' *igxPaginator' : ''}${makeNgIf(propName, value) ? ` *ngIf="${value}"` : ''}>${moveTemplate(paginatorTemplate)}</igx-paginator>`;
Expand Down

0 comments on commit 7abccfb

Please sign in to comment.