/*
 * Copyright (c) 2018, 2026, 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 Utils=require("../jetwc-lib/jetwcutils-componentAuditUtils");const TsxUtils=require("../jetwc-lib/jetwcutils-tsxUtils");const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-version-openrange"};ruleDef.prototype.getShortDescription=function(){return"Verify that any version ranges for dependencies have an upper bound"};ruleDef.prototype.getDescription=function(){return"This rule checks that the component is not promising to support all future JET versions or unknown future versions of dependent components"};ruleDef.prototype.register=function(context){return{compjson:this._doAudit,Program:this._doVComponentAudit}};ruleDef.prototype._doAudit=function(context){if(context.suppData.obj){let issue;const meta=context.suppData.obj;const fullName=Utils.getComponentFullName(meta);this._verifyJetVersion(context,fullName,meta.jetVersion);if(meta.dependencies){const deps=Object.keys(meta.dependencies);deps.forEach(dependency=>{const checkVersion=meta.dependencies[dependency];this._verifyDependencyVersion(context,fullName,dependency,checkVersion)})}}};ruleDef.prototype._doVComponentAudit=function(context){const componentInfo=TsxUtils.matchToVComponentImpl(context,context.filepath);if(componentInfo){this._verifyJetVersion(context,componentInfo.fullName,componentInfo.jetVersion);if(componentInfo.dependencies){componentInfo.dependencies.forEach(dependency=>{this._verifyDependencyVersion(context,componentInfo.fullName,dependency.name,dependency.range)})}}};ruleDef.prototype._hasNoUpper=function(auditContext,rangeString){let noUpper=false;if(rangeString){if(!auditContext.utils.semVerUtils.isValid(rangeString)&&auditContext.utils.semVerUtils.isValidRange(rangeString)){noUpper=auditContext.utils.semVerUtils.satisfiesRange("99999.0.0",rangeString)}}return noUpper};ruleDef.prototype._verifyJetVersion=function(auditContext,fullName,jetVersion){if(this._hasNoUpper(auditContext,jetVersion)){issue=new auditContext.Issue(`Component ${fullName} specifies a JET Range that has no upper limit '${jetVersion}'`);issue.setMsgKey(auditContext.ruleName+"_1");auditContext.reporter.addIssue(issue,auditContext)}};ruleDef.prototype._verifyDependencyVersion=function(auditContext,fullName,dependency,checkVersion){if(this._hasNoUpper(auditContext,checkVersion)){issue=new auditContext.Issue(`Component ${fullName} specifies a dependency range for ${dependency} that has no upper limit '${checkVersion}'`);issue.setMsgKey(auditContext.ruleName+"_2");auditContext.reporter.addIssue(issue,auditContext)}};module.exports=ruleDef;