@interlace/eslint-devkit 1.21.1 → 1.21.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interlace/eslint-devkit",
3
- "version": "1.21.1",
3
+ "version": "1.21.3",
4
4
  "description": "Toolkit for building ESLint plugins in TypeScript — AST helpers, type utilities, rule-creation scaffolding, security benchmarks, and SARIF output.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isPathAlias=isPathAlias;exports.sourceMatches=sourceMatches;exports.eitherOrigin=eitherOrigin;exports.isFunctionNode=isFunctionNode;exports.writtenAnnotation=writtenAnnotation;exports.createOriginClassifier=createOriginClassifier;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const module_evidence_1=require("./module-evidence");const value_follow_1=require("./value-follow");const PLATFORM_GLOBALS=new Set(["Array","Map","Set","WeakMap","WeakSet","Object","JSON","Math","Promise","Reflect","URL","URLSearchParams","Headers","FormData","Date","globalThis","window","document","console","process","Buffer"]);function isPathAlias(specifier){return/^(?:[@~]\/|~|#)/.test(specifier)}function sourceMatches(source,modules){return modules.some(m=>source===m||source.startsWith(`${m}/`))}function eitherOrigin(a,b){if(a==="ours"||b==="ours")return"ours";return a==="foreign"&&b==="foreign"?"foreign":"unknown"}function isFunctionNode(node){return node.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||node.type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression}function writtenAnnotation(identifier){const holder=identifier;return holder.typeAnnotation?.typeAnnotation}function enclosingClass(node){for(let n=node.parent;n;n=n.parent){if(n.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||n.type===ast_node_types_1.AST_NODE_TYPES.ClassExpression){return n}}return void 0}function typeRoot(name){let cursor=name;while(cursor.type===ast_node_types_1.AST_NODE_TYPES.TSQualifiedName)cursor=cursor.left;return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor:null}function createOriginClassifier(modules){const loadsDriver=(0,module_evidence_1.createModuleEvidence)({packages:modules});const classOrigin=(cls,value,depth,seen)=>cls.superClass===null?"foreign":classify((0,value_follow_1.valueAt)(cls.superClass,value.env),depth+1,seen);const declarationOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration){return typeOrigin((0,value_follow_1.valueAt)(node.typeAnnotation,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration){return node.extends.map(base=>classify((0,value_follow_1.valueAt)(base.expression,value.env),depth+1,seen)).includes("ours")?"ours":"foreign"}return classify(value,depth+1,seen)};const typeOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSUnionType||node.type===ast_node_types_1.AST_NODE_TYPES.TSIntersectionType){return node.types.map(t=>typeOrigin((0,value_follow_1.valueAt)(t,value.env),depth+1,seen)).reduce(eitherOrigin)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeQuery){const root2=node.exprName.type===ast_node_types_1.AST_NODE_TYPES.TSImportType?null:typeRoot(node.exprName);return root2===null?"unknown":classify((0,value_follow_1.valueAt)(root2,value.env),depth+1,seen)}if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSTypeReference)return"foreign";const root=typeRoot(node.typeName);const variable=(0,value_follow_1.lookup)(root.name,value.env.scopeOf(root));if(variable===null)return"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const origin=(0,value_follow_1.importOrigin)(variable,value.env);const exported=origin===null||origin.name==="*"?null:(0,value_follow_1.exportedValue)(origin.module,origin.name,value.env.parser);return exported===null?"unknown":declarationOrigin(exported,depth+1,seen)}return declarationOrigin((0,value_follow_1.valueAt)(variable.defs[0].node,value.env),depth,seen)};const thisOrigin=(at,key,value,depth,seen)=>{const cls=enclosingClass(at);if(cls===void 0)return"unknown";for(const element of key===null?[]:cls.body.body){if(element.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition&&!element.computed&&element.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&element.key.name===key){if(element.typeAnnotation){return typeOrigin((0,value_follow_1.valueAt)(element.typeAnnotation.typeAnnotation,value.env),depth+1,seen)}return element.value===null?"unknown":classify((0,value_follow_1.valueAt)(element.value,value.env),depth+1,seen)}if(element.type!==ast_node_types_1.AST_NODE_TYPES.MethodDefinition||element.kind!=="constructor"){continue}for(const param of element.value.params){if(param.type!==ast_node_types_1.AST_NODE_TYPES.TSParameterProperty)continue;const inner=param.parameter.type===ast_node_types_1.AST_NODE_TYPES.AssignmentPattern?param.parameter.left:param.parameter;if(inner.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||inner.name!==key)continue;const annotation=writtenAnnotation(inner);return annotation===void 0?"unknown":typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}for(const statement of element.value.body?.body??[]){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.left.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statement.expression.left.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&(0,spellings_1.propertyName)(statement.expression.left)===key){return classify((0,value_follow_1.valueAt)(statement.expression.right,value.env),depth+1,seen)}}}return classOrigin(cls,value,depth,seen)==="ours"?"ours":"unknown"};const parameterOrigin=(identifier,fn,value,depth,seen)=>{const annotation=writtenAnnotation(identifier);if(annotation!==void 0){return typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}const call=fn.parent;if(call?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&call.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&call.arguments.some(argument=>argument===fn)&&classify((0,value_follow_1.valueAt)(call.callee.object,value.env),depth+1,seen)==="ours"){return"ours"}return"unknown"};const identifierOrigin=(node,value,depth,seen)=>{const variable=(0,value_follow_1.lookup)(node.name,value.scope);if(variable===null)return PLATFORM_GLOBALS.has(node.name)?"foreign":"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const followed2=(0,value_follow_1.follow)(value);const judged=followed2.node===value.node||followed2.node.type===ast_node_types_1.AST_NODE_TYPES.Program?"unknown":classify(followed2,depth+1,seen);if(judged!=="unknown")return judged;const origin=(0,value_follow_1.importOrigin)(variable,value.env);return origin!==null&&loadsDriver(origin.module.ast)?"ours":judged}const def=variable.defs[0];if(def.type==="Parameter"){return parameterOrigin(variable.identifiers[0],def.node,value,depth,seen)}if(def.type==="ClassName"){return classOrigin(def.node,value,depth,seen)}if(def.type!=="Variable")return"foreign";const annotation=writtenAnnotation(def.node.id);if(annotation!==void 0){const typed=typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen);if(typed!=="unknown")return typed}const followed=(0,value_follow_1.follow)(value);if(followed.node!==value.node)return classify(followed,depth+1,seen);const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>classify((0,value_follow_1.valueAt)(r.writeExpr,value.env),depth+1,seen));return writes.length===0?"unknown":writes.reduce(eitherOrigin)};function classify(value,depth=0,seen=new Set){if(depth>value_follow_1.MAX_FOLLOW_DEPTH||seen.has(value.node))return"unknown";seen.add(value.node);const node=(0,type_syntax_1.unwrapTypeSyntax)(value.node);const inner=target=>classify((0,value_follow_1.valueAt)(target,value.env),depth+1,seen);switch(node.type){case ast_node_types_1.AST_NODE_TYPES.AwaitExpression:return inner(node.argument);case ast_node_types_1.AST_NODE_TYPES.ChainExpression:return inner(node.expression);case ast_node_types_1.AST_NODE_TYPES.Identifier:return identifierOrigin(node,(0,value_follow_1.valueAt)(node,value.env),depth,seen);case ast_node_types_1.AST_NODE_TYPES.ThisExpression:return thisOrigin(node,null,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.MemberExpression:{if(node.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression)return thisOrigin(node,(0,spellings_1.propertyName)(node),value,depth,seen);const followed=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node,value.env));if(followed.node!==node)return classify(followed,depth+1,seen);return inner(node.object)}case ast_node_types_1.AST_NODE_TYPES.CallExpression:{const callee=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node.callee,value.env));if(isFunctionNode(callee.node)){const returned=(0,value_follow_1.returnedValue)(callee.node);return returned===null?"unknown":classify((0,value_follow_1.valueAt)(returned,callee.env),depth+1,seen)}return inner(node.callee)}case ast_node_types_1.AST_NODE_TYPES.NewExpression:return inner(node.callee);case ast_node_types_1.AST_NODE_TYPES.ClassDeclaration:case ast_node_types_1.AST_NODE_TYPES.ClassExpression:return classOrigin(node,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.LogicalExpression:return eitherOrigin(inner(node.left),inner(node.right));case ast_node_types_1.AST_NODE_TYPES.ConditionalExpression:return eitherOrigin(inner(node.consequent),inner(node.alternate));case ast_node_types_1.AST_NODE_TYPES.Literal:case ast_node_types_1.AST_NODE_TYPES.TemplateLiteral:case ast_node_types_1.AST_NODE_TYPES.ObjectExpression:case ast_node_types_1.AST_NODE_TYPES.ArrayExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionExpression:case ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration:return"foreign";default:return"unknown"}}return Object.assign(value=>classify(value),{ofType:value=>typeOrigin(value,0,new Set)})}
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isPathAlias=isPathAlias;exports.sourceMatches=sourceMatches;exports.eitherOrigin=eitherOrigin;exports.isFunctionNode=isFunctionNode;exports.writtenAnnotation=writtenAnnotation;exports.createOriginClassifier=createOriginClassifier;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const module_evidence_1=require("./module-evidence");const value_follow_1=require("./value-follow");const PLATFORM_GLOBALS=new Set(["Array","Map","Set","WeakMap","WeakSet","Object","JSON","Math","Promise","Reflect","URL","URLSearchParams","Headers","FormData","Date","globalThis","window","document","console","process","Buffer"]);function isPathAlias(specifier){return/^(?:[@~]\/|~|#)/.test(specifier)}function sourceMatches(source,modules){return modules.some(m=>source===m||source.startsWith(`${m}/`))}function eitherOrigin(a,b){if(a==="ours"||b==="ours")return"ours";return a==="foreign"&&b==="foreign"?"foreign":"unknown"}function isFunctionNode(node){return node.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||node.type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression}function writtenAnnotation(identifier){const holder=identifier;return holder.typeAnnotation?.typeAnnotation}function enclosingClass(node){for(let n=node.parent;n;n=n.parent){if(n.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||n.type===ast_node_types_1.AST_NODE_TYPES.ClassExpression){return n}}return void 0}function typeRoot(name){let cursor=name;while(cursor.type===ast_node_types_1.AST_NODE_TYPES.TSQualifiedName)cursor=cursor.left;return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor:null}function createOriginClassifier(modules){const loadsDriver=(0,module_evidence_1.createModuleEvidence)({packages:modules});const classOrigin=(cls,value,depth,seen)=>cls.superClass===null?"foreign":classify((0,value_follow_1.valueAt)(cls.superClass,value.env),depth+1,seen);const declarationOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration){return typeOrigin((0,value_follow_1.valueAt)(node.typeAnnotation,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration){return node.extends.map(base=>classify((0,value_follow_1.valueAt)(base.expression,value.env),depth+1,seen)).includes("ours")?"ours":"foreign"}return classify(value,depth+1,seen)};const typeOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSUnionType||node.type===ast_node_types_1.AST_NODE_TYPES.TSIntersectionType){return node.types.map(t=>typeOrigin((0,value_follow_1.valueAt)(t,value.env),depth+1,seen)).reduce(eitherOrigin)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeQuery){const root2=node.exprName.type===ast_node_types_1.AST_NODE_TYPES.TSImportType?null:typeRoot(node.exprName);return root2===null?"unknown":classify((0,value_follow_1.valueAt)(root2,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSAnyKeyword||node.type===ast_node_types_1.AST_NODE_TYPES.TSUnknownKeyword){return"unknown"}if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSTypeReference)return"foreign";const root=typeRoot(node.typeName);const variable=(0,value_follow_1.lookup)(root.name,value.env.scopeOf(root));if(variable===null)return"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const origin=(0,value_follow_1.importOrigin)(variable,value.env);const exported=origin===null||origin.name==="*"?null:(0,value_follow_1.exportedValue)(origin.module,origin.name,value.env.parser);return exported===null?"unknown":declarationOrigin(exported,depth+1,seen)}return declarationOrigin((0,value_follow_1.valueAt)(variable.defs[0].node,value.env),depth,seen)};const thisOrigin=(at,key,value,depth,seen)=>{const cls=enclosingClass(at);if(cls===void 0)return"unknown";for(const element of key===null?[]:cls.body.body){if(element.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition&&!element.computed&&element.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&element.key.name===key){if(element.typeAnnotation){return typeOrigin((0,value_follow_1.valueAt)(element.typeAnnotation.typeAnnotation,value.env),depth+1,seen)}return element.value===null?"unknown":classify((0,value_follow_1.valueAt)(element.value,value.env),depth+1,seen)}if(element.type!==ast_node_types_1.AST_NODE_TYPES.MethodDefinition||element.kind!=="constructor"){continue}for(const param of element.value.params){if(param.type!==ast_node_types_1.AST_NODE_TYPES.TSParameterProperty)continue;const inner=param.parameter.type===ast_node_types_1.AST_NODE_TYPES.AssignmentPattern?param.parameter.left:param.parameter;if(inner.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||inner.name!==key)continue;const annotation=writtenAnnotation(inner);return annotation===void 0?"unknown":typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}for(const statement of element.value.body?.body??[]){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.left.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statement.expression.left.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&(0,spellings_1.propertyName)(statement.expression.left)===key){return classify((0,value_follow_1.valueAt)(statement.expression.right,value.env),depth+1,seen)}}}return classOrigin(cls,value,depth,seen)==="ours"?"ours":"unknown"};const parameterOrigin=(identifier,fn,value,depth,seen)=>{const annotation=writtenAnnotation(identifier);if(annotation!==void 0){return typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}const call=fn.parent;if(call?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&call.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&call.arguments.some(argument=>argument===fn)&&classify((0,value_follow_1.valueAt)(call.callee.object,value.env),depth+1,seen)==="ours"){return"ours"}return"unknown"};const identifierOrigin=(node,value,depth,seen)=>{const variable=(0,value_follow_1.lookup)(node.name,value.scope);if(variable===null)return PLATFORM_GLOBALS.has(node.name)?"foreign":"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const followed2=(0,value_follow_1.follow)(value);const judged=followed2.node===value.node||followed2.node.type===ast_node_types_1.AST_NODE_TYPES.Program?"unknown":classify(followed2,depth+1,seen);if(judged!=="unknown")return judged;const origin=(0,value_follow_1.importOrigin)(variable,value.env);return origin!==null&&loadsDriver(origin.module.ast)?"ours":judged}const def=variable.defs[0];if(def.type==="Parameter"){return parameterOrigin(variable.identifiers[0],def.node,value,depth,seen)}if(def.type==="ClassName"){return classOrigin(def.node,value,depth,seen)}if(def.type!=="Variable")return"foreign";const annotation=writtenAnnotation(def.node.id);if(annotation!==void 0){const typed=typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen);if(typed!=="unknown")return typed}const followed=(0,value_follow_1.follow)(value);if(followed.node!==value.node)return classify(followed,depth+1,seen);const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>classify((0,value_follow_1.valueAt)(r.writeExpr,value.env),depth+1,seen));return writes.length===0?"unknown":writes.reduce(eitherOrigin)};function classify(value,depth=0,seen=new Set){if(depth>value_follow_1.MAX_FOLLOW_DEPTH||seen.has(value.node))return"unknown";seen.add(value.node);const node=(0,type_syntax_1.unwrapTypeSyntax)(value.node);const inner=target=>classify((0,value_follow_1.valueAt)(target,value.env),depth+1,seen);switch(node.type){case ast_node_types_1.AST_NODE_TYPES.AwaitExpression:return inner(node.argument);case ast_node_types_1.AST_NODE_TYPES.ChainExpression:return inner(node.expression);case ast_node_types_1.AST_NODE_TYPES.Identifier:return identifierOrigin(node,(0,value_follow_1.valueAt)(node,value.env),depth,seen);case ast_node_types_1.AST_NODE_TYPES.ThisExpression:return thisOrigin(node,null,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.MemberExpression:{if(node.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression)return thisOrigin(node,(0,spellings_1.propertyName)(node),value,depth,seen);const followed=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node,value.env));if(followed.node!==node)return classify(followed,depth+1,seen);return inner(node.object)}case ast_node_types_1.AST_NODE_TYPES.CallExpression:{const callee=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node.callee,value.env));if(isFunctionNode(callee.node)){const returned=(0,value_follow_1.returnedValue)(callee.node);return returned===null?"unknown":classify((0,value_follow_1.valueAt)(returned,callee.env),depth+1,seen)}return inner(node.callee)}case ast_node_types_1.AST_NODE_TYPES.NewExpression:return inner(node.callee);case ast_node_types_1.AST_NODE_TYPES.ClassDeclaration:case ast_node_types_1.AST_NODE_TYPES.ClassExpression:return classOrigin(node,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.LogicalExpression:return eitherOrigin(inner(node.left),inner(node.right));case ast_node_types_1.AST_NODE_TYPES.ConditionalExpression:return eitherOrigin(inner(node.consequent),inner(node.alternate));case ast_node_types_1.AST_NODE_TYPES.Literal:case ast_node_types_1.AST_NODE_TYPES.TemplateLiteral:case ast_node_types_1.AST_NODE_TYPES.ObjectExpression:case ast_node_types_1.AST_NODE_TYPES.ArrayExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionExpression:case ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration:return"foreign";default:return"unknown"}}return Object.assign(value=>classify(value),{ofType:value=>typeOrigin(value,0,new Set)})}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.looksLikeSqlStatement=looksLikeSqlStatement;exports.createSqlInjectionRule=createSqlInjectionRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const sql_safety_1=require("./sql-safety");const sql_position_1=require("./sql-position");const value_follow_1=require("./value-follow");const SQL_STATEMENTS=[/^[\s(]*select\b[\s\S]*\bfrom\b/i,/^[\s(]*select\s/i,/^\s*insert\s+(?:or\s+\w+\s+|ignore\s+)?into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*(?:replace|merge)\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*(?:create|drop|alter|truncate)\s+(?:unique\s+|temp(?:orary)?\s+|or\s+replace\s+)*(?:table|index|view|schema|database|sequence|trigger|function|procedure|user|role)\b/i,/^\s*(?:pragma|attach|detach|vacuum|analyze|reindex|show|describe|explain)\b/i,/^\s*call\s+[\w".`]+\s*\(/i];const PARTITION_SHARED_SINKS=new Set(["query","execute"]);function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if((0,driver_origin_1.isFunctionNode)(n))return n}return void 0}function callableName(fn){if(fn.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.id)return fn.id.name;const parent=fn.parent;if(parent.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.id.name}if((parent.type===ast_node_types_1.AST_NODE_TYPES.MethodDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.Property)&&parent.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.key.name}return void 0}function createSqlInjectionRule(config){const statementSinks=new Set(config.methods);const fragmentSinks=new Map;for(const sink of config.fragmentMethods??[]){const spec=typeof sink==="string"?{name:sink}:sink;fragmentSinks.set(spec.name,{name:spec.name,arg:spec.arg??0,requireDriverReceiver:spec.requireDriverReceiver??false})}const fragmentFunctions=new Set(config.fragmentFunctions??[]);const aliasCallbacks=new Set(config.aliasCallbacks??[]);const sqlTextProperties=new Set(config.sqlTextProperties??[]);const statementFactories=new Set(config.statementFactories??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);const safety=(0,sql_safety_1.createSqlSafety)({origin:originOf,escapers:new Set(config.escapers??[]),fragmentFactories:new Set(config.fragmentFactories??[]),modules:config.modules,escaperModules:config.escaperModules});return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{noUnsafeQuery:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink}),unsafeTemplateLiteral:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let wrappers;let definitions;let wrapperFns;let pending;let ownsFile;let requestTextSink;const judge=(node,gate,depth=0)=>{const arg=(0,type_syntax_1.unwrapTypeSyntax)(node);if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral||arg.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&arg.operator==="+"){if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&arg.expressions.length===0){return false}const kind=arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral?"template":"concat";const text=(0,sql_safety_1.sqlStaticText)(arg);if(kind==="concat"&&text.trim()==="")return false;if(gate&&!looksLikeSqlStatement(text))return false;return(0,sql_position_1.sqlHoles)(arg).some(isUnsafeHole)?kind:false}if(depth>2)return false;if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return judgeBinding(arg,gate,depth)}if(arg.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const target=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(arg.callee,env));if(target.env!==env||!(0,driver_origin_1.isFunctionNode)(target.node))return false;for(const result of(0,value_follow_1.returnedValues)(target.node)??[]){const kind=judge(result,gate,depth+1);if(kind)return kind}}return false};const isUnsafeHole=({expression,preceding})=>!safety.isSafe((0,value_follow_1.valueAt)(expression,env),(0,sql_position_1.isQuoted)(preceding))&&((0,sql_position_1.isValuePosition)(preceding,config.doubleQuotedStrings)||(0,sql_position_1.isRequestDerived)(expression,env));const judgeBinding=(node,gate,depth)=>{const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));if(variable===null||!variable.defs.some(def=>def.type==="Variable")||variable.defs.some(def=>def.type==="Parameter")){return false}const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>r.writeExpr);const text=writes.map(sql_safety_1.sqlStaticText).join(" ");if(gate&&!looksLikeSqlStatement(text))return false;for(const write of writes){const kind=judge(write,false,depth+1);if(kind)return kind}return false};const isRequestText=node=>{let cursor=(0,type_syntax_1.unwrapTypeSyntax)(node);while(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&cursor.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&cursor.callee.name==="String"&&cursor.arguments.length===1){cursor=(0,type_syntax_1.unwrapTypeSyntax)(cursor.arguments[0])}for(let link=cursor;;link=link.object){if((0,request_source_1.isRequestPayload)(link,env))return true;if(link.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return false}};const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};const isStatementObject=receiver=>{const value=(0,type_syntax_1.unwrapTypeSyntax)((0,value_follow_1.follow)((0,value_follow_1.valueAt)(receiver,env)).node);return value.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&value.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statementFactories.has((0,spellings_1.propertyName)(value.callee)??"")};const check=(arg,gate)=>{let text=(0,type_syntax_1.unwrapTypeSyntax)(arg);if(text.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&sqlTextProperties.size>0){const property=text.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&sqlTextProperties.has((0,spellings_1.objectKeyName)(p)??""));if(property===void 0)return;text=property.value}const kind=judge(text,gate);if(kind){report(text,kind);return}if(requestTextSink&&isRequestText(text))report(text,"concat")};const importedName=callee=>{const variable=(0,value_follow_1.lookup)(callee.name,env.scopeOf(callee));const specifier=variable===null?null:(0,value_follow_1.importSpecifierOf)(variable);return specifier!==null&&config.modules.some(m=>specifier.source===m||specifier.source.startsWith(`${m}/`))?specifier.name:null};const checkAliasCallback=node=>{const callback=node.arguments[0];if(!callback||!(0,driver_origin_1.isFunctionNode)(callback))return;const alias=callback.params[0];for(const result of(0,value_follow_1.returnedValues)(callback)??[]){const text=(0,type_syntax_1.unwrapTypeSyntax)(result);if(text.type!==ast_node_types_1.AST_NODE_TYPES.TemplateLiteral)continue;const unsafe=(0,sql_position_1.sqlHoles)(text).some(hole=>!(hole.expression.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&alias?.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&hole.expression.name===alias.name)&&isUnsafeHole(hole));if(unsafe)report(text,"template")}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);wrappers=new Map;definitions=new Map;wrapperFns=new Map;pending=[];ownsFile=false;requestTextSink=true;return true},Program(program){ownsFile=usesModule(program)},"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node){const name=callableName(node);if(name!==void 0)definitions.set(name,(definitions.get(name)??0)+1)},CallExpression(node){const callee=node.callee;const isMember=callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;const calleeName=isMember?(0,spellings_1.propertyName)(callee)??void 0:callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?callee.name:void 0;if(calleeName===void 0)return;if(!isMember&&ownsFile){const imported=importedName(callee);if(imported!==null&&fragmentFunctions.has(imported)){requestTextSink=true;if(node.arguments[0])check(node.arguments[0],false);return}if(imported!==null&&aliasCallbacks.has(imported)){checkAliasCallback(node);return}}const fragment=isMember?fragmentSinks.get(calleeName):void 0;if(!isMember||!statementSinks.has(calleeName)&&!fragment){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;pending.push({name:calleeName,index,arg})}return}const receiver=originOf((0,value_follow_1.valueAt)(callee.object,env));if(receiver==="foreign")return;if(fragment?.requireDriverReceiver&&receiver!=="ours")return;if(!ownsFile&&!(receiver==="ours"&&!PARTITION_SHARED_SINKS.has(calleeName))){return}const queryArg=node.arguments[fragment?.arg??0];if(!queryArg||queryArg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return;if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const fn=enclosingFunction(node);const index=fn?.params.findIndex(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name===queryArg.name)??-1;const name=fn&&index>=0?callableName(fn):void 0;if(name!==void 0&&fn){wrappers.set(name,index);wrapperFns.set(fn,name)}}const gate=fragment===void 0&&config.requireSqlKeywords&&receiver!=="ours";requestTextSink=!isStatementObject(callee.object);check(queryArg,gate)},"Program:exit"(){if(!ownsFile)return;const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;const kind=judge(arg,true);if(kind)report((0,type_syntax_1.unwrapTypeSyntax)(arg),kind)}}}}})}
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.looksLikeSqlStatement=looksLikeSqlStatement;exports.createSqlInjectionRule=createSqlInjectionRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const sql_safety_1=require("./sql-safety");const sql_position_1=require("./sql-position");const value_follow_1=require("./value-follow");const SQL_STATEMENTS=[/^[\s(]*select\b[\s\S]*\bfrom\b/i,/^[\s(]*select\s/i,/^\s*insert\s+(?:or\s+\w+\s+|ignore\s+)?into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*(?:replace|merge)\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*(?:create|drop|alter|truncate)\s+(?:unique\s+|temp(?:orary)?\s+|or\s+replace\s+)*(?:table|index|view|schema|database|sequence|trigger|function|procedure|user|role)\b/i,/^\s*(?:pragma|attach|detach|vacuum|analyze|reindex|show|describe|explain)\b/i,/^\s*call\s+[\w".`]+\s*\(/i];const PARTITION_SHARED_SINKS=new Set(["query","execute"]);function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if((0,driver_origin_1.isFunctionNode)(n))return n}return void 0}function callableName(fn){if(fn.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.id)return fn.id.name;const parent=fn.parent;if(parent.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.id.name}if((parent.type===ast_node_types_1.AST_NODE_TYPES.MethodDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.Property)&&parent.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.key.name}return void 0}function createSqlInjectionRule(config){const statementSinks=new Set(config.methods);const fragmentSinks=new Map;for(const sink of config.fragmentMethods??[]){const spec=typeof sink==="string"?{name:sink}:sink;fragmentSinks.set(spec.name,{name:spec.name,arg:spec.arg??0,requireDriverReceiver:spec.requireDriverReceiver??false})}const fragmentFunctions=new Set(config.fragmentFunctions??[]);const aliasCallbacks=new Set(config.aliasCallbacks??[]);const sqlTextProperties=new Set(config.sqlTextProperties??[]);const statementFactories=new Set(config.statementFactories??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);const safety=(0,sql_safety_1.createSqlSafety)({origin:originOf,escapers:new Set(config.escapers??[]),fragmentFactories:new Set(config.fragmentFactories??[]),modules:config.modules,escaperModules:config.escaperModules});return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{noUnsafeQuery:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink}),unsafeTemplateLiteral:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let wrappers;let definitions;let wrapperFns;let pending;let ownsFile;const judge=(node,gate,depth=0)=>{const arg=(0,type_syntax_1.unwrapTypeSyntax)(node);if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral||arg.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&arg.operator==="+"){if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&arg.expressions.length===0){return false}const kind=arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral?"template":"concat";const text=(0,sql_safety_1.sqlStaticText)(arg);if(kind==="concat"&&text.trim()==="")return false;if(gate&&!looksLikeSqlStatement(text))return false;return(0,sql_position_1.sqlHoles)(arg).some(isUnsafeHole)?kind:false}if(depth>2)return false;if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return judgeBinding(arg,gate,depth)}if(arg.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const target=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(arg.callee,env));if(target.env!==env||!(0,driver_origin_1.isFunctionNode)(target.node))return false;for(const result of(0,value_follow_1.returnedValues)(target.node)??[]){const kind=judge(result,gate,depth+1);if(kind)return kind}}return false};const isUnsafeHole=({expression,preceding})=>!safety.isSafe((0,value_follow_1.valueAt)(expression,env),(0,sql_position_1.isQuoted)(preceding))&&((0,sql_position_1.isValuePosition)(preceding,config.doubleQuotedStrings)||(0,sql_position_1.isRequestDerived)(expression,env));const judgeBinding=(node,gate,depth)=>{const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));if(variable===null||!variable.defs.some(def=>def.type==="Variable")||variable.defs.some(def=>def.type==="Parameter")){return false}const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>r.writeExpr);const text=writes.map(sql_safety_1.sqlStaticText).join(" ");if(gate&&!looksLikeSqlStatement(text))return false;for(const write of writes){const kind=judge(write,false,depth+1);if(kind)return kind}return false};const isRequestText=node=>{let cursor=(0,type_syntax_1.unwrapTypeSyntax)(node);while(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&cursor.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&cursor.callee.name==="String"&&cursor.arguments.length===1){cursor=(0,type_syntax_1.unwrapTypeSyntax)(cursor.arguments[0])}for(let link=cursor;;link=link.object){if((0,request_source_1.isRequestPayload)(link,env))return true;if(link.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return false}};const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};const isStatementObject=receiver=>{const value=(0,type_syntax_1.unwrapTypeSyntax)((0,value_follow_1.follow)((0,value_follow_1.valueAt)(receiver,env)).node);return value.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&value.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statementFactories.has((0,spellings_1.propertyName)(value.callee)??"")};const check=(arg,gate)=>{let text=(0,type_syntax_1.unwrapTypeSyntax)(arg);if(text.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&sqlTextProperties.size>0){const property=text.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&sqlTextProperties.has((0,spellings_1.objectKeyName)(p)??""));if(property===void 0)return;text=property.value}const kind=judge(text,gate);if(kind){report(text,kind);return}if(isRequestText(text))report(text,"concat")};const importedName=callee=>{const variable=(0,value_follow_1.lookup)(callee.name,env.scopeOf(callee));const specifier=variable===null?null:(0,value_follow_1.importSpecifierOf)(variable);return specifier!==null&&config.modules.some(m=>specifier.source===m||specifier.source.startsWith(`${m}/`))?specifier.name:null};const checkAliasCallback=node=>{const callback=node.arguments[0];if(!callback||!(0,driver_origin_1.isFunctionNode)(callback))return;const alias=callback.params[0];for(const result of(0,value_follow_1.returnedValues)(callback)??[]){const text=(0,type_syntax_1.unwrapTypeSyntax)(result);if(text.type!==ast_node_types_1.AST_NODE_TYPES.TemplateLiteral)continue;const unsafe=(0,sql_position_1.sqlHoles)(text).some(hole=>!(hole.expression.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&alias?.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&hole.expression.name===alias.name)&&isUnsafeHole(hole));if(unsafe)report(text,"template")}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);wrappers=new Map;definitions=new Map;wrapperFns=new Map;pending=[];ownsFile=false;return true},Program(program){ownsFile=usesModule(program)},"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node){const name=callableName(node);if(name!==void 0)definitions.set(name,(definitions.get(name)??0)+1)},CallExpression(node){const callee=node.callee;const isMember=callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;const calleeName=isMember?(0,spellings_1.propertyName)(callee)??void 0:callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?callee.name:void 0;if(calleeName===void 0)return;if(!isMember&&ownsFile){const imported=importedName(callee);if(imported!==null&&fragmentFunctions.has(imported)){if(node.arguments[0])check(node.arguments[0],false);return}if(imported!==null&&aliasCallbacks.has(imported)){checkAliasCallback(node);return}}const fragment=isMember?fragmentSinks.get(calleeName):void 0;if(!isMember||!statementSinks.has(calleeName)&&!fragment){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;pending.push({name:calleeName,index,arg})}return}const receiver=originOf((0,value_follow_1.valueAt)(callee.object,env));if(receiver==="foreign")return;if(fragment?.requireDriverReceiver&&receiver!=="ours")return;if(!ownsFile&&!(receiver==="ours"&&!PARTITION_SHARED_SINKS.has(calleeName))){return}const queryArg=node.arguments[fragment?.arg??0];if(!queryArg||queryArg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return;if(isStatementObject(callee.object))return;if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const fn=enclosingFunction(node);const index=fn?.params.findIndex(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name===queryArg.name)??-1;const name=fn&&index>=0?callableName(fn):void 0;if(name!==void 0&&fn){wrappers.set(name,index);wrapperFns.set(fn,name)}}const gate=fragment===void 0&&config.requireSqlKeywords&&receiver!=="ours";check(queryArg,gate)},"Program:exit"(){if(!ownsFile)return;const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;const kind=judge(arg,true);if(kind)report((0,type_syntax_1.unwrapTypeSyntax)(arg),kind)}}}}})}
@@ -1 +1 @@
1
- "use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __setModuleDefault=this&&this.__setModuleDefault||(Object.create?(function(o,v){Object.defineProperty(o,"default",{enumerable:true,value:v})}):function(o,v){o["default"]=v});var __importStar=this&&this.__importStar||(function(){var ownKeys=function(o){ownKeys=Object.getOwnPropertyNames||function(o2){var ar=[];for(var k in o2)if(Object.prototype.hasOwnProperty.call(o2,k))ar[ar.length]=k;return ar};return ownKeys(o)};return function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null){for(var k=ownKeys(mod),i=0;i<k.length;i++)if(k[i]!=="default")__createBinding(result,mod,k[i])}__setModuleDefault(result,mod);return result}})();Object.defineProperty(exports,"__esModule",{value:true});exports.MAX_CACHED_MODULES=exports.MAX_FOLLOW_DEPTH=void 0;exports.cachedModuleCount=cachedModuleCount;exports.isRelativeSpecifier=isRelativeSpecifier;exports.envOf=envOf;exports.valueAt=valueAt;exports.resolveRelative=resolveRelative;exports.parseModule=parseModule;exports.loadModule=loadModule;exports.scopeIn=scopeIn;exports.moduleEnv=moduleEnv;exports.lookup=lookup;exports.requiredSpecifier=requiredSpecifier;exports.findProperty=findProperty;exports.exportedValue=exportedValue;exports.importOrigin=importOrigin;exports.importSpecifierOf=importSpecifierOf;exports.returnedValue=returnedValue;exports.returnedValues=returnedValues;exports.singleWrite=singleWrite;exports.follow=follow;const fs=__importStar(require("node:fs"));const path=__importStar(require("node:path"));const node_module_1=require("node:module");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const dependency_analysis_1=require("../resolver/dependency-analysis");exports.MAX_FOLLOW_DEPTH=8;const EXTENSIONS=[".ts",".tsx",".js",".mjs",".cjs",".json"];const RELATIVE=/^\.{1,2}(?:\/|$)/;const JS_EXTENSION=/\.[cm]?js$/;exports.MAX_CACHED_MODULES=512;const modules=(0,dependency_analysis_1.createLRUCache)(exports.MAX_CACHED_MODULES);function cachedModuleCount(){return modules.size}function isRelativeSpecifier(specifier){return RELATIVE.test(specifier)}function envOf(context){return{file:path.resolve(context.physicalFilename),parser:context.languageOptions?.parser,module:null,scopeOf:node=>context.sourceCode.getScope(node)}}function valueAt(node,env){return{node,scope:env.scopeOf(node),env}}function isFile(candidate){return fs.statSync(candidate,{throwIfNoEntry:false})?.isFile()===true}function resolveRelative(fromFile,specifier){if(!isRelativeSpecifier(specifier))return null;const base=path.resolve(path.dirname(fromFile),specifier);const candidates=[base,...EXTENSIONS.map(extension=>`${base}${extension}`),...EXTENSIONS.map(extension=>path.join(base,`index${extension}`))];if(JS_EXTENSION.test(base)){const stem=base.replace(JS_EXTENSION,"");candidates.push(`${stem}.ts`,`${stem}.tsx`)}return candidates.find(isFile)??null}function fallbackParser(){try{return(0,node_module_1.createRequire)(path.join(process.cwd(),"package.json"))("@typescript-eslint/parser")}catch{return null}}function linkParents(node,parent){node.parent=parent;for(const[key,value]of Object.entries(node)){if(key==="parent")continue;const children=Array.isArray(value)?value:[value];for(const child of children){if(typeof child==="object"&&child!==null&&typeof child.type==="string"){linkParents(child,node)}}}}function parseModule(text,file,parser){for(const candidate of[parser,fallbackParser()]){const parseForESLint=candidate?.parseForESLint;if(typeof parseForESLint!=="function")continue;try{const result=parseForESLint(text,{filePath:file,sourceType:"module",ecmaVersion:"latest",range:true,loc:true});if(!result.scopeManager)continue;linkParents(result.ast,void 0);return{file,ast:result.ast,scopeManager:result.scopeManager}}catch{}}return null}function loadModule(file,parser){const stat=fs.statSync(file,{throwIfNoEntry:false});if(stat===void 0||!stat.isFile())return null;const cached=modules.get(file);if(cached!==void 0&&cached.mtimeMs===stat.mtimeMs)return cached.info;let info=null;try{const text=fs.readFileSync(file,"utf8");if(path.extname(file)===".json"){JSON.parse(text);info=parseModule(`export default (${text});`,`${file}.ts`,parser)}else{info=parseModule(text,file,parser)}}catch{info=null}modules.set(file,{mtimeMs:stat.mtimeMs,info});return info}function scopeIn(module,node){let current=node;for(;current.parent;current=current.parent){const scope=module.scopeManager.acquire(current,true);if(scope!==null)return scope}return module.scopeManager.acquire(current,true)}function moduleEnv(module,parser){return{file:module.file,parser,module,scopeOf:node=>scopeIn(module,node)}}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.set.get(name);if(variable!==void 0)return variable}return null}function requiredSpecifier(node){if(node?.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression||node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require"){return null}const[argument]=node.arguments;return argument?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof argument.value==="string"?argument.value:null}function moduleExportName(node){return node.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.name:node.value}function memberChain(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return node.name;if(node.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return null;const key=(0,spellings_1.propertyName)(node);const head=memberChain(node.object);return key===null||head===null?null:`${head}.${key}`}function findProperty(object,key){for(const property of object.properties){if(property.type===ast_node_types_1.AST_NODE_TYPES.Property&&(0,spellings_1.objectKeyName)(property)===key){return property.value}}return null}function exportedValue(module,name,parser,depth=0){if(depth>exports.MAX_FOLLOW_DEPTH)return null;const env=moduleEnv(module,parser);for(const statement of module.ast.body){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExportDefaultDeclaration){if(name==="default")return valueAt(statement.declaration,env);continue}if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExportNamedDeclaration){const declaration=statement.declaration;if(declaration?.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclaration){const declarator=declaration.declarations.find(d=>d.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&d.id.name===name);if(declarator?.init)return valueAt(declarator.init,env)}else if((declaration?.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration)&&declaration.id?.name===name){return valueAt(declaration,env)}for(const specifier of statement.specifiers){if(moduleExportName(specifier.exported)!==name)continue;const local=moduleExportName(specifier.local);if(statement.source!==null){const target=resolveRelative(module.file,String(statement.source.value));const next=target===null?null:loadModule(target,parser);return next===null?null:exportedValue(next,local,parser,depth+1)}const variable=lookup(local,env.scopeOf(module.ast));return variable===null?null:follow(valueAt(variable.identifiers[0],env),depth+1)}continue}if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.operator==="="){const{left,right}=statement.expression;const target=memberChain(left);if(target==="module.exports"){if(name==="default")return valueAt(right,env);const object=follow(valueAt(right,env),depth+1);if(object.node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){const property=findProperty(object.node,name);if(property!==null)return valueAt(property,object.env)}}else if(target===`module.exports.${name}`||target===`exports.${name}`){return valueAt(right,env)}}}return null}function importOrigin(variable,env){const specifier=importSpecifierOf(variable);if(specifier===null)return null;const file=resolveRelative(env.file,specifier.source);const module=file===null?null:loadModule(file,env.parser);return module===null?null:{module,name:specifier.name}}function importSpecifierOf(variable){const def=variable.defs[0];if(def===void 0)return null;if(def.type==="ImportBinding"){if(def.node.type===ast_node_types_1.AST_NODE_TYPES.TSImportEqualsDeclaration){const reference=def.node.moduleReference;return reference.type===ast_node_types_1.AST_NODE_TYPES.TSExternalModuleReference&&reference.expression.type===ast_node_types_1.AST_NODE_TYPES.Literal?{source:String(reference.expression.value),name:"*"}:null}const declaration=def.parent;const source2=String(declaration.source.value);if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportSpecifier)return{source:source2,name:moduleExportName(def.node.imported)};if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportNamespaceSpecifier)return{source:source2,name:"*"};return{source:source2,name:"default"}}if(def.type!=="Variable")return null;const declarator=def.node;const init=declarator.init?.type===ast_node_types_1.AST_NODE_TYPES.AwaitExpression?declarator.init.argument:declarator.init;const source=requiredSpecifier(init)??(init?.type===ast_node_types_1.AST_NODE_TYPES.ImportExpression&&init.source.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof init.source.value==="string"?init.source.value:null);if(source===null)return null;if(declarator.id.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return{source,name:"*"};const property=declarator.id.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&p.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.value.name===variable.name);const name=property===void 0?null:(0,spellings_1.objectKeyName)(property);return name===null?null:{source,name}}function returnedValue(fn){if(fn.type!==ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.type!==ast_node_types_1.AST_NODE_TYPES.FunctionExpression&&fn.type!==ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression){return null}if(fn.body.type!==ast_node_types_1.AST_NODE_TYPES.BlockStatement)return fn.body;const last=fn.body.body.at(-1);return last?.type===ast_node_types_1.AST_NODE_TYPES.ReturnStatement?last.argument:null}function returnedValues(fn){if(fn.body===null||fn.body===void 0)return null;if(fn.body.type!==ast_node_types_1.AST_NODE_TYPES.BlockStatement)return[fn.body];const found=[];let bare=false;const visit=node=>{if(node.type===ast_node_types_1.AST_NODE_TYPES.ReturnStatement){if(node.argument===null)bare=true;else found.push(node.argument);return}for(const[key,value]of Object.entries(node)){if(key==="parent")continue;const children=Array.isArray(value)?value:[value];for(const child of children){if(typeof child!=="object"||child===null||typeof child.type!=="string")continue;const type=child.type;if(type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression)continue;visit(child)}}};visit(fn.body);if(bare||!alwaysLeaves(fn.body)||found.length===0)return null;return found}function alwaysLeaves(statement){switch(statement.type){case ast_node_types_1.AST_NODE_TYPES.ReturnStatement:case ast_node_types_1.AST_NODE_TYPES.ThrowStatement:return true;case ast_node_types_1.AST_NODE_TYPES.BlockStatement:{const last=statement.body.at(-1);return last!==void 0&&alwaysLeaves(last)}case ast_node_types_1.AST_NODE_TYPES.IfStatement:return statement.alternate!==null&&alwaysLeaves(statement.consequent)&&alwaysLeaves(statement.alternate);default:return false}}function singleWrite(variable){if(variable.defs.some(def=>def.type==="Parameter"))return null;const writes=variable.references.filter(reference=>reference.isWrite());if(writes.length!==1)return null;return writes[0].writeExpr??null}function follow(start,depth=0,seen=new Set){if(depth>exports.MAX_FOLLOW_DEPTH||seen.has(start.node))return start;seen.add(start.node);const{scope,env}=start;const node=(0,type_syntax_1.unwrapTypeSyntax)(start.node);const next=(target,nextEnv=env)=>follow(valueAt(target,nextEnv),depth+1,seen);if(node!==start.node)return next(node);if(node.type===ast_node_types_1.AST_NODE_TYPES.AwaitExpression)return next(node.argument);if(node.type===ast_node_types_1.AST_NODE_TYPES.ChainExpression)return next(node.expression);if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const variable=lookup(node.name,scope);if(variable===null)return start;const def=variable.defs[0];if(def?.type==="FunctionName"||def?.type==="ClassName")return next(def.node);const origin=importOrigin(variable,env);if(origin!==null){const commonJs=origin.name==="*"&&def?.type==="Variable"?exportedValue(origin.module,"default",env.parser,depth+1):null;if(commonJs!==null)return follow(commonJs,depth+1,seen);if(origin.name==="*"){return valueAt(origin.module.ast,moduleEnv(origin.module,env.parser))}const exported=exportedValue(origin.module,origin.name,env.parser,depth+1);return exported===null?start:follow(exported,depth+1,seen)}if(def?.type==="Variable"&&def.node.id.type===ast_node_types_1.AST_NODE_TYPES.ObjectPattern){const property=def.node.id.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&p.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.value.name===node.name);const key=property===void 0?null:(0,spellings_1.objectKeyName)(property);if(key===null||def.node.init===null)return start;const object=next(def.node.init);if(object.node.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)return start;const value=findProperty(object.node,key);return value===null?start:next(value,object.env)}const written=singleWrite(variable);return written===null?start:next(written)}if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const key=(0,spellings_1.propertyName)(node);if(key===null)return start;const object=next(node.object);if(object.node.type===ast_node_types_1.AST_NODE_TYPES.Program&&object.env.module!==null){const exported=exportedValue(object.env.module,key,env.parser,depth+1);return exported===null?start:follow(exported,depth+1,seen)}if(object.node.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)return start;const value=findProperty(object.node,key);return value===null?start:next(value,object.env)}if(node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const callee=next(node.callee);const returned=returnedValue(callee.node);return returned===null?start:next(returned,callee.env)}return start}
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __setModuleDefault=this&&this.__setModuleDefault||(Object.create?(function(o,v){Object.defineProperty(o,"default",{enumerable:true,value:v})}):function(o,v){o["default"]=v});var __importStar=this&&this.__importStar||(function(){var ownKeys=function(o){ownKeys=Object.getOwnPropertyNames||function(o2){var ar=[];for(var k in o2)if(Object.prototype.hasOwnProperty.call(o2,k))ar[ar.length]=k;return ar};return ownKeys(o)};return function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null){for(var k=ownKeys(mod),i=0;i<k.length;i++)if(k[i]!=="default")__createBinding(result,mod,k[i])}__setModuleDefault(result,mod);return result}})();Object.defineProperty(exports,"__esModule",{value:true});exports.MAX_CACHED_MODULES=exports.MAX_FOLLOW_DEPTH=void 0;exports.cachedModuleCount=cachedModuleCount;exports.isRelativeSpecifier=isRelativeSpecifier;exports.envOf=envOf;exports.valueAt=valueAt;exports.resolveRelative=resolveRelative;exports.parseModule=parseModule;exports.loadModule=loadModule;exports.scopeIn=scopeIn;exports.moduleEnv=moduleEnv;exports.lookup=lookup;exports.requiredSpecifier=requiredSpecifier;exports.findProperty=findProperty;exports.exportedValue=exportedValue;exports.importOrigin=importOrigin;exports.importSpecifierOf=importSpecifierOf;exports.returnedValue=returnedValue;exports.returnedValues=returnedValues;exports.singleWrite=singleWrite;exports.follow=follow;const fs=__importStar(require("node:fs"));const path=__importStar(require("node:path"));const node_module_1=require("node:module");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const dependency_analysis_1=require("../resolver/dependency-analysis");exports.MAX_FOLLOW_DEPTH=8;const EXTENSIONS=[".ts",".tsx",".js",".mjs",".cjs",".json"];const RELATIVE=/^\.{1,2}(?:\/|$)/;const JS_EXTENSION=/\.[cm]?js$/;exports.MAX_CACHED_MODULES=512;const modules=(0,dependency_analysis_1.createLRUCache)(exports.MAX_CACHED_MODULES);function cachedModuleCount(){return modules.size}function isRelativeSpecifier(specifier){return RELATIVE.test(specifier)}function envOf(context){return{file:path.resolve(context.physicalFilename),parser:context.languageOptions?.parser,module:null,scopeOf:node=>context.sourceCode.getScope(node)}}function valueAt(node,env){return{node,scope:env.scopeOf(node),env}}function isFile(candidate){return fs.statSync(candidate,{throwIfNoEntry:false})?.isFile()===true}function resolveRelative(fromFile,specifier){if(!isRelativeSpecifier(specifier))return null;const base=path.resolve(path.dirname(fromFile),specifier);const candidates=[base,...EXTENSIONS.map(extension=>`${base}${extension}`),...EXTENSIONS.map(extension=>path.join(base,`index${extension}`))];if(JS_EXTENSION.test(base)){const stem=base.replace(JS_EXTENSION,"");candidates.push(`${stem}.ts`,`${stem}.tsx`)}return candidates.find(isFile)??null}function fallbackParser(){try{const fromEslint=(0,node_module_1.createRequire)((0,node_module_1.createRequire)(path.join(process.cwd(),"package.json")).resolve("eslint"));const espree=fromEslint("espree");const{analyze}=fromEslint("eslint-scope");return{parseForESLint(code){const ast=espree.parse(code,{ecmaVersion:"latest",sourceType:"module",range:true,loc:true});return{ast,scopeManager:analyze(ast,{ecmaVersion:espree.latestEcmaVersion,sourceType:"module"})}}}}catch{return null}}function linkParents(node,parent){node.parent=parent;for(const[key,value]of Object.entries(node)){if(key==="parent")continue;const children=Array.isArray(value)?value:[value];for(const child of children){if(typeof child==="object"&&child!==null&&typeof child.type==="string"){linkParents(child,node)}}}}function parseModule(text,file,parser){for(const candidate of[parser,fallbackParser()]){const parseForESLint=candidate?.parseForESLint;if(typeof parseForESLint!=="function")continue;try{const result=parseForESLint(text,{filePath:file,sourceType:"module",ecmaVersion:"latest",range:true,loc:true});if(!result.scopeManager)continue;linkParents(result.ast,void 0);return{file,ast:result.ast,scopeManager:result.scopeManager}}catch{}}return null}function loadModule(file,parser){const stat=fs.statSync(file,{throwIfNoEntry:false});if(stat===void 0||!stat.isFile())return null;const cached=modules.get(file);if(cached!==void 0&&cached.mtimeMs===stat.mtimeMs)return cached.info;let info=null;try{const text=fs.readFileSync(file,"utf8");if(path.extname(file)===".json"){JSON.parse(text);info=parseModule(`export default (${text});`,`${file}.ts`,parser)}else{info=parseModule(text,file,parser)}}catch{info=null}modules.set(file,{mtimeMs:stat.mtimeMs,info});return info}function scopeIn(module,node){let current=node;for(;current.parent;current=current.parent){const scope=module.scopeManager.acquire(current,true);if(scope!==null)return scope}return module.scopeManager.acquire(current,true)}function moduleEnv(module,parser){return{file:module.file,parser,module,scopeOf:node=>scopeIn(module,node)}}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.set.get(name);if(variable!==void 0)return variable}return null}function requiredSpecifier(node){if(node?.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression||node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require"){return null}const[argument]=node.arguments;return argument?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof argument.value==="string"?argument.value:null}function moduleExportName(node){return node.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.name:node.value}function memberChain(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return node.name;if(node.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return null;const key=(0,spellings_1.propertyName)(node);const head=memberChain(node.object);return key===null||head===null?null:`${head}.${key}`}function findProperty(object,key){for(const property of object.properties){if(property.type===ast_node_types_1.AST_NODE_TYPES.Property&&(0,spellings_1.objectKeyName)(property)===key){return property.value}}return null}function exportedValue(module,name,parser,depth=0){if(depth>exports.MAX_FOLLOW_DEPTH)return null;const env=moduleEnv(module,parser);for(const statement of module.ast.body){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExportDefaultDeclaration){if(name==="default")return valueAt(statement.declaration,env);continue}if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExportNamedDeclaration){const declaration=statement.declaration;if(declaration?.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclaration){const declarator=declaration.declarations.find(d=>d.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&d.id.name===name);if(declarator?.init)return valueAt(declarator.init,env)}else if((declaration?.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration||declaration?.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration)&&declaration.id?.name===name){return valueAt(declaration,env)}for(const specifier of statement.specifiers){if(moduleExportName(specifier.exported)!==name)continue;const local=moduleExportName(specifier.local);if(statement.source!==null){const target=resolveRelative(module.file,String(statement.source.value));const next=target===null?null:loadModule(target,parser);return next===null?null:exportedValue(next,local,parser,depth+1)}const variable=lookup(local,env.scopeOf(module.ast));return variable===null?null:follow(valueAt(variable.identifiers[0],env),depth+1)}continue}if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.operator==="="){const{left,right}=statement.expression;const target=memberChain(left);if(target==="module.exports"){if(name==="default")return valueAt(right,env);const object=follow(valueAt(right,env),depth+1);if(object.node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){const property=findProperty(object.node,name);if(property!==null)return valueAt(property,object.env)}}else if(target===`module.exports.${name}`||target===`exports.${name}`){return valueAt(right,env)}}}return null}function importOrigin(variable,env){const specifier=importSpecifierOf(variable);if(specifier===null)return null;const file=resolveRelative(env.file,specifier.source);const module=file===null?null:loadModule(file,env.parser);return module===null?null:{module,name:specifier.name}}function importSpecifierOf(variable){const def=variable.defs[0];if(def===void 0)return null;if(def.type==="ImportBinding"){if(def.node.type===ast_node_types_1.AST_NODE_TYPES.TSImportEqualsDeclaration){const reference=def.node.moduleReference;return reference.type===ast_node_types_1.AST_NODE_TYPES.TSExternalModuleReference&&reference.expression.type===ast_node_types_1.AST_NODE_TYPES.Literal?{source:String(reference.expression.value),name:"*"}:null}const declaration=def.parent;const source2=String(declaration.source.value);if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportSpecifier)return{source:source2,name:moduleExportName(def.node.imported)};if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportNamespaceSpecifier)return{source:source2,name:"*"};return{source:source2,name:"default"}}if(def.type!=="Variable")return null;const declarator=def.node;const init=declarator.init?.type===ast_node_types_1.AST_NODE_TYPES.AwaitExpression?declarator.init.argument:declarator.init;const source=requiredSpecifier(init)??(init?.type===ast_node_types_1.AST_NODE_TYPES.ImportExpression&&init.source.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof init.source.value==="string"?init.source.value:null);if(source===null)return null;if(declarator.id.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return{source,name:"*"};const property=declarator.id.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&p.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.value.name===variable.name);const name=property===void 0?null:(0,spellings_1.objectKeyName)(property);return name===null?null:{source,name}}function returnedValue(fn){if(fn.type!==ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.type!==ast_node_types_1.AST_NODE_TYPES.FunctionExpression&&fn.type!==ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression){return null}if(fn.body.type!==ast_node_types_1.AST_NODE_TYPES.BlockStatement)return fn.body;const last=fn.body.body.at(-1);return last?.type===ast_node_types_1.AST_NODE_TYPES.ReturnStatement?last.argument:null}function returnedValues(fn){if(fn.body===null||fn.body===void 0)return null;if(fn.body.type!==ast_node_types_1.AST_NODE_TYPES.BlockStatement)return[fn.body];const found=[];let bare=false;const visit=node=>{if(node.type===ast_node_types_1.AST_NODE_TYPES.ReturnStatement){if(node.argument===null)bare=true;else found.push(node.argument);return}for(const[key,value]of Object.entries(node)){if(key==="parent")continue;const children=Array.isArray(value)?value:[value];for(const child of children){if(typeof child!=="object"||child===null||typeof child.type!=="string")continue;const type=child.type;if(type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression)continue;visit(child)}}};visit(fn.body);if(bare||!alwaysLeaves(fn.body)||found.length===0)return null;return found}function alwaysLeaves(statement){switch(statement.type){case ast_node_types_1.AST_NODE_TYPES.ReturnStatement:case ast_node_types_1.AST_NODE_TYPES.ThrowStatement:return true;case ast_node_types_1.AST_NODE_TYPES.BlockStatement:{const last=statement.body.at(-1);return last!==void 0&&alwaysLeaves(last)}case ast_node_types_1.AST_NODE_TYPES.IfStatement:return statement.alternate!==null&&alwaysLeaves(statement.consequent)&&alwaysLeaves(statement.alternate);default:return false}}function singleWrite(variable){if(variable.defs.some(def=>def.type==="Parameter"))return null;const writes=variable.references.filter(reference=>reference.isWrite());if(writes.length!==1)return null;return writes[0].writeExpr??null}function follow(start,depth=0,seen=new Set){if(depth>exports.MAX_FOLLOW_DEPTH||seen.has(start.node))return start;seen.add(start.node);const{scope,env}=start;const node=(0,type_syntax_1.unwrapTypeSyntax)(start.node);const next=(target,nextEnv=env)=>follow(valueAt(target,nextEnv),depth+1,seen);if(node!==start.node)return next(node);if(node.type===ast_node_types_1.AST_NODE_TYPES.AwaitExpression)return next(node.argument);if(node.type===ast_node_types_1.AST_NODE_TYPES.ChainExpression)return next(node.expression);if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const variable=lookup(node.name,scope);if(variable===null)return start;const def=variable.defs[0];if(def?.type==="FunctionName"||def?.type==="ClassName")return next(def.node);const origin=importOrigin(variable,env);if(origin!==null){const commonJs=origin.name==="*"&&def?.type==="Variable"?exportedValue(origin.module,"default",env.parser,depth+1):null;if(commonJs!==null)return follow(commonJs,depth+1,seen);if(origin.name==="*"){return valueAt(origin.module.ast,moduleEnv(origin.module,env.parser))}const exported=exportedValue(origin.module,origin.name,env.parser,depth+1);return exported===null?start:follow(exported,depth+1,seen)}if(def?.type==="Variable"&&def.node.id.type===ast_node_types_1.AST_NODE_TYPES.ObjectPattern){const property=def.node.id.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&p.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.value.name===node.name);const key=property===void 0?null:(0,spellings_1.objectKeyName)(property);if(key===null||def.node.init===null)return start;const object=next(def.node.init);if(object.node.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)return start;const value=findProperty(object.node,key);return value===null?start:next(value,object.env)}const written=singleWrite(variable);return written===null?start:next(written)}if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const key=(0,spellings_1.propertyName)(node);if(key===null)return start;const object=next(node.object);if(object.node.type===ast_node_types_1.AST_NODE_TYPES.Program&&object.env.module!==null){const exported=exportedValue(object.env.module,key,env.parser,depth+1);return exported===null?start:follow(exported,depth+1,seen)}if(object.node.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)return start;const value=findProperty(object.node,key);return value===null?start:next(value,object.env)}if(node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const callee=next(node.callee);const returned=returnedValue(callee.node);return returned===null?start:next(returned,callee.env)}return start}