Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support: Restrict the 'populate' method in model modules #7689

Merged
merged 9 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"lint:swagger2openapi": "node node_modules/.bin/oas-validate tmp/swagger.json",
"lint": "run-p lint:*",
"prelint:swagger2openapi": "yarn openapi:v3",
"test": "cross-env NODE_ENV=test NODE_OPTIONS=\"--max-old-space-size=4096\" jest --logHeapUsage",
"test": "run-p test:*",
"test:jest": "cross-env NODE_ENV=test NODE_OPTIONS=\"--max-old-space-size=4096\" jest --logHeapUsage",
"test:vitest": "cross-env NODE_ENV=test vitest run src",
"jest:run": "cross-env NODE_ENV=test jest --passWithNoTests -- ",
"reg:run": "reg-suit run",
"//// misc": "",
Expand Down
12 changes: 12 additions & 0 deletions apps/app/src/server/models/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const rulesDirPlugin = require('eslint-plugin-rulesdir');

rulesDirPlugin.RULES_DIR = 'src/server/models/eslint-rules-dir';

module.exports = {
plugins: [
'rulesdir',
],
rules: {
'rulesdir/no-populate': 'warn',
},
};
27 changes: 27 additions & 0 deletions apps/app/src/server/models/eslint-rules-dir/no-populate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @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.",
});
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { RuleTester } from 'eslint';
import { test } from 'vitest';

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." }],
},
],
});
});
7 changes: 7 additions & 0 deletions apps/app/vitest.config.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineProject } from 'vitest/config';

export default defineProject({
test: {
environment: 'node',
},
});
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,25 @@
"@swc/jest": "^0.2.24",
"@testing-library/cypress": "^8.0.2",
"@types/css-modules": "^1.0.2",
"@types/eslint": "^8.37.0",
"@types/estree": "^1.0.1",
"@types/jest": "^26.0.22",
"@types/node": "^17.0.43",
"@types/rewire": "^2.5.28",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
"@vitejs/plugin-react": "^3.1.0",
"cypress": "^12.0.1",
"cypress-wait-until": "^1.7.2",
"eslint": "^8.35.0",
"eslint": "^8.41.0",
"eslint-config-next": "^12.1.6",
"eslint-config-weseek": "^2.1.1",
"eslint-import-resolver-typescript": "^3.2.5",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-rulesdir": "^0.2.2",
"glob": "^8.1.0",
"jest": "^28.1.3",
"jest-date-mock": "^1.0.8",
Expand All @@ -95,7 +98,8 @@
"typescript": "~4.9",
"unplugin-swc": "^1.3.2",
"vite": "^4.2.2",
"vite-plugin-dts": "^2.0.0-beta.0"
"vite-plugin-dts": "^2.0.0-beta.0",
"vitest": "^0.31.1"
},
"engines": {
"node": "^16 || ^18",
Expand Down
4 changes: 4 additions & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default [
'apps/*/vitest.config.{e2e,unit}.ts',
'packages/*/vitest.config.{e2e,unit}.ts',
];
Loading