Skip to content
Open
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
6 changes: 3 additions & 3 deletions package-lock.json

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

30 changes: 23 additions & 7 deletions src/analyze/flavors/lit-element/discover-members.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { GetAccessorDeclaration, Node, PropertyDeclaration, PropertySignature, ReturnStatement, SetAccessorDeclaration } from "typescript";
import {
Expression,
GetAccessorDeclaration,
Node,
PropertyDeclaration,
PropertySignature,
ReturnStatement,
SetAccessorDeclaration
} from "typescript";
import { ComponentMember } from "../../types/features/component-member";
import { LitElementPropertyConfig } from "../../types/features/lit-element-property-config";
import { getMemberVisibilityFromNode, getModifiersFromNode, getNodeSourceFileLang, hasModifier } from "../../util/ast-util";
Expand All @@ -23,13 +31,21 @@ export function discoverMembers(node: Node, context: AnalyzerDeclarationVisitCon
return undefined;
}

// static properties = { myProp: {type: String} }
if (ts.isPropertyDeclaration(node) && hasModifier(node, ts.SyntaxKind.StaticKeyword, ts)) {
const name = node.name.getText();
if (name === "properties" && node.initializer != null) {
return parseStaticProperties(node.initializer, context);
}
}

// static get properties() { return { myProp: {type: String} } }
if (ts.isGetAccessor(node) && hasModifier(node, ts.SyntaxKind.StaticKeyword, ts)) {
const name = node.name.getText();
if (name === "properties" && node.body != null) {
const returnStatement = node.body.statements.find<ReturnStatement>(ts.isReturnStatement.bind(ts));
if (returnStatement != null) {
return parseStaticProperties(returnStatement, context);
if (returnStatement != null && returnStatement.expression != null) {
return parseStaticProperties(returnStatement.expression, context);
}
}
}
Expand Down Expand Up @@ -155,17 +171,17 @@ function getLitAttributeName(propName: string, litConfig: LitElementPropertyConf
/**
* Visits static properties
* static get properties() { return { myProp: {type: String, attribute: "my-attr"} } }
* @param returnStatement
* @param expression
* @param context
*/
function parseStaticProperties(returnStatement: ReturnStatement, context: AnalyzerDeclarationVisitContext): ComponentMember[] {
function parseStaticProperties(expression: Expression, context: AnalyzerDeclarationVisitContext): ComponentMember[] {
const { ts } = context;

const memberResults: ComponentMember[] = [];

if (returnStatement.expression != null && ts.isObjectLiteralExpression(returnStatement.expression)) {
if (expression != null && ts.isObjectLiteralExpression(expression)) {
// Each property in the object literal expression corresponds to a class field.
for (const propNode of returnStatement.expression.properties) {
for (const propNode of expression.properties) {
// Get propName
const propName = propNode.name != null && ts.isIdentifier(propNode.name) ? propNode.name.text : undefined;
if (propName == null) {
Expand Down
50 changes: 50 additions & 0 deletions test/flavors/lit-element/member-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,56 @@ tsTest("LitElement: Discovers properties from 'static get properties'", t => {
);
});

tsTest("LitElement: Discovers properties from 'static properties = {}'", t => {
const {
results: [result],
checker
} = analyzeTextWithCurrentTsModule(`
/**
* @element
*/
class MyElement extends HTMLElement {
static properties = {
/**
* This is a comment
* @default hello 123
* @type {String}
*/
myProp: {type: String}
}
}
`);

const { members = [] } = result.componentDefinitions[0]?.declaration || {};

assertHasMembers(
members,
[
{
kind: "property",
propName: "myProp",
attrName: "myProp",
jsDoc: {
description: "This is a comment"
},
default: "hello 123",
typeHint: "String",
type: () => ({ kind: "STRING" }),
visibility: undefined,
reflect: "to-property",
deprecated: undefined,
required: undefined
},
{
kind: "property",
propName: "properties"
}
],
t,
checker
);
});

tsTest("LitElement: Discovers properties from '@property'", t => {
const {
results: [result],
Expand Down