Skip to content

Commit

Permalink
fix(docs): prepare docs generator for mixin version of lit analyzer
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 581038415
  • Loading branch information
Elliott Marquez authored and copybara-github committed Nov 9, 2023
1 parent 710b87e commit 5e0000a
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions scripts/analyzer/analyze-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type {ReactiveProperty} from '@lit-labs/analyzer/lib/model.js';
import {
AbsolutePath,
Analyzer,
ClassDeclaration,
LitElementDeclaration,
LitElementExport,
Module,
Expand Down Expand Up @@ -96,21 +98,43 @@ export function analyzeElementApi(
) {
// The description of the module
const elementModule = analyzer.getModule(elementEntrypoint as AbsolutePath);
// The description of the custom element / superclass
const customElementModule =
elementModule.getCustomElementExports()[0] ||
(elementModule.getDeclaration(superClassName) as LitElementDeclaration);
let customElementModule: LitElementDeclaration | ClassDeclaration =
elementModule.getCustomElementExports()[0];

if (!customElementModule) {
const unknownSuperClassDeclaration =
elementModule.getDeclaration(superClassName);

// Type-cast declaration
if (
unknownSuperClassDeclaration.isLitElementDeclaration() ||
unknownSuperClassDeclaration.isClassDeclaration()
) {
customElementModule = unknownSuperClassDeclaration;
} else {
throw new Error(
`Unknown superclass declaration type for superclass or entrypoint: '${
superClassName || elementEntrypoint
}'`,
);
}
}

const {properties, reactiveProperties} = analyzeFields(
customElementModule,
elementModule,
);
const methods = analyzeMethods(customElementModule);
const events = analyzeEvents(customElementModule);
let events: MdEventInfo[] = [];
if (customElementModule.isLitElementDeclaration()) {
events = analyzeEvents(customElementModule);
}

const superclass = customElementModule.heritage.superClass;

const elementDocModule: MdModuleInfo = {
customElementName: customElementModule.tagname,
customElementName: (customElementModule as unknown as {tagname?: string})
.tagname,
className: customElementModule.name,
classPath: elementEntrypoint,
summary: makeMarkdownFriendly(customElementModule.summary),
Expand Down Expand Up @@ -159,7 +183,7 @@ const FIELDS_TO_IGNORE = new Set(['isListItem', 'isMenuItem']);
* LitElement class.
*/
export function analyzeFields(
classDeclaration: LitElementExport | LitElementDeclaration,
classDeclaration: LitElementExport | LitElementDeclaration | ClassDeclaration,
module: Module,
): {properties: MdPropertyInfo[]; reactiveProperties: MdPropertyInfo[]} {
const properties: MdPropertyInfo[] = [];
Expand All @@ -171,8 +195,11 @@ export function analyzeFields(
continue;
}

const reactiveProp = classDeclaration.reactiveProperties.get(field.name);
let defaultVal = field.default;
let reactiveProp: ReactiveProperty | null = null;
if (classDeclaration.isLitElementDeclaration()) {
reactiveProp = classDeclaration.reactiveProperties.get(field.name);
}

// Check the module and see if the default value is a variable declared in
// the same file.
Expand Down Expand Up @@ -266,7 +293,7 @@ const METHODS_TO_IGNORE = new Set([
* @returns The information about the methods of the LitElement class.
*/
export function analyzeMethods(
classDeclaration: LitElementExport | LitElementDeclaration,
classDeclaration: LitElementExport | LitElementDeclaration | ClassDeclaration,
) {
const methods: MdMethodInfo[] = [];
for (const method of classDeclaration.methods) {
Expand Down

0 comments on commit 5e0000a

Please sign in to comment.