/*
 * Copyright (c) 2018, 2025, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const path=require("path");const Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-events"};ruleDef.prototype.getShortDescription=function(){return"Checks that any events raised are declared"};ruleDef.prototype.getDescription=function(){return"This rule ensures that all events omitted from a component are declared in the metadata"};ruleDef.prototype.register=function(context){return{CallExpression:this._doAudit}};ruleDef.prototype._doAudit=function(context,node){const NT=context.utils.AstUtils.getNodeTypes();let issue;if(node.callee.type===NT.MEMBER_EXPR&&node.callee.property.type===NT.IDENTIFIER&&node.callee.property.name==="dispatchEvent"){if(node.arguments[0].type===NT.CALL_EXPR||node.arguments[0].type===NT.NEW_EXPR){if(node.arguments[0].callee.name==="CustomEvent"){const eventNameNode=node.arguments[0].arguments[0];if(eventNameNode.type===NT.LITERAL){const eventName=eventNameNode.value;const withinComponent=Utils.matchComponentPath(context,path.dirname(context.filepath));const componentInfo=Utils.componentInfo(context,withinComponent);if(withinComponent&&componentInfo.type==="composite"){if(!componentInfo.events||!componentInfo.events.hasOwnProperty(eventName)){issue=new context.Issue(`Event '${eventName}' raised for component ${withinComponent} has not been declared in the component metadata`);issue.setMsgKey(context.ruleName+"_1");context.reporter.addIssue(issue,context)}}else{issue=new context.Issue(`Custom event '${eventName}' raised by code outside of a web component and so cannot be verified against any component API`);issue.setMsgKey(context.ruleName+"_2");context.reporter.addIssue(issue,context)}}}}}};module.exports=ruleDef;