@masknet/eslint-plugin
Version:
eslint plugin for masknet
92 lines • 3.04 kB
JavaScript
export function closest(node, test) {
if (typeof test === 'string') {
const typeName = test;
test = (node) => node.type === typeName;
}
while (node) {
if (test(node))
return node;
node = node.parent;
}
return undefined;
}
export function isMultiline({ loc }) {
if (!loc)
return false;
return loc.start.line !== loc.end.line;
}
export function isIdentifier(node) {
return node?.type === 'Identifier' || node?.type === 'PrivateIdentifier';
}
export function isMemberExpression(node) {
return node?.type === 'MemberExpression';
}
export function isChainExpression(node) {
return node?.type === 'ChainExpression';
}
export function isCallExpression(node) {
return node?.type === 'CallExpression';
}
export function isBindCall(node) {
return isCallExpression(node) && isMemberExpression(node.callee) && isIdentifierName(node.callee.property, 'bind');
}
export function isLiteral(node) {
return node?.type === 'Literal';
}
export function isAwait(node) {
return node?.type === 'AwaitExpression';
}
export function isStringLiteral(node) {
return node?.type === 'Literal' && typeof node.value === 'string';
}
export function isNumberLiteral(node) {
return node?.type === 'Literal' && typeof node.value === 'number';
}
export function isBigIntLiteral(node) {
return node?.type === 'Literal' && Reflect.has(node, 'bigint');
}
export function isRegExpLiteral(node) {
return node?.type === 'Literal' && Reflect.has(node, 'regex');
}
export function isIdentifierName(node, name) {
if (!isIdentifier(node))
return false;
if (typeof name === 'function')
return name(node.name);
if (Array.isArray(name))
return name.includes(node.name);
return node.name === name;
}
export function isLiteralValue(node, value) {
if (node?.type !== 'Literal')
return false;
if (Array.isArray(value))
return value.includes(node.value);
if (typeof node.value === 'string' && value instanceof RegExp)
return value.test(node.value);
return node.value === value;
}
export function isFunctionLike(node) {
const ALLOWED_TYPES = ['ArrowFunctionExpression', 'FunctionDeclaration', 'FunctionExpression'];
return node ? ALLOWED_TYPES.includes(node.type) : false;
}
export function isIdentifierFunction(node) {
if (!isFunctionLike(node))
return false;
if (node.params.length !== 1)
return false;
if (node.params[0].type !== 'Identifier')
return false;
if (!node.body)
return false;
if (isSameIdentifier(node.params[0], node.body))
return true;
return (node.body.type === 'BlockStatement' &&
node.body.body.length === 1 &&
node.body.body[0].type === 'ReturnStatement' &&
isSameIdentifier(node.params[0], node.body.body[0].argument));
}
export function isSameIdentifier(a, b) {
return a?.type === 'Identifier' && b?.type === a.type && a.name === b.name;
}
//# sourceMappingURL=node.js.map