Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/hot-apricots-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit-labs/analyzer': patch
---

Always use consumer's typescript rather than analyzer's dependency to avoid version mismatches
33 changes: 18 additions & 15 deletions packages/labs/analyzer/src/lib/custom-elements/custom-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* subclasses.
*/

import ts from 'typescript';
import type ts from 'typescript';
import {getClassMembers, getHeritage} from '../javascript/classes.js';
import {
AnalyzerInterface,
Expand All @@ -33,7 +33,7 @@ const _isCustomElementClassDeclaration = (
return (
declarations?.some(
(declaration) =>
(ts.isInterfaceDeclaration(declaration) &&
(analyzer.typescript.isInterfaceDeclaration(declaration) &&
declaration.name?.text === 'HTMLElement') ||
isCustomElementSubclass(declaration, analyzer)
) === true
Expand All @@ -48,10 +48,10 @@ export const isCustomElementSubclass = (
node: ts.Node,
analyzer: AnalyzerInterface
): node is CustomElementClassDeclaration => {
if (!ts.isClassLike(node)) {
if (!analyzer.typescript.isClassLike(node)) {
return false;
}
if (getTagName(node) !== undefined) {
if (getTagName(node, analyzer) !== undefined) {
return true;
}
const checker = analyzer.program.getTypeChecker();
Expand All @@ -66,9 +66,10 @@ export const isCustomElementSubclass = (
};

export const getTagName = (
node: ts.ClassDeclaration | ts.ClassExpression
node: ts.ClassDeclaration | ts.ClassExpression,
analyzer: AnalyzerInterface
): string | undefined => {
const jsdocTag = ts
const jsdocTag = analyzer.typescript
.getJSDocTags(node)
.find((tag) => tag.tagName.text.toLowerCase() === 'customelement');

Expand All @@ -82,20 +83,22 @@ export const getTagName = (
// `customElements.define('x-foo', XFoo);`
node.parent.forEachChild((child) => {
if (
ts.isExpressionStatement(child) &&
ts.isCallExpression(child.expression) &&
ts.isPropertyAccessExpression(child.expression.expression) &&
analyzer.typescript.isExpressionStatement(child) &&
analyzer.typescript.isCallExpression(child.expression) &&
analyzer.typescript.isPropertyAccessExpression(
child.expression.expression
) &&
child.expression.arguments.length >= 2
) {
const [tagNameArg, ctorArg] = child.expression.arguments;
const {expression, name} = child.expression.expression;
if (
ts.isIdentifier(expression) &&
analyzer.typescript.isIdentifier(expression) &&
expression.text === 'customElements' &&
ts.isIdentifier(name) &&
analyzer.typescript.isIdentifier(name) &&
name.text === 'define' &&
ts.isStringLiteralLike(tagNameArg) &&
ts.isIdentifier(ctorArg) &&
analyzer.typescript.isStringLiteralLike(tagNameArg) &&
analyzer.typescript.isIdentifier(ctorArg) &&
ctorArg.text === node.name?.text
) {
tagName = tagNameArg.text;
Expand Down Expand Up @@ -133,7 +136,7 @@ export const getJSDocData = (
const slots = new Map<string, NamedDescribed>();
const cssProperties = new Map<string, NamedDescribed>();
const cssParts = new Map<string, NamedDescribed>();
const jsDocTags = ts.getJSDocTags(node);
const jsDocTags = analyzer.typescript.getJSDocTags(node);
if (jsDocTags !== undefined) {
for (const tag of jsDocTags) {
switch (tag.tagName.text) {
Expand Down Expand Up @@ -170,7 +173,7 @@ export const getCustomElementDeclaration = (
analyzer: AnalyzerInterface
): CustomElementDeclaration => {
return new CustomElementDeclaration({
tagname: getTagName(node),
tagname: getTagName(node, analyzer),
name: node.name?.text ?? '',
node,
...getJSDocData(node, analyzer),
Expand Down
18 changes: 11 additions & 7 deletions packages/labs/analyzer/src/lib/lit-element/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const getLitElementDeclaration = (
analyzer: AnalyzerInterface
): LitElementDeclaration => {
return new LitElementDeclaration({
tagname: getTagName(analyzer.typescript, declaration),
tagname: getTagName(declaration, analyzer),
// TODO(kschaaf): support anonymous class expressions when assigned to a const
name: declaration.name?.text ?? '',
node: declaration,
Expand Down Expand Up @@ -127,19 +127,23 @@ export const isLitElementSubclass = (
* @returns
*/
export const getTagName = (
ts: TypeScript,
declaration: LitClassDeclaration
declaration: LitClassDeclaration,
analyzer: AnalyzerInterface
) => {
const customElementDecorator = ts
const customElementDecorator = analyzer.typescript
.getDecorators(declaration)
?.find((d): d is CustomElementDecorator => isCustomElementDecorator(ts, d));
?.find((d): d is CustomElementDecorator =>
isCustomElementDecorator(analyzer.typescript, d)
);
if (
customElementDecorator !== undefined &&
customElementDecorator.expression.arguments.length === 1 &&
ts.isStringLiteral(customElementDecorator.expression.arguments[0])
analyzer.typescript.isStringLiteral(
customElementDecorator.expression.arguments[0]
)
) {
// Get tag from decorator: `@customElement('x-foo')`
return customElementDecorator.expression.arguments[0].text;
}
return getCustomElementTagName(declaration);
return getCustomElementTagName(declaration, analyzer);
};