refactor(compiler): use options
argument for parsers (#28055)
This commit consolidates the options that can modify the parsing of text (e.g. HTML, Angular templates, CSS, i18n) into an AST for further processing into a single `options` hash. This makes the code cleaner and more readable, but also enables us to support further options to parsing without triggering wide ranging changes to code that should not be affected by these new options. Specifically, it will let us pass information about the placement of a template that is being parsed in its containing file, which is essential for accurate SourceMap processing. PR Close #28055
This commit is contained in:

committed by
Misko Hevery

parent
81df5dcfc0
commit
673ac2945c
@ -1562,20 +1562,35 @@ function interpolate(args: o.Expression[]): o.Expression {
|
||||
return o.importExpr(R3.interpolationV).callFn([o.literalArr(args)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that can be used to modify how a template is parsed by `parseTemplate()`.
|
||||
*/
|
||||
export interface ParseTemplateOptions {
|
||||
/**
|
||||
* Include whitespace nodes in the parsed output.
|
||||
*/
|
||||
preserveWhitespaces?: boolean;
|
||||
/**
|
||||
* How to parse interpolation markers.
|
||||
*/
|
||||
interpolationConfig?: InterpolationConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a template into render3 `Node`s and additional metadata, with no other dependencies.
|
||||
*
|
||||
* @param template text of the template to parse
|
||||
* @param templateUrl URL to use for source mapping of the parsed template
|
||||
* @param options options to modify how the template is parsed
|
||||
*/
|
||||
export function parseTemplate(
|
||||
template: string, templateUrl: string,
|
||||
options: {preserveWhitespaces?: boolean, interpolationConfig?: InterpolationConfig} = {}):
|
||||
{errors?: ParseError[], nodes: t.Node[]} {
|
||||
options: ParseTemplateOptions = {}): {errors?: ParseError[], nodes: t.Node[]} {
|
||||
const {interpolationConfig, preserveWhitespaces} = options;
|
||||
const bindingParser = makeBindingParser(interpolationConfig);
|
||||
const htmlParser = new HtmlParser();
|
||||
const parseResult = htmlParser.parse(template, templateUrl, true, interpolationConfig);
|
||||
const parseResult =
|
||||
htmlParser.parse(template, templateUrl, {...options, tokenizeExpansionForms: true});
|
||||
|
||||
if (parseResult.errors && parseResult.errors.length > 0) {
|
||||
return {errors: parseResult.errors, nodes: []};
|
||||
|
Reference in New Issue
Block a user