Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec8c8f8

Browse files
crisbetoalxhub
authored andcommitted
refactor(compiler): allow name parsing logic to be reused (#60561)
Moves the logic for parsing event names out into methods on the `BindingParser` so we don't have to duplicate it. Also updates the types to more accurately represent the runtime value. PR Close #60561
1 parent 9f18c7c commit ec8c8f8

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

‎packages/compiler/src/expression_parser/ast.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ export class ParsedEvent {
790790
// Animation events have a phase
791791
constructor(
792792
name: string,
793-
targetOrPhase: string,
793+
targetOrPhase: string | null,
794794
type: ParsedEventType.TwoWay,
795795
handler: ASTWithSource<NonNullAssert | PropertyRead | KeyedRead>,
796796
sourceSpan: ParseSourceSpan,
@@ -800,7 +800,7 @@ export class ParsedEvent {
800800

801801
constructor(
802802
name: string,
803-
targetOrPhase: string,
803+
targetOrPhase: string | null,
804804
type: ParsedEventType,
805805
handler: ASTWithSource,
806806
sourceSpan: ParseSourceSpan,
@@ -810,7 +810,7 @@ export class ParsedEvent {
810810

811811
constructor(
812812
public name: string,
813-
public targetOrPhase: string,
813+
public targetOrPhase: string | null,
814814
public type: ParsedEventType,
815815
public handler: ASTWithSource,
816816
public sourceSpan: ParseSourceSpan,

‎packages/compiler/src/template_parser/binding_parser.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,16 @@ export class BindingParser {
660660
return calcPossibleSecurityContexts(this._schemaRegistry, selector, prop, isAttribute);
661661
}
662662

663+
parseEventListenerName(rawName: string): {eventName: string; target: string | null} {
664+
const [target, eventName] = splitAtColon(rawName, [null, rawName]);
665+
return {eventName: eventName!, target};
666+
}
667+
668+
parseAnimationEventName(rawName: string): {eventName: string; phase: string | null} {
669+
const matches = splitAtPeriod(rawName, [rawName, null]);
670+
return {eventName: matches[0]!, phase: matches[1] === null ? null : matches[1].toLowerCase()};
671+
}
672+
663673
private _parseAnimationEvent(
664674
name: string,
665675
expression: string,
@@ -668,9 +678,7 @@ export class BindingParser {
668678
targetEvents: ParsedEvent[],
669679
keySpan: ParseSourceSpan,
670680
) {
671-
const matches = splitAtPeriod(name, [name, '']);
672-
const eventName = matches[0];
673-
const phase = matches[1].toLowerCase();
681+
const {eventName, phase} = this.parseAnimationEventName(name);
674682
const ast = this._parseAction(expression, handlerSpan);
675683
targetEvents.push(
676684
new ParsedEvent(
@@ -713,7 +721,7 @@ export class BindingParser {
713721
keySpan: ParseSourceSpan,
714722
): void {
715723
// long format: 'target: eventName'
716-
const [target, eventName] = splitAtColon(name, [null!, name]);
724+
const {eventName, target} = this.parseEventListenerName(name);
717725
const prevErrorCount = this.errors.length;
718726
const ast = this._parseAction(expression, handlerSpan);
719727
const isValid = this.errors.length === prevErrorCount;

‎packages/compiler/src/util.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ export function dashCaseToCamelCase(input: string): string {
1212
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
1313
}
1414

15-
export function splitAtColon(input: string, defaultValues: string[]): string[] {
15+
export function splitAtColon(input: string, defaultValues: (string | null)[]): (string | null)[] {
1616
return _splitAt(input, ':', defaultValues);
1717
}
1818

19-
export function splitAtPeriod(input: string, defaultValues: string[]): string[] {
19+
export function splitAtPeriod(input: string, defaultValues: (string | null)[]): (string | null)[] {
2020
return _splitAt(input, '.', defaultValues);
2121
}
2222

23-
function _splitAt(input: string, character: string, defaultValues: string[]): string[] {
23+
function _splitAt(
24+
input: string,
25+
character: string,
26+
defaultValues: (string | null)[],
27+
): (string | null)[] {
2428
const characterIndex = input.indexOf(character);
2529
if (characterIndex == -1) return defaultValues;
2630
return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];

0 commit comments

Comments
 (0)