@interlace/eslint-devkit 1.20.0 → 1.20.2

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.20.0",
3
+ "version": "1.20.2",
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.resolveModuleBinding=resolveModuleBinding;exports.isModuleBinding=isModuleBinding;const spellings_1=require("./spellings");const ast_node_types_1=require("../ast-node-types");function normalize(specifier,options){const bare=specifier.startsWith("node:")?specifier.slice("node:".length):specifier;return options.equivalents?.[bare]??bare}function requireTarget(node,options){if(node.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require")return void 0;const[arg]=node.arguments;if(arg?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;return typeof arg.value==="string"?normalize(arg.value,options):void 0}function destructuredKey(pattern,boundName){if(pattern.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return void 0;for(const property of pattern.properties){if(property.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;if(property.computed)continue;if(property.value.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)continue;if(property.value.name!==boundName)continue;const{key}=property;return key.type===ast_node_types_1.AST_NODE_TYPES.Identifier?key.name:String(key.value)}return void 0}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.variables.find(v=>v.name===name);if(variable)return variable}return void 0}function resolveModuleBinding(node,scope,options={}){return resolve(node,scope,options,new Set)}function resolve(node,scope,options,seen){if(seen.has(node))return void 0;seen.add(node);const required=requireTarget(node,options);if(required!==void 0)return{module:required,path:[]};if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const property=(0,spellings_1.propertyName)(node);if(property===null)return void 0;const base2=resolve(node.object,scope,options,seen);return base2&&{module:base2.module,path:[...base2.path,property]}}if(node.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;const variable=lookup(node.name,scope);const def=variable?.defs[0];if(!def)return void 0;if(def.type==="ImportBinding"){const declaration=def.parent;if(declaration?.type!==ast_node_types_1.AST_NODE_TYPES.ImportDeclaration)return void 0;const module=normalize(String(declaration.source.value),options);if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportSpecifier){const imported=def.node.imported;const name=imported.type===ast_node_types_1.AST_NODE_TYPES.Identifier?imported.name:String(imported.value);return{module,path:[name]}}return{module,path:[]}}if(def.type!=="Variable"||!def.node.init)return void 0;if(variable&&variable.references.filter(ref=>ref.isWrite()).length!==1)return void 0;const base=resolve(def.node.init,scope,options,seen);if(!base)return void 0;if(def.node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return base;const key=destructuredKey(def.node.id,node.name);return key===void 0?void 0:{module:base.module,path:[...base.path,key]}}function isModuleBinding(node,scope,module,exportPath,options={}){const binding=resolveModuleBinding(node,scope,options);if(!binding||binding.module!==module)return false;if(!exportPath)return true;return binding.path.length===exportPath.length&&exportPath.every((part,i)=>binding.path[i]===part)}
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.resolveModuleBinding=resolveModuleBinding;exports.isModuleBinding=isModuleBinding;const spellings_1=require("./spellings");const ast_node_types_1=require("../ast-node-types");function normalize(specifier,options){const bare=specifier.startsWith("node:")?specifier.slice("node:".length):specifier;return options.equivalents?.[bare]??bare}function requireTarget(node,options){if(node.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require")return void 0;const[arg]=node.arguments;if(arg?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;return typeof arg.value==="string"?normalize(arg.value,options):void 0}function destructuredKey(pattern,boundName){if(pattern.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return void 0;for(const property of pattern.properties){if(property.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;if(property.value.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)continue;if(property.value.name!==boundName)continue;return(0,spellings_1.objectKeyName)(property)??void 0}return void 0}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.variables.find(v=>v.name===name);if(variable)return variable}return void 0}function resolveModuleBinding(node,scope,options={}){return resolve(node,scope,options,new Set)}function resolve(node,scope,options,seen){if(seen.has(node))return void 0;seen.add(node);const required=requireTarget(node,options);if(required!==void 0)return{module:required,path:[]};if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const property=(0,spellings_1.propertyName)(node);if(property===null)return void 0;const base2=resolve(node.object,scope,options,seen);return base2&&{module:base2.module,path:[...base2.path,property]}}if(node.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;const variable=lookup(node.name,scope);const def=variable?.defs[0];if(!def)return void 0;if(def.type==="ImportBinding"){const declaration=def.parent;if(declaration?.type!==ast_node_types_1.AST_NODE_TYPES.ImportDeclaration)return void 0;const module=normalize(String(declaration.source.value),options);if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportSpecifier){const imported=def.node.imported;const name=imported.type===ast_node_types_1.AST_NODE_TYPES.Identifier?imported.name:String(imported.value);return{module,path:[name]}}return{module,path:[]}}if(def.type!=="Variable"||!def.node.init)return void 0;if(variable&&variable.references.filter(ref=>ref.isWrite()).length!==1)return void 0;const base=resolve(def.node.init,scope,options,seen);if(!base)return void 0;if(def.node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return base;const key=destructuredKey(def.node.id,node.name);return key===void 0?void 0:{module:base.module,path:[...base.path,key]}}function isModuleBinding(node,scope,module,exportPath,options={}){const binding=resolveModuleBinding(node,scope,options);if(!binding||binding.module!==module)return false;if(!exportPath)return true;return binding.path.length===exportPath.length&&exportPath.every((part,i)=>binding.path[i]===part)}
@@ -29,6 +29,20 @@ export interface Value {
29
29
  }
30
30
  /** How far a value is followed before giving up. */
31
31
  export declare const MAX_FOLLOW_DEPTH = 8;
32
+ /**
33
+ * Parsed modules by absolute path, invalidated by modification time.
34
+ *
35
+ * Bounded. Each entry holds a whole AST and its scope manager, and this is module state: it
36
+ * outlives every file and every ESLint instance in the process. Unbounded, it
37
+ * kept one parsed copy of every relatively-imported module a run ever opened,
38
+ * which took the corpus gates (106k files) past an 8 GB heap.
39
+ * Relative imports cluster by directory, so a recency window keeps the hits
40
+ * that matter. 512 is measured, not guessed: on amplication (3.3k files) 128
41
+ * thrashed to 3x the lint time, 512 matched unbounded speed at a flat heap.
42
+ */
43
+ export declare const MAX_CACHED_MODULES = 512;
44
+ /** How many parsed modules are held right now. For the bound's regression test. */
45
+ export declare function cachedModuleCount(): number;
32
46
  /** The slice of a rule context this module reads. */
33
47
  export interface FollowContextLike {
34
48
  readonly physicalFilename: string;
@@ -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_FOLLOW_DEPTH=void 0;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");exports.MAX_FOLLOW_DEPTH=8;const EXTENSIONS=[".ts",".tsx",".js",".mjs",".cjs",".json"];const RELATIVE=/^\.{1,2}(?:\/|$)/;const JS_EXTENSION=/\.[cm]?js$/;const modules=new Map;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{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}