import type * as t from '@babel/types';
export const GeneratedSource = Symbol();
export type SourceLocation =
| {
readonly loc: t.SourceLocation;
readonly start: t.Node['start'];
readonly end: t.Node['end'];
}
| typeof GeneratedSource;
export function getSourceLocation(
node: t.Node | null | undefined,
fallback: SourceLocation = GeneratedSource,
): SourceLocation {
if (node?.loc == null) {
return fallback;
}
return {loc: node.loc, start: node.start, end: node.end};
}
export function getBabelLocation(
source: SourceLocation | t.SourceLocation,
): t.SourceLocation | typeof GeneratedSource {
if (typeof source === 'symbol') {
return source;
}
return 'loc' in source ? source.loc : source;
}
export function areSourceLocationsEqual(
left: SourceLocation,
right: SourceLocation,
): boolean {
return (
left === right ||
(left !== GeneratedSource &&
right !== GeneratedSource &&
left.loc === right.loc)
);
}