Skip to content

Commit

Permalink
some slightly clumsy stuff for better typechecking
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed Jun 6, 2023
1 parent 934373d commit 7072ad7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/compiler/docs/generate-doc-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ const getUserReadmeContent = async (compilerCtx: d.CompilerCtx, readmePath: stri
* @param jsdoc the JSDoc associated with the component's declaration
* @returns the generated documentation
*/
const generateDocs = (readme: string, jsdoc: d.CompilerJsDoc): string => {
const generateDocs = (readme: string | undefined, jsdoc: d.CompilerJsDoc): string => {
const docs = jsdoc.text;
if (docs !== '' || !readme) {
// just return the existing docs if they exist. these would have been captured earlier in the compilation process.
Expand Down
25 changes: 21 additions & 4 deletions src/compiler/transformers/type-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,25 @@ export function addFileToLibrary(filePath: string): void {

const importPath = node.moduleSpecifier.text;
const module = ts.resolveModuleName(importPath, sourceFile.fileName, options, compilerHost);
const exportHomeModule = program.getSourceFile(module.resolvedModule.resolvedFileName);
const resolvedFileName = module?.resolvedModule?.resolvedFileName;

if (!resolvedFileName) {
return;
}
const exportHomeModule = program.getSourceFile(resolvedFileName);
if (!exportHomeModule) {
return;
}

// if there are named exports (like `export { Pie, Cake } from './dessert'`)
// we get each export specifier (`Pie`, `Cake`), use the typechecker
// to get it's type, figure out the name, and so on.
if (node.exportClause && ts.isNamedExports(node.exportClause)) {
for (const exportSpecifier of node.exportClause.elements) {
const identifier = exportSpecifier.getChildAt(0);
if (!identifier) {
return;
}
// if this symbol is being aliased like
//
// ```ts
Expand All @@ -120,7 +131,11 @@ export function addFileToLibrary(filePath: string): void {
//
// this will give us 'Best' as a symbol, letting us look that name up
// in the source module, below
const unaliasedSymbol = unalias(checker.getSymbolAtLocation(identifier), checker);
const possiblyAliasedSymbol = checker.getSymbolAtLocation(identifier);
if (!possiblyAliasedSymbol) {
return;
}
const unaliasedSymbol = unalias(possiblyAliasedSymbol, checker);
const name = unaliasedSymbol.getName();

ts.forEachChild(exportHomeModule, (child) => {
Expand Down Expand Up @@ -216,8 +231,10 @@ function isNotPrivate(node: TypeDeclLike): boolean {
function getTypeDeclaration(checker: ts.TypeChecker, type: ts.Type): string {
const maybeSymbol = getSymbolForType(type);

if (maybeSymbol) {
return maybeSymbol?.declarations?.[0]?.getText();
const declaration = maybeSymbol?.declarations?.[0];

if (declaration) {
return declaration.getText();
} else {
// in the case that we couldn't resolve the declaration, `typeToString`
// provides a reasonable fallback
Expand Down
2 changes: 1 addition & 1 deletion src/declarations/stencil-public-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export interface JsonDocsProp {
reflectToAttr: boolean;
docs: string;
docsTags: JsonDocsTag[];
default: string;
default?: string;
deprecation?: string;
values: JsonDocsValue[];
optional: boolean;
Expand Down

0 comments on commit 7072ad7

Please sign in to comment.