Skip to content

Commit d6a0a6a

Browse files
committed
feat: add parsing of lwc:on directive
1 parent 69bb3c2 commit d6a0a6a

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/@lwc/errors/src/compiler/error-info/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
/**
8-
* Next error code: 1203
8+
* Next error code: 1204
99
*/
1010

1111
export * from './compiler';

packages/@lwc/errors/src/compiler/error-info/template-transform.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,12 @@ export const ParserDiagnostics = {
955955
level: DiagnosticLevel.Error,
956956
url: '',
957957
},
958+
959+
INVALID_LWC_ON_LITERAL_PROP: {
960+
code: 1203,
961+
message:
962+
'Invalid lwc:on usage on element "{0}". The directive binding must be an expression.',
963+
level: DiagnosticLevel.Error,
964+
url: '',
965+
},
958966
};

packages/@lwc/template-compiler/src/parser/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ const LWC_DIRECTIVE_PROCESSORS = [
884884
applyLwcInnerHtmlDirective,
885885
applyRefDirective,
886886
applyLwcSpreadDirective,
887+
applyLwcOnDirective,
887888
applyLwcSlotBindDirective,
888889
];
889890

@@ -972,6 +973,26 @@ function applyLwcSpreadDirective(
972973
element.directives.push(ast.spreadDirective(lwcSpreadAttr, lwcSpread.location));
973974
}
974975

976+
function applyLwcOnDirective(
977+
ctx: ParserCtx,
978+
parsedAttr: ParsedAttribute,
979+
element: BaseElement
980+
): void {
981+
const { name: tag } = element;
982+
983+
const lwcOn = parsedAttr.pick(ElementDirectiveName.On);
984+
if (!lwcOn) {
985+
return;
986+
}
987+
988+
const { value: lwcOnValue } = lwcOn;
989+
if (!ast.isExpression(lwcOnValue)) {
990+
ctx.throwOnNode(ParserDiagnostics.INVALID_LWC_ON_LITERAL_PROP, element, [`<${tag}>`]);
991+
}
992+
993+
element.directives.push(ast.OnDirective(lwcOnValue, lwcOn.location));
994+
}
995+
975996
function applyLwcExternalDirective(
976997
ctx: ParserCtx,
977998
parsedAttr: ParsedAttribute,

0 commit comments

Comments
 (0)