Skip to content

Commit

Permalink
feat: add parsing of lwc:on directive
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-rk9 committed Feb 27, 2025
1 parent 69bb3c2 commit d6a0a6a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/@lwc/errors/src/compiler/error-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
/**
* Next error code: 1203
* Next error code: 1204
*/

export * from './compiler';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,12 @@ export const ParserDiagnostics = {
level: DiagnosticLevel.Error,
url: '',
},

INVALID_LWC_ON_LITERAL_PROP: {
code: 1203,
message:
'Invalid lwc:on usage on element "{0}". The directive binding must be an expression.',
level: DiagnosticLevel.Error,
url: '',
},
};
21 changes: 21 additions & 0 deletions packages/@lwc/template-compiler/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ const LWC_DIRECTIVE_PROCESSORS = [
applyLwcInnerHtmlDirective,
applyRefDirective,
applyLwcSpreadDirective,
applyLwcOnDirective,
applyLwcSlotBindDirective,
];

Expand Down Expand Up @@ -972,6 +973,26 @@ function applyLwcSpreadDirective(
element.directives.push(ast.spreadDirective(lwcSpreadAttr, lwcSpread.location));
}

function applyLwcOnDirective(
ctx: ParserCtx,
parsedAttr: ParsedAttribute,
element: BaseElement
): void {
const { name: tag } = element;

const lwcOn = parsedAttr.pick(ElementDirectiveName.On);
if (!lwcOn) {
return;
}

const { value: lwcOnValue } = lwcOn;
if (!ast.isExpression(lwcOnValue)) {
ctx.throwOnNode(ParserDiagnostics.INVALID_LWC_ON_LITERAL_PROP, element, [`<${tag}>`]);
}

element.directives.push(ast.OnDirective(lwcOnValue, lwcOn.location));
}

function applyLwcExternalDirective(
ctx: ParserCtx,
parsedAttr: ParsedAttribute,
Expand Down

0 comments on commit d6a0a6a

Please sign in to comment.