Skip to content

Commit a50015d

Browse files
committed
Fix and add tests for type reference serialization
1 parent da0366e commit a50015d

9 files changed

Lines changed: 190 additions & 29 deletions

File tree

lit-next.code-workspace

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@
5656
"name": "cli",
5757
"path": "packages/labs/cli"
5858
},
59+
{
60+
"name": "gen-manifest",
61+
"path": "packages/labs/gen-manifest"
62+
},
63+
{
64+
"name": "gen-react",
65+
"path": "packages/labs/gen-manifest"
66+
},
67+
{
68+
"name": "gen-angular",
69+
"path": "packages/labs/gen-manifest"
70+
},
71+
{
72+
"name": "gen-vue",
73+
"path": "packages/labs/gen-manifest"
74+
},
75+
{
76+
"name": "gen-utils",
77+
"path": "packages/labs/gen-manifest"
78+
},
5979
{
6080
"name": "lit-monorepo",
6181
"path": "."

package-lock.json

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/labs/analyzer/src/lib/references.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const npmModule = /^(?<package>(@\w+\/\w+)|\w+)\/?(?<module>.*)$/;
1515
* Returns the module specifier for a declaration if it was imported,
1616
* or `undefined` if the declaration was not imported.
1717
*/
18-
const getImportModuleSpecifier = (declaration: ts.Node): string | undefined => {
18+
const getImportNameAndModuleSpecifier = (
19+
declaration: ts.Node
20+
): {module: string; name: string} | undefined => {
1921
// TODO(kschaaf) support the various import syntaxes, e.g. `import {foo as bar} from 'baz'`
2022
if (
2123
ts.isImportSpecifier(declaration) &&
@@ -27,7 +29,10 @@ const getImportModuleSpecifier = (declaration: ts.Node): string | undefined => {
2729
.getText()
2830
// Remove quotes
2931
.slice(1, -1);
30-
return module;
32+
return {
33+
module,
34+
name: declaration.propertyName?.text ?? declaration.name.text,
35+
};
3136
}
3237
return undefined;
3338
};
@@ -45,7 +50,7 @@ export function getReferenceForSymbol(
4550
analyzer: AnalyzerInterface
4651
): Reference {
4752
const {path} = analyzer;
48-
const {name} = symbol;
53+
const {name: symbolName} = symbol;
4954
// TODO(kschaaf): Do we need to check other declarations? The assumption is
5055
// that even with multiple declarations (e.g. because of class interface +
5156
// constructor), the reference would point to the same location for all,
@@ -55,7 +60,7 @@ export function getReferenceForSymbol(
5560
if (declaration === undefined) {
5661
throw new DiagnosticsError(
5762
location,
58-
`Could not find declaration for symbol '${name}'`
63+
`Could not find declaration for symbol '${symbolName}'`
5964
);
6065
}
6166
const declarationSourceFile = declaration.getSourceFile();
@@ -84,12 +89,13 @@ export function getReferenceForSymbol(
8489
// (that don't have any e.g. source to link to) from other ambient
8590
// declarations where we could at least point to a declaration file
8691
return new Reference({
87-
name,
92+
name: symbolName,
8893
isGlobal: true,
8994
});
9095
} else {
91-
const moduleSpecifier = getImportModuleSpecifier(declaration);
92-
if (moduleSpecifier !== undefined) {
96+
const importInfo = getImportNameAndModuleSpecifier(declaration);
97+
if (importInfo !== undefined) {
98+
const {module: moduleSpecifier, name: importName} = importInfo;
9399
let refPackage;
94100
let refModule;
95101
// The symbol was imported; check whether it is a URL, absolute, package
@@ -125,15 +131,15 @@ export function getReferenceForSymbol(
125131
}
126132
}
127133
return new Reference({
128-
name,
134+
name: importName,
129135
package: refPackage,
130136
module: refModule,
131137
});
132138
} else {
133139
// Declared in this file: use the current package and module
134140
const module = getModule(location.getSourceFile(), analyzer);
135141
return new Reference({
136-
name,
142+
name: symbolName,
137143
package: module.packageJson.name,
138144
module: module.jsPath,
139145
});

packages/labs/gen-manifest/goldens/test-element-a/custom-elements.json

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77
"summary": "TODO",
88
"description": "TODO",
99
"declarations": [
10-
{
11-
"kind": "variable",
12-
"name": "numberVar",
13-
"summary": "TODO",
14-
"description": "TODO",
15-
"type": {"text": "number", "references": []}
16-
},
1710
{
1811
"kind": "class",
1912
"name": "ElementA",
@@ -31,6 +24,108 @@
3124
"cssProperties": [],
3225
"demos": [],
3326
"customElement": true
27+
},
28+
{
29+
"kind": "variable",
30+
"name": "localTypeVar",
31+
"summary": "TODO",
32+
"description": "TODO",
33+
"type": {
34+
"text": "ElementA",
35+
"references": [
36+
{
37+
"name": "ElementA",
38+
"package": "@lit-internal/test-element-a",
39+
"module": "element-a.js",
40+
"start": 0,
41+
"end": 8
42+
}
43+
]
44+
}
45+
},
46+
{
47+
"kind": "variable",
48+
"name": "packageTypeVar",
49+
"summary": "TODO",
50+
"description": "TODO",
51+
"type": {
52+
"text": "Foo<Bar>",
53+
"references": [
54+
{
55+
"name": "Foo",
56+
"package": "@lit-internal/test-element-a",
57+
"module": "package-stuff.js",
58+
"start": 0,
59+
"end": 3
60+
},
61+
{
62+
"name": "Bar",
63+
"package": "@lit-internal/test-element-a",
64+
"module": "package-stuff.js",
65+
"start": 4,
66+
"end": 7
67+
}
68+
]
69+
}
70+
},
71+
{
72+
"kind": "variable",
73+
"name": "externalTypeVar",
74+
"summary": "TODO",
75+
"description": "TODO",
76+
"type": {
77+
"text": "LitElement",
78+
"references": [
79+
{"name": "LitElement", "package": "lit", "start": 0, "end": 10}
80+
]
81+
}
82+
},
83+
{
84+
"kind": "variable",
85+
"name": "globalTypeVar",
86+
"summary": "TODO",
87+
"description": "TODO",
88+
"type": {
89+
"text": "HTMLElement",
90+
"references": [
91+
{
92+
"name": "HTMLElement",
93+
"package": "global:",
94+
"start": 0,
95+
"end": 11
96+
}
97+
]
98+
}
99+
}
100+
],
101+
"exports": [],
102+
"deprecated": false
103+
},
104+
{
105+
"kind": "javascript-module",
106+
"path": "package-stuff.js",
107+
"summary": "TODO",
108+
"description": "TODO",
109+
"declarations": [
110+
{
111+
"kind": "class",
112+
"name": "Bar",
113+
"summary": "TODO",
114+
"description": "TODO",
115+
"mixins": [],
116+
"members": [],
117+
"source": {"href": "TODO"},
118+
"deprecated": false
119+
},
120+
{
121+
"kind": "class",
122+
"name": "Foo",
123+
"summary": "TODO",
124+
"description": "TODO",
125+
"mixins": [],
126+
"members": [],
127+
"source": {"href": "TODO"},
128+
"deprecated": false
34129
}
35130
],
36131
"exports": [],

packages/labs/gen-manifest/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
],
2525
"files": [
2626
"src/**/*",
27-
"tsconfig.json"
27+
"tsconfig.json",
28+
"../test-projects/test-element-a"
2829
],
2930
"output": [
3031
"test",
@@ -48,7 +49,7 @@
4849
"dependencies": {
4950
"@lit-labs/analyzer": "^0.3.0",
5051
"@lit-labs/gen-utils": "^0.1.0",
51-
"custom-elements-manifest": "webcomponents/custom-elements-manifest#f893b20"
52+
"custom-elements-manifest": "^2.0.0"
5253
},
5354
"devDependencies": {
5455
"@types/node": "^17.0.31",

packages/labs/gen-manifest/src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,16 @@ const convertTypeReference = (
159159
};
160160

161161
const convertReference = (reference: Reference): cem.TypeReference => {
162-
return {
162+
const refObj: cem.TypeReference = {
163163
name: reference.name,
164-
...(reference.package
165-
? {package: reference.isGlobal ? 'global:' : reference.package}
166-
: {}),
167-
...(reference.name ? {name: reference.name} : {}),
168164
};
165+
if (reference.isGlobal) {
166+
refObj.package = 'global:';
167+
} else if (reference.package !== undefined) {
168+
refObj.package = reference.package;
169+
}
170+
if (reference.module !== undefined) {
171+
refObj.module = reference.module;
172+
}
173+
return refObj;
169174
};

packages/labs/test-projects/test-element-a/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"pack": "wireit"
88
},
99
"files": [
10-
"/element-a.{js,js.map,d.ts,d.ts.map}"
10+
"/element-a.{js,js.map,d.ts,d.ts.map}",
11+
"/package-stuff.{js,js.map,d.ts,d.ts.map}"
1112
],
1213
"dependencies": {
1314
"lit": "^2.0.0"

packages/labs/test-projects/test-element-a/src/element-a.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import {LitElement, html, css} from 'lit';
88
import {customElement, property} from 'lit/decorators.js';
9-
10-
export const numberVar = 42;
9+
import {Foo, Bar as Baz} from './package-stuff.js';
1110

1211
/**
1312
* My awesome element
@@ -28,3 +27,8 @@ export class ElementA extends LitElement {
2827
return html`<h1>${this.foo}</h1>`;
2928
}
3029
}
30+
31+
export let localTypeVar: ElementA;
32+
export let packageTypeVar: Foo<Baz>;
33+
export let externalTypeVar: LitElement;
34+
export let globalTypeVar: HTMLElement;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
export interface BarInterface {
8+
bar: boolean;
9+
}
10+
11+
export class Bar implements BarInterface {
12+
bar = true;
13+
}
14+
15+
export class Foo<T extends BarInterface> {
16+
bar?: T;
17+
}

0 commit comments

Comments
 (0)