Skip to content

Commit

Permalink
Remove unused type declarations and remove LitExtendedWindow and `L…
Browse files Browse the repository at this point in the history
…itExtraGlobals`. (#2041)

* Remove unused type declarations.

* Revert "Cast `window` to a new type (instead of extending it) to prevent collisions. (#1904)"

This reverts commit ec5ab1b, with resolved conflicts.

* Remove unused, duplicate declarations for types added by other packages in the repo.

* Add a changeset.
  • Loading branch information
bicknellr committed Aug 18, 2021
1 parent 5768cc6 commit 52a47c7
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 168 deletions.
7 changes: 7 additions & 0 deletions .changeset/four-pianos-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'lit-element': patch
'lit-html': patch
'@lit/reactive-element': patch
---

Remove some unnecessary internal type declarations.
21 changes: 1 addition & 20 deletions packages/lit-element/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

interface LitExtendedWindow extends Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reactiveElementPlatformSupport: (options: {[index: string]: any}) => void;
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litElementPlatformSupport: (options: {[index: string]: any}) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litHtmlPlatformSupport: (template: unknown, childPart: unknown) => void;
}

type LitExtraGlobals = typeof globalThis & LitExtendedWindow;

// Augment existing types with styling API
interface ShadowRoot {
adoptedStyleSheets: CSSStyleSheet[];
}

// eslint-disable-next-line no-var
declare var ShadowRoot: {prototype: ShadowRoot; new (): ShadowRoot};

interface CSSStyleSheet {
replaceSync(cssText: string): void;
replace(cssText: string): Promise<unknown>;
}
6 changes: 2 additions & 4 deletions packages/lit-element/src/polyfill-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ interface PatchableLitElement extends HTMLElement {
}: {
LitElement: PatchableLitElement;
}) => {
const extraGlobals = window as LitExtraGlobals;

// polyfill-support is only needed if ShadyCSS or the ApplyShim is in use
// We test at the point of patching, which makes it safe to load
// webcomponentsjs and polyfill-support in either order
if (
extraGlobals.ShadyCSS === undefined ||
(extraGlobals.ShadyCSS.nativeShadow && !extraGlobals.ShadyCSS.ApplyShim)
window.ShadyCSS === undefined ||
(window.ShadyCSS.nativeShadow && !window.ShadyCSS.ApplyShim)
) {
return;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/lit-element/src/test/lit-element_styling_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
} from './test-helpers.js';
import {assert} from '@esm-bundle/chai';

const extraGlobals = window as LitExtraGlobals;

(canTestLitElement ? suite : suite.skip)('Styling', () => {
suite('Basic styling', () => {
let container: HTMLElement;
Expand Down Expand Up @@ -309,7 +307,7 @@ const extraGlobals = window as LitExtraGlobals;
'3px'
);
// Verify there is one scoping style under ShadyDOM
if (extraGlobals.ShadyDOM?.inUse) {
if (window.ShadyDOM?.inUse) {
assert.equal(
document.querySelectorAll(`style[scope=${name}`).length,
1
Expand All @@ -322,7 +320,7 @@ const extraGlobals = window as LitExtraGlobals;
let container: HTMLElement;

setup(function () {
if (!extraGlobals.ShadyDOM) {
if (!window.ShadyDOM) {
this.skip();
} else {
container = document.createElement('div');
Expand Down
4 changes: 1 addition & 3 deletions packages/lit-element/src/test/lit-element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {assert} from '@esm-bundle/chai';

import {createRef, ref} from 'lit-html/directives/ref.js';

const extraGlobals = window as LitExtraGlobals;

(canTestLitElement ? suite : suite.skip)('LitElement', () => {
let container: HTMLElement;

Expand Down Expand Up @@ -324,7 +322,7 @@ const extraGlobals = window as LitExtraGlobals;
assert.ok(a.hasUpdated);
});

(extraGlobals.ShadyDOM && extraGlobals.ShadyDOM.inUse ? test.skip : test)(
(window.ShadyDOM && window.ShadyDOM.inUse ? test.skip : test)(
'can customize shadowRootOptions',
async () => {
class A extends LitElement {
Expand Down
10 changes: 4 additions & 6 deletions packages/lit-element/src/test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ export const generateElementName = () => `x-${count++}`;
export const nextFrame = () =>
new Promise((resolve) => requestAnimationFrame(resolve));

const extraGlobals = window as LitExtraGlobals;

export const getComputedStyleValue = (element: Element, property: string) =>
extraGlobals.ShadyCSS
? extraGlobals.ShadyCSS.getComputedStyleValue(element, property)
window.ShadyCSS
? window.ShadyCSS.getComputedStyleValue(element, property)
: getComputedStyle(element).getPropertyValue(property);

export const stripExpressionComments = (html: string) =>
Expand All @@ -25,8 +23,8 @@ export const stripExpressionComments = (html: string) =>
// Only test LitElement if ShadowRoot is available and either ShadyDOM is not
// in use or it is and LitElement platform support is available.
export const canTestLitElement =
(window.ShadowRoot && !extraGlobals.ShadyDOM?.inUse) ||
extraGlobals.litElementPlatformSupport;
(window.ShadowRoot && !window.ShadyDOM?.inUse) ||
window.litElementPlatformSupport;

export interface ShadyRenderOptions extends RenderOptions {
scope?: string;
Expand Down
8 changes: 3 additions & 5 deletions packages/lit-html/src/directive-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ type ChildPart = InstanceType<typeof ChildPart>;

const ENABLE_SHADYDOM_NOPATCH = true;

const extraGlobals = window as LitExtraGlobals;

const wrap =
ENABLE_SHADYDOM_NOPATCH &&
extraGlobals.ShadyDOM?.inUse &&
extraGlobals.ShadyDOM?.noPatch === true
? extraGlobals.ShadyDOM!.wrap
window.ShadyDOM?.inUse &&
window.ShadyDOM?.noPatch === true
? window.ShadyDOM!.wrap
: (node: Node) => node;

/**
Expand Down
21 changes: 1 addition & 20 deletions packages/lit-html/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

interface LitExtendedWindow extends Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reactiveElementPlatformSupport: (options: {[index: string]: any}) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litElementPlatformSupport: (options: {[index: string]: any}) => void;
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
litHtmlPlatformSupport: (template: unknown, childPart: unknown) => void;
}

type LitExtraGlobals = typeof globalThis & LitExtendedWindow;

// Augment existing types with styling API
interface ShadowRoot {
adoptedStyleSheets: CSSStyleSheet[];
}

// eslint-disable-next-line no-var
declare var ShadowRoot: {prototype: ShadowRoot; new (): ShadowRoot};

interface CSSStyleSheet {
replaceSync(cssText: string): void;
replace(cssText: string): Promise<unknown>;
}
8 changes: 3 additions & 5 deletions packages/lit-html/src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ if (DEV_MODE) {
console.warn('lit-html is in dev mode. Not recommended for production!');
}

const extraGlobals = window as LitExtraGlobals;

const wrap =
ENABLE_SHADYDOM_NOPATCH &&
extraGlobals.ShadyDOM?.inUse &&
extraGlobals.ShadyDOM?.noPatch === true
? extraGlobals.ShadyDOM!.wrap
window.ShadyDOM?.inUse &&
window.ShadyDOM?.noPatch === true
? window.ShadyDOM!.wrap
: (node: Node) => node;

const trustedTypes = (globalThis as unknown as Partial<Window>).trustedTypes;
Expand Down
22 changes: 10 additions & 12 deletions packages/lit-html/src/polyfill-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ const ENABLE_SHADYDOM_NOPATCH = true;
Template: PatchableTemplateConstructor,
ChildPart: PatchableChildPartConstructor
) => {
const extraGlobals = window as LitExtraGlobals;

// polyfill-support is only needed if ShadyCSS or the ApplyShim is in use
// We test at the point of patching, which makes it safe to load
// webcomponentsjs and polyfill-support in either order
if (
extraGlobals.ShadyCSS === undefined ||
(extraGlobals.ShadyCSS.nativeShadow && !extraGlobals.ShadyCSS.ApplyShim)
window.ShadyCSS === undefined ||
(window.ShadyCSS.nativeShadow && !window.ShadyCSS.ApplyShim)
) {
return;
}
Expand All @@ -114,9 +112,9 @@ const ENABLE_SHADYDOM_NOPATCH = true;

const wrap =
ENABLE_SHADYDOM_NOPATCH &&
extraGlobals.ShadyDOM?.inUse &&
extraGlobals.ShadyDOM?.noPatch === true
? extraGlobals.ShadyDOM!.wrap
window.ShadyDOM?.inUse &&
window.ShadyDOM?.noPatch === true
? window.ShadyDOM!.wrap
: (node: Node) => node;

const needsPrepareStyles = (name: string | undefined) =>
Expand Down Expand Up @@ -147,11 +145,11 @@ const ENABLE_SHADYDOM_NOPATCH = true;
scopeCssStore.delete(name);
// ShadyCSS removes scopes and removes the style under ShadyDOM and leaves
// it under native Shadow DOM
extraGlobals.ShadyCSS!.prepareTemplateStyles(template, name);
window.ShadyCSS!.prepareTemplateStyles(template, name);
// Note, under native Shadow DOM, the style is added to the beginning of the
// template. It must be moved to the *end* of the template so it doesn't
// mess up part indices.
if (hasScopeCss && extraGlobals.ShadyCSS!.nativeShadow) {
if (hasScopeCss && window.ShadyCSS!.nativeShadow) {
// If there were styles but the CSS text was empty, ShadyCSS will
// eliminate the style altogether, so the style here could be null
const style = template.content.querySelector('style');
Expand All @@ -176,8 +174,8 @@ const ENABLE_SHADYDOM_NOPATCH = true;
const element = originalCreateElement.call(Template, html, options);
const scope = options?.scope;
if (scope !== undefined) {
if (!extraGlobals.ShadyCSS!.nativeShadow) {
extraGlobals.ShadyCSS!.prepareTemplateDom(element, scope);
if (!window.ShadyCSS!.nativeShadow) {
window.ShadyCSS!.prepareTemplateDom(element, scope);
}
// Process styles only if this scope is being prepared. Otherwise,
// leave styles as is for back compat with Lit1.
Expand Down Expand Up @@ -247,7 +245,7 @@ const ENABLE_SHADYDOM_NOPATCH = true;
// Note, this is the temporary startNode.
renderContainer.removeChild(renderContainerMarker);
// When using native Shadow DOM, include prepared style in shadowRoot.
if (extraGlobals.ShadyCSS?.nativeShadow) {
if (window.ShadyCSS?.nativeShadow) {
const style = template.content.querySelector('style');
if (style !== null) {
renderContainer.appendChild(style.cloneNode(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {html as htmlWithApply} from '../../lit-html.js';
import {renderShadowRoot} from '../test-utils/shadow-root.js';
import {assert} from '@esm-bundle/chai';

const extraGlobals = window as LitExtraGlobals;

suite('@apply', () => {
test('styles with css custom properties using @apply render', function () {
const container = document.createElement('scope-5');
Expand All @@ -31,7 +29,7 @@ suite('@apply', () => {
<div>Testing...</div>
`;
renderShadowRoot(result, container);
extraGlobals.ShadyCSS?.styleElement(container);
window.ShadyCSS?.styleElement(container);
const div = container.shadowRoot!.querySelector('div');
const computedStyle = getComputedStyle(div!);
assert.equal(
Expand Down Expand Up @@ -61,7 +59,7 @@ suite('@apply', () => {
const applyUser = document.createElement('apply-user');
document.body.appendChild(applyUser);
renderShadowRoot(applyUserContent, applyUser);
extraGlobals.ShadyCSS?.styleElement(applyUser);
window.ShadyCSS?.styleElement(applyUser);
const applyUserDiv = applyUser.shadowRoot!.querySelector('div');
const applyUserStyle = getComputedStyle(applyUserDiv!);
assert.equal(
Expand Down Expand Up @@ -101,13 +99,13 @@ suite('@apply', () => {
const div = applyProducer.shadowRoot!.querySelector('#test');
assert.ok(div?.hasAttribute('some-attr'));
assert.ok(div?.textContent, 'test');
extraGlobals.ShadyCSS?.styleElement(applyProducer);
window.ShadyCSS?.styleElement(applyProducer);
const usersInProducer =
applyProducer.shadowRoot!.querySelectorAll('apply-user');
renderShadowRoot(applyUserContent, usersInProducer[0]);
extraGlobals.ShadyCSS?.styleElement(usersInProducer[0] as HTMLElement);
window.ShadyCSS?.styleElement(usersInProducer[0] as HTMLElement);
renderShadowRoot(applyUserContent, usersInProducer[1]);
extraGlobals.ShadyCSS?.styleElement(usersInProducer[1] as HTMLElement);
window.ShadyCSS?.styleElement(usersInProducer[1] as HTMLElement);
const userInProducerStyle1 = getComputedStyle(
usersInProducer[0].shadowRoot!.querySelector('div')!
);
Expand Down Expand Up @@ -151,7 +149,7 @@ suite('@apply', () => {
</style>
<div>Testing...</div>`;
renderShadowRoot(result, this);
extraGlobals.ShadyCSS?.styleElement(this);
window.ShadyCSS?.styleElement(this);
}
}
customElements.define('apply-user-ce1', E);
Expand Down Expand Up @@ -214,7 +212,7 @@ suite('@apply', () => {
<div>Testing...</div>
`;
renderShadowRoot(result, container);
extraGlobals.ShadyCSS?.styleElement(container);
window.ShadyCSS?.styleElement(container);
document.body.removeChild(container);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import {renderShadowRoot} from '../test-utils/shadow-root.js';
import {html} from '../../lit-html.js';
import {assert} from '@esm-bundle/chai';

const extraGlobals = window as LitExtraGlobals;

suite('ShadyCSS scoping shim', () => {
setup(function () {
if (
typeof extraGlobals.ShadyDOM === 'undefined' ||
!extraGlobals.ShadyDOM.inUse ||
typeof extraGlobals.ShadyCSS === 'undefined' ||
extraGlobals.ShadyCSS.nativeShadow ||
extraGlobals.ShadyCSS.ScopingShim === undefined
typeof window.ShadyDOM === 'undefined' ||
!window.ShadyDOM.inUse ||
typeof window.ShadyCSS === 'undefined' ||
window.ShadyCSS.nativeShadow ||
window.ShadyCSS.ScopingShim === undefined
) {
this.skip();
return;
Expand All @@ -26,7 +24,7 @@ suite('ShadyCSS scoping shim', () => {

test('scoped styles are applied for non-TemplateResult values', function () {
const container = document.createElement('scope-1');
extraGlobals.ShadyCSS!.ScopingShim!.prepareAdoptedCssText(
window.ShadyCSS!.ScopingShim!.prepareAdoptedCssText(
[':host { border-top: 2px solid black; }'],
'scope-1'
);
Expand All @@ -41,7 +39,7 @@ suite('ShadyCSS scoping shim', () => {

test('adopted CSS remains when rendering a TemplateResult after an initial non-TemplateResult', function () {
const container = document.createElement('scope-2');
extraGlobals.ShadyCSS!.ScopingShim!.prepareAdoptedCssText(
window.ShadyCSS!.ScopingShim!.prepareAdoptedCssText(
[':host { border-top: 2px solid black; } button { font-size: 7px; } '],
'scope-2'
);
Expand Down

0 comments on commit 52a47c7

Please sign in to comment.