Skip to content

Add TS externs. #457

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

Merged
merged 32 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
309a74d
Add TS version of Shady CSS externs.
bicknellr Jun 17, 2021
697ae80
Add `types` field and include '.d.ts' externs in exported files.
bicknellr Jun 25, 2021
a0d3448
Concatenate TS externs from all sub-packages.
bicknellr Jun 26, 2021
42b7108
Add a more complete `ScopingShim` type and a workaround for Tsickle t…
bicknellr Jun 29, 2021
4a832de
Add TS version of the custom elements polyfill externs.
bicknellr Jun 30, 2021
46bbeb8
Add TS version of the Shady DOM externs.
bicknellr Jun 30, 2021
0097de2
Add TS version of the HTML Imports polyfill externs.
bicknellr Jun 30, 2021
c37a795
Fix license comments.
bicknellr Jun 30, 2021
868913e
Change `.d.ts` bundle location.
bicknellr Jun 30, 2021
1327378
Add TS version of the webcomponents.js externs.
bicknellr Jun 30, 2021
eff16d1
Remove `types` field from Shady CSS package.json, since it doesn't ap…
bicknellr Jun 30, 2021
9e0e87a
Add TS externs to the published Shady DOM package content.
bicknellr Jun 30, 2021
15c7e3e
Remove unused `cast` property in webcomponents.js externs.
bicknellr Jul 1, 2021
3371667
Remove the custom elements polyfill's 'internal' externs.
bicknellr Jul 1, 2021
dd6ddca
Fix implicit any for `WebComponents` global.
bicknellr Jul 1, 2021
d904051
Move Error properties used by the custom elements polyfill that weren…
bicknellr Jul 1, 2021
dafe73a
Remove `HTMLImportDocument` type in the custom elements polyfill.
bicknellr Jul 1, 2021
e1dc950
Move template polyfill externs into the template polyfill package.
bicknellr Jul 15, 2021
f294d7d
shadycss-externs.d.ts -> shadycss.d.ts
bicknellr Jul 16, 2021
cc7072f
Explicitly list declarations to be bundled to prevent including unrel…
bicknellr Jul 16, 2021
0f98e69
Revert changes to JS externs for the webcomponentsjs package.
bicknellr Jul 16, 2021
639bfcd
Add comments to all points where Closure's type name constraints requ…
bicknellr Jul 16, 2021
b61a1bd
Fix static `bootstrap` method in template polyfill externs.
bicknellr Jul 16, 2021
a5e00fe
Wrap externs in `declare global { ... }` (declare globals inside modu…
bicknellr Jul 20, 2021
ef7e648
Merge remote-tracking branch 'origin/master' into ts-externs
bicknellr Jul 26, 2021
b7780f4
Add changelog entries.
bicknellr Jul 26, 2021
93e7972
custom-elements: Move all internal types into 'env.d.ts'.
bicknellr Jul 26, 2021
f0d632e
shadycss: Move all internal types into 'env.d.ts'.
bicknellr Jul 26, 2021
0cbaaae
shadydom: Move all internal types into 'env.d.ts'.
bicknellr Jul 27, 2021
24c955f
Merge remote-tracking branch 'origin/master' into ts-externs
bicknellr Jul 28, 2021
868d317
Remove unnecessary `@externs`.
bicknellr Jul 28, 2021
59892af
Update comments about `export {};` in `.d.ts` files.
bicknellr Jul 28, 2021
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ packages/webcomponentsjs/src/platform/
packages/webcomponentsjs/src/flag-parser.*
packages/webcomponentsjs/src/unresolved.*
packages/webcomponentsjs/custom-elements-es5-adapter.js
packages/webcomponentsjs/webcomponents-bundle.d.ts
packages/webcomponentsjs/webcomponents-bundle.js*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ packages/webcomponentsjs/src/platform/
packages/webcomponentsjs/src/flag-parser.*
packages/webcomponentsjs/src/unresolved.*
packages/webcomponentsjs/custom-elements-es5-adapter.js
packages/webcomponentsjs/webcomponents-bundle.d.ts
packages/webcomponentsjs/webcomponents-bundle.js*
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ packages/webcomponentsjs/src/platform/
packages/webcomponentsjs/src/flag-parser.*
packages/webcomponentsjs/src/unresolved.*
packages/webcomponentsjs/custom-elements-es5-adapter.js
packages/webcomponentsjs/webcomponents-bundle.d.ts
packages/webcomponentsjs/webcomponents-bundle.js*

LICENSE
Expand Down
4 changes: 3 additions & 1 deletion packages/custom-elements/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- ## [Unreleased] -->
## [Unreleased]

- Add TS externs. ([#457](https://github.com/webcomponents/polyfills/pull/457))

## [1.4.3] - 2020-10-21

Expand Down
23 changes: 23 additions & 0 deletions packages/custom-elements/externs/custom-elements.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright (c) 2021 The Polymer Project Authors. All rights reserved. This
* code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
* Google as part of the polymer project is also subject to an additional IP
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

// When building externally, this file is always assumed to be a module, but by
// default it isn't when building internally, so we need this export statement.
export {};

declare global {
interface CustomElementRegistry {
forcePolyfill?: boolean;
polyfillWrapFlushCallback?(outer: (fn: () => void) => void): void;
noDocumentConstructionObserver?: boolean;
shadyDomFastWalk?: boolean;
}
}
40 changes: 39 additions & 1 deletion packages/custom-elements/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/custom-elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
"webcomponents",
"polyfill",
"shim"
]
],
"devDependencies": {
"@webcomponents/html-imports": "^1.2.6",
"@webcomponents/shadydom": "^1.8.0"
}
}
46 changes: 26 additions & 20 deletions packages/custom-elements/ts_src/CustomElementInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

import './Externs.js';
import CustomElementRegistry from './CustomElementRegistry.js';
import {CustomElementState as CEState} from './CustomElementState.js';
import {CustomElementDefinition, HTMLImportElement} from './Externs.js';
import * as Native from './Patch/Native.js';
import * as Utilities from './Utilities.js';

Expand Down Expand Up @@ -217,7 +216,7 @@ export default class CustomElementInternals {
element.localName === 'link' &&
element.getAttribute('rel') === 'import'
) {
const importElem = element as HTMLImportElement;
const importElem = element as HTMLLinkElement;
// The HTML Imports polyfill sets a descendant element of the link to
// the `import` property, specifically this is *not* a Document.
const importNode = importElem.import;
Expand All @@ -228,10 +227,7 @@ export default class CustomElementInternals {
importNode.__CE_registry = document.__CE_registry;
}

if (
importNode &&
(importNode as HTMLImportDocument).readyState === 'complete'
) {
if (importNode && importNode.readyState === 'complete') {
importNode.__CE_documentLoadHandled = true;
} else {
// If this link's import root is not available, its contents can't
Expand Down Expand Up @@ -417,7 +413,9 @@ export default class CustomElementInternals {
return;
}

return registry.internal_localNameToDefinition(localName);
return (registry as CustomElementRegistry).internal_localNameToDefinition(
localName
);
}

/**
Expand All @@ -438,7 +436,9 @@ export default class CustomElementInternals {
// Only create custom elements if the document is associated with a
// registry.
if (registry && (namespace === null || namespace === NS_HTML)) {
const definition = registry.internal_localNameToDefinition(localName);
const definition = (registry as CustomElementRegistry).internal_localNameToDefinition(
localName
);
if (definition) {
try {
const result = new definition.constructorFunction();
Expand Down Expand Up @@ -539,14 +539,24 @@ export default class CustomElementInternals {
*
* @see https://html.spec.whatwg.org/multipage/webappapis.html#report-the-exception
*/
reportTheException(error: Error) {
reportTheException(errorArg: Error) {
interface ExtendedError extends Error {
// Non-standard Safari properties.
sourceURL?: string;
line?: number;
column?: number;

// Non-standard Firefox properties.
fileName?: string;
lineNumber?: number;
columnNumber?: number;
}

const error = errorArg as ExtendedError;
const message = error.message;
const filename =
/* Safari */ error.sourceURL || /* Firefox */ error.fileName || '';
const lineno =
/* Safari */ error.line || /* Firefox */ error.lineNumber || 0;
const colno =
/* Safari */ error.column || /* Firefox */ error.columnNumber || 0;
const filename = error.sourceURL || error.fileName || '';
const lineno = error.line || error.lineNumber || 0;
const colno = error.column || error.columnNumber || 0;

let event: ErrorEvent | undefined = undefined;
if (ErrorEvent.prototype.initErrorEvent === undefined) {
Expand Down Expand Up @@ -594,7 +604,3 @@ export default class CustomElementInternals {
}
}
}

declare interface HTMLImportDocument extends Node {
readyState: 'complete' | string;
}
3 changes: 1 addition & 2 deletions packages/custom-elements/ts_src/CustomElementRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {AlreadyConstructedMarkerType} from './AlreadyConstructedMarker.js';
import CustomElementInternals from './CustomElementInternals.js';
import Deferred from './Deferred.js';
import DocumentConstructionObserver from './DocumentConstructionObserver.js';
import {Constructor, CustomElementDefinition} from './Externs.js';
import * as Utilities from './Utilities.js';

interface ElementConstructor {
Expand All @@ -35,7 +34,7 @@ export default class CustomElementRegistry {
CustomElementDefinition
>();
private readonly _constructorToDefinition = new Map<
Constructor<HTMLElement>,
ElementConstructor,
CustomElementDefinition
>();
private _elementDefinitionIsRunning = false;
Expand Down
115 changes: 0 additions & 115 deletions packages/custom-elements/ts_src/Externs.ts

This file was deleted.

8 changes: 5 additions & 3 deletions packages/custom-elements/ts_src/Patch/HTMLElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import AlreadyConstructedMarker from '../AlreadyConstructedMarker.js';
import CustomElementInternals from '../CustomElementInternals.js';
import CustomElementRegistry from '../CustomElementRegistry.js';
import CEState from '../CustomElementState.js';
import {Constructor} from '../Externs.js';
import * as Native from './Native.js';

export default function (internals: CustomElementInternals) {
Expand All @@ -21,11 +21,13 @@ export default function (internals: CustomElementInternals) {
// emulated in ES5. Assuming the user keeps the default value of the
// constructor's prototype's `constructor` property, this is
// equivalent.
const constructor = this.constructor as Constructor<HTMLElement>;
const constructor = this.constructor as {new (): HTMLElement};

// Always look up the definition from the global registry.
const registry = document.__CE_registry!;
const definition = registry.internal_constructorToDefinition(constructor);
const definition = (registry as CustomElementRegistry).internal_constructorToDefinition(
constructor
);
if (!definition) {
throw new Error(
'Failed to construct a custom element: ' +
Expand Down
4 changes: 1 addition & 3 deletions packages/custom-elements/ts_src/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/

import {HTMLImportElement} from './Externs.js';

const reservedElementNameSet = new Set<string>();
// IE11 does not support constructing a set using an iterable.
[
Expand Down Expand Up @@ -111,7 +109,7 @@ export function walkDeepDescendantElements(
if (localName === 'link' && element.getAttribute('rel') === 'import') {
// If this import (polyfilled or not) has its root node available,
// walk it.
const importNode = (element as HTMLImportElement).import;
const importNode = (element as HTMLLinkElement).import;
if (visitedImports === undefined) {
visitedImports = new Set();
}
Expand Down
Loading