Skip to content

Commit

Permalink
Removes node.object.type === 'CallExpression' as it was ignoring inva…
Browse files Browse the repository at this point in the history
…lid function called from variables instead of in the chain
  • Loading branch information
filipenevola committed Dec 15, 2023
1 parent 192e31f commit 190808e
Showing 1 changed file with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -53,7 +73,7 @@ module.exports = {
},
fixable: 'code',
},
create: context => {
create: (context) => {
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
Expand All @@ -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
Expand All @@ -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 (
Expand All @@ -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];
Expand All @@ -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,
Expand Down

0 comments on commit 190808e

Please sign in to comment.