'use strict';
import { nodeTypes, isIdentifier, isAsyncFunction } from '@flint/infra/analysis';
import { createEslintRule } from '@flint/infra/create-eslint-rule';
import { astStructuralKeys } from '@flint/infra/constants/rules-meta';

var __defProp=Object['defineProperty'],__name=(_0x27d32f,_0x43fd8b)=>__defProp(_0x27d32f,'name',{'value':_0x43fd8b,'configurable':!![]});const ruleName='no-naked-query-return';const ruleContent='\x0a###\x20**Must**\x20wrap\x20query\x20objects\x20in\x20`{\x20query\x20}`\x20when\x20returning\x20from\x20async\x20functions.\x0a\x0aDrizzle\x20query\x20builders\x20are\x20Promise-like\x20objects.\x20Returning\x20a\x20bare\x20query\x20variable\x20from\x20an\x20async\x20function\x20causes\x20JavaScript\x20to\x20implicitly\x20call\x20`.then()`\x20on\x20it,\x20executing\x20the\x20query\x20prematurely\x20—\x20before\x20all\x20modifiers\x20(filters,\x20joins,\x20pagination)\x20have\x20been\x20applied.\x0a\x0a####\x20Wrong\x0a\x0aasync\x20function\x20tryFilter(query:\x20SelectQuery,\x20...)\x20{\x0a\x20\x20\x20\x20applyFilterToQuery(query,\x20or(...orExpressions));\x0a\x20\x20\x20\x20return\x20query;\x0a}\x0a\x0a####\x20Correct\x0a\x0aasync\x20function\x20tryFilter(query:\x20SelectQuery,\x20...)\x20{\x0a\x20\x20\x20\x20applyFilterToQuery(query,\x20or(...orExpressions));\x0a\x20\x20\x20\x20return\x20{\x20query\x20};\x0a}\x0a\x0a####\x20Why\x0a\x0aBecause\x20Drizzle\x20query\x20builders\x20are\x20Promise-like\x20objects,\x20returning\x20them\x20directly\x20from\x20an\x20async\x20function\x20triggers\x20automatic\x20execution\x20of\x20the\x20query\x20before\x20all\x20intended\x20modifiers\x20(.limit(),\x20.join(),\x20etc.)\x20are\x20applied.';var noNakedQueryReturn = createEslintRule(ruleName,ruleContent,__name(function create(_0x46e383){return {'ReturnStatement'(_0x29b837){const _0x3b7650=getAsyncFunctionAncestor(_0x29b837);if(!_0x3b7650)return;if(!_0x29b837['argument'])return;const _0x42b6ad=_0x29b837['argument'];_0x42b6ad['type']===nodeTypes['identifier']&&isQueryLikeName(_0x42b6ad['name'])&&isFunctionParameter(_0x3b7650,_0x42b6ad['name'])&&_0x46e383['report']({'node':_0x29b837,'messageId':ruleName});}};},'create'));function isFunctionParameter(_0x2a182c,_0xee84f3){return _0x2a182c['params']['some'](_0xab247b=>{let _0x5a8cac=![];return isIdentifier(_0xab247b)&&(_0x5a8cac=_0xab247b['name']===_0xee84f3),_0xab247b['type']===nodeTypes['assignmentPattern']&&isIdentifier(_0xab247b['left'])&&(_0x5a8cac=_0xab247b['left']['name']===_0xee84f3),_0x5a8cac;});}__name(isFunctionParameter,'isFunctionParameter');function isQueryLikeName(_0x685fcf){return _0x685fcf['toLowerCase']()['endsWith'](astStructuralKeys['query']);}__name(isQueryLikeName,'isQueryLikeName');function getAsyncFunctionAncestor(_0x179a84){let _0x4c7481=_0x179a84['parent'];while(_0x4c7481){if(isAsyncFunction(_0x4c7481))return _0x4c7481;_0x4c7481=_0x4c7481['parent'];}return void(-0x1*0x1acf+0xcec+0xde3);}__name(getAsyncFunctionAncestor,'getAsyncFunctionAncestor');

export { noNakedQueryReturn as default, ruleName };
