SERVER-99572: Cover jstests with custom eslint rules (#32277)

Co-authored-by: Serhii Lysenko <serhiilysenko@mongodb.com>
Co-authored-by: Zack Winter <zack.winter@mongodb.com>
Co-authored-by: romanskas <30618745+romanskas@users.noreply.github.com>
GitOrigin-RevId: 7f9fb5e323eb4c87de26ed6756f44b6cdec48305
This commit is contained in:
Serhii Lysenko 2025-02-18 12:40:55 +00:00 committed by MongoDB Bot
parent 2f2816982b
commit 55f2df1754
9 changed files with 424 additions and 266 deletions

View File

@ -1,4 +1,5 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
load("//bazel/install_rules:install_rules.bzl", "mongo_install")
load("//bazel/toolchains:mongo_toolchain.bzl", "setup_mongo_toolchain_aliases")
load("//bazel/config:render_template.bzl", "render_template")
@ -19,13 +20,19 @@ exports_files([
npm_link_all_packages(name = "node_modules")
npm_link_package(
name = "node_modules/eslint-plugin-mongodb",
src = "//buildscripts/eslint-plugin-mongodb:npm_package",
)
js_library(
name = "eslintrc",
srcs = ["eslint.config.mjs"],
deps = [
":node_modules/@eslint/eslintrc",
":node_modules/@eslint/js",
":node_modules/eslint-plugin-mongodb",
":node_modules/globals",
"//:node_modules/@eslint/eslintrc",
],
)

View File

@ -0,0 +1,13 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
npm_package(
name = "npm_package",
srcs = [
"package.json",
"plugin.js",
"rules/no-print-fn.js",
"rules/no-tojson-fn.js",
],
package = "eslint-plugin-mongodb",
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,7 @@
{
"name": "eslint-plugin-mongodb",
"version": "1.0.0",
"private": true,
"main": "plugin.js",
"type": "module"
}

View File

@ -0,0 +1,11 @@
// plugin.js
import {default as no_print} from "./rules/no-print-fn.js";
import {default as no_tojson} from "./rules/no-tojson-fn.js";
export default {
rules: {
"no-print-fn": no_print,
"no-tojson-fn": no_tojson,
},
};

View File

@ -0,0 +1,35 @@
const stopList = [
"print",
"printjson",
"printjsononeline",
];
export default {
meta: {
type: "problem",
docs: {
description: "Ensure no direct calls to print* functions",
},
fixable: "code",
},
create(context) {
return {
CallExpression: function(node) {
if (node.callee.type == "Identifier" &&
stopList.some(fn => fn == node.callee.name)) {
context.report(
{
node,
message: `Direct use of '${
node.callee
.name}()'. Consider using jsTest.log.info() instead or disable mongodb/no-print-fn rule when necessary, e.g., '// eslint-disable-next-line mongodb/no-print-fn'
More about rules configuration: https://eslint.org/docs/latest/use/configure/rules`,
});
}
}
};
}
};

View File

@ -0,0 +1,34 @@
const stopList = [
"tojson",
"tojsononeline",
];
export default {
meta: {
type: "problem",
docs: {
description: "Ensure no direct calls to tojson* functions",
},
fixable: "code",
},
create(context) {
return {
CallExpression: function(node) {
if (node.callee.type == "Identifier" &&
stopList.some(fn => fn == node.callee.name)) {
context.report(
{
node,
message: `Direct use of '${
node.callee
.name}()'. Consider using jsTest.log.info() instead or disable mongodb/no-tojson-fn rule when necessary, e.g., '// eslint-disable-next-line mongodb/no-print-fn'
More about rules configuration: https://eslint.org/docs/latest/use/configure/rules`,
});
}
}
};
}
};

View File

@ -1,9 +1,10 @@
import globals from "globals";
import path from "node:path";
import { fileURLToPath } from "node:url";
import {FlatCompat} from "@eslint/eslintrc";
import eslint from "@eslint/js";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
import {default as mongodb_plugin} from "eslint-plugin-mongodb";
import globals from "globals";
import path from "node:path";
import {fileURLToPath} from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@ -13,9 +14,13 @@ const compat = new FlatCompat({
allConfig: js.configs.all
});
export default [{
export default [
...compat
.extends("eslint:recommended"),
{
ignores: ["src/mongo/gotools/*", "**/*.tpl.js", "jstests/third_party/**/*.js"],
}, ...compat.extends("eslint:recommended"), {
},
{
languageOptions: {
globals: {
...globals.mongo,
@ -240,16 +245,27 @@ export default [{
sourceType: "module",
},
plugins: {
"mongodb": mongodb_plugin,
},
rules: {
// TODO SERVER-99571 : enable mongodb/* rules.
"mongodb/no-print-fn": 1,
"mongodb/no-tojson-fn": 1,
"no-prototype-builtins": 0,
"no-useless-escape": 0,
"no-irregular-whitespace": 0,
"no-inner-declarations": 0,
"no-unused-vars": [0, {
"no-unused-vars": [
0,
{
varsIgnorePattern: "^_",
args: "none",
}],
}
],
"no-empty": 0,
"no-redeclare": 0,
@ -257,9 +273,47 @@ export default [{
"no-loss-of-precision": 0,
semi: 2,
"no-restricted-syntax": ["error", {
message: "Invalid load call. Please convert your library to a module and import it instead.",
"no-restricted-syntax": [
"error",
{
message:
"Invalid load call. Please convert your library to a module and import it instead.",
selector: "CallExpression > Identifier[name=\"load\"]",
}],
}
],
},
}];
},
{
// It's ok for golden tests to use print() and tojson() directly.
plugins: {
"mongodb": mongodb_plugin,
},
files: [
"jstests/libs/begin_golden_test.js",
"jstests/libs/golden_test.js",
"jstests/libs/override_methods/golden_overrides.js",
"jstests/libs/override_methods/sharded_golden_overrides.js",
"jstests/libs/query/golden_test_utils.js",
"jstests/libs/query_golden_sharding_utils.js",
"jstests/query_golden/**/*.js",
"jstests/query_golden_sharding/**/*.js",
],
rules: {
"mongodb/no-print-fn": 0,
"mongodb/no-tojson-fn": 0,
}
},
{
// Don't run mongodb linter rules on src/
plugins: {
"mongodb": mongodb_plugin,
},
files: [
"src/**/*.js",
],
rules: {
"mongodb/no-print-fn": 0,
"mongodb/no-tojson-fn": 0,
}
}
];

View File

@ -3,11 +3,11 @@
"version": "1.0.0",
"private": true,
"devDependencies": {
"@eslint/js": "^9",
"@eslint/eslintrc": "3.2.0",
"@eslint/js": "^9",
"eslint": "9.19.0",
"globals": "14.0.0",
"eslint-formatter-unix": "^8.40.0",
"globals": "14.0.0",
"prettier": "3.4.2"
},
"pnpm": {

View File

@ -6,10 +6,7 @@ settings:
onlyBuiltDependencies: []
importers:
.:
devDependencies:
devDependencies:
'@eslint/eslintrc':
specifier: 3.2.0
version: 3.2.0