@prisma-next/eslint-plugin 0.3.0-dev.1 → 0.3.0-dev.10

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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // package.json
2
2
  var name = "@prisma-next/eslint-plugin";
3
- var version = "0.3.0-dev.1";
3
+ var version = "0.3.0-dev.10";
4
4
 
5
5
  // src/rules/lint-build-call.ts
6
6
  import { ESLintUtils as ESLintUtils2 } from "@typescript-eslint/utils";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../package.json","../src/rules/lint-build-call.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@prisma-next/eslint-plugin\",\n \"version\": \"0.3.0-dev.1\",\n \"type\": \"module\",\n \"sideEffects\": false,\n \"description\": \"ESLint plugin for Prisma Next query builder type checking and linting\",\n \"keywords\": [\n \"eslint\",\n \"eslintplugin\",\n \"typescript\",\n \"prisma-next\",\n \"query-builder\"\n ],\n \"files\": [\n \"dist\"\n ],\n \"main\": \"./dist/index.js\",\n \"exports\": {\n \".\": \"./dist/index.js\"\n },\n \"scripts\": {\n \"build\": \"tsup --config tsup.config.ts && tsc --emitDeclarationOnly\",\n \"test\": \"vitest run\",\n \"test:coverage\": \"vitest run --coverage\",\n \"typecheck\": \"tsc --noEmit\",\n \"lint\": \"biome check . --config-path ../../../../biome.json --error-on-warnings\",\n \"lint:fix\": \"biome check --write . --config-path ../../../../biome.json\",\n \"lint:fix:unsafe\": \"biome check --write --unsafe . --config-path ../../../../biome.json\",\n \"clean\": \"node ../../../../scripts/clean.mjs\"\n },\n \"dependencies\": {\n \"@typescript-eslint/types\": \"^8.0.0\",\n \"@typescript-eslint/utils\": \"^8.0.0\"\n },\n \"devDependencies\": {\n \"@prisma-next/sql-contract\": \"workspace:*\",\n \"@prisma-next/sql-contract-ts\": \"workspace:*\",\n \"@prisma-next/sql-lane\": \"workspace:*\",\n \"@prisma-next/sql-relational-core\": \"workspace:*\",\n \"@prisma-next/sql-runtime\": \"workspace:*\",\n \"@typescript-eslint/parser\": \"^8.0.0\",\n \"@types/eslint\": \"^9.0.0\",\n \"@types/node\": \"catalog:\",\n \"@vitest/coverage-v8\": \"catalog:\",\n \"eslint\": \"^9.0.0\",\n \"tsup\": \"^8.3.0\",\n \"typescript\": \"^5.9.3\",\n \"vitest\": \"catalog:\"\n },\n \"peerDependencies\": {\n \"@typescript-eslint/parser\": \"^8.0.0\",\n \"eslint\": \"^9.0.0\",\n \"typescript\": \"*\"\n }\n}\n","import type { TSESTree } from '@typescript-eslint/types';\nimport { ESLintUtils } from '@typescript-eslint/utils';\nimport {\n type BuilderCall,\n extractCallChain,\n getTypeScriptServices,\n isPrismaNextQueryBuildCall,\n} from '../utils';\n\nconst DEFAULT_OPTIONS = {\n requireLimit: true,\n maxLimit: 1000,\n requiredFields: [],\n};\n\nconst SELECT_QUERY_METHODS = ['select', 'from'] as const;\n\n// Types\ntype MessageIds = 'unboundedQuery' | 'maxLimitExceeded';\n\ninterface RuleOptions {\n /** Enforce limit() calls on SELECT queries to prevent unbounded queries */\n requireLimit?: boolean;\n /** Maximum allowed limit value */\n maxLimit?: number;\n}\n\ntype Options = [RuleOptions];\n\n// Rule implementation\nexport const lintBuildCall = ESLintUtils.RuleCreator.withoutDocs<Options, MessageIds>({\n meta: {\n type: 'problem',\n docs: {\n description: 'Validate query builder build() calls using TypeScript type information',\n },\n schema: [\n {\n type: 'object',\n properties: {\n requireLimit: {\n type: 'boolean',\n description: 'Enforce limit() calls on SELECT queries',\n },\n maxLimit: {\n type: 'number',\n description: 'Maximum allowed limit value',\n minimum: 1,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n unboundedQuery:\n 'Query build() call may result in unbounded query. Consider adding .limit() to prevent fetching too many rows.',\n maxLimitExceeded:\n 'Query build() call has a limit() value that exceeds the maximum allowed of {{maxLimit}}.',\n },\n },\n defaultOptions: [DEFAULT_OPTIONS],\n create(context, [options]) {\n const services = getTypeScriptServices(context);\n\n if (!services) {\n throw new Error(\n 'TypeScript services are required for lint-build-call rule. Please ensure you are using @typescript-eslint/parser.',\n );\n }\n\n return {\n CallExpression(node: TSESTree.CallExpression) {\n if (!isPrismaNextQueryBuildCall(node, services)) {\n return;\n }\n lintQuery(node, extractCallChain(node));\n },\n };\n\n function lintQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (isSelectQuery(callChain)) {\n checkUnboundedQuery(node, callChain);\n checkLimitExceedsMax(node, callChain);\n }\n }\n\n function isSelectQuery(callChain: BuilderCall[]): boolean {\n return SELECT_QUERY_METHODS.some((method) =>\n callChain.some((call) => call.method === method),\n );\n }\n\n function checkUnboundedQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (options.requireLimit && !callChain.some((call) => call.method === 'limit')) {\n reportUnboundedQuery(node);\n }\n }\n\n function checkLimitExceedsMax(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (!options.maxLimit) return;\n\n const limitArg = callChain.find((call) => call.method === 'limit')?.args.pop();\n const literalValue = limitArg ? extractNumericLiteral(limitArg) : undefined;\n if (literalValue !== undefined && literalValue > options.maxLimit) {\n reportLimitExceeded(node);\n }\n }\n\n function extractNumericLiteral(\n arg: TSESTree.Expression | TSESTree.SpreadElement,\n ): number | undefined {\n if (arg?.type === 'Literal' && 'value' in arg && typeof arg.value === 'number') {\n return arg.value;\n }\n return;\n }\n\n function reportUnboundedQuery(node: TSESTree.CallExpression) {\n context.report({\n node,\n messageId: 'unboundedQuery',\n });\n }\n\n function reportLimitExceeded(node: TSESTree.CallExpression) {\n context.report({\n node,\n messageId: 'maxLimitExceeded',\n data: { maxLimit: options.maxLimit?.toString() ?? 'undefined' },\n });\n }\n },\n});\n","import type { TSESTree } from '@typescript-eslint/types';\nimport type { ParserServices } from '@typescript-eslint/utils';\nimport { ESLintUtils } from '@typescript-eslint/utils';\nimport type * as ts from 'typescript';\n\nconst PRISMA_NEXT_SQL_PACKAGES = [\n '@prisma-next/sql-lane',\n 'packages/2-sql/4-lanes/sql-lane',\n] as const;\n\nconst PLAN_TYPE_PATTERNS = [/^SqlQueryPlan$/] as const;\n\nexport type BuilderCall = { method: string; args: TSESTree.CallExpressionArgument[] };\n\n// Types\nexport interface TypeScriptServices {\n program: ts.Program;\n checker: ts.TypeChecker;\n esTreeNodeToTSNodeMap: ParserServices['esTreeNodeToTSNodeMap'];\n tsNodeToESTreeNodeMap: ParserServices['tsNodeToESTreeNodeMap'];\n}\n\n/**\n * Get TypeScript services from ESLint context\n */\nexport function getTypeScriptServices(\n context: Parameters<typeof ESLintUtils.getParserServices>[0],\n): TypeScriptServices | null {\n try {\n const parserServices = ESLintUtils.getParserServices(context, false);\n\n if (!parserServices?.program) {\n return null;\n }\n\n return {\n program: parserServices.program,\n checker: parserServices.program.getTypeChecker(),\n esTreeNodeToTSNodeMap: parserServices.esTreeNodeToTSNodeMap,\n tsNodeToESTreeNodeMap: parserServices.tsNodeToESTreeNodeMap,\n };\n } catch {\n return null;\n }\n}\n\n/**\n * Check if a call expression is a method call with a specific name\n */\nexport function isMethodCall(node: TSESTree.CallExpression, methodName: string): boolean {\n return (\n node.callee.type === 'MemberExpression' &&\n node.callee.property.type === 'Identifier' &&\n node.callee.property.name === methodName\n );\n}\n\n/**\n * Check if a call expression is a query builder build() call\n * Uses type information to verify it's actually our query builder's build method\n */\nexport function isPrismaNextQueryBuildCall(\n node: TSESTree.CallExpression,\n services?: TypeScriptServices | null,\n): boolean {\n if (!isMethodCall(node, 'build') || node.arguments.length > 1 || !services) {\n return false;\n }\n\n if (node.callee.type !== 'MemberExpression') {\n return false;\n }\n\n const objectType = getTypeOfNode(node.callee.object, services);\n if (!objectType) {\n return false;\n }\n\n if (!isTypeFromPackages(objectType, PRISMA_NEXT_SQL_PACKAGES)) {\n return false;\n }\n\n const returnType = getTypeOfNode(node, services);\n return returnType ? isPrismaNextQueryPlanType(returnType) : false;\n}\n\n/**\n * Get the TypeScript type of an ESTree node\n */\nexport function getTypeOfNode(node: TSESTree.Node, services: TypeScriptServices): ts.Type | null {\n try {\n const tsNode = services.esTreeNodeToTSNodeMap.get(node);\n return tsNode ? services.checker.getTypeAtLocation(tsNode) : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Check if a type has a specific property\n */\nexport function typeHasProperty(\n type: ts.Type,\n propertyName: string,\n checker: ts.TypeChecker,\n): boolean {\n try {\n const properties = checker.getPropertiesOfType(type);\n return properties.some((prop) => prop.getName() === propertyName);\n } catch {\n return false;\n }\n}\n\n/**\n * Check if type is a Prisma Next query plan type by name and origin\n */\nexport function isPrismaNextQueryPlanType(type: ts.Type): boolean {\n return PLAN_TYPE_PATTERNS.some((pattern) => pattern.test(type.symbol.name));\n}\n\n/**\n * Extract call chain from a call expression\n * Returns array of method names called in sequence\n */\nexport function extractCallChain(node: TSESTree.CallExpression): BuilderCall[] {\n const chain: BuilderCall[] = [];\n\n function traverse(current: TSESTree.Node): void {\n switch (current.type) {\n case 'CallExpression':\n if (current.callee.type === 'MemberExpression') {\n traverse(current.callee.object);\n if (current.callee.property.type === 'Identifier') {\n chain.push({ method: current.callee.property.name, args: current.arguments });\n }\n }\n break;\n\n case 'MemberExpression':\n traverse(current.object);\n break;\n\n case 'Identifier':\n break;\n }\n }\n\n traverse(node);\n return chain;\n}\n\n/**\n * Helper to check if a type originates from specific packages\n */\nfunction isTypeFromPackages(type: ts.Type, packages: readonly string[]): boolean {\n const fileName = type.getSymbol()?.valueDeclaration?.getSourceFile().fileName;\n return fileName ? packages.some((pkg) => fileName.includes(`${pkg}/`)) : false;\n}\n","import { name, version } from '../package.json';\nimport { lintBuildCall } from './rules/lint-build-call';\n\n// Plugin metadata\nconst PLUGIN_META = {\n name,\n version,\n};\n\n// Rule definitions\nconst RULES = {\n 'lint-build-call': lintBuildCall,\n};\n\n// Configuration presets\nconst RULE_CONFIG = {\n '@prisma-next/lint-build-call': 'error',\n};\n\n// Plugin interface\ninterface ESLintPlugin {\n meta: {\n name: string;\n version: string;\n };\n // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility\n rules: Record<string, any>;\n // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility\n configs: Record<string, any>;\n}\n\n// Plugin implementation\nconst plugin: ESLintPlugin = {\n meta: PLUGIN_META,\n rules: RULES,\n configs: {\n recommended: {\n plugins: ['@prisma-next'],\n rules: RULE_CONFIG,\n },\n },\n};\n\n// Add flat config after plugin is defined to avoid circular reference\nplugin.configs['flat/recommended'] = {\n plugins: {\n '@prisma-next': plugin,\n },\n rules: RULE_CONFIG,\n};\n\n// Exports\nexport default plugin;\nexport { lintBuildCall };\nexport { plugin };\nexport type { ESLintPlugin };\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACDb,SAAS,eAAAA,oBAAmB;;;ACC5B,SAAS,mBAAmB;AAG5B,IAAM,2BAA2B;AAAA,EAC/B;AAAA,EACA;AACF;AAEA,IAAM,qBAAqB,CAAC,gBAAgB;AAerC,SAAS,sBACd,SAC2B;AAC3B,MAAI;AACF,UAAM,iBAAiB,YAAY,kBAAkB,SAAS,KAAK;AAEnE,QAAI,CAAC,gBAAgB,SAAS;AAC5B,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,MACL,SAAS,eAAe;AAAA,MACxB,SAAS,eAAe,QAAQ,eAAe;AAAA,MAC/C,uBAAuB,eAAe;AAAA,MACtC,uBAAuB,eAAe;AAAA,IACxC;AAAA,EACF,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAKO,SAAS,aAAa,MAA+B,YAA6B;AACvF,SACE,KAAK,OAAO,SAAS,sBACrB,KAAK,OAAO,SAAS,SAAS,gBAC9B,KAAK,OAAO,SAAS,SAAS;AAElC;AAMO,SAAS,2BACd,MACA,UACS;AACT,MAAI,CAAC,aAAa,MAAM,OAAO,KAAK,KAAK,UAAU,SAAS,KAAK,CAAC,UAAU;AAC1E,WAAO;AAAA,EACT;AAEA,MAAI,K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A,IACP,gBAAgB;AAAA,EAClB;AAAA,EACA,OAAO;AACT;AAGA,IAAO,gBAAQ;","names":["ESLintUtils","ESLintUtils"]}
1
+ {"version":3,"sources":["../package.json","../src/rules/lint-build-call.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@prisma-next/eslint-plugin\",\n \"version\": \"0.3.0-dev.10\",\n \"type\": \"module\",\n \"sideEffects\": false,\n \"description\": \"ESLint plugin for Prisma Next query builder type checking and linting\",\n \"keywords\": [\n \"eslint\",\n \"eslintplugin\",\n \"typescript\",\n \"prisma-next\",\n \"query-builder\"\n ],\n \"files\": [\n \"dist\",\n \"src\"\n ],\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"exports\": {\n \".\": {\n \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.js\"\n }\n },\n \"scripts\": {\n \"build\": \"tsup --config tsup.config.ts && tsc --project tsconfig.build.json\",\n \"test\": \"vitest run\",\n \"test:coverage\": \"vitest run --coverage\",\n \"typecheck\": \"tsc --noEmit\",\n \"lint\": \"biome check . --config-path ../../../../biome.json --error-on-warnings\",\n \"lint:fix\": \"biome check --write . --config-path ../../../../biome.json\",\n \"lint:fix:unsafe\": \"biome check --write --unsafe . --config-path ../../../../biome.json\",\n \"clean\": \"node ../../../../scripts/clean.mjs\"\n },\n \"dependencies\": {\n \"@typescript-eslint/types\": \"^8.0.0\",\n \"@typescript-eslint/utils\": \"^8.0.0\"\n },\n \"devDependencies\": {\n \"@prisma-next/sql-contract\": \"workspace:*\",\n \"@prisma-next/sql-contract-ts\": \"workspace:*\",\n \"@prisma-next/sql-lane\": \"workspace:*\",\n \"@prisma-next/sql-relational-core\": \"workspace:*\",\n \"@prisma-next/sql-runtime\": \"workspace:*\",\n \"@typescript-eslint/parser\": \"^8.0.0\",\n \"@types/eslint\": \"^9.0.0\",\n \"@types/node\": \"catalog:\",\n \"@vitest/coverage-v8\": \"catalog:\",\n \"eslint\": \"^9.0.0\",\n \"tsup\": \"catalog:\",\n \"typescript\": \"catalog:\",\n \"vitest\": \"catalog:\"\n },\n \"peerDependencies\": {\n \"@typescript-eslint/parser\": \"^8.0.0\",\n \"eslint\": \"^9.0.0\",\n \"typescript\": \"*\"\n }\n}\n","import type { TSESTree } from '@typescript-eslint/types';\nimport { ESLintUtils } from '@typescript-eslint/utils';\nimport {\n type BuilderCall,\n extractCallChain,\n getTypeScriptServices,\n isPrismaNextQueryBuildCall,\n} from '../utils';\n\nconst DEFAULT_OPTIONS = {\n requireLimit: true,\n maxLimit: 1000,\n requiredFields: [],\n};\n\nconst SELECT_QUERY_METHODS = ['select', 'from'] as const;\n\n// Types\ntype MessageIds = 'unboundedQuery' | 'maxLimitExceeded';\n\ninterface RuleOptions {\n /** Enforce limit() calls on SELECT queries to prevent unbounded queries */\n requireLimit?: boolean;\n /** Maximum allowed limit value */\n maxLimit?: number;\n}\n\ntype Options = [RuleOptions];\n\n// Rule implementation\nexport const lintBuildCall = ESLintUtils.RuleCreator.withoutDocs<Options, MessageIds>({\n meta: {\n type: 'problem',\n docs: {\n description: 'Validate query builder build() calls using TypeScript type information',\n },\n schema: [\n {\n type: 'object',\n properties: {\n requireLimit: {\n type: 'boolean',\n description: 'Enforce limit() calls on SELECT queries',\n },\n maxLimit: {\n type: 'number',\n description: 'Maximum allowed limit value',\n minimum: 1,\n },\n },\n additionalProperties: false,\n },\n ],\n messages: {\n unboundedQuery:\n 'Query build() call may result in unbounded query. Consider adding .limit() to prevent fetching too many rows.',\n maxLimitExceeded:\n 'Query build() call has a limit() value that exceeds the maximum allowed of {{maxLimit}}.',\n },\n },\n defaultOptions: [DEFAULT_OPTIONS],\n create(context, [options]) {\n const services = getTypeScriptServices(context);\n\n if (!services) {\n throw new Error(\n 'TypeScript services are required for lint-build-call rule. Please ensure you are using @typescript-eslint/parser.',\n );\n }\n\n return {\n CallExpression(node: TSESTree.CallExpression) {\n if (!isPrismaNextQueryBuildCall(node, services)) {\n return;\n }\n lintQuery(node, extractCallChain(node));\n },\n };\n\n function lintQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (isSelectQuery(callChain)) {\n checkUnboundedQuery(node, callChain);\n checkLimitExceedsMax(node, callChain);\n }\n }\n\n function isSelectQuery(callChain: BuilderCall[]): boolean {\n return SELECT_QUERY_METHODS.some((method) =>\n callChain.some((call) => call.method === method),\n );\n }\n\n function checkUnboundedQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (options.requireLimit && !callChain.some((call) => call.method === 'limit')) {\n reportUnboundedQuery(node);\n }\n }\n\n function checkLimitExceedsMax(node: TSESTree.CallExpression, callChain: BuilderCall[]) {\n if (!options.maxLimit) return;\n\n const limitArg = callChain.find((call) => call.method === 'limit')?.args.pop();\n const literalValue = limitArg ? extractNumericLiteral(limitArg) : undefined;\n if (literalValue !== undefined && literalValue > options.maxLimit) {\n reportLimitExceeded(node);\n }\n }\n\n function extractNumericLiteral(\n arg: TSESTree.Expression | TSESTree.SpreadElement,\n ): number | undefined {\n if (arg?.type === 'Literal' && 'value' in arg && typeof arg.value === 'number') {\n return arg.value;\n }\n return;\n }\n\n function reportUnboundedQuery(node: TSESTree.CallExpression) {\n context.report({\n node,\n messageId: 'unboundedQuery',\n });\n }\n\n function reportLimitExceeded(node: TSESTree.CallExpression) {\n context.report({\n node,\n messageId: 'maxLimitExceeded',\n data: { maxLimit: options.maxLimit?.toString() ?? 'undefined' },\n });\n }\n },\n});\n","import type { TSESTree } from '@typescript-eslint/types';\nimport type { ParserServices } from '@typescript-eslint/utils';\nimport { ESLintUtils } from '@typescript-eslint/utils';\nimport type * as ts from 'typescript';\n\nconst PRISMA_NEXT_SQL_PACKAGES = [\n '@prisma-next/sql-lane',\n 'packages/2-sql/4-lanes/sql-lane',\n] as const;\n\nconst PLAN_TYPE_PATTERNS = [/^SqlQueryPlan$/] as const;\n\nexport type BuilderCall = { method: string; args: TSESTree.CallExpressionArgument[] };\n\n// Types\nexport interface TypeScriptServices {\n program: ts.Program;\n checker: ts.TypeChecker;\n esTreeNodeToTSNodeMap: ParserServices['esTreeNodeToTSNodeMap'];\n tsNodeToESTreeNodeMap: ParserServices['tsNodeToESTreeNodeMap'];\n}\n\n/**\n * Get TypeScript services from ESLint context\n */\nexport function getTypeScriptServices(\n context: Parameters<typeof ESLintUtils.getParserServices>[0],\n): TypeScriptServices | null {\n try {\n const parserServices = ESLintUtils.getParserServices(context, false);\n\n if (!parserServices?.program) {\n return null;\n }\n\n return {\n program: parserServices.program,\n checker: parserServices.program.getTypeChecker(),\n esTreeNodeToTSNodeMap: parserServices.esTreeNodeToTSNodeMap,\n tsNodeToESTreeNodeMap: parserServices.tsNodeToESTreeNodeMap,\n };\n } catch {\n return null;\n }\n}\n\n/**\n * Check if a call expression is a method call with a specific name\n */\nexport function isMethodCall(node: TSESTree.CallExpression, methodName: string): boolean {\n return (\n node.callee.type === 'MemberExpression' &&\n node.callee.property.type === 'Identifier' &&\n node.callee.property.name === methodName\n );\n}\n\n/**\n * Check if a call expression is a query builder build() call\n * Uses type information to verify it's actually our query builder's build method\n */\nexport function isPrismaNextQueryBuildCall(\n node: TSESTree.CallExpression,\n services?: TypeScriptServices | null,\n): boolean {\n if (!isMethodCall(node, 'build') || node.arguments.length > 1 || !services) {\n return false;\n }\n\n if (node.callee.type !== 'MemberExpression') {\n return false;\n }\n\n const objectType = getTypeOfNode(node.callee.object, services);\n if (!objectType) {\n return false;\n }\n\n if (!isTypeFromPackages(objectType, PRISMA_NEXT_SQL_PACKAGES)) {\n return false;\n }\n\n const returnType = getTypeOfNode(node, services);\n return returnType ? isPrismaNextQueryPlanType(returnType) : false;\n}\n\n/**\n * Get the TypeScript type of an ESTree node\n */\nexport function getTypeOfNode(node: TSESTree.Node, services: TypeScriptServices): ts.Type | null {\n try {\n const tsNode = services.esTreeNodeToTSNodeMap.get(node);\n return tsNode ? services.checker.getTypeAtLocation(tsNode) : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Check if a type has a specific property\n */\nexport function typeHasProperty(\n type: ts.Type,\n propertyName: string,\n checker: ts.TypeChecker,\n): boolean {\n try {\n const properties = checker.getPropertiesOfType(type);\n return properties.some((prop) => prop.getName() === propertyName);\n } catch {\n return false;\n }\n}\n\n/**\n * Check if type is a Prisma Next query plan type by name and origin\n */\nexport function isPrismaNextQueryPlanType(type: ts.Type): boolean {\n return PLAN_TYPE_PATTERNS.some((pattern) => pattern.test(type.symbol.name));\n}\n\n/**\n * Extract call chain from a call expression\n * Returns array of method names called in sequence\n */\nexport function extractCallChain(node: TSESTree.CallExpression): BuilderCall[] {\n const chain: BuilderCall[] = [];\n\n function traverse(current: TSESTree.Node): void {\n switch (current.type) {\n case 'CallExpression':\n if (current.callee.type === 'MemberExpression') {\n traverse(current.callee.object);\n if (current.callee.property.type === 'Identifier') {\n chain.push({ method: current.callee.property.name, args: current.arguments });\n }\n }\n break;\n\n case 'MemberExpression':\n traverse(current.object);\n break;\n\n case 'Identifier':\n break;\n }\n }\n\n traverse(node);\n return chain;\n}\n\n/**\n * Helper to check if a type originates from specific packages\n */\nfunction isTypeFromPackages(type: ts.Type, packages: readonly string[]): boolean {\n const fileName = type.getSymbol()?.valueDeclaration?.getSourceFile().fileName;\n return fileName ? packages.some((pkg) => fileName.includes(`${pkg}/`)) : false;\n}\n","import { name, version } from '../package.json';\nimport { lintBuildCall } from './rules/lint-build-call';\n\n// Plugin metadata\nconst PLUGIN_META = {\n name,\n version,\n};\n\n// Rule definitions\nconst RULES = {\n 'lint-build-call': lintBuildCall,\n};\n\n// Configuration presets\nconst RULE_CONFIG = {\n '@prisma-next/lint-build-call': 'error',\n};\n\n// Plugin interface\ninterface ESLintPlugin {\n meta: {\n name: string;\n version: string;\n };\n // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility\n rules: Record<string, any>;\n // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility\n configs: Record<string, any>;\n}\n\n// Plugin implementation\nconst plugin: ESLintPlugin = {\n meta: PLUGIN_META,\n rules: RULES,\n configs: {\n recommended: {\n plugins: ['@prisma-next'],\n rules: RULE_CONFIG,\n },\n },\n};\n\n// Add flat config after plugin is defined to avoid circular reference\nplugin.configs['flat/recommended'] = {\n plugins: {\n '@prisma-next': plugin,\n },\n rules: RULE_CONFIG,\n};\n\n// Exports\nexport default plugin;\nexport { lintBuildCall };\nexport { plugin };\nexport type { ESLintPlugin };\n"],"mappings":";AACE,WAAQ;AACR,cAAW;;;ACDb,SAAS,eAAAA,oBAAmB;;;ACC5B,SAAS,mBAAmB;AAG5B,IAAM,2BAA2B;AAAA,EAC/B;AAAA,EACA;AACF;AAEA,IAAM,qBAAqB,CAAC,gBAAgB;AAerC,SAAS,sBACd,SAC2B;AAC3B,MAAI;AACF,UAAM,iBAAiB,YAAY,kBAAkB,SAAS,KAAK;AAEnE,QAAI,CAAC,gBAAgB,SAAS;AAC5B,aAAO;AAAA,IACT;AAEA,WAAO;AAAA,MACL,SAAS,eAAe;AAAA,MACxB,SAAS,eAAe,QAAQ,eAAe;AAAA,MAC/C,uBAAuB,eAAe;AAAA,MACtC,uBAAuB,eAAe;AAAA,IACxC;AAAA,EACF,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAKO,SAAS,aAAa,MAA+B,YAA6B;AACvF,SACE,KAAK,OAAO,SAAS,sBACrB,KAAK,OAAO,SAAS,SAAS,gBAC9B,KAAK,OAAO,SAAS,SAAS;AAElC;AAMO,SAAS,2BACd,MACA,UACS;AACT,MAAI,CAAC,aAAa,MAAM,OAAO,KAAK,KAAK,UAAU,SAAS,KAAK,CAAC,UAAU;AAC1E,WAAO;AAAA,EACT;AAEA,MAAI,KAAK,OAAO,SAAS,oBAAoB;AAC3C,WAAO;AAAA,EACT;AAEA,QAAM,aAAa,cAAc,KAAK,OAAO,QAAQ,QAAQ;AAC7D,MAAI,CAAC,YAAY;AACf,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,mBAAmB,YAAY,wBAAwB,GAAG;AAC7D,WA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ACrF,UAAI,CAAC,QAAQ,SAAU;AAEvB,YAAM,WAAW,UAAU,KAAK,CAAC,SAAS,KAAK,WAAW,OAAO,GAAG,KAAK,IAAI;AAC7E,YAAM,eAAe,WAAW,sBAAsB,QAAQ,IAAI;AAClE,UAAI,iBAAiB,UAAa,eAAe,QAAQ,UAAU;AACjE,4BAAoB,IAAI;AAAA,MAC1B;AAAA,IACF;AAEA,aAAS,sBACP,KACoB;AACpB,UAAI,KAAK,SAAS,aAAa,WAAW,OAAO,OAAO,IAAI,UAAU,UAAU;AAC9E,eAAO,IAAI;AAAA,MACb;AACA;AAAA,IACF;AAEA,aAAS,qBAAqB,MAA+B;AAC3D,cAAQ,OAAO;AAAA,QACb;AAAA,QACA,WAAW;AAAA,MACb,CAAC;AAAA,IACH;AAEA,aAAS,oBAAoB,MAA+B;AAC1D,cAAQ,OAAO;AAAA,QACb;AAAA,QACA,WAAW;AAAA,QACX,MAAM,EAAE,UAAU,QAAQ,UAAU,SAAS,KAAK,YAAY;AAAA,MAChE,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AEhID,IAAM,cAAc;AAAA,EAClB;AAAA,EACA;AACF;AAGA,IAAM,QAAQ;AAAA,EACZ,mBAAmB;AACrB;AAGA,IAAM,cAAc;AAAA,EAClB,gCAAgC;AAClC;AAeA,IAAM,SAAuB;AAAA,EAC3B,MAAM;AAAA,EACN,OAAO;AAAA,EACP,SAAS;AAAA,IACP,aAAa;AAAA,MACX,SAAS,CAAC,cAAc;AAAA,MACxB,OAAO;AAAA,IACT;AAAA,EACF;AACF;AAGA,OAAO,QAAQ,kBAAkB,IAAI;AAAA,EACnC,SAAS;AAAA,IACP,gBAAgB;AAAA,EAClB;AAAA,EACA,OAAO;AACT;AAGA,IAAO,gBAAQ;","names":["ESLintUtils","ESLintUtils"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma-next/eslint-plugin",
3
- "version": "0.3.0-dev.1",
3
+ "version": "0.3.0-dev.10",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "ESLint plugin for Prisma Next query builder type checking and linting",
@@ -12,11 +12,16 @@
12
12
  "query-builder"
13
13
  ],
14
14
  "files": [
15
- "dist"
15
+ "dist",
16
+ "src"
16
17
  ],
17
18
  "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
18
20
  "exports": {
19
- ".": "./dist/index.js"
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ }
20
25
  },
21
26
  "dependencies": {
22
27
  "@typescript-eslint/types": "^8.0.0",
@@ -26,16 +31,16 @@
26
31
  "@typescript-eslint/parser": "^8.0.0",
27
32
  "@types/eslint": "^9.0.0",
28
33
  "@types/node": "24.10.4",
29
- "@vitest/coverage-v8": "^4.0.0",
34
+ "@vitest/coverage-v8": "4.0.16",
30
35
  "eslint": "^9.0.0",
31
- "tsup": "^8.3.0",
32
- "typescript": "^5.9.3",
33
- "vitest": "^4.0.16",
34
- "@prisma-next/sql-contract": "0.3.0-dev.1",
35
- "@prisma-next/sql-contract-ts": "0.3.0-dev.1",
36
- "@prisma-next/sql-lane": "0.3.0-dev.1",
37
- "@prisma-next/sql-relational-core": "0.3.0-dev.1",
38
- "@prisma-next/sql-runtime": "0.3.0-dev.1"
36
+ "tsup": "8.5.1",
37
+ "typescript": "5.9.3",
38
+ "vitest": "4.0.16",
39
+ "@prisma-next/sql-contract": "0.3.0-dev.10",
40
+ "@prisma-next/sql-contract-ts": "0.3.0-dev.10",
41
+ "@prisma-next/sql-lane": "0.3.0-dev.10",
42
+ "@prisma-next/sql-relational-core": "0.3.0-dev.10",
43
+ "@prisma-next/sql-runtime": "0.3.0-dev.10"
39
44
  },
40
45
  "peerDependencies": {
41
46
  "@typescript-eslint/parser": "^8.0.0",
@@ -43,7 +48,7 @@
43
48
  "typescript": "*"
44
49
  },
45
50
  "scripts": {
46
- "build": "tsup --config tsup.config.ts && tsc --emitDeclarationOnly",
51
+ "build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
47
52
  "test": "vitest run",
48
53
  "test:coverage": "vitest run --coverage",
49
54
  "typecheck": "tsc --noEmit",
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { name, version } from '../package.json';
2
+ import { lintBuildCall } from './rules/lint-build-call';
3
+
4
+ // Plugin metadata
5
+ const PLUGIN_META = {
6
+ name,
7
+ version,
8
+ };
9
+
10
+ // Rule definitions
11
+ const RULES = {
12
+ 'lint-build-call': lintBuildCall,
13
+ };
14
+
15
+ // Configuration presets
16
+ const RULE_CONFIG = {
17
+ '@prisma-next/lint-build-call': 'error',
18
+ };
19
+
20
+ // Plugin interface
21
+ interface ESLintPlugin {
22
+ meta: {
23
+ name: string;
24
+ version: string;
25
+ };
26
+ // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility
27
+ rules: Record<string, any>;
28
+ // biome-ignore lint/suspicious/noExplicitAny: Required for ESLint plugin interface compatibility
29
+ configs: Record<string, any>;
30
+ }
31
+
32
+ // Plugin implementation
33
+ const plugin: ESLintPlugin = {
34
+ meta: PLUGIN_META,
35
+ rules: RULES,
36
+ configs: {
37
+ recommended: {
38
+ plugins: ['@prisma-next'],
39
+ rules: RULE_CONFIG,
40
+ },
41
+ },
42
+ };
43
+
44
+ // Add flat config after plugin is defined to avoid circular reference
45
+ plugin.configs['flat/recommended'] = {
46
+ plugins: {
47
+ '@prisma-next': plugin,
48
+ },
49
+ rules: RULE_CONFIG,
50
+ };
51
+
52
+ // Exports
53
+ export default plugin;
54
+ export { lintBuildCall };
55
+ export { plugin };
56
+ export type { ESLintPlugin };
@@ -0,0 +1,133 @@
1
+ import type { TSESTree } from '@typescript-eslint/types';
2
+ import { ESLintUtils } from '@typescript-eslint/utils';
3
+ import {
4
+ type BuilderCall,
5
+ extractCallChain,
6
+ getTypeScriptServices,
7
+ isPrismaNextQueryBuildCall,
8
+ } from '../utils';
9
+
10
+ const DEFAULT_OPTIONS = {
11
+ requireLimit: true,
12
+ maxLimit: 1000,
13
+ requiredFields: [],
14
+ };
15
+
16
+ const SELECT_QUERY_METHODS = ['select', 'from'] as const;
17
+
18
+ // Types
19
+ type MessageIds = 'unboundedQuery' | 'maxLimitExceeded';
20
+
21
+ interface RuleOptions {
22
+ /** Enforce limit() calls on SELECT queries to prevent unbounded queries */
23
+ requireLimit?: boolean;
24
+ /** Maximum allowed limit value */
25
+ maxLimit?: number;
26
+ }
27
+
28
+ type Options = [RuleOptions];
29
+
30
+ // Rule implementation
31
+ export const lintBuildCall = ESLintUtils.RuleCreator.withoutDocs<Options, MessageIds>({
32
+ meta: {
33
+ type: 'problem',
34
+ docs: {
35
+ description: 'Validate query builder build() calls using TypeScript type information',
36
+ },
37
+ schema: [
38
+ {
39
+ type: 'object',
40
+ properties: {
41
+ requireLimit: {
42
+ type: 'boolean',
43
+ description: 'Enforce limit() calls on SELECT queries',
44
+ },
45
+ maxLimit: {
46
+ type: 'number',
47
+ description: 'Maximum allowed limit value',
48
+ minimum: 1,
49
+ },
50
+ },
51
+ additionalProperties: false,
52
+ },
53
+ ],
54
+ messages: {
55
+ unboundedQuery:
56
+ 'Query build() call may result in unbounded query. Consider adding .limit() to prevent fetching too many rows.',
57
+ maxLimitExceeded:
58
+ 'Query build() call has a limit() value that exceeds the maximum allowed of {{maxLimit}}.',
59
+ },
60
+ },
61
+ defaultOptions: [DEFAULT_OPTIONS],
62
+ create(context, [options]) {
63
+ const services = getTypeScriptServices(context);
64
+
65
+ if (!services) {
66
+ throw new Error(
67
+ 'TypeScript services are required for lint-build-call rule. Please ensure you are using @typescript-eslint/parser.',
68
+ );
69
+ }
70
+
71
+ return {
72
+ CallExpression(node: TSESTree.CallExpression) {
73
+ if (!isPrismaNextQueryBuildCall(node, services)) {
74
+ return;
75
+ }
76
+ lintQuery(node, extractCallChain(node));
77
+ },
78
+ };
79
+
80
+ function lintQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {
81
+ if (isSelectQuery(callChain)) {
82
+ checkUnboundedQuery(node, callChain);
83
+ checkLimitExceedsMax(node, callChain);
84
+ }
85
+ }
86
+
87
+ function isSelectQuery(callChain: BuilderCall[]): boolean {
88
+ return SELECT_QUERY_METHODS.some((method) =>
89
+ callChain.some((call) => call.method === method),
90
+ );
91
+ }
92
+
93
+ function checkUnboundedQuery(node: TSESTree.CallExpression, callChain: BuilderCall[]) {
94
+ if (options.requireLimit && !callChain.some((call) => call.method === 'limit')) {
95
+ reportUnboundedQuery(node);
96
+ }
97
+ }
98
+
99
+ function checkLimitExceedsMax(node: TSESTree.CallExpression, callChain: BuilderCall[]) {
100
+ if (!options.maxLimit) return;
101
+
102
+ const limitArg = callChain.find((call) => call.method === 'limit')?.args.pop();
103
+ const literalValue = limitArg ? extractNumericLiteral(limitArg) : undefined;
104
+ if (literalValue !== undefined && literalValue > options.maxLimit) {
105
+ reportLimitExceeded(node);
106
+ }
107
+ }
108
+
109
+ function extractNumericLiteral(
110
+ arg: TSESTree.Expression | TSESTree.SpreadElement,
111
+ ): number | undefined {
112
+ if (arg?.type === 'Literal' && 'value' in arg && typeof arg.value === 'number') {
113
+ return arg.value;
114
+ }
115
+ return;
116
+ }
117
+
118
+ function reportUnboundedQuery(node: TSESTree.CallExpression) {
119
+ context.report({
120
+ node,
121
+ messageId: 'unboundedQuery',
122
+ });
123
+ }
124
+
125
+ function reportLimitExceeded(node: TSESTree.CallExpression) {
126
+ context.report({
127
+ node,
128
+ messageId: 'maxLimitExceeded',
129
+ data: { maxLimit: options.maxLimit?.toString() ?? 'undefined' },
130
+ });
131
+ }
132
+ },
133
+ });
package/src/utils.ts ADDED
@@ -0,0 +1,159 @@
1
+ import type { TSESTree } from '@typescript-eslint/types';
2
+ import type { ParserServices } from '@typescript-eslint/utils';
3
+ import { ESLintUtils } from '@typescript-eslint/utils';
4
+ import type * as ts from 'typescript';
5
+
6
+ const PRISMA_NEXT_SQL_PACKAGES = [
7
+ '@prisma-next/sql-lane',
8
+ 'packages/2-sql/4-lanes/sql-lane',
9
+ ] as const;
10
+
11
+ const PLAN_TYPE_PATTERNS = [/^SqlQueryPlan$/] as const;
12
+
13
+ export type BuilderCall = { method: string; args: TSESTree.CallExpressionArgument[] };
14
+
15
+ // Types
16
+ export interface TypeScriptServices {
17
+ program: ts.Program;
18
+ checker: ts.TypeChecker;
19
+ esTreeNodeToTSNodeMap: ParserServices['esTreeNodeToTSNodeMap'];
20
+ tsNodeToESTreeNodeMap: ParserServices['tsNodeToESTreeNodeMap'];
21
+ }
22
+
23
+ /**
24
+ * Get TypeScript services from ESLint context
25
+ */
26
+ export function getTypeScriptServices(
27
+ context: Parameters<typeof ESLintUtils.getParserServices>[0],
28
+ ): TypeScriptServices | null {
29
+ try {
30
+ const parserServices = ESLintUtils.getParserServices(context, false);
31
+
32
+ if (!parserServices?.program) {
33
+ return null;
34
+ }
35
+
36
+ return {
37
+ program: parserServices.program,
38
+ checker: parserServices.program.getTypeChecker(),
39
+ esTreeNodeToTSNodeMap: parserServices.esTreeNodeToTSNodeMap,
40
+ tsNodeToESTreeNodeMap: parserServices.tsNodeToESTreeNodeMap,
41
+ };
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Check if a call expression is a method call with a specific name
49
+ */
50
+ export function isMethodCall(node: TSESTree.CallExpression, methodName: string): boolean {
51
+ return (
52
+ node.callee.type === 'MemberExpression' &&
53
+ node.callee.property.type === 'Identifier' &&
54
+ node.callee.property.name === methodName
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Check if a call expression is a query builder build() call
60
+ * Uses type information to verify it's actually our query builder's build method
61
+ */
62
+ export function isPrismaNextQueryBuildCall(
63
+ node: TSESTree.CallExpression,
64
+ services?: TypeScriptServices | null,
65
+ ): boolean {
66
+ if (!isMethodCall(node, 'build') || node.arguments.length > 1 || !services) {
67
+ return false;
68
+ }
69
+
70
+ if (node.callee.type !== 'MemberExpression') {
71
+ return false;
72
+ }
73
+
74
+ const objectType = getTypeOfNode(node.callee.object, services);
75
+ if (!objectType) {
76
+ return false;
77
+ }
78
+
79
+ if (!isTypeFromPackages(objectType, PRISMA_NEXT_SQL_PACKAGES)) {
80
+ return false;
81
+ }
82
+
83
+ const returnType = getTypeOfNode(node, services);
84
+ return returnType ? isPrismaNextQueryPlanType(returnType) : false;
85
+ }
86
+
87
+ /**
88
+ * Get the TypeScript type of an ESTree node
89
+ */
90
+ export function getTypeOfNode(node: TSESTree.Node, services: TypeScriptServices): ts.Type | null {
91
+ try {
92
+ const tsNode = services.esTreeNodeToTSNodeMap.get(node);
93
+ return tsNode ? services.checker.getTypeAtLocation(tsNode) : null;
94
+ } catch {
95
+ return null;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Check if a type has a specific property
101
+ */
102
+ export function typeHasProperty(
103
+ type: ts.Type,
104
+ propertyName: string,
105
+ checker: ts.TypeChecker,
106
+ ): boolean {
107
+ try {
108
+ const properties = checker.getPropertiesOfType(type);
109
+ return properties.some((prop) => prop.getName() === propertyName);
110
+ } catch {
111
+ return false;
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Check if type is a Prisma Next query plan type by name and origin
117
+ */
118
+ export function isPrismaNextQueryPlanType(type: ts.Type): boolean {
119
+ return PLAN_TYPE_PATTERNS.some((pattern) => pattern.test(type.symbol.name));
120
+ }
121
+
122
+ /**
123
+ * Extract call chain from a call expression
124
+ * Returns array of method names called in sequence
125
+ */
126
+ export function extractCallChain(node: TSESTree.CallExpression): BuilderCall[] {
127
+ const chain: BuilderCall[] = [];
128
+
129
+ function traverse(current: TSESTree.Node): void {
130
+ switch (current.type) {
131
+ case 'CallExpression':
132
+ if (current.callee.type === 'MemberExpression') {
133
+ traverse(current.callee.object);
134
+ if (current.callee.property.type === 'Identifier') {
135
+ chain.push({ method: current.callee.property.name, args: current.arguments });
136
+ }
137
+ }
138
+ break;
139
+
140
+ case 'MemberExpression':
141
+ traverse(current.object);
142
+ break;
143
+
144
+ case 'Identifier':
145
+ break;
146
+ }
147
+ }
148
+
149
+ traverse(node);
150
+ return chain;
151
+ }
152
+
153
+ /**
154
+ * Helper to check if a type originates from specific packages
155
+ */
156
+ function isTypeFromPackages(type: ts.Type, packages: readonly string[]): boolean {
157
+ const fileName = type.getSymbol()?.valueDeclaration?.getSourceFile().fileName;
158
+ return fileName ? packages.some((pkg) => fileName.includes(`${pkg}/`)) : false;
159
+ }