-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add linting rule to recursively check for Prisma includes using… (
#13652) * feat: Add linting rule to recursively check for Prisma includes using only 'true' * Fixing linting errors * More linting fixes * Disabled linting for forms * Update recommended.ts * Removed ignores * Fixed warning * Type fix --------- Co-authored-by: Keith Williams <keithwillcode@gmail.com>
- Loading branch information
1 parent
fc16ec5
commit 7c35432
Showing
10 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/eslint-plugin/src/rules/no-prisma-include-true.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import type { TSESTree } from "@typescript-eslint/utils"; | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
import type { ReportDescriptor } from "@typescript-eslint/utils/dist/ts-eslint"; | ||
|
||
const createRule = ESLintUtils.RuleCreator((name) => `https://developer.cal.com/eslint/rule/${name}`); | ||
|
||
const assesIncludePropertyIncludesTrue = ( | ||
includeProperty: TSESTree.Property, | ||
reporter: { (reportObj: ReportDescriptor<"no-prisma-include-true">): void } | ||
) => { | ||
if (includeProperty.value.type === "ObjectExpression") { | ||
includeProperty.value.properties.forEach((childProperty) => { | ||
if ( | ||
childProperty.type === "Property" && | ||
childProperty.value.type === "Literal" && | ||
childProperty.value.value === true | ||
) { | ||
reporter({ | ||
node: childProperty, | ||
messageId: "no-prisma-include-true", | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
const searchIncludeProperty = ( | ||
property: TSESTree.Property, | ||
reporter: { (reportObj: ReportDescriptor<"no-prisma-include-true">): void } | ||
) => { | ||
if (property.type === "Property") { | ||
// If property is include, check if it has a child property with value true | ||
if (property.key.type === "Identifier" && property.key.name === "include") { | ||
assesIncludePropertyIncludesTrue(property, reporter); | ||
} | ||
|
||
// If property value is also an object, recursively search for include property | ||
if (property.value.type === "ObjectExpression") { | ||
property.value.properties.forEach((childProperty) => { | ||
if (childProperty.type === "Property") { | ||
searchIncludeProperty(childProperty, reporter); | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const rule = createRule({ | ||
create: function (context) { | ||
return { | ||
CallExpression(node) { | ||
if (!(node.callee as TSESTree.MemberExpression).property) { | ||
return null; | ||
} | ||
|
||
const nodeName = ((node.callee as TSESTree.MemberExpression).property as TSESTree.Identifier).name; | ||
|
||
if ( | ||
!["findUnique", "findUniqueOrThrow", "findFirst", "findFirstOrThrow", "findMany"].includes(nodeName) | ||
) { | ||
return null; | ||
} | ||
|
||
const nodeArgs = node.arguments[0] as TSESTree.ObjectExpression; | ||
if (!nodeArgs) { | ||
return null; | ||
} | ||
|
||
const backReporter = (reportObj: ReportDescriptor<"no-prisma-include-true">) => { | ||
context.report(reportObj); | ||
}; | ||
|
||
nodeArgs.properties?.forEach((property) => { | ||
if (property.type === "Property") { | ||
searchIncludeProperty(property, backReporter); | ||
} | ||
}); | ||
return null; | ||
}, | ||
}; | ||
}, | ||
|
||
name: "no-prisma-include-true", | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"Disallow passing argument object with include: { AnyPropertyName: true } to prisma methods", | ||
recommended: "error", | ||
}, | ||
messages: { | ||
"no-prisma-include-true": `Do not pass argument object with include: { AnyPropertyName: true } to prisma methods`, | ||
}, | ||
fixable: "code", | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
}); | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters