diff --git a/npm-packages/eslint-plugin-meteor/lib/rules/no-sync-mongo-methods-on-server/no-sync-mongo-methods-on-server.js b/npm-packages/eslint-plugin-meteor/lib/rules/no-sync-mongo-methods-on-server/no-sync-mongo-methods-on-server.js index fc5f5545f99..e65801ec7d9 100644 --- a/npm-packages/eslint-plugin-meteor/lib/rules/no-sync-mongo-methods-on-server/no-sync-mongo-methods-on-server.js +++ b/npm-packages/eslint-plugin-meteor/lib/rules/no-sync-mongo-methods-on-server/no-sync-mongo-methods-on-server.js @@ -19,22 +19,42 @@ const INVALID_FUNCTIONS = { suggestion: 'createIndexAsync', isCollection: true, skipForRawCollection: true, - debug: true, }, fetch: { suggestion: 'fetchAsync', isCollection: true }, count: { suggestion: 'countAsync', isCursor: true }, - map: { suggestion: 'mapAsync', isCursor: true }, + map: { suggestion: 'mapAsync', isCursor: true, debug: true }, forEach: { suggestion: 'forEachAsync', isCursor: true }, // TODO we can go to the parent to check if it's also a call expression from a find function }; const INVALID_FUNCTIONS_NAMES = Object.keys(INVALID_FUNCTIONS); +function wasCreatedBySpecificFunction({ node, functionName }) { + // Check if the node is an assignment expression + if (node.type !== 'AssignmentExpression') { + return false; + } + + // Check if the right-hand side of the assignment is a call expression + const right = node.right; + if (right.type !== 'CallExpression') { + return false; + } + + // Check if the call expression is a member expression (e.g., MembersCollection.find) + const callee = right.callee; + if (callee.type !== 'MemberExpression') { + return false; + } + + // Check if the property of the member expression matches the function name + return callee.property.name === functionName; +} -function hasRawCollectionInTheChain(node) { +function hasSpecificFunctionInTheChain({ node, functionName }) { const previousFunction = node.object.callee; if (!previousFunction || previousFunction.type !== 'MemberExpression') { return false; } - return previousFunction.property.name === 'rawCollection'; + return previousFunction.property.name === functionName; } function getInitFolder(context) { @@ -53,7 +73,7 @@ module.exports = { }, fixable: 'code', }, - create: context => { + create: (context) => { // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- @@ -80,7 +100,7 @@ module.exports = { // --------------------------------------------------------------------------- return { - Program: function() { + Program: function () { // if NYC_PROCESS_ID is present it means we are running tests const isTest = !!process.env.NYC_PROCESS_ID; // TODO support multiple directories https://quave.slack.com/archives/C0606SXCXFW/p1702639670046879?thread_ts=1702637224.400439&cid=C0606SXCXFW @@ -92,7 +112,7 @@ module.exports = { }, }); }, - MemberExpression: function(node) { + MemberExpression: function (node) { const walker = new Walker(getInitFolder(context)); const realPath = fs.realpathSync.native(context.physicalFilename); if ( @@ -105,11 +125,10 @@ module.exports = { // CallExpression means it's a function call so we don't throw an error for example for a property called count in an object but we do throw when it's a count() function call. if ( node.property && - node.property.type === 'Identifier' && - node.object.type === 'CallExpression' + node.property.type === 'Identifier' ) { const invalidFunction = INVALID_FUNCTIONS_NAMES.find( - ifn => ifn === node.property.name + (ifn) => ifn === node.property.name ); const invalidFunctionDefinition = invalidFunction && INVALID_FUNCTIONS[invalidFunction]; @@ -119,13 +138,34 @@ module.exports = { } if ( invalidFunctionDefinition.skipForRawCollection && - hasRawCollectionInTheChain(node) + hasSpecificFunctionInTheChain({ + node, + functionName: 'rawCollection', + }) ) { debug( `Skipping ${invalidFunction} to be considered error because it was used after rawCollection()` ); return; } + // if (invalidFunctionDefinition.isCursor) { + // const isCursorChain = hasSpecificFunctionInTheChain({ + // node, + // functionName: 'find', + // }); + // const wasCreatedByFind = wasCreatedBySpecificFunction({ + // node, + // functionName: 'find', + // }); + // + // if (!isCursorChain && !wasCreatedByFind) { + // debug( + // `Skipping ${invalidFunction} to be considered error because it was used not in a cursor`, + // { isCursor: isCursorChain, wasCreatedByFind } + // ); + // return; + // } + // } createError({ context, node,