UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

89 lines 3.97 kB
import { arePathStatesSame, clonePathState, createEntryState, updatePathState } from "./callback-handling-state.js"; import { enqueueNextSegments } from "./done-callback-paths.js"; function collectSegmentReportedNodes(context, entryState, segment, selectReportedNode) { const reportedNodes = []; let nextState = clonePathState(entryState); const operations = context.operationsBySegmentId.get(segment.id) ?? []; for (const operation of operations) { const reportedNode = selectReportedNode(context, nextState, operation); if (reportedNode !== undefined) { reportedNodes.push(reportedNode); } nextState = updatePathState(context.sourceCode, nextState, operation); } return [reportedNodes, nextState]; } function recordReportedNodes(reportedNodes, reportedNodeSet, segmentReportedNodes) { let nextReportedNodes = reportedNodes; segmentReportedNodes.forEach(function recordReportedNode(reportedNode) { if (!reportedNodeSet.has(reportedNode)) { reportedNodeSet.add(reportedNode); nextReportedNodes = [...nextReportedNodes, reportedNode]; } }); return nextReportedNodes; } function shouldRevisitSegment(collection, nextState, segment, segmentReportedNodes) { return segmentReportedNodes.length > 0 || !arePathStatesSame(collection.exitStatesBySegmentId.get(segment.id), nextState); } function processPendingSegment(context, collection, segment, selectReportedNode) { const entryState = createEntryState(context, segment, collection.exitStatesBySegmentId); const [segmentReportedNodes, nextState] = collectSegmentReportedNodes(context, entryState, segment, selectReportedNode); const reportedNodes = recordReportedNodes(collection.reportedNodes, collection.reportedNodeSet, segmentReportedNodes); if (shouldRevisitSegment(collection, nextState, segment, segmentReportedNodes)) { const [pendingSegments, queuedSegmentIds] = enqueueNextSegments(segment.nextSegments, collection.pendingSegments, collection.queuedSegmentIds); return { ...collection, exitStatesBySegmentId: new Map([ ...collection.exitStatesBySegmentId, [segment.id, nextState] ]), pendingSegments, queuedSegmentIds, reportedNodes }; } return { ...collection, reportedNodes }; } function createPendingSegmentCollection(context) { const queuedSegmentIds = new Set(); return { exitStatesBySegmentId: new Map(), pendingSegments: [context.codePath.initialSegment], queuedSegmentIds, reportedNodeSet: new WeakSet(), reportedNodes: [] }; } function dropPendingSegment(collection) { const [segment, ...pendingSegments] = collection.pendingSegments; return [ segment, { ...collection, pendingSegments, queuedSegmentIds: new Set(Array.from(collection.queuedSegmentIds).filter(function (segmentId) { return segmentId !== segment.id; })) } ]; } function hasPendingSegment(collection) { return collection.pendingSegments.length > 0; } function processPendingSegments(context, initialCollection, selectReportedNode) { let collection = initialCollection; while (hasPendingSegment(collection)) { const [segment, nextCollection] = dropPendingSegment(collection); collection = nextCollection; collection = processPendingSegment(context, collection, segment, selectReportedNode); } return collection; } export function collectCallbackHandlingNodes(context, selectReportedNode) { const collection = createPendingSegmentCollection(context); const processedCollection = processPendingSegments(context, collection, selectReportedNode); return processedCollection.reportedNodes; } //# sourceMappingURL=callback-handling-node-collector.js.map