element-vir
Version:
Heroic. Reactive. Declarative. Type safe. Web components without compromise.
38 lines (37 loc) • 1.29 kB
JavaScript
import { PartType, } from '../../lit-exports/all-lit-exports.js';
/**
* Extracts the element from the given part info. Used in lit directives.
*
* @category Internal
*/
export function extractElement(partInfo, directiveName) {
assertIsElementPartInfo(partInfo, directiveName);
const element = partInfo.element;
return element;
}
function getPartHostTagName(partInfo) {
try {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const tagName = partInfo.options.host.tagName.toLowerCase();
return tagName;
}
catch {
return undefined;
}
}
/**
* Asserts that the given part info is an instance of {@link FullElementPartInfo}.
*
* @category Internal
*/
export function assertIsElementPartInfo(partInfo, directiveName) {
const hostTagName = getPartHostTagName(partInfo);
const hostTagMessage = hostTagName ? `: in ${hostTagName}` : '';
if (partInfo.type !== PartType.ELEMENT) {
throw new Error(`${directiveName} directive can only be attached directly to an element${hostTagMessage}.`);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!partInfo.element) {
throw new Error(`${directiveName} directive found no element${hostTagMessage}.`);
}
}