Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log shadow mode through engine reporting API #3878

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/@lwc/engine-core/src/framework/reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
*/
import { noop } from '@lwc/shared';

import { ShadowMode } from './vm';

export const enum ReportingEventId {
CrossRootAriaInSyntheticShadow = 'CrossRootAriaInSyntheticShadow',
CompilerRuntimeVersionMismatch = 'CompilerRuntimeVersionMismatch',
NonStandardAriaReflection = 'NonStandardAriaReflection',
TemplateMutation = 'TemplateMutation',
StylesheetMutation = 'StylesheetMutation',
ConnectedCallbackWhileDisconnected = 'ConnectedCallbackWhileDisconnected',
ShadowModeUsage = 'ShadowModeUsage',
}

export interface BasePayload {
Expand Down Expand Up @@ -44,13 +47,18 @@ export interface StylesheetMutationPayload extends BasePayload {

export interface ConnectedCallbackWhileDisconnectedPayload extends BasePayload {}

export interface ShadowModeUsagePayload extends BasePayload {
mode: ShadowMode;
}

export type ReportingPayloadMapping = {
[ReportingEventId.CrossRootAriaInSyntheticShadow]: CrossRootAriaInSyntheticShadowPayload;
[ReportingEventId.CompilerRuntimeVersionMismatch]: CompilerRuntimeVersionMismatchPayload;
[ReportingEventId.NonStandardAriaReflection]: NonStandardAriaReflectionPayload;
[ReportingEventId.TemplateMutation]: TemplateMutationPayload;
[ReportingEventId.StylesheetMutation]: StylesheetMutationPayload;
[ReportingEventId.ConnectedCallbackWhileDisconnected]: ConnectedCallbackWhileDisconnectedPayload;
[ReportingEventId.ShadowModeUsage]: ShadowModeUsagePayload;
};

export type ReportingDispatcher<T extends ReportingEventId = ReportingEventId> = (
Expand Down
8 changes: 8 additions & 0 deletions packages/@lwc/engine-core/src/framework/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ export function createVM<HostNode, HostElement>(
vm.shadowMode = computeShadowMode(def, vm.owner, renderer);
vm.tro = getTemplateReactiveObserver(vm);

// We don't need to report the shadow mode if we're rendering in light DOM
if (isReportingEnabled() && vm.renderMode === RenderMode.Shadow) {
report(ReportingEventId.ShadowModeUsage, {
tagName: vm.tagName,
mode: vm.shadowMode,
});
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked, and this gets tree-shaken if the customer is not using the reporting API! 🎉

if (process.env.NODE_ENV !== 'production') {
vm.toString = (): string => {
return `[object:vm ${def.name} (${vm.idx})]`;
Expand Down
19 changes: 19 additions & 0 deletions packages/@lwc/integration-karma/helpers/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ window.TestUtils = (function (lwc, jasmine, beforeAll) {
jasmine.addMatchers(customMatchers);
});

/**
*
* @param {jasmine.Spy} dispatcher
* @param {String[]} runtimeEvents List of runtime events to filter by. If no list is provided, all events will be dispatched.
*/
function attachReportingControlDispatcher(dispatcher, runtimeEvents) {
lwc.__unstable__ReportingControl.attachDispatcher((eventName, payload) => {
if (!runtimeEvents || runtimeEvents.includes(eventName)) {
dispatcher(eventName, payload);
}
});
}

function detachReportingControlDispatcher() {
lwc.__unstable__ReportingControl.detachDispatcher();
}

function extractDataIds(root) {
var nodes = {};

Expand Down Expand Up @@ -566,5 +583,7 @@ window.TestUtils = (function (lwc, jasmine, beforeAll) {
nonStandardAriaProperties: nonStandardAriaProperties,
nonPolyfilledAriaProperties: nonPolyfilledAriaProperties,
getPropertyDescriptor: getPropertyDescriptor,
attachReportingControlDispatcher: attachReportingControlDispatcher,
detachReportingControlDispatcher: detachReportingControlDispatcher,
};
})(LWC, jasmine, beforeAll);
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createElement, __unstable__ReportingControl as reportingControl } from 'lwc';
import { nonStandardAriaProperties } from 'test-utils';
import { createElement } from 'lwc';
import {
attachReportingControlDispatcher,
detachReportingControlDispatcher,
nonStandardAriaProperties,
} from 'test-utils';
import Light from 'x/light';
import Shadow from 'x/shadow';

Expand All @@ -10,11 +14,11 @@ if (process.env.ENABLE_ARIA_REFLECTION_GLOBAL_POLYFILL) {

beforeEach(() => {
dispatcher = jasmine.createSpy();
reportingControl.attachDispatcher(dispatcher);
attachReportingControlDispatcher(dispatcher, ['NonStandardAriaReflection']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!

});

afterEach(() => {
reportingControl.detachDispatcher();
detachReportingControlDispatcher();
});

nonStandardAriaProperties.forEach((prop) => {
Expand Down Expand Up @@ -61,7 +65,6 @@ if (process.env.ENABLE_ARIA_REFLECTION_GLOBAL_POLYFILL) {
elm.getPropOnElement(prop);
}).toLogWarningDev(inComponentWarning);

expect(dispatcher).toHaveBeenCalledTimes(2);
expect(dispatcher.calls.allArgs()).toEqual([
[
'NonStandardAriaReflection',
Expand Down Expand Up @@ -93,7 +96,6 @@ if (process.env.ENABLE_ARIA_REFLECTION_GLOBAL_POLYFILL) {
return unused; // remove lint warning
}).toLogWarningDev(outsideComponentWarning);

expect(dispatcher).toHaveBeenCalledTimes(2);
expect(dispatcher.calls.allArgs()).toEqual([
[
'NonStandardAriaReflection',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createElement, __unstable__ReportingControl as reportingControl } from 'lwc';
import { createElement } from 'lwc';
import { attachReportingControlDispatcher, detachReportingControlDispatcher } from 'test-utils';

import AriaContainer from 'x/ariaContainer';
import Valid from 'x/valid';

Expand All @@ -18,11 +20,14 @@ if (!process.env.NATIVE_SHADOW) {

beforeEach(() => {
dispatcher = jasmine.createSpy();
reportingControl.attachDispatcher(dispatcher);
attachReportingControlDispatcher(dispatcher, [
'CrossRootAriaInSyntheticShadow',
'NonStandardAriaReflection',
]);
});

afterEach(() => {
reportingControl.detachDispatcher();
detachReportingControlDispatcher();
});

describe('detection', () => {
Expand Down Expand Up @@ -150,44 +155,45 @@ if (!process.env.NATIVE_SHADOW) {
expectWarningForNonStandardPropertyAccess(() => {
sourceElm.setAriaLabelledBy(value);
});
expect(dispatcher.calls.allArgs()).toEqual([
...(usePropertyAccess
? [
[
'NonStandardAriaReflection',
{
tagName: 'x-aria-source',
propertyName: 'ariaLabelledBy',
isSetter: true,
setValueType:
value === null ? 'null' : typeof value,
},
],
]
: []),
]);

if (usePropertyAccess) {
expect(dispatcher.calls.allArgs()).toEqual([
[
'NonStandardAriaReflection',
{
tagName: 'x-aria-source',
propertyName: 'ariaLabelledBy',
isSetter: true,
setValueType: value === null ? 'null' : typeof value,
},
],
]);
} else {
expect(dispatcher).not.toHaveBeenCalled();
}
});
});

it('ignores id that references nonexistent element', () => {
expectWarningForNonStandardPropertyAccess(() => {
sourceElm.setAriaLabelledBy('does-not-exist-at-all-lol');
});
expect(dispatcher.calls.allArgs()).toEqual([
...(usePropertyAccess
? [
[
'NonStandardAriaReflection',
{
tagName: 'x-aria-source',
propertyName: 'ariaLabelledBy',
isSetter: true,
setValueType: 'string',
},
],
]
: []),
]);

if (usePropertyAccess) {
expect(dispatcher.calls.allArgs()).toEqual([
[
'NonStandardAriaReflection',
{
tagName: 'x-aria-source',
propertyName: 'ariaLabelledBy',
isSetter: true,
setValueType: 'string',
},
],
]);
} else {
expect(dispatcher).not.toHaveBeenCalled();
}
});

[
Expand Down Expand Up @@ -215,6 +221,7 @@ if (!process.env.NATIVE_SHADOW) {
...expectedMessages,
expectedMessageForCrossRootForSecondTarget,
]);

expect(dispatcher.calls.allArgs()).toEqual([
...getExpectedDispatcherCalls(true),
[
Expand Down Expand Up @@ -269,6 +276,7 @@ if (!process.env.NATIVE_SHADOW) {
const valid = createElement('x-valid', { is: Valid });
document.body.appendChild(valid);
valid.linkElements({ reverseOrder });

expect(dispatcher).not.toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import {
registerTemplate,
freezeTemplate,
setFeatureFlagForTest,
__unstable__ReportingControl as reportingControl,
} from 'lwc';
import { registerTemplate, freezeTemplate, setFeatureFlagForTest } from 'lwc';

import { attachReportingControlDispatcher, detachReportingControlDispatcher } from 'test-utils';

describe('freezeTemplate', () => {
let dispatcher;

beforeEach(() => {
dispatcher = jasmine.createSpy();
reportingControl.attachDispatcher(dispatcher);
attachReportingControlDispatcher(dispatcher, ['StylesheetMutation', 'TemplateMutation']);
});

afterEach(() => {
reportingControl.detachDispatcher();
detachReportingControlDispatcher();
});

it('should warn when setting tmpl.stylesheetToken', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { __unstable__ReportingControl as reportingControl, createElement } from 'lwc';
import { createElement } from 'lwc';
import { attachReportingControlDispatcher, detachReportingControlDispatcher } from 'test-utils';

import Component from 'x/component';
import Parent from 'x/parent';

Expand All @@ -9,12 +11,12 @@ if (!window.lwcRuntimeFlags.ENABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE) {

beforeEach(() => {
dispatcher = jasmine.createSpy();
reportingControl.attachDispatcher(dispatcher);
attachReportingControlDispatcher(dispatcher, ['ConnectedCallbackWhileDisconnected']);
logger = spyOn(console, 'warn');
});

afterEach(() => {
reportingControl.detachDispatcher();
detachReportingControlDispatcher();
});

function expectLogs(regexes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { createElement } from 'lwc';
import { attachReportingControlDispatcher, detachReportingControlDispatcher } from 'test-utils';

import Component from 'x/component';
import Parent from 'x/parent';
import Light from 'x/light';

// Should be kept in sync with the enum in vm.ts
const ShadowMode = {
Native: 0,
Synthetic: 1,
};

describe('ShadowModeUsage', () => {
let dispatcher;

beforeEach(() => {
dispatcher = jasmine.createSpy();
attachReportingControlDispatcher(dispatcher, ['ShadowModeUsage']);
});

afterEach(() => {
detachReportingControlDispatcher();
});

it('should report the shadow mode for the rendered component', () => {
const element = createElement('x-component', { is: Component });
document.body.appendChild(element);

expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-component',
mode: process.env.NATIVE_SHADOW ? ShadowMode.Native : ShadowMode.Synthetic,
});
});

it('should report the shadow mode for all rendered components', () => {
const element = createElement('x-parent', { is: Parent });
document.body.appendChild(element);

expect(dispatcher).toHaveBeenCalledTimes(3);
// x-parent depends on environment
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-parent',
mode: process.env.NATIVE_SHADOW ? ShadowMode.Native : ShadowMode.Synthetic,
});
// x-native should be set to native always
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-native',
mode: ShadowMode.Native,
});
// x-component depends on environment
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-component',
mode: process.env.NATIVE_SHADOW ? ShadowMode.Native : ShadowMode.Synthetic,
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a test here for CustomElementConstructor since it has its own slightly different code path (doesn't use lwc.createElement()).

It would also make sense to add a test for a light-DOM component, to ensure that it reports nothing.


it('should report the shadow mode for components when created using CustomElementConstructor', () => {
const ParentCustomElement = Parent.CustomElementConstructor;
customElements.define('x-parent-custom-element', ParentCustomElement);

const element = document.createElement('x-parent-custom-element');
document.body.appendChild(element);

expect(dispatcher).toHaveBeenCalledTimes(3);
// x-parent depends on environment
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'X-PARENT-CUSTOM-ELEMENT',
mode: process.env.NATIVE_SHADOW ? ShadowMode.Native : ShadowMode.Synthetic,
});
// x-native should be set to native always
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-native',
mode: ShadowMode.Native,
});
// x-component depends on environment
expect(dispatcher).toHaveBeenCalledWith('ShadowModeUsage', {
tagName: 'x-component',
mode: process.env.NATIVE_SHADOW ? ShadowMode.Native : ShadowMode.Synthetic,
});
});

it('should report no shadow mode for light DOM components', () => {
const element = createElement('x-light', { is: Light });
document.body.appendChild(element);

expect(dispatcher).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>default component</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template lwc:render-mode="light">
<p>Hello, Light DOM</p>
</template>
Loading