基础解析工具适配api_check工具

Signed-off-by: wangqing <wangqing136@huawei.com>
This commit is contained in:
wangqing 2024-04-09 14:53:44 +08:00
parent e1d1323718
commit 9d9816a1dd
3 changed files with 145 additions and 28 deletions

View File

@ -195,13 +195,14 @@ export class JsDocProcessorHelper {
* @param { Comment.CommentInfo } jsDoc JsDoc的信息解析的注释对象
* @returns JsDoc得到的JsDocInfo对象
*/
static processJsDoc(jsDoc: Comment.CommentInfo, parentKitInfo: string): Comment.JsDocInfo {
static processJsDoc(jsDoc: Comment.CommentInfo, parentKitInfo: string, parentIsFile: boolean): Comment.JsDocInfo {
const jsDocInfo: Comment.JsDocInfo = new Comment.JsDocInfo();
jsDocInfo.setDescription(jsDoc.description);
jsDocInfo.setKit(parentKitInfo);
jsDocInfo.setIsFile(parentIsFile);
for (let i = 0; i < jsDoc.commentTags.length; i++) {
const commentTag: Comment.CommentTag = jsDoc.commentTags[i];
jsDocInfo.addTag(commentTag);
jsDocInfo.setKit(parentKitInfo);
const jsDocProcessor = jsDocProcessorMap.get(commentTag.tag.toLowerCase());
if (!jsDocProcessor) {
continue;
@ -218,7 +219,7 @@ export class JsDocProcessorHelper {
* @param { ts.SourceFile } sourceFile node节点的sourceFile
* @returns JsDoc的信息数组
*/
static processJsDocInfos(node: ts.Node, apiType: string, parentKitInfo: string): Comment.JsDocInfo[] {
static processJsDocInfos(node: ts.Node, apiType: string, parentKitInfo: string, parentIsFile: boolean): Comment.JsDocInfo[] {
const sourceFile = node.getSourceFile();
const allCommentInfos: Comment.CommentInfo[] = CommentHelper.getNodeLeadingComments(node, sourceFile);
const commentInfos: Comment.CommentInfo[] = allCommentInfos.filter((commentInfo: Comment.CommentInfo) => {
@ -232,11 +233,12 @@ export class JsDocProcessorHelper {
if (commentInfos.length === 0 && parentKitInfo !== '') {
const jsDocInfo: Comment.JsDocInfo = new Comment.JsDocInfo();
jsDocInfo.setKit(parentKitInfo);
jsDocInfo.setIsFile(parentIsFile);
jsDocInfos.push(jsDocInfo);
}
for (let i = 0; i < commentInfos.length; i++) {
const commentInfo: Comment.CommentInfo = commentInfos[i];
const jsDocInfo: Comment.JsDocInfo = JsDocProcessorHelper.processJsDoc(commentInfo, parentKitInfo);
const jsDocInfo: Comment.JsDocInfo = JsDocProcessorHelper.processJsDoc(commentInfo, parentKitInfo, parentIsFile);
jsDocInfos.push(jsDocInfo);
}
return jsDocInfos;

View File

@ -46,6 +46,8 @@ import {
GenericInfo,
ParentClass,
ParserParam,
FileTag,
TypeParamInfo,
} from '../../typedef/parser/ApiInfoDefination';
import { Comment } from '../../typedef/parser/Comment';
import { StringUtils } from '../../utils/StringUtils';
@ -196,6 +198,7 @@ export class NodeProcessorHelper {
apiInfo.setIsJoinType(true);
} else {
apiInfo.setApiName(`${apiInfo.getApiName()}_${type.getText()}`);
apiInfo.setIsJoinType(true);
}
if (apiInfos.length === 0) {
apiInfos.push(apiInfo);
@ -305,7 +308,7 @@ export class NodeProcessorHelper {
//export * from 'test';
exportDeclareInfo.setApiName(
StringConstant.EXPORT +
(exportDeclarationNode.moduleSpecifier ? exportDeclarationNode.moduleSpecifier.getText() : '')
(exportDeclarationNode.moduleSpecifier ? exportDeclarationNode.moduleSpecifier.getText() : '')
);
} else if (ts.isNamespaceExport(exportClause)) {
//export * as myTest from 'test';
@ -615,8 +618,12 @@ export class NodeProcessorHelper {
methodInfo.setReturnValue(returnValues);
methodInfo.setReturnValueType(methodNode.type.kind);
if (Boolean(process.env.NEED_DETECTION)) {
NodeProcessorHelper.processFunctionTypeReference(methodNode.type, methodInfo, new ParamInfo(ApiType
.PARAM), false);
NodeProcessorHelper.processFunctionTypeReference(
methodNode.type,
methodInfo,
new ParamInfo(ApiType.PARAM),
false
);
}
}
for (let i = 0; i < methodNode.parameters.length; i++) {
@ -665,11 +672,16 @@ export class NodeProcessorHelper {
* @param { ts.TypeNode } typeNode
* @param { MethodInfo } methodInfo MethodInfo对象
* @param { ParamInfo } paramInfo ParamInfo对象
* @param { boolean } [isParam = true] type
* @param { boolean } [isParam = true] type
* true
* false
*/
static processFunctionTypeReference(typeNode: ts.TypeNode, methodInfo: MethodInfo, paramInfo: ParamInfo, isParam: boolean = true): void {
static processFunctionTypeReference(
typeNode: ts.TypeNode,
methodInfo: MethodInfo,
paramInfo: ParamInfo,
isParam: boolean = true
): void {
if (ts.isTypeLiteralNode(typeNode)) {
NodeProcessorHelper.processFunctionTypeObject(typeNode, methodInfo, paramInfo, isParam);
} else if (ts.isUnionTypeNode(typeNode)) {
@ -689,8 +701,13 @@ export class NodeProcessorHelper {
return;
}
const declaration: ts.Declaration = declarations[0];
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(declaration, ApiType.TYPE_ALIAS,
methodInfo.getKitInfoFromParent(methodInfo));
const fileTags: FileTag = methodInfo.getKitInfoFromParent(methodInfo);
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(
declaration,
ApiType.TYPE_ALIAS,
fileTags.kitInfo,
fileTags.isFile
);
if (jsDocInfos.length === 0) {
return;
}
@ -712,15 +729,24 @@ export class NodeProcessorHelper {
* @param {ts.TypeLiteralNode} typeObject
* @param {MethodInfo} methodInfo MethodInfo对象
* @param {ParamInfo} paramInfo ParamInfo对象
* @param { boolean } [isParam = true] type
* @param { boolean } [isParam = true] type
* true
* false
*/
static processFunctionTypeObject(typeObject: ts.TypeLiteralNode, methodInfo: MethodInfo, paramInfo: ParamInfo,
isParam: boolean = true): void {
static processFunctionTypeObject(
typeObject: ts.TypeLiteralNode,
methodInfo: MethodInfo,
paramInfo: ParamInfo,
isParam: boolean = true
): void {
const fileTags: FileTag = methodInfo.getKitInfoFromParent(methodInfo);
typeObject.members.forEach((member: ts.TypeElement) => {
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(member, ApiType.TYPE_ALIAS,
methodInfo.getKitInfoFromParent(methodInfo));
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(
member,
ApiType.TYPE_ALIAS,
fileTags.kitInfo,
fileTags.isFile
);
if (jsDocInfos.length === 0) {
return;
}
@ -804,6 +830,19 @@ export class NodeProcessorHelper {
if (typeName) {
typeAliasInfo.setTypeName(typeName);
}
let nodeType: ts.TypeNode = node.type;
if (ts.isFunctionTypeNode(nodeType)) {
const typeParameters = nodeType.parameters;
typeParameters.forEach((typeParameter: ts.ParameterDeclaration) => {
const typeParamInfo: TypeParamInfo = new TypeParamInfo();
typeParamInfo.setParamName(typeParameter.name.getText());
typeParamInfo.setParamType(typeParameter.type?.getText());
typeAliasInfo.setParamInfos(typeParamInfo);
});
typeAliasInfo.setReturnType(nodeType.type.getText());
typeAliasInfo.setTypeIsFunction(true);
}
typeAliasInfo.setDefinedText(node.getText());
ModifierHelper.processModifiers(node.modifiers, typeAliasInfo);
typeAliasInfo.addType(NodeProcessorHelper.processDataType(node.type));

View File

@ -231,7 +231,7 @@ export class BasicApiInfo {
}
}
export class ExportDefaultInfo extends BasicApiInfo { }
export class ExportDefaultInfo extends BasicApiInfo {}
export class ReferenceInfo extends BasicApiInfo {
pathName: string = '';
@ -287,24 +287,36 @@ export class ApiInfo extends BasicApiInfo {
constructor(apiType: string = '', node: ts.Node, parentApi: BasicApiInfo | undefined) {
super(apiType, node, parentApi);
let parentKitInfo = '';
let parentKitInfo: string = '';
let parentIsFile: boolean = false;
if (parentApi) {
parentKitInfo = this.getKitInfoFromParent(parentApi);
parentKitInfo = this.getKitInfoFromParent(parentApi).kitInfo;
parentIsFile = this.getKitInfoFromParent(parentApi).isFile;
}
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(node, apiType, parentKitInfo);
const jsDocText = node.getFullText().substring(0, node.getFullText().length - node.getText().length);
const jsDocInfos: Comment.JsDocInfo[] = JsDocProcessorHelper.processJsDocInfos(
node,
apiType,
parentKitInfo,
parentIsFile
);
const jsDocText: string = node
.getFullText()
.substring(0, node.getFullText().length - node.getText().length)
.trim();
this.setJsDocText(jsDocText);
this.addJsDocInfos(jsDocInfos);
}
getKitInfoFromParent(parentApi: BasicApiInfo): string {
getKitInfoFromParent(parentApi: BasicApiInfo): FileTag {
const parentApiInfo = parentApi as ApiInfo;
const jsDocInfos: Comment.JsDocInfo[] = parentApiInfo.getJsDocInfos();
let kitInfo: string = '';
let isFile: boolean = false;
jsDocInfos.forEach((jsDocInfo: Comment.JsDocInfo) => {
kitInfo = jsDocInfo.getKit();
isFile = jsDocInfo.getIsFile();
});
return kitInfo;
return { kitInfo, isFile };
}
getJsDocInfos(): Comment.JsDocInfo[] {
@ -515,6 +527,9 @@ export class ConstantInfo extends ApiInfo {
export class TypeAliasInfo extends ApiInfo {
type: string[] = []; // type定义的类型
typeName: TypeAliasType = '' as TypeAliasType; //type的类型
returnType: string = ''; //type类型为function时的返回值
paramInfos: TypeParamInfo[] = []; //type类型为function时的参数名和参数类型
typeIsFunction: boolean = false; //type类型是否为function
addType(type: string[]): void {
this.type.push(...type);
@ -532,6 +547,63 @@ export class TypeAliasInfo extends ApiInfo {
getTypeName(): string {
return this.typeName;
}
setReturnType(returnType: string): TypeAliasInfo {
this.returnType = returnType;
return this;
}
getReturnType() {
return this.returnType;
}
setParamInfos(paramInfo: TypeParamInfo) {
this.paramInfos.push(paramInfo);
}
getParamInfos(): TypeParamInfo[] {
return this.paramInfos;
}
setTypeIsFunction(typeIsFunction: boolean): TypeAliasInfo {
this.typeIsFunction = typeIsFunction;
return this;
}
getTypeIsFunction(): boolean {
return this.typeIsFunction;
}
}
/**
* type自定义类型为function时
*/
export class TypeParamInfo {
//type类型为function时的参数名
paramName: string = '';
//type类型为function时的参数类型
paramType: string = '';
setParamName(paramName: string): TypeParamInfo {
this.paramName = paramName;
return this;
}
getParamName(): string {
return this.paramName;
}
setParamType(paramType: string | undefined): TypeParamInfo {
if (!paramType) {
return this;
}
this.paramType = paramType;
return this;
}
getParamType(): string {
return this.paramType;
}
}
export class EnumValueInfo extends ApiInfo {
@ -744,9 +816,9 @@ export class ParserParam {
rootNames: string[] = [];
tsProgram: ts.Program = ts.createProgram({
rootNames: [],
options: {}
options: {},
});
constructor() { }
constructor() {}
getFileDir(): string {
return this.fileDir;
@ -801,10 +873,9 @@ export class ParserParam {
};
this.tsProgram = ts.createProgram({
rootNames: [...apiLibs, ...this.rootNames],
options: compilerOption
options: compilerOption,
});
};
}
}
export type ExportImportValue = { key: string; value: string };
@ -818,6 +889,11 @@ export interface ModifierProcessorInterface {
(propertyInfo: BasicApiInfo): void;
}
export interface FileTag {
kitInfo: string;
isFile: boolean;
}
/**
* ts中所有方法节点
*/