Skip to content

Commit dfe10a5

Browse files
authored
Merge pull request #457 from webcomponents/ts-externs
Add TS externs.
2 parents 1f39d6a + 59892af commit dfe10a5

32 files changed

+1192
-152
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ packages/webcomponentsjs/src/platform/
3636
packages/webcomponentsjs/src/flag-parser.*
3737
packages/webcomponentsjs/src/unresolved.*
3838
packages/webcomponentsjs/custom-elements-es5-adapter.js
39+
packages/webcomponentsjs/webcomponents-bundle.d.ts
3940
packages/webcomponentsjs/webcomponents-bundle.js*

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ packages/webcomponentsjs/src/platform/
3131
packages/webcomponentsjs/src/flag-parser.*
3232
packages/webcomponentsjs/src/unresolved.*
3333
packages/webcomponentsjs/custom-elements-es5-adapter.js
34+
packages/webcomponentsjs/webcomponents-bundle.d.ts
3435
packages/webcomponentsjs/webcomponents-bundle.js*

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ packages/webcomponentsjs/src/platform/
3636
packages/webcomponentsjs/src/flag-parser.*
3737
packages/webcomponentsjs/src/unresolved.*
3838
packages/webcomponentsjs/custom-elements-es5-adapter.js
39+
packages/webcomponentsjs/webcomponents-bundle.d.ts
3940
packages/webcomponentsjs/webcomponents-bundle.js*
4041

4142
LICENSE

packages/custom-elements/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
<!-- ## [Unreleased] -->
8+
## [Unreleased]
9+
10+
- Add TS externs. ([#457](https://github.com/webcomponents/polyfills/pull/457))
911

1012
## [1.4.3] - 2020-10-21
1113

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 The Polymer Project Authors. All rights reserved. This
4+
* code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt The complete set of authors may be found
6+
* at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
7+
* be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
8+
* Google as part of the polymer project is also subject to an additional IP
9+
* rights grant found at http://polymer.github.io/PATENTS.txt
10+
*/
11+
12+
// When building externally, this file is always assumed to be a module, but by
13+
// default it isn't when building internally, so we need this export statement.
14+
export {};
15+
16+
declare global {
17+
interface CustomElementRegistry {
18+
forcePolyfill?: boolean;
19+
polyfillWrapFlushCallback?(outer: (fn: () => void) => void): void;
20+
noDocumentConstructionObserver?: boolean;
21+
shadyDomFastWalk?: boolean;
22+
}
23+
}

packages/custom-elements/package-lock.json

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/custom-elements/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@
3434
"webcomponents",
3535
"polyfill",
3636
"shim"
37-
]
37+
],
38+
"devDependencies": {
39+
"@webcomponents/html-imports": "^1.2.6",
40+
"@webcomponents/shadydom": "^1.8.0"
41+
}
3842
}

packages/custom-elements/ts_src/CustomElementInternals.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
* rights grant found at http://polymer.github.io/PATENTS.txt
1010
*/
1111

12-
import './Externs.js';
12+
import CustomElementRegistry from './CustomElementRegistry.js';
1313
import {CustomElementState as CEState} from './CustomElementState.js';
14-
import {CustomElementDefinition, HTMLImportElement} from './Externs.js';
1514
import * as Native from './Patch/Native.js';
1615
import * as Utilities from './Utilities.js';
1716

@@ -217,7 +216,7 @@ export default class CustomElementInternals {
217216
element.localName === 'link' &&
218217
element.getAttribute('rel') === 'import'
219218
) {
220-
const importElem = element as HTMLImportElement;
219+
const importElem = element as HTMLLinkElement;
221220
// The HTML Imports polyfill sets a descendant element of the link to
222221
// the `import` property, specifically this is *not* a Document.
223222
const importNode = importElem.import;
@@ -228,10 +227,7 @@ export default class CustomElementInternals {
228227
importNode.__CE_registry = document.__CE_registry;
229228
}
230229

231-
if (
232-
importNode &&
233-
(importNode as HTMLImportDocument).readyState === 'complete'
234-
) {
230+
if (importNode && importNode.readyState === 'complete') {
235231
importNode.__CE_documentLoadHandled = true;
236232
} else {
237233
// If this link's import root is not available, its contents can't
@@ -417,7 +413,9 @@ export default class CustomElementInternals {
417413
return;
418414
}
419415

420-
return registry.internal_localNameToDefinition(localName);
416+
return (registry as CustomElementRegistry).internal_localNameToDefinition(
417+
localName
418+
);
421419
}
422420

423421
/**
@@ -438,7 +436,9 @@ export default class CustomElementInternals {
438436
// Only create custom elements if the document is associated with a
439437
// registry.
440438
if (registry && (namespace === null || namespace === NS_HTML)) {
441-
const definition = registry.internal_localNameToDefinition(localName);
439+
const definition = (registry as CustomElementRegistry).internal_localNameToDefinition(
440+
localName
441+
);
442442
if (definition) {
443443
try {
444444
const result = new definition.constructorFunction();
@@ -539,14 +539,24 @@ export default class CustomElementInternals {
539539
*
540540
* @see https://html.spec.whatwg.org/multipage/webappapis.html#report-the-exception
541541
*/
542-
reportTheException(error: Error) {
542+
reportTheException(errorArg: Error) {
543+
interface ExtendedError extends Error {
544+
// Non-standard Safari properties.
545+
sourceURL?: string;
546+
line?: number;
547+
column?: number;
548+
549+
// Non-standard Firefox properties.
550+
fileName?: string;
551+
lineNumber?: number;
552+
columnNumber?: number;
553+
}
554+
555+
const error = errorArg as ExtendedError;
543556
const message = error.message;
544-
const filename =
545-
/* Safari */ error.sourceURL || /* Firefox */ error.fileName || '';
546-
const lineno =
547-
/* Safari */ error.line || /* Firefox */ error.lineNumber || 0;
548-
const colno =
549-
/* Safari */ error.column || /* Firefox */ error.columnNumber || 0;
557+
const filename = error.sourceURL || error.fileName || '';
558+
const lineno = error.line || error.lineNumber || 0;
559+
const colno = error.column || error.columnNumber || 0;
550560

551561
let event: ErrorEvent | undefined = undefined;
552562
if (ErrorEvent.prototype.initErrorEvent === undefined) {
@@ -594,7 +604,3 @@ export default class CustomElementInternals {
594604
}
595605
}
596606
}
597-
598-
declare interface HTMLImportDocument extends Node {
599-
readyState: 'complete' | string;
600-
}

packages/custom-elements/ts_src/CustomElementRegistry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {AlreadyConstructedMarkerType} from './AlreadyConstructedMarker.js';
1313
import CustomElementInternals from './CustomElementInternals.js';
1414
import Deferred from './Deferred.js';
1515
import DocumentConstructionObserver from './DocumentConstructionObserver.js';
16-
import {Constructor, CustomElementDefinition} from './Externs.js';
1716
import * as Utilities from './Utilities.js';
1817

1918
interface ElementConstructor {
@@ -35,7 +34,7 @@ export default class CustomElementRegistry {
3534
CustomElementDefinition
3635
>();
3736
private readonly _constructorToDefinition = new Map<
38-
Constructor<HTMLElement>,
37+
ElementConstructor,
3938
CustomElementDefinition
4039
>();
4140
private _elementDefinitionIsRunning = false;

packages/custom-elements/ts_src/Externs.ts

Lines changed: 0 additions & 115 deletions
This file was deleted.

packages/custom-elements/ts_src/Patch/HTMLElement.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
import AlreadyConstructedMarker from '../AlreadyConstructedMarker.js';
1313
import CustomElementInternals from '../CustomElementInternals.js';
14+
import CustomElementRegistry from '../CustomElementRegistry.js';
1415
import CEState from '../CustomElementState.js';
15-
import {Constructor} from '../Externs.js';
1616
import * as Native from './Native.js';
1717

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

2626
// Always look up the definition from the global registry.
2727
const registry = document.__CE_registry!;
28-
const definition = registry.internal_constructorToDefinition(constructor);
28+
const definition = (registry as CustomElementRegistry).internal_constructorToDefinition(
29+
constructor
30+
);
2931
if (!definition) {
3032
throw new Error(
3133
'Failed to construct a custom element: ' +

packages/custom-elements/ts_src/Utilities.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* rights grant found at http://polymer.github.io/PATENTS.txt
1010
*/
1111

12-
import {HTMLImportElement} from './Externs.js';
13-
1412
const reservedElementNameSet = new Set<string>();
1513
// IE11 does not support constructing a set using an iterable.
1614
[
@@ -111,7 +109,7 @@ export function walkDeepDescendantElements(
111109
if (localName === 'link' && element.getAttribute('rel') === 'import') {
112110
// If this import (polyfilled or not) has its root node available,
113111
// walk it.
114-
const importNode = (element as HTMLImportElement).import;
112+
const importNode = (element as HTMLLinkElement).import;
115113
if (visitedImports === undefined) {
116114
visitedImports = new Set();
117115
}

0 commit comments

Comments
 (0)