Skip to content

Commit

Permalink
Merge branch 'main' into Planetscale-tests-speed-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelelz authored Dec 2, 2023
2 parents 20d513c + cdb6280 commit 84e23de
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changelogs/eslint-plugin-drizzle/0.2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# eslint-plugin-drizzle 0.2.2

- fix: Correct detection of `drizzleObjectName` when it's a nested object
2 changes: 1 addition & 1 deletion eslint-plugin-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-drizzle",
"version": "0.2.1",
"version": "0.2.2",
"description": "Eslint plugin for drizzle users to avoid common pitfalls",
"main": "src/index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions eslint-plugin-drizzle/src/enforce-delete-with-where.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const deleteRule = createRule<Options, MessageIds>({
},
fixable: 'code',
messages: {
enforceDeleteWithWhere: 'Without `.where(...)` you will delete all the rows in a table. If you didn\'t want to do it, please use `db.delete(...).where(...)` instead. Otherwise you can ignore this rule here',
enforceDeleteWithWhere:
"Without `.where(...)` you will delete all the rows in a table. If you didn't want to do it, please use `db.delete(...).where(...)` instead. Otherwise you can ignore this rule here",
},
schema: [{
type: 'object',
Expand All @@ -33,7 +34,7 @@ const deleteRule = createRule<Options, MessageIds>({
return {
MemberExpression: (node) => {
if (node.property.type === 'Identifier') {
if (isDrizzleObj(node, options) && node.property.name === 'delete' && lastNodeName !== 'where') {
if (node.property.name === 'delete' && lastNodeName !== 'where' && isDrizzleObj(node, options)) {
context.report({
node,
messageId: 'enforceDeleteWithWhere',
Expand Down
39 changes: 29 additions & 10 deletions eslint-plugin-drizzle/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,40 @@ export const isDrizzleObj = (
) => {
const drizzleObjectName = options[0].drizzleObjectName;

if (
node.object.type === 'Identifier' && typeof drizzleObjectName === 'string'
&& node.object.name === drizzleObjectName
) {
return true;
}

if (Array.isArray(drizzleObjectName)) {
if (drizzleObjectName.length === 0) {
if (node.object.type === 'Identifier') {
if (
typeof drizzleObjectName === 'string'
&& node.object.name === drizzleObjectName
) {
return true;
}

if (node.object.type === 'Identifier' && drizzleObjectName.includes(node.object.name)) {
if (Array.isArray(drizzleObjectName)) {
if (drizzleObjectName.length === 0) {
return true;
}

if (drizzleObjectName.includes(node.object.name)) {
return true;
}
}
} else if (node.object.type === 'MemberExpression' && node.object.property.type === 'Identifier') {
if (
typeof drizzleObjectName === 'string'
&& node.object.property.name === drizzleObjectName
) {
return true;
}

if (Array.isArray(drizzleObjectName)) {
if (drizzleObjectName.length === 0) {
return true;
}

if (drizzleObjectName.includes(node.object.property.name)) {
return true;
}
}
}

return false;
Expand Down
20 changes: 20 additions & 0 deletions eslint-plugin-drizzle/tests/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ ruleTester.run('enforce delete with where (default options)', myRule, {
`dataSource
.delete()
.where()`,
`this.database.delete({}).where()`,
],
invalid: [
{
code: 'db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
},
{
code: 'this.dataSource.db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
},
{
code: 'const a = await db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
Expand All @@ -41,6 +46,7 @@ ruleTester.run('enforce delete with where (default options)', myRule, {
ruleTester.run('enforce delete with where (string option)', myRule, {
valid: [
{ code: 'const a = db.delete({}).where({});', options: [{ drizzleObjectName: 'db' }] },
{ code: 'const a = this.database.db.delete({}).where({});', options: [{ drizzleObjectName: 'db' }] },
{ code: 'const a = something.delete({})', options: [{ drizzleObjectName: 'db' }] },
{ code: 'delete db.something', options: [{ drizzleObjectName: 'db' }] },
{
Expand All @@ -61,6 +67,11 @@ ruleTester.run('enforce delete with where (string option)', myRule, {
errors: [{ messageId: 'enforceDeleteWithWhere' }],
options: [{ drizzleObjectName: 'db' }],
},
{
code: 'this.database.db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
options: [{ drizzleObjectName: 'db' }],
},
{
code: 'const a = await db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
Expand All @@ -77,6 +88,10 @@ ruleTester.run('enforce delete with where (string option)', myRule, {
ruleTester.run('enforce delete with where (array option)', myRule, {
valid: [
{ code: 'const a = db.delete({}).where({});', options: [{ drizzleObjectName: ['db'] }] },
{
code: 'const a = this.database.dataSource.delete({}).where({});',
options: [{ drizzleObjectName: ['db', 'dataSource'] }],
},
{ code: 'delete db.something', options: [{ drizzleObjectName: ['db'] }] },
{
code: `dataSource
Expand All @@ -96,6 +111,11 @@ ruleTester.run('enforce delete with where (array option)', myRule, {
errors: [{ messageId: 'enforceDeleteWithWhere' }],
options: [{ drizzleObjectName: ['db', 'anotherName'] }],
},
{
code: 'this.dataSource.db.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
options: [{ drizzleObjectName: ['db', 'anotherName'] }],
},
{
code: 'dataSource.delete({})',
errors: [{ messageId: 'enforceDeleteWithWhere' }],
Expand Down
21 changes: 21 additions & 0 deletions eslint-plugin-drizzle/tests/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ ruleTester.run('enforce update with where (default options)', myRule, {
`dataSource
.update()
.set()
.where()`,
`this
.dataSource
.update()
.set()
.where()`,
],
invalid: [
{
code: 'db.update({}).set()',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
},
{
code: 'this.database.db.update({}).set()',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
},
{
code: 'const a = await db.update({}).set()',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
Expand All @@ -48,6 +57,7 @@ ruleTester.run('enforce update with where (default options)', myRule, {
ruleTester.run('enforce update with where (string option)', myRule, {
valid: [
{ code: 'const a = db.update({}).set().where({});', options: [{ drizzleObjectName: 'db' }] },
{ code: 'const a = this.database.db.update({}).set().where({});', options: [{ drizzleObjectName: 'db' }] },
{ code: 'update.db.update()', options: [{ drizzleObjectName: 'db' }] },
{
code: `dataSource
Expand All @@ -67,6 +77,11 @@ ruleTester.run('enforce update with where (string option)', myRule, {
errors: [{ messageId: 'enforceUpdateWithWhere' }],
options: [{ drizzleObjectName: 'db' }],
},
{
code: 'this.dataSource.db.update({}).set({})',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
options: [{ drizzleObjectName: 'db' }],
},
{
code: 'const a = await db.update({}).set()',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
Expand All @@ -83,6 +98,7 @@ ruleTester.run('enforce update with where (string option)', myRule, {
ruleTester.run('enforce delete with where (array option)', myRule, {
valid: [
{ code: 'const a = db.update({}).set().where({});', options: [{ drizzleObjectName: ['db'] }] },
{ code: 'const a = this.dataSource.db.update({}).set().where({});', options: [{ drizzleObjectName: ['db'] }] },
{ code: 'update.db.something', options: [{ drizzleObjectName: ['db'] }] },
{
code: `dataSource
Expand All @@ -103,6 +119,11 @@ ruleTester.run('enforce delete with where (array option)', myRule, {
errors: [{ messageId: 'enforceUpdateWithWhere' }],
options: [{ drizzleObjectName: ['db', 'anotherName'] }],
},
{
code: 'this.dataSource.db.update({}).set()',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
options: [{ drizzleObjectName: ['db', 'anotherName'] }],
},
{
code: 'dataSource.update({}).set({})',
errors: [{ messageId: 'enforceUpdateWithWhere' }],
Expand Down

0 comments on commit 84e23de

Please sign in to comment.