Skip to content

Commit

Permalink
account for property assignment in body
Browse files Browse the repository at this point in the history
  • Loading branch information
KonnorRogers committed May 6, 2024
1 parent bf49c38 commit 00f0f5c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createClass(node, moduleDoc, context) {
*/
if (isProperty(member)) {
if (member?.name?.getText() === 'observedAttributes') {
/**
/**
* @example static observedAttributes
*/
if (ts.isPropertyDeclaration(member)) {
Expand Down Expand Up @@ -64,7 +64,7 @@ export function createClass(node, moduleDoc, context) {

/**
* Second pass through a class's members.
* We do this in two passes, because we need to know whether or not a class has any
* We do this in two passes, because we need to know whether or not a class has any
* attributes, so we handle those first.
*/
node?.members?.forEach(member => {
Expand Down Expand Up @@ -124,7 +124,7 @@ export function createClass(node, moduleDoc, context) {
/**
* A class can have a static prop and an instance prop with the same name,
* both should be output in the CEM
*
*
* If not a static prop, we merge getter and setter pairs here
*/
if (field?.static) {
Expand All @@ -144,7 +144,7 @@ export function createClass(node, moduleDoc, context) {

/**
* Handle events
*
*
* In order to find `this.dispatchEvent` calls, we have to traverse a method's AST
*/
if (ts.isMethodDeclaration(member)) {
Expand Down Expand Up @@ -210,7 +210,7 @@ export function getDefaultValuesFromConstructorVisitor(source, classTemplate, co
function visitNode(node) {
switch (node.kind) {
case ts.SyntaxKind.Constructor:
/**
/**
* For every member that was added in the classDoc, we want to add a default value if we can
* To do this, we visit a class's constructor, and loop through the statements
*/
Expand Down Expand Up @@ -274,5 +274,27 @@ function mapClassMember(source, classTemplate, context, node, statement, express
...resolveModuleOrPackageSpecifier({ path: source.getSourceFile().fileName }, context, expression?.right?.getText()),
}
}

if (hasAttrAnnotation(statement)) {
const field = existingMember
let attribute = createAttributeFromField(field);
attribute = handleAttrJsDoc(existingMember, attribute);

field.attribute = attribute.name;

/**
* If the attribute already exists, merge it together with the extra
* information we got from the field (like type, summary, description, etc)
*/
let attrAlreadyExists = classTemplate.attributes.find(attr => attr.name === attribute.name);

if (attrAlreadyExists) {
classTemplate.attributes = classTemplate.attributes.map(attr => {
return attr.name === attribute.name ? { ...attrAlreadyExists, ...attribute } : attr;
});
} else {
classTemplate.attributes.push(attribute);
}
}
}
}
}
7 changes: 4 additions & 3 deletions packages/analyzer/src/utils/ast-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export const isCustomElementsDefineCall = node => (node?.expression?.getText() =

/**
* @example @attr
* @example @attribute
*/
export function hasAttrAnnotation(member) {
return member?.jsDoc?.some(jsDoc => jsDoc?.tags?.some(tag => safe(() => tag?.tagName?.getText()) === 'attr'));
return member?.jsDoc?.some(jsDoc => jsDoc?.tags?.some(tag => safe(() => ["attribute", "attr"].includes(tag?.tagName?.getText()))));
}


Expand Down Expand Up @@ -92,8 +93,8 @@ export const getOptionsObject = decorator => decorator?.expression?.arguments?.f
* Get the return value expression of a return statement, omitting the type assertion
*/
export const getReturnValue = returnStatement => {
let value = returnStatement.expression?.kind === ts.SyntaxKind.AsExpression
? returnStatement.expression.expression.getText()
let value = returnStatement.expression?.kind === ts.SyntaxKind.AsExpression
? returnStatement.expression.expression.getText()
: returnStatement.expression?.getText()

return value?.split?.(' ')?.[0];
Expand Down

0 comments on commit 00f0f5c

Please sign in to comment.