eslint-plugin-mocha
Version:
Eslint rules for mocha.
58 lines • 2.29 kB
JavaScript
import { getStringIfConstant } from '@eslint-community/eslint-utils';
import { getParentNode, isCallExpression, isIdentifier, isMemberExpression } from "./node-types.js";
const dynamicMemberSymbol = Symbol('dynamic member access symbol');
function isConstantPathElement(element) {
return element !== dynamicMemberSymbol;
}
function isCallExpressionCallee(node) {
const parent = getParentNode(node);
return parent.type === 'CallExpression' && Object.is(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);
}
export function extractMemberExpressionPath(sourceCode, node) {
const path = [];
function appendPathSegment(currentNode) {
if (currentNode !== null) {
if (isMemberExpression(currentNode)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- bad typing in eslint core
appendPathSegment(currentNode.object);
const propertyName = extractPropertyName(currentNode, sourceCode);
path.push(formatName(propertyName, currentNode));
}
else if (isCallExpression(currentNode)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- bad typing in eslint core
appendPathSegment(currentNode.callee);
}
else if (isIdentifier(currentNode)) {
path.push(formatName(currentNode.name, currentNode));
}
}
}
appendPathSegment(node);
return path;
}
export function isConstantPath(path) {
return path.every(isConstantPathElement);
}
//# sourceMappingURL=member-expression.js.map