-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs-infra] Change CSS vars generation to be extracted from Enum (#4…
- Loading branch information
Showing
2 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Symbol, isPropertySignature, isEnumDeclaration, forEachChild, Node } from 'typescript'; | ||
import { TypeScriptProject } from './createTypeScriptProject'; | ||
import { ParsedProperty } from '../types/ApiBuilder.types'; | ||
import { getSymbolDescription, getSymbolJSDocTags, stringifySymbol } from '../buildApiUtils'; | ||
|
||
const parseProperty = async ( | ||
propertySymbol: Symbol, | ||
project: TypeScriptProject, | ||
name: string, | ||
): Promise<ParsedProperty> => ({ | ||
name, | ||
description: getSymbolDescription(propertySymbol, project), | ||
tags: getSymbolJSDocTags(propertySymbol), | ||
required: !propertySymbol.declarations?.find(isPropertySignature)?.questionToken, | ||
typeStr: await stringifySymbol(propertySymbol, project), | ||
}); | ||
|
||
const extractInfoFromEnum = async ( | ||
typeName: string, | ||
sourceFileNamePattern: RegExp, | ||
project: TypeScriptProject, | ||
): Promise<ParsedProperty[]> => { | ||
// Generate the params | ||
let result: ParsedProperty[] = []; | ||
|
||
try { | ||
const cssVarDeclarationCandidates = project.program | ||
.getSourceFiles() | ||
.filter((file) => sourceFileNamePattern.test(file.fileName)); | ||
|
||
let enumSymbol: Symbol | null = null; | ||
cssVarDeclarationCandidates.forEach((file) => { | ||
forEachChild(file, (node: Node) => { | ||
if (isEnumDeclaration(node) && node.name.getText() === typeName) { | ||
enumSymbol = project.checker.getSymbolAtLocation(node.name)!; | ||
} | ||
}); | ||
}); | ||
|
||
if (!enumSymbol) { | ||
return []; | ||
} | ||
|
||
const type = project.checker.getDeclaredTypeOfSymbol(enumSymbol!); | ||
|
||
// @ts-ignore | ||
const typeDeclaration = type?.types ?? [type]; | ||
if (!typeDeclaration) { | ||
return []; | ||
} | ||
const properties: Record<string, ParsedProperty> = {}; | ||
|
||
// @ts-ignore | ||
await Promise.all( | ||
typeDeclaration.map(async (t: any) => { | ||
const propertySymbol = t.symbol; | ||
properties[t.value] = await parseProperty(propertySymbol, project, t.value); | ||
}), | ||
); | ||
|
||
result = Object.values(properties) | ||
.filter((property) => !property.tags.ignore) | ||
.sort((a, b) => a.name.localeCompare(b.name)); | ||
} catch { | ||
console.error(`No declaration for ${typeName}`); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export default extractInfoFromEnum; |