UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

67 lines 2.58 kB
/* eslint-disable no-warning-comments -- needed for c8 ignore comments */ import { getStringIfConstant } from '@eslint-community/eslint-utils'; import { isCallExpression, isIdentifier, isMemberExpression } from './node-types.js'; const dynamicMemberSymbol = Symbol('dynamic member access symbol'); function isConstantPathElement(element) { return element !== dynamicMemberSymbol; } function isCallExpressionCallee(node) { return node.parent.type === 'CallExpression' && node.parent.callee === node; } function formatName(name, node) { if (isConstantPathElement(name)) { if (isCallExpressionCallee(node)) { return `${name}()`; } return name; } return name; } export function getIdentifierName(node) { if (isIdentifier(node)) { return node.name; } return dynamicMemberSymbol; } function extractPropertyName(memberExpression, sourceCode) { if (memberExpression.computed) { const constantName = getStringIfConstant(memberExpression.property, sourceCode.getScope(memberExpression)); return constantName ?? dynamicMemberSymbol; } return getIdentifierName(memberExpression.property); } // eslint-disable-next-line max-statements -- no good idea how to split this up export function extractMemberExpressionPath(sourceCode, node) { const path = []; let currentNode = node; while (currentNode !== null) { if (isMemberExpression(currentNode)) { const propertyName = extractPropertyName(currentNode, sourceCode); const formattedProperty = formatName(propertyName, currentNode); path.unshift(formattedProperty); // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- bad typing in eslint core currentNode = currentNode.object; } else if (isCallExpression(currentNode)) { // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- bad typing in eslint core currentNode = currentNode.callee; } else if (isIdentifier(currentNode)) { const formattedName = formatName(currentNode.name, currentNode); path.unshift(formattedName); return path; /* c8 ignore next */ } else { /* c8 ignore next */ return path; /* c8 ignore next */ } } /* c8 ignore next */ return path; } export function isConstantPath(path) { return path.every(isConstantPathElement); } //# sourceMappingURL=member-expression.js.map