Skip to content

Commit 961de54

Browse files
committed
fix: two bugfixes
1 parent d084439 commit 961de54

File tree

12 files changed

+162
-95
lines changed

12 files changed

+162
-95
lines changed

packages/analyzer/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Release 0.4.13
2+
- Add support for class expressions in `customElements.define` calls, e.g.: `customElements.define('m-e', class extends HTMLElement{})`
3+
- Fixed bug in lit-plugin to avoid duplicate attributes
4+
15
## Release 0.4.12
26
- Handle `@ignore` and `@internal` jsdoc
37

packages/analyzer/custom-elements.json

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,40 @@
77
"path": "fixtures/-default/package/bar.js",
88
"declarations": [
99
{
10-
"kind": "variable",
11-
"name": "variable",
12-
"type": {
13-
"text": "string"
10+
"kind": "class",
11+
"description": "",
12+
"name": "anonymous_0",
13+
"superclass": {
14+
"name": "HTMLElement"
1415
},
15-
"default": "'var'"
16+
"tagName": "my-element",
17+
"customElement": true
1618
},
1719
{
1820
"kind": "class",
1921
"description": "",
20-
"name": "IncludeMe",
21-
"members": [
22-
{
23-
"kind": "field",
24-
"name": "included",
25-
"type": {
26-
"text": "string"
27-
},
28-
"default": "'hello world'"
29-
}
30-
],
22+
"name": "MyEl",
3123
"superclass": {
3224
"name": "HTMLElement"
3325
},
34-
"tagName": "include-me",
26+
"tagName": "my-element",
3527
"customElement": true
3628
}
3729
],
3830
"exports": [
3931
{
40-
"kind": "js",
41-
"name": "variable",
42-
"declaration": {
43-
"name": "variable",
44-
"module": "fixtures/-default/package/bar.js"
45-
}
46-
},
47-
{
48-
"kind": "js",
49-
"name": "IncludeMe",
32+
"kind": "custom-element-definition",
33+
"name": "my-element",
5034
"declaration": {
51-
"name": "IncludeMe",
35+
"name": "anonymous_0",
5236
"module": "fixtures/-default/package/bar.js"
5337
}
5438
},
5539
{
5640
"kind": "custom-element-definition",
57-
"name": "include-me",
41+
"name": "my-element",
5842
"declaration": {
59-
"name": "IncludeMe",
43+
"name": "MyEl",
6044
"module": "fixtures/-default/package/bar.js"
6145
}
6246
}
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,3 @@
1-
/** @internal */
2-
export const dontIncludeMe = false; // should not be in declarations
1+
customElements.define('my-element', class extends HTMLElement {});
32

4-
/** @ignore */
5-
export const meNeither = false; // should not be in declarations
6-
7-
export const variable = 'var';
8-
9-
/** @ignore */
10-
export class IgnoreMe extends HTMLElement { }
11-
12-
customElements.define("ignore-me", IgnoreMe);
13-
14-
export class IncludeMe extends HTMLElement {
15-
included = 'hello world';
16-
17-
/** @ignore */
18-
sneaky = 'deaky';
19-
20-
ignoreThis2 = '';
21-
22-
constructor() {
23-
super();
24-
25-
/** @ignore */
26-
this.ignoreThisAlso = 'hidden';
27-
28-
/** @ignore */
29-
this.ignoreThis2 = 'hidden';
30-
}
31-
32-
/** @internal */
33-
hideMe() {
34-
return '🙈'
35-
}
36-
}
37-
38-
customElements.define("include-me", IncludeMe);
39-
40-
/** @ignore */
41-
var ignoreMePlease = 'haha';
42-
43-
/** @internal */
44-
var excludeMe, andMe = 'something private';
45-
46-
export {
47-
ignoreMePlease,
48-
excludeMe,
49-
andMe,
50-
}
3+
customElements.define('my-element', class MyEl extends HTMLElement {});

packages/analyzer/fixtures/custom-elements-define/fixture/custom-elements.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
},
3636
"tagName": "my-window",
3737
"customElement": true
38+
},
39+
{
40+
"kind": "class",
41+
"description": "",
42+
"name": "anonymous_4",
43+
"superclass": {
44+
"name": "HTMLElement"
45+
},
46+
"tagName": "anon-1",
47+
"customElement": true
48+
},
49+
{
50+
"kind": "class",
51+
"description": "",
52+
"name": "MyEl",
53+
"superclass": {
54+
"name": "HTMLElement"
55+
},
56+
"tagName": "anon-2",
57+
"customElement": true
3858
}
3959
],
4060
"exports": [
@@ -69,6 +89,22 @@
6989
"name": "MyWindow",
7090
"module": "fixtures/custom-elements-define/package/my-element.js"
7191
}
92+
},
93+
{
94+
"kind": "custom-element-definition",
95+
"name": "anon-1",
96+
"declaration": {
97+
"name": "anonymous_4",
98+
"module": "fixtures/custom-elements-define/package/my-element.js"
99+
}
100+
},
101+
{
102+
"kind": "custom-element-definition",
103+
"name": "anon-2",
104+
"declaration": {
105+
"name": "MyEl",
106+
"module": "fixtures/custom-elements-define/package/my-element.js"
107+
}
72108
}
73109
]
74110
}

packages/analyzer/fixtures/custom-elements-define/output.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
},
3636
"tagName": "my-window",
3737
"customElement": true
38+
},
39+
{
40+
"kind": "class",
41+
"description": "",
42+
"name": "anonymous_4",
43+
"superclass": {
44+
"name": "HTMLElement"
45+
},
46+
"tagName": "anon-1",
47+
"customElement": true
48+
},
49+
{
50+
"kind": "class",
51+
"description": "",
52+
"name": "MyEl",
53+
"superclass": {
54+
"name": "HTMLElement"
55+
},
56+
"tagName": "anon-2",
57+
"customElement": true
3858
}
3959
],
4060
"exports": [
@@ -69,6 +89,22 @@
6989
"name": "MyWindow",
7090
"module": "fixtures/custom-elements-define/package/my-element.js"
7191
}
92+
},
93+
{
94+
"kind": "custom-element-definition",
95+
"name": "anon-1",
96+
"declaration": {
97+
"name": "anonymous_4",
98+
"module": "fixtures/custom-elements-define/package/my-element.js"
99+
}
100+
},
101+
{
102+
"kind": "custom-element-definition",
103+
"name": "anon-2",
104+
"declaration": {
105+
"name": "MyEl",
106+
"module": "fixtures/custom-elements-define/package/my-element.js"
107+
}
72108
}
73109
]
74110
}

packages/analyzer/fixtures/custom-elements-define/package/my-element.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ class MyWindow extends HTMLElement {}
99
customElements.define('my-foo', MyFoo);
1010
customElements.define('my-bar', MyBar);
1111
customElements.define('my-element', MyElement);
12-
window.customElements.define('my-window', MyWindow);
12+
window.customElements.define('my-window', MyWindow);
13+
14+
customElements.define('anon-1', class extends HTMLElement {});
15+
16+
customElements.define('anon-2', class MyEl extends HTMLElement {});

packages/analyzer/fixtures/plugin-lit/fixture/custom-elements.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
}
103103
],
104104
"attributes": [
105+
{
106+
"name": "my-attr",
107+
"fieldName": "attr"
108+
},
105109
{
106110
"name": "decoratedProperty",
107111
"fieldName": "decoratedProperty",
@@ -134,10 +138,6 @@
134138
"name": "prop2",
135139
"fieldName": "prop2"
136140
},
137-
{
138-
"name": "my-attr",
139-
"fieldName": "attr"
140-
},
141141
{
142142
"name": "_privateProp",
143143
"type": {

packages/analyzer/fixtures/plugin-lit/output.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
}
103103
],
104104
"attributes": [
105+
{
106+
"name": "my-attr",
107+
"fieldName": "attr"
108+
},
105109
{
106110
"name": "decoratedProperty",
107111
"type": {
@@ -134,10 +138,6 @@
134138
"name": "prop2",
135139
"fieldName": "prop2"
136140
},
137-
{
138-
"name": "my-attr",
139-
"fieldName": "attr"
140-
},
141141
{
142142
"name": "_privateProp",
143143
"type": {

packages/analyzer/fixtures/plugin-lit/package/my-element.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LitElement, property, customElement } from 'lit-element';
22

33
/**
4+
* @attr my-attr
45
* @prop {string} prop1 this is the description
56
*/
67
@customElement('my-element')

packages/analyzer/src/features/analyse-phase/custom-elements-define-calls.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getDeclarationInFile, hasIgnoreJSDoc, isCustomElementsDefineCall } from '../../utils/ast-helpers.js';
22
import { resolveModuleOrPackageSpecifier } from '../../utils/index.js';
3+
import { createClass } from './creators/createClass.js';
34

45
/**
56
* CUSTOM-ELEMENTS-DEFINE-CALLS
@@ -9,9 +10,14 @@ import { resolveModuleOrPackageSpecifier } from '../../utils/index.js';
910
* @example window.customElements.define()
1011
*/
1112
export function customElementsDefineCallsPlugin() {
13+
let counter;
1214
return {
1315
name: 'CORE - CUSTOM-ELEMENTS-DEFINE-CALLS',
14-
analyzePhase({node, moduleDoc, context}){
16+
analyzePhase({ts, node, moduleDoc, context}){
17+
if(node?.kind === ts.SyntaxKind.SourceFile) {
18+
counter = 0;
19+
}
20+
1521
if (hasIgnoreJSDoc(node))
1622
return;
1723

@@ -20,10 +26,49 @@ export function customElementsDefineCallsPlugin() {
2026
* @example window.customElements.define('my-el', MyEl);
2127
*/
2228
if(isCustomElementsDefineCall(node)) {
23-
const elementClass = node.parent.arguments[1].text;
29+
const classArg = node.parent.arguments[1];
30+
let isAnonymousClass = classArg?.kind === ts.SyntaxKind.ClassExpression;
31+
let isUnnamed = classArg?.name === undefined;
32+
33+
if(isAnonymousClass) {
34+
const klass = createClass(classArg, moduleDoc, context);
35+
36+
if(isUnnamed) {
37+
klass.name = `anonymous_${counter}`;
38+
}
39+
moduleDoc.declarations.push(klass);
40+
}
41+
42+
let elementClass;
43+
44+
/**
45+
* @example customElements.define('m-e', class extends HTMLElement{})
46+
* ^
47+
*/
48+
if(isUnnamed) {
49+
elementClass = `anonymous_${counter}`;
50+
counter = counter + 1;
51+
}
52+
53+
/**
54+
* @example customElements.define('m-e', MyElement)
55+
* ^^^^^^^^^
56+
*/
57+
if(node?.parent?.arguments?.[1]?.text) {
58+
elementClass = node.parent.arguments[1].text;
59+
}
60+
61+
/**
62+
* @example customElements.define('m-e', class MyElement extends HTMLElement{})
63+
* ^^^^^^^^^
64+
*/
65+
if(classArg?.name) {
66+
elementClass = classArg?.name?.getText();
67+
}
68+
2469
const elementTag = node.parent.arguments[0].text;
2570

26-
const klass = getDeclarationInFile(elementClass, node.getSourceFile());
71+
const klass = getDeclarationInFile(elementClass, node?.getSourceFile());
2772

2873
if (hasIgnoreJSDoc(klass))
2974
return;

0 commit comments

Comments
 (0)