@unito/integration-sdk
Version:
Integration SDK
92 lines (91 loc) • 3.05 kB
JavaScript
export const HTTP_ERRORS_NAMESPACE = 'HttpErrors';
export const LOGGER_NAME_BROAD_RE = /\b(logger|console|log)\b/i;
export const LOGGER_NAME_STRICT_RE = /\blogger\b/i;
/**
* Walk a (possibly chained) MemberExpression and return its identifier
* components.
*
* foo.bar.baz() → { root: 'foo', props: ['baz', 'bar'] }
* this.x.y → { root: null, props: ['y', 'x'] }
*/
export function walkMemberChain(node) {
const props = [];
let current = node;
while (current && current.type === 'MemberExpression') {
if (current.property && current.property.type === 'Identifier') {
props.push(current.property.name);
}
current = current.object;
}
const root = current && current.type === 'Identifier' ? current.name : null;
return { root, props };
}
/**
* Walks parents from `node` until either:
* - finds a CatchClause (returns true)
* - crosses a function boundary (returns false)
*
* Use to scope rules to lexically-enclosed catch blocks; helpers called from a
* catch via a separate function are intentionally NOT matched.
*/
export function isInsideCatchClause(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'CatchClause') {
return true;
}
if (parent.type === 'FunctionDeclaration' ||
parent.type === 'FunctionExpression' ||
parent.type === 'ArrowFunctionExpression') {
return false;
}
parent = parent.parent;
}
return false;
}
/**
* Like isInsideCatchClause but returns the parameter name of the enclosing
* CatchClause. Returns null if the node is not inside a catch, or if the catch
* uses destructuring / has no parameter binding.
*/
export function getCatchClauseParam(node) {
let parent = node.parent;
while (parent) {
if (parent.type === 'CatchClause') {
const catchClause = parent;
if (catchClause.param && catchClause.param.type === 'Identifier') {
return catchClause.param.name;
}
return null;
}
if (parent.type === 'FunctionDeclaration' ||
parent.type === 'FunctionExpression' ||
parent.type === 'ArrowFunctionExpression') {
return null;
}
parent = parent.parent;
}
return null;
}
/**
* Returns true if `callee` is a `HttpErrors.<X>` MemberExpression. If `subClass`
* is provided, additionally requires the property name to match.
*/
export function isHttpErrorsCallee(callee, subClass) {
if (!callee || callee.type !== 'MemberExpression') {
return false;
}
if (!callee.object || callee.object.type !== 'Identifier') {
return false;
}
if (callee.object.name !== HTTP_ERRORS_NAMESPACE) {
return false;
}
if (subClass === undefined) {
return true;
}
if (!callee.property || callee.property.type !== 'Identifier') {
return false;
}
return callee.property.name === subClass;
}