How to use the @lwc/shared.assert.invariant function in @lwc/shared

To help you get started, we’ve selected a few @lwc/shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github salesforce / lwc / packages / @lwc / node-reactions / src / api.ts View on Github external
function reactWhenDisconnectedRedirect(elm: Element, callback: ReactionCallback) {
    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(!isUndefined(elm), 'Missing required node param');
        // Eventually when the library can handle all types of Nodes, then this assetion will go away
        assert.invariant(elm instanceof Element, 'Expected to only register Elements');
        assert.invariant(isFunction(callback), 'Expected a callback function');
        assert.isFalse(
            isCustomElementsRegistryAvailable,
            'use node-reactions in browsers that do not support custom element registry'
        );
    }

    reactWhenDisconnected(elm, callback);
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / focus.ts View on Github external
export function handleFocusIn(elm: HTMLElement) {
    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(
            tabIndexGetter.call(elm) === -1,
            `Invalid attempt to handle focus in  ${toString(elm)}. ${toString(
                elm
            )} should have tabIndex -1, but has tabIndex ${tabIndexGetter.call(elm)}`
        );
    }

    bindDocumentMousedownMouseupHandlers(elm);

    // Unbind any focus listeners we may have going on
    ignoreFocus(elm);

    // This focusin listener is to catch focusin events from keyboard interactions
    // A better solution would perhaps be to listen for keydown events, but
    // the keydown event happens on whatever element already has focus (or no element
    // at all in the case of the location bar. So, instead we have to assume that focusin
github salesforce / lwc / packages / @lwc / engine / src / framework / base-lightning-element.ts View on Github external
const vmBeingRendered = getVMBeingRendered();
                assert.invariant(
                    !isInvokingRender,
                    `${vmBeingRendered}.render() method has side effects on the state of ${vm}.${propName}`
                );
                assert.invariant(
                    !isUpdatingTemplate,
                    `When updating the template of ${vmBeingRendered}, one of the accessors used by the template has side effects on the state of ${vm}.${propName}`
                );
                assert.isFalse(
                    isBeingConstructed(vm),
                    `Failed to construct '${getComponentTag(
                        vm
                    )}': The result must not have attributes.`
                );
                assert.invariant(
                    !isObject(newValue) || isNull(newValue),
                    `Invalid value "${newValue}" for "${propName}" of ${vm}. Value cannot be an object, must be a primitive value.`
                );
            }

            if (newValue !== vm.cmpProps[propName]) {
                vm.cmpProps[propName] = newValue;
                if (isFalse(vm.isDirty)) {
                    // perf optimization to skip this step if not in the DOM
                    valueMutated(this, propName);
                }
            }
            return set.call(vm.elm, newValue);
        },
    };
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / traverse.ts View on Github external
function isNodeSlotted(host: Element, node: Node): boolean {
    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(
            host instanceof HTMLElement,
            `isNodeSlotted() should be called with a host as the first argument instead of ${host}`
        );
        assert.invariant(
            node instanceof Node,
            `isNodeSlotted() should be called with a node as the second argument instead of ${node}`
        );
        assert.invariant(
            compareDocumentPosition.call(node, host) & DOCUMENT_POSITION_CONTAINS,
            `isNodeSlotted() should never be called with a node that is not a child node of ${host}`
        );
    }
    const hostKey = getNodeKey(host);
    // this routine assumes that the node is coming from a different shadow (it is not owned by the host)
    // just in case the provided node is not an element
    let currentElement = node instanceof Element ? node : parentElementGetter.call(node);
    while (!isNull(currentElement) && currentElement !== host) {
        const elmOwnerKey = getNodeNearestOwnerKey(currentElement);
        const parent = parentElementGetter.call(currentElement);
        if (elmOwnerKey === hostKey) {
            // we have reached an element inside the host's template, and only if
            // that element is an slot, then the node is considered slotted
            return isSlotElement(currentElement);
        } else if (parent === host) {
github salesforce / lwc / packages / @lwc / engine / src / framework / modules / props.ts View on Github external
function update(oldVnode: VNode, vnode: VNode) {
    const props = vnode.data.props;
    if (isUndefined(props)) {
        return;
    }
    const oldProps = oldVnode.data.props;
    if (oldProps === props) {
        return;
    }

    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(
            isUndefined(oldProps) || keys(oldProps).join(',') === keys(props).join(','),
            'vnode.data.props cannot change shape.'
        );
    }

    const elm = vnode.elm as Element;
    const isFirstPatch = isUndefined(oldProps);
    const { sel } = vnode;

    for (const key in props) {
        const cur: any = props[key];

        if (process.env.NODE_ENV !== 'production') {
            if (!(key in elm)) {
                // TODO [#1297]: Move this validation to the compiler
                assert.fail(
github salesforce / lwc / packages / @lwc / engine / src / framework / decorators / track.ts View on Github external
set(this: ComponentInterface, newValue: any) {
            const vm = getAssociatedVM(this);
            if (process.env.NODE_ENV !== 'production') {
                const vmBeingRendered = getVMBeingRendered();
                assert.invariant(
                    !isInvokingRender,
                    `${vmBeingRendered}.render() method has side effects on the state of ${vm}.${toString(
                        key
                    )}`
                );
                assert.invariant(
                    !isUpdatingTemplate,
                    `Updating the template of ${vmBeingRendered} has side effects on the state of ${vm}.${toString(
                        key
                    )}`
                );
            }
            const reactiveOrAnyValue = reactiveMembrane.getProxy(newValue);
            if (reactiveOrAnyValue !== vm.cmpTrack[key]) {
                vm.cmpTrack[key] = reactiveOrAnyValue;
                if (isFalse(vm.isDirty)) {
github salesforce / lwc / packages / @lwc / engine / src / framework / base-lightning-element.ts View on Github external
type: string,
        listener: EventListener,
        options?: boolean | AddEventListenerOptions
    ) {
        const vm = getAssociatedVM(this);
        if (process.env.NODE_ENV !== 'production') {
            const vmBeingRendered = getVMBeingRendered();
            assert.invariant(
                !isInvokingRender,
                `${vmBeingRendered}.render() method has side effects on the state of ${vm} by adding an event listener for "${type}".`
            );
            assert.invariant(
                !isUpdatingTemplate,
                `Updating the template of ${vmBeingRendered} has side effects on the state of ${vm} by adding an event listener for "${type}".`
            );
            assert.invariant(
                isFunction(listener),
                `Invalid second argument for this.addEventListener() in ${vm} for event "${type}". Expected an EventListener but received ${listener}.`
            );
        }
        const wrappedListener = getWrappedComponentsListener(vm, listener);
        vm.elm.addEventListener(type, wrappedListener, options);
    },
    removeEventListener(
github salesforce / lwc / packages / @lwc / engine / src / framework / component.ts View on Github external
export function renderComponent(vm: VM): VNodes {
    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(vm.isDirty, `${vm} is not dirty.`);
    }

    vm.tro.reset();
    const vnodes = invokeComponentRenderMethod(vm);
    vm.isDirty = false;
    vm.isScheduled = false;

    if (process.env.NODE_ENV !== 'production') {
        assert.invariant(
            isArray(vnodes),
            `${vm}.render() should always return an array of vnodes instead of ${vnodes}`
        );
    }
    return vnodes;
}
github salesforce / lwc / packages / @lwc / engine / src / framework / wiring.ts View on Github external
def: { wire },
    } = vm;
    if (getOwnPropertyNames(wire).length === 0) {
        if (process.env.NODE_ENV !== 'production') {
            assert.fail(
                `Internal Error: wire adapters should only be installed in instances with at least one wire declaration.`
            );
        }
    } else {
        const connect = (vm.context.wiredConnecting = []);
        const disconnect = (vm.context.wiredDisconnecting = []);
        for (const fieldNameOrMethod in wire) {
            const descriptor = wire[fieldNameOrMethod];
            const wireDef = WireMetaMap.get(descriptor);
            if (process.env.NODE_ENV !== 'production') {
                assert.invariant(wireDef, `Internal Error: invalid wire definition found.`);
            }
            if (!isUndefined(wireDef)) {
                const adapterInstance = createConnector(vm, fieldNameOrMethod, wireDef);
                ArrayPush.call(connect, () => adapterInstance.connect());
                ArrayPush.call(disconnect, () => adapterInstance.disconnect());
            }
        }
    }
}