@interlace/eslint-devkit 2.3.0 → 2.3.1
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": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "ESLint plugin 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
|
"sideEffects": false,
|
|
@@ -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 __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};Object.defineProperty(exports,"__esModule",{value:true});__exportStar(require("./generated-file"),exports);__exportStar(require("./minified-file"),exports);__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);__exportStar(require("./module-evidence"),exports);__exportStar(require("./sdk-flow"),exports);
|
|
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 __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};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.valueFollow=void 0;__exportStar(require("./generated-file"),exports);__exportStar(require("./minified-file"),exports);__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);__exportStar(require("./module-evidence"),exports);__exportStar(require("./sdk-flow"),exports);exports.valueFollow=__importStar(require("./value-follow"));
|
|
@@ -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{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}
|
|
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 writesTo(name,module,env){const variable=lookup(name,env.scopeOf(module.ast));return variable.references.filter(reference=>reference.isWrite()).length}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!==void 0){return declarator.init&&writesTo(name,module,env)<=1?valueAt(declarator.init,env):null}}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 writesTo(name,module,env)===0?valueAt(declaration,env):null}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.ExportAllDeclaration&&statement.exported!==null){if(moduleExportName(statement.exported)!==name)continue;const target=resolveRelative(module.file,String(statement.source.value));const next=target===null?null:loadModule(target,parser);return next===null?null:valueAt(next.ast,moduleEnv(next,parser))}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)}}}if(name==="default")return null;let starred=null;for(const statement of module.ast.body){if(statement.type!==ast_node_types_1.AST_NODE_TYPES.ExportAllDeclaration||statement.exported!==null)continue;const target=resolveRelative(module.file,String(statement.source.value));const next=target===null?null:loadModule(target,parser);const found=next===null?null:exportedValue(next,name,parser,depth+1);if(found===null)continue;if(starred!==null&&starred.node!==found.node)return null;starred=found}return starred}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"){if(variable.references.some(reference=>reference.isWrite())){return start}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}
|