-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
Query Execution
-
QE 2025-03-17
-
None
-
None
-
None
-
None
-
None
-
None
-
None
In SERVER-101533 we've added a new custom eslint rule to prohibit using tojson() within the print() function.
It detects well print(tojson(o)) and also print("hello: ", tojson(o)), but it fails to detect print("hello: " + tojson(o)), because in the latter case 'tojson()' is not an immediate child of the call expression.
I think we can easily cover the missing case by using ESLint selectors either in a new rule or in an additional configuration to the "no-restricted-syntax" rule. This rule allows declaring patterns against the AST and the following configuration will cover quite a few cases:
{ message: "Do not use 'tojson()' for logging debug information." + " Use 'toJsonForLog()' or 'attr' parameter of 'jsTest.log.debug()'.", selector: "CallExpression[callee.name=/(jsTestLog|print)/]" + " CallExpression[callee.name=/(tojson|tojsononeline)/]", }
The selector above matches the AST ancestor print() or jsTestLog() and the descendent tojson() or tojsononeline().
Matching jsTest.log("hello " + tojson()) requires a different selector, because "jsTest.log" is a MemberExpression and not just a simple Identifier. The same applies to to jsTest.log.info, jsTest.log.debug, etc.
The configuration most probably must initially be disabled because of many violations. Alternatively, it can be enabled on the list of "clean" files and folders which can grow over time.
- is related to
-
SERVER-101533 Add eslint rule that catches attempts of calling print(tojson(...)) in jstests
-
- Closed
-