Просмотр исходного кода

impliment no-populate as a biome plugin with GritQL

Yuki Takei 3 месяцев назад
Родитель
Сommit
69cf7e263e

+ 9 - 0
apps/app/.biome/plugins/no-populate.grit

@@ -0,0 +1,9 @@
+engine biome(1.0)
+language js(typescript)
+
+`$obj.populate($args)` where {
+  register_diagnostic(
+    span=$obj,
+    message="The 'populate' method should not be called in model modules. Consider restructuring your code to avoid using populate in model files."
+  )
+}

+ 0 - 10
apps/app/src/server/models/.eslintrc.js

@@ -1,10 +0,0 @@
-const rulesDirPlugin = require('eslint-plugin-rulesdir');
-
-rulesDirPlugin.RULES_DIR = 'src/server/models/eslint-rules-dir';
-
-module.exports = {
-  plugins: ['rulesdir'],
-  rules: {
-    'rulesdir/no-populate': 'warn',
-  },
-};

+ 0 - 28
apps/app/src/server/models/eslint-rules-dir/no-populate.js

@@ -1,28 +0,0 @@
-/**
- * @typedef {import('eslint').Rule} Rule
- * @typedef {import('./lib/html.js').HtmlOptions} HtmlOptions
- */
-
-/** @type {Rule.RuleModule} */
-module.exports = {
-  meta: {
-    type: 'problem',
-  },
-  /**
-   * @property {Rule.RuleContext} context
-   * @return {Rule.RuleListener}
-   */
-  create: (context) => {
-    return {
-      CallExpression(node) {
-        if (node.callee.property && node.callee.property.name === 'populate') {
-          context.report({
-            node,
-            message:
-              "The 'populate' method should not be called in model modules.",
-          });
-        }
-      },
-    };
-  },
-};

+ 0 - 26
apps/app/src/server/models/eslint-rules-dir/test/no-populate.spec.ts

@@ -1,26 +0,0 @@
-import { RuleTester } from 'eslint';
-
-import noPopulate from '../no-populate';
-
-const ruleTester = new RuleTester({
-  parserOptions: {
-    ecmaVersion: 2015,
-  },
-});
-
-test('test no-populate', () => {
-  ruleTester.run('no-populate', noPopulate, {
-    valid: [{ code: 'Model.find();' }],
-    invalid: [
-      {
-        code: "Model.find().populate('children');",
-        errors: [
-          {
-            message:
-              "The 'populate' method should not be called in model modules.",
-          },
-        ],
-      },
-    ],
-  });
-});

+ 5 - 0
apps/app/src/server/models/obsolete-page.js

@@ -79,6 +79,7 @@ export const populateDataToShowRevision = (
   userPublicFields,
   shouldExcludeBody = false,
 ) => {
+  // biome-ignore lint/plugin: populating is the purpose of this method
   return page.populate([
     { path: 'lastUpdateUser', select: userPublicFields },
     { path: 'creator', select: userPublicFields },
@@ -150,6 +151,7 @@ export const getPageSchema = (crowi) => {
 
   pageSchema.methods.findRelatedTagsById = async function () {
     const PageTagRelation = mongoose.model('PageTagRelation');
+    // biome-ignore lint/plugin: allow populate for backward compatibility
     const relations = await PageTagRelation.find({
       relatedPage: this._id,
     }).populate('relatedTag');
@@ -159,6 +161,7 @@ export const getPageSchema = (crowi) => {
   };
 
   pageSchema.methods.isUpdatable = async function (previousRevision, origin) {
+    // biome-ignore lint/plugin: allow populate for backward compatibility
     const populatedPageDataWithRevisionOrigin = await this.populate(
       'revision',
       'origin',
@@ -284,6 +287,7 @@ export const getPageSchema = (crowi) => {
     if (revisionId != null) {
       this.revision = revisionId;
     }
+    // biome-ignore lint/plugin: populating is the purpose of this method
     return this.populate('revision');
   };
 
@@ -680,6 +684,7 @@ export const getPageSchema = (crowi) => {
       );
     });
 
+    // biome-ignore lint/plugin: allow populate for backward compatibility
     const templatePages = await this.find({ path: { $in: regexpList } })
       .populate({ path: 'revision', model: 'Revision' })
       .exec();

+ 1 - 0
apps/app/src/server/models/page-tag-relation.ts

@@ -130,6 +130,7 @@ schema.statics.createTagListWithCount = createTagListWithCount;
 
 const findByPageId: FindByPageId = async function (pageId, options = {}) {
   const isAcceptRelatedTagNull = options.nullable || null;
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   const relations = await this.find({ relatedPage: pageId })
     .populate('relatedTag')
     .select('relatedTag');

+ 1 - 0
apps/app/src/server/models/page.ts

@@ -639,6 +639,7 @@ export class PageQueryBuilder {
   }
 
   populateDataToList(userPublicFields): PageQueryBuilder {
+    // biome-ignore lint/plugin: populating is the purpose of this method
     this.query = this.query.populate({
       path: 'lastUpdateUser',
       select: userPublicFields,

+ 2 - 2
apps/app/src/server/models/update-post.ts

@@ -105,8 +105,8 @@ updatePostSchema.statics.findSettingsByPath = async function (path) {
   return validSettings;
 };
 
-// eslint-disable-next-line @typescript-eslint/no-unused-vars
-updatePostSchema.statics.findAll = function (offset = 0) {
+updatePostSchema.statics.findAll = function (_offset = 0) {
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   return this.find().sort({ createdAt: 1 }).populate('creator').exec();
 };
 

+ 4 - 0
apps/app/src/server/models/user-group-relation.ts

@@ -90,6 +90,7 @@ schema.statics.removeAllInvalidRelations = function () {
  * @memberof UserGroupRelation
  */
 schema.statics.findAllRelation = function () {
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   return this.find().populate('relatedUser').populate('relatedGroup').exec();
 };
 
@@ -103,6 +104,7 @@ schema.statics.findAllRelation = function () {
  */
 schema.statics.findAllRelationForUserGroup = function (userGroup) {
   logger.debug('findAllRelationForUserGroup is called', userGroup);
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   return this.find({ relatedGroup: userGroup }).populate('relatedUser').exec();
 };
 
@@ -126,6 +128,7 @@ schema.statics.findAllUserIdsForUserGroups = async function (
  * @memberof UserGroupRelation
  */
 schema.statics.findAllRelationForUserGroups = function (userGroups) {
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   return this.find({ relatedGroup: { $in: userGroups } })
     .populate('relatedUser')
     .exec();
@@ -142,6 +145,7 @@ schema.statics.findAllRelationForUserGroups = function (userGroups) {
 schema.statics.findAllGroupsForUser = async function (
   user,
 ): Promise<UserGroupDocument[]> {
+  // biome-ignore lint/plugin: allow populate for backward compatibility
   const userGroupRelations = await this.find({
     relatedUser: user._id,
   }).populate('relatedGroup');

+ 1 - 0
biome.json

@@ -10,6 +10,7 @@
       "!**/vite.config.ts.timestamp-*",
       "!**/vite.server.config.ts.timestamp-*",
       "!**/vite.client.config.ts.timestamp-*",
+      "!**/*.grit",
       "!**/.turbo",
       "!**/.vscode",
       "!**/turbo.json",