Skip to content

Commit 7a336c1

Browse files
aparziAndrewKushnir
authored andcommitted
refactor(migrations): centralize parseTemplate method (#62983)
refactor - Moved parseTemplate logic to a shared utility function to improve code reuse and maintainability PR Close #62983
1 parent 6ddb250 commit 7a336c1

File tree

13 files changed

+62
-150
lines changed

13 files changed

+62
-150
lines changed

packages/core/schematics/migrations/control-flow-migration/cases.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@
88

99
import {visitAll} from '@angular/compiler';
1010

11-
import {
12-
ElementCollector,
13-
ElementToMigrate,
14-
endMarker,
15-
MigrateError,
16-
Result,
17-
startMarker,
18-
} from './types';
11+
import {ElementCollector, ElementToMigrate, endMarker, Result, startMarker} from './types';
1912
import {
2013
calculateNesting,
2114
getMainBlock,
2215
getOriginals,
2316
hasLineBreaks,
24-
parseTemplate,
2517
reduceNestingOffset,
2618
} from './util';
19+
import {MigrateError, parseTemplate} from '../../utils/parse_html';
2720

2821
export const boundcase = '[ngSwitchCase]';
2922
export const switchcase = '*ngSwitchCase';

packages/core/schematics/migrations/control-flow-migration/fors.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,17 @@
88

99
import {visitAll} from '@angular/compiler';
1010

11-
import {
12-
ElementCollector,
13-
ElementToMigrate,
14-
endMarker,
15-
MigrateError,
16-
Result,
17-
startMarker,
18-
} from './types';
11+
import {ElementCollector, ElementToMigrate, endMarker, Result, startMarker} from './types';
1912
import {
2013
calculateNesting,
2114
getMainBlock,
2215
getOriginals,
2316
getPlaceholder,
2417
hasLineBreaks,
25-
parseTemplate,
2618
PlaceholderKind,
2719
reduceNestingOffset,
2820
} from './util';
21+
import {MigrateError, parseTemplate} from '../../utils/parse_html';
2922

3023
export const ngfor = '*ngFor';
3124
export const nakedngfor = 'ngFor';

packages/core/schematics/migrations/control-flow-migration/ifs.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,16 @@
88

99
import {visitAll} from '@angular/compiler';
1010

11-
import {
12-
ElementCollector,
13-
ElementToMigrate,
14-
endMarker,
15-
MigrateError,
16-
Result,
17-
startMarker,
18-
} from './types';
11+
import {ElementCollector, ElementToMigrate, endMarker, Result, startMarker} from './types';
1912
import {
2013
calculateNesting,
2114
getMainBlock,
2215
getOriginals,
2316
getPlaceholder,
2417
hasLineBreaks,
25-
parseTemplate,
2618
reduceNestingOffset,
2719
} from './util';
20+
import {MigrateError, parseTemplate} from '../../utils/parse_html';
2821

2922
export const ngif = '*ngIf';
3023
export const boundngif = '[ngIf]';

packages/core/schematics/migrations/control-flow-migration/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {join, relative} from 'path';
1212
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';
1313

1414
import {migrateTemplate} from './migration';
15-
import {AnalyzedFile, MigrateError} from './types';
15+
import {AnalyzedFile} from './types';
1616
import {analyze} from './util';
1717
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
1818
import {normalizePath} from '../../utils/change_tracker';
19+
import {MigrateError} from '../../utils/parse_html';
1920

2021
interface Options {
2122
path?: string;

packages/core/schematics/migrations/control-flow-migration/migration.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@ import {migrateCase} from './cases';
1212
import {migrateFor} from './fors';
1313
import {migrateIf} from './ifs';
1414
import {migrateSwitch} from './switches';
15-
import {
16-
AnalyzedFile,
17-
endI18nMarker,
18-
endMarker,
19-
MigrateError,
20-
startI18nMarker,
21-
startMarker,
22-
} from './types';
15+
import {AnalyzedFile, endI18nMarker, endMarker, startI18nMarker, startMarker} from './types';
2316
import {
2417
canRemoveCommonModule,
2518
formatTemplate,
2619
processNgTemplates,
2720
removeImports,
2821
validateMigratedTemplate,
2922
} from './util';
23+
import {MigrateError} from '../../utils/parse_html';
3024

3125
/**
3226
* Actually migrates a given template to the new syntax

packages/core/schematics/migrations/control-flow-migration/switches.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@
99
import {Element, Node, Text, visitAll} from '@angular/compiler';
1010

1111
import {cases} from './cases';
12-
import {
13-
ElementCollector,
14-
ElementToMigrate,
15-
endMarker,
16-
MigrateError,
17-
Result,
18-
startMarker,
19-
} from './types';
12+
import {ElementCollector, ElementToMigrate, endMarker, Result, startMarker} from './types';
2013
import {
2114
calculateNesting,
2215
getMainBlock,
2316
getOriginals,
2417
hasLineBreaks,
25-
parseTemplate,
2618
reduceNestingOffset,
2719
} from './util';
20+
import {MigrateError, parseTemplate} from '../../utils/parse_html';
2821

2922
export const ngswitch = '[ngSwitch]';
3023

packages/core/schematics/migrations/control-flow-migration/types.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {
10-
Attribute,
11-
Block,
12-
Element,
13-
LetDeclaration,
14-
ParseTreeResult,
15-
RecursiveVisitor,
16-
Text,
17-
} from '@angular/compiler';
9+
import {Attribute, Block, Element, LetDeclaration, RecursiveVisitor, Text} from '@angular/compiler';
1810
import ts from 'typescript';
1911

2012
import {lookupIdentifiersInSourceFile} from './identifier-lookup';
@@ -116,18 +108,7 @@ export interface AliasAttributes {
116108
aliases: Map<string, string>;
117109
}
118110

119-
export interface ParseResult {
120-
tree: ParseTreeResult | undefined;
121-
errors: MigrateError[];
122-
}
123-
124-
/**
125-
* Represents an error that happened during migration
126-
*/
127-
export type MigrateError = {
128-
type: string;
129-
error: unknown;
130-
};
111+
export type {MigrateError} from '../../utils/parse_html';
131112

132113
/**
133114
* Represents an element with a migratable attribute

packages/core/schematics/migrations/control-flow-migration/util.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Attribute, Element, HtmlParser, Node, ParseTreeResult, visitAll} from '@angular/compiler';
9+
import {Attribute, Element, Node, ParseTreeResult, visitAll} from '@angular/compiler';
1010
import {dirname, join} from 'path';
1111
import ts from 'typescript';
1212

@@ -20,13 +20,12 @@ import {
2020
i18nCollector,
2121
importRemovals,
2222
importWithCommonRemovals,
23-
MigrateError,
24-
ParseResult,
2523
startI18nMarker,
2624
startMarker,
2725
Template,
2826
TemplateCollector,
2927
} from './types';
28+
import {MigrateError, parseTemplate} from '../../utils/parse_html';
3029

3130
const startMarkerRegex = new RegExp(startMarker, 'gm');
3231
const endMarkerRegex = new RegExp(endMarker, 'gm');
@@ -265,36 +264,6 @@ function getNestedCount(etm: ElementToMigrate, aggregator: number[]) {
265264
}
266265
}
267266

268-
/**
269-
* parses the template string into the Html AST
270-
*/
271-
export function parseTemplate(template: string): ParseResult {
272-
let parsed: ParseTreeResult;
273-
try {
274-
// Note: we use the HtmlParser here, instead of the `parseTemplate` function, because the
275-
// latter returns an Ivy AST, not an HTML AST. The HTML AST has the advantage of preserving
276-
// interpolated text as text nodes containing a mixture of interpolation tokens and text tokens,
277-
// rather than turning them into `BoundText` nodes like the Ivy AST does. This allows us to
278-
// easily get the text-only ranges without having to reconstruct the original text.
279-
parsed = new HtmlParser().parse(template, '', {
280-
// Allows for ICUs to be parsed.
281-
tokenizeExpansionForms: true,
282-
// Explicitly disable blocks so that their characters are treated as plain text.
283-
tokenizeBlocks: true,
284-
preserveLineEndings: true,
285-
});
286-
287-
// Don't migrate invalid templates.
288-
if (parsed.errors && parsed.errors.length > 0) {
289-
const errors = parsed.errors.map((e) => ({type: 'parse', error: e}));
290-
return {tree: undefined, errors};
291-
}
292-
} catch (e: any) {
293-
return {tree: undefined, errors: [{type: 'parse', error: e}]};
294-
}
295-
return {tree: parsed, errors: []};
296-
}
297-
298267
export function validateMigratedTemplate(migrated: string, fileName: string): MigrateError[] {
299268
const parsed = parseTemplate(migrated);
300269
let errors: MigrateError[] = [];

packages/core/schematics/migrations/output-migration/output-replacements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import ts from 'typescript';
1010

11-
import {ImportManager, PartialEvaluator} from '@angular/compiler-cli/private/migrations';
11+
import {ImportManager} from '@angular/compiler-cli/private/migrations';
1212
import {
1313
ProgramInfo,
1414
ProjectFile,

packages/core/schematics/migrations/self-closing-tags-migration/to-self-closing-tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
Text,
1414
visitAll,
1515
} from '@angular/compiler';
16-
import {parseTemplate} from './util';
16+
import {parseTemplate} from '../../utils/parse_html';
1717

1818
export function migrateTemplateToSelfClosingTags(template: string): {
1919
replacementCount: number;

0 commit comments

Comments
 (0)