/*
 * 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 RULE_NAME="oj-js-no-ext-final";const LONG_DESCR="Starting in 8.0.0, constructors for classes marked with @final in the JET JS Doc cannot be invoked without use of 'new'";const MSG_PART="' constructor without 'new' will fail in version 8.x or later";const OJ="oj";const OJDOT="oj.";var lookup;function getName(){return RULE_NAME};function getShortDescription(){return"Classes marked @final must be constructed with 'new'"};function getDescription(){return LONG_DESCR};function register(){return{CallExpression:_fnCall,MemberExpression:_fnMemExpr,file:_fnNewFile,ImportDeclaration:_fnImport}};function _fnNewFile(context){lookup={}};function _fnImport(context,node){var obj;obj=context.utils.AstUtils.parseImport(node);if(obj){_getClassNames(context,obj);for(var name in obj){obj[name]=name}Object.assign(lookup,obj)}};function _fnCall(context,node){let callee,obj,exit=false;if(!node.callee){return}callee=node.callee;if(callee){if(callee.name==="define"){obj=context.utils.AstUtils.parseDefine(node);exit=true}else if(callee.name==="require"){obj=_parseRequire(context,node);exit=true}if(obj){_getClassNames(context,obj);Object.assign(lookup,obj)}if(exit){return}}if(_specialCase(callee)){return}return _handleCall(context,node,callee)};function _handleCall(context,node,callee){var clazz,ojclazz,obj,classBase,pos,immed=false;if(callee.type==="Identifier"){clazz=callee.name;pos=callee;immed=true}else if(callee.type==="MemberExpression"&&callee.object){if(callee.object.type==="NewExpression"){return}if(callee.object.type==="Identifier"){if(callee.object.name===OJ){if(callee.property.type==="Identifier"){clazz=OJDOT+callee.property.name;pos=callee.object.property}}else{clazz=callee.object.name;pos=callee.object}}else if(callee.object.type==="MemberExpression"){if(callee.object.object&&callee.object.object.type==="Identifier"&&callee.object.object.name===OJ){classBase=callee.object.property.name;clazz=OJDOT+classBase}}}if(clazz){ojclazz=lookup[clazz];if(ojclazz){if(_checkFinal(context,ojclazz,pos)){return}}else if(!immed){ojclazz=clazz;if(context.utils.metaLib.isClass(ojclazz)){obj=_checkStatic(context,ojclazz,callee);if(obj.exists&&!obj.static){_emitIssue("uninstantiated '"+ojclazz+" : use of non-static method '"+obj.fn+"' will fail in JET 8.x or higher",context,pos)}else if(!obj.exists){if(callee.property&&callee.property.type==="Identifier"&&callee.property.name){if(clazz.substring(3)===callee.property.name||callee.property.name==="call"||callee.property.name==="apply"){_emitIssue("'"+clazz+MSG_PART,context)}}}}}}};function _emitIssue(msg,context,pos){var issue=new context.Issue(msg);if(pos){issue.setPosition(null,null,pos.start,pos.end)}context.reporter.addIssue(issue,context)}function _fnMemExpr(context,node){let ojclazz,attr;if(node.parent&&node.parent.type==="CallExpression"){return}if(node.object&&node.object.type&&node.object.type==="MemberExpression"){if(node.object.object&&node.object.object.type==="Identifier"&&node.object.property){if(node.object.property.type==="Identifier"){ojclazz=node.object.object.name+"."+node.object.property.name;attr=node.property.name;if(context.utils.metaLib.isClassMember(ojclazz,attr)&&!context.utils.metaLib.isClassMemberStatic(ojclazz,attr)){_emitIssue("uninstantiated '"+ojclazz+" : use of non-static member '"+attr+"' will fail in JET 8.x or higher",context,node.property)}}}}};function _checkFinal(context,clazz,pos){var ret=false;if(context.utils.metaLib.isClassFinal(clazz)){_emitIssue("'"+clazz+MSG_PART,context,pos);ret=true}return ret};function _getClassNames(context,obj){var clazz,module,name;if(obj){for(name in obj){module=obj[name];clazz=context.utils.metaLib.getClassFromModule(module);obj[name]=clazz}}};function _parseRequire(context,node,obj){var module,clazz,p;obj=obj||null;if(node.arguments){module=node.arguments[0];if(module&&module.type==="Literal"){module=module.value;p=node.parent;if(p.type==="VariableDeclarator"){if(p.id&&p.id.type==="Identifier"){clazz=p.id.name}}else if(p.type==="AssignmentExpression"){if(p.left.type==="Identifier"){clazz=p.left.name}}if(clazz){obj=obj||{};obj[clazz]=module}}}return obj};function _specialCase(callee){var obj;if(callee){if(callee.object&&callee.object.name){if(callee.object.object){return false}obj=callee.object.name;if(obj==="Object"){return true}}}return false};function _checkStatic(context,ojclazz,callee){var name;var ret={};if(context.utils.metaLib.isClassFinal(ojclazz)){if(callee.property&&callee.property.type==="Identifier"&&callee.property.name){name=callee.property.name;ret.fn=name;if(context.utils.metaLib.isClassMethod(ojclazz,name)){ret.exists=true;ret.static=context.utils.metaLib.isClassMethodStatic(ojclazz,name);return ret}}}ret.fn=null;return ret};module.exports={getName,getDescription,getShortDescription,register};