From 91995492b0dc8cfd0168ddf091fa651e0fb12955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=9B=B9=E5=AE=87?= Date: Thu, 10 Jul 2025 17:37:32 +0800 Subject: [PATCH] change node use default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王曹宇 --- BUILD.gn | 25 +--- .../src/arkui_config_util.ts | 85 +++++------ .../arkui_transformer/src/component_file.ts | 16 +-- .../src/interface_converter.ts | 132 +++++++++--------- 4 files changed, 123 insertions(+), 135 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index d5d74aba31..1ebada10b2 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -26,13 +26,6 @@ action("ohos_base_split") { script = "//build/ohos/sdk/parse_interface_sdk.py" ohos_sdk_arkts_description_file = "${interface_sdk_path}/ohos_sdk_pub_description_std.json" - if (host_os == "mac") { - node_path = "//prebuilts/build-tools/common/nodejs/node-v16.20.2-darwin-x64/bin/node" - npm_path = "//prebuilts/build-tools/common/nodejs/node-v16.20.2-darwin-x64/bin/npm" - } else { - node_path = "//prebuilts/build-tools/common/nodejs/node-v16.20.2-linux-x64/bin/node" - npm_path = "//prebuilts/build-tools/common/nodejs/node-v16.20.2-linux-x64/bin/npm" - } args = [ "--sdk-description-file", rebase_path("//build/ohos/sdk/ohos_sdk_description_std.json", @@ -40,7 +33,7 @@ action("ohos_base_split") { "--root-build-dir", rebase_path("//", root_build_dir), "--node-js", - rebase_path(node_path, root_build_dir), + rebase_path(nodejs, root_build_dir), "--output-arkts-sdk-desc-file", rebase_path(ohos_sdk_arkts_description_file, root_build_dir), "--sdk-build-public", @@ -48,7 +41,7 @@ action("ohos_base_split") { "--sdk-build-arkts", "${sdk_build_arkts}", "--npm-path", - rebase_path(npm_path, root_build_dir), + rebase_path(npm, root_build_dir), "--output-interface-sdk", rebase_path(interface_sdk_path, root_build_dir), ] @@ -323,16 +316,6 @@ action("ohos_ets_api_arkui_tmp") { deps = [ ":ohos_ets_api_tmp" ] script = "//interface/sdk-js/arkui_transformer.py" - if (host_os == "mac") { - node_path = "//prebuilts/build-tools/common/nodejs/node-v16.20.2-darwin-x64/bin/node" - npm_path = - "//prebuilts/build-tools/common/nodejs/node-v16.20.2-darwin-x64/bin/npm" - } else { - node_path = - "//prebuilts/build-tools/common/nodejs/node-v16.20.2-linux-x64/bin/node" - npm_path = - "//prebuilts/build-tools/common/nodejs/node-v16.20.2-linux-x64/bin/npm" - } args = [ "--input", rebase_path(ohos_ets_api_tmp_path + "/@internal/component/ets", @@ -342,9 +325,9 @@ action("ohos_ets_api_arkui_tmp") { "--source_root_dir", rebase_path("//", root_build_dir), "--npm-path", - rebase_path(npm_path, root_build_dir), + rebase_path(npm, root_build_dir), "--node-js", - rebase_path(node_path, root_build_dir), + rebase_path(nodejs, root_build_dir), ] outputs = [ "$ohos_ets_api_arkui_tmp_path" ] } diff --git a/build-tools/arkui_transformer/src/arkui_config_util.ts b/build-tools/arkui_transformer/src/arkui_config_util.ts index 5fe33269f8..c276e701b3 100644 --- a/build-tools/arkui_transformer/src/arkui_config_util.ts +++ b/build-tools/arkui_transformer/src/arkui_config_util.ts @@ -13,66 +13,71 @@ * limitations under the License. */ -import { OptionValues } from "commander" -import * as fs from "fs" +import { OptionValues } from 'commander'; +import * as fs from 'fs'; -import path from "path" +import path from 'path'; export interface ArkUIConfig { - components: Array + components: Array; } interface NoneUIConfig { - files: Array + files: Array; } export class ArkUIConfigUtil { - static instance: ArkUIConfigUtil = new ArkUIConfigUtil + static instance: ArkUIConfigUtil = new ArkUIConfigUtil(); constructor() { - this.config = JSON.parse(fs.readFileSync("./config/arkui_config.json", 'utf-8')) + this.config = JSON.parse(fs.readFileSync('./config/arkui_config.json', 'utf-8')); this.config.components.forEach(c => { - this.componentSet.add(c) - }) - this.noneUIconfig = JSON.parse(fs.readFileSync("./config/none_arkui_files.json", 'utf-8')) + this.componentSet.add(c); + }); + this.noneUIconfig = JSON.parse(fs.readFileSync('./config/none_arkui_files.json', 'utf-8')); this.noneUIconfig.files.forEach(f => { - this.noneUIFielSet.add(f) + this.noneUIFielSet.add(f); }) } // ui components - private config: ArkUIConfig + private config: ArkUIConfig; // None ui files - private noneUIconfig: NoneUIConfig - private noneUIFielSet: Set = new Set + private noneUIconfig: NoneUIConfig; + private noneUIFielSet: Set = new Set(); // Full set of component, should be manually mantained by config file - private componentSet: Set = new Set + private componentSet: Set = new Set(); // Component superclass set, generated by traversing the declartion AST - private componentSuperclassSet: Set = new Set + private componentSuperclassSet: Set = new Set(); // All class/interface name related to component attribute heritage - private componentHeritage: Set = new Set + private componentHeritage: Set = new Set(); // All implement relationship of component heritage - private componentHerirageRelation: Map = new Map + private componentHerirageRelation: Map = new Map(); // All component filename - private componentFiles: Set = new Set - private file2Attrbiute: Map = new Map - private shouldNotHaveAttributeModifier: Set = new Set - private _useMemoM3: boolean = false + private componentFiles: Set = new Set(); + private file2Attrbiute: Map = new Map(); + private shouldNotHaveAttributeModifier: Set = new Set(); + private _useMemoM3: boolean = false; get useMemoM3(): boolean { - return this._useMemoM3 + return this._useMemoM3; } public loadConfig(config: OptionValues): void { if (config.useMemoM3) { - this._useMemoM3 = true + this._useMemoM3 = true; } } private getPureName(name: string): string { - return path.basename(name).replaceAll(".d.ts", "").replaceAll(".d.ets", "").replaceAll("_","").toLowerCase() + return path + .basename(name) + .replace(/\.d\.e?ts/g, '') + .replace(/_/g, '') + .toLowerCase(); } public isRelatedToComponent(name: string): boolean { - return this.componentSet.has(name) || this.componentSuperclassSet.has(name) + return this.componentSet.has(name) || this.componentSuperclassSet.has(name); } public isComponent(name: string, subfix: string): boolean { - return this.componentSet.has(name.replaceAll(subfix, "")) + const regSubfix = new RegExp(subfix, 'g'); + return this.componentSet.has(name.replace(regSubfix, '')); } public addComponentSuperclass(name: string): void { if (!this.isComponent(name, 'Attribute')) { @@ -80,36 +85,36 @@ export class ArkUIConfigUtil { } } public addComponentAttributeHeritage(name: string[]): void { - let prev: string | undefined = undefined + let prev: string | undefined = undefined; name.forEach(n => { if (prev) { - this.componentHerirageRelation.set(prev, n) + this.componentHerirageRelation.set(prev, n); } - prev = n - this.componentHeritage.add(n) - }) + prev = n; + this.componentHeritage.add(n); + }); if (name.length == 1) { - this.shouldNotHaveAttributeModifier.add(name[0]) + this.shouldNotHaveAttributeModifier.add(name[0]); } } public getComponentSuperclass(name: string): string | undefined { - return this.componentHerirageRelation.get(name) + return this.componentHerirageRelation.get(name); } public isUIHeritage(name: string): boolean { - return this.componentHeritage.has(name) + return this.componentHeritage.has(name); } public notUIFile(name: string): boolean { - return this.noneUIFielSet.has(this.getPureName(name)) + return this.noneUIFielSet.has(this.getPureName(name)); } public addComponentFile(name: string): void { - this.componentFiles.add(this.getPureName(name)) + this.componentFiles.add(this.getPureName(name)); } public isComponentFile(name: string): boolean { - return this.componentFiles.has(this.getPureName(name)) + return this.componentFiles.has(this.getPureName(name)); } public shouldHaveAttributeModifier(name: string): boolean { - return !this.shouldNotHaveAttributeModifier.has(name) && this.isComponent(name, 'Attribute') + return !this.shouldNotHaveAttributeModifier.has(name) && this.isComponent(name, 'Attribute'); } } -export default ArkUIConfigUtil.instance +export default ArkUIConfigUtil.instance; diff --git a/build-tools/arkui_transformer/src/component_file.ts b/build-tools/arkui_transformer/src/component_file.ts index a1a9c54750..c0dc192399 100644 --- a/build-tools/arkui_transformer/src/component_file.ts +++ b/build-tools/arkui_transformer/src/component_file.ts @@ -17,8 +17,8 @@ import * as ts from 'typescript'; import * as path from 'path'; export class ComponentFile { - public componentName: string - public outFileName: string + public componentName: string; + public outFileName: string; static snake2Camel(name: string, low: boolean = false): string { if (!name.includes('_')) { @@ -37,15 +37,15 @@ export class ComponentFile { } public appendAttribute(str: string) { - this.attributeSource.push(str) + this.attributeSource.push(str); } public appendFunction(str: string) { - this.functionSource = str + this.functionSource = str; } get concactSource() { - return [...this.attributeSource, this.functionSource].join('\n') + return [...this.attributeSource, this.functionSource].join('\n'); } constructor( @@ -54,8 +54,8 @@ export class ComponentFile { public attributeSource: string[] = [], public functionSource: string = '', ) { - const pureName = path.basename(this.fileName).replaceAll(".d.ts", "").replaceAll(".d.ets", ""); - this.componentName = ComponentFile.snake2Camel(pureName) - this.outFileName = ComponentFile.snake2Camel(pureName, true).concat(".d.ets") + const pureName = path.basename(this.fileName).replace(/\.d\.e?ts/g, ""); + this.componentName = ComponentFile.snake2Camel(pureName); + this.outFileName = ComponentFile.snake2Camel(pureName, true).concat(".d.ets"); } } \ No newline at end of file diff --git a/build-tools/arkui_transformer/src/interface_converter.ts b/build-tools/arkui_transformer/src/interface_converter.ts index 9d72ecfee4..34a3842e5d 100644 --- a/build-tools/arkui_transformer/src/interface_converter.ts +++ b/build-tools/arkui_transformer/src/interface_converter.ts @@ -17,12 +17,12 @@ import * as ts from 'typescript'; import * as fs from 'fs'; import * as path from 'path'; import { assert } from 'console'; -import uiconfig from './arkui_config_util' +import uiconfig from './arkui_config_util'; import { ComponentFile } from './component_file'; -import { analyzeBaseClasses, isComponentHerirage, getBaseClassName, removeDuplicateMethods, mergeUniqueOrdered } from './lib/attribute_utils' +import { analyzeBaseClasses, isComponentHerirage, getBaseClassName, removeDuplicateMethods, mergeUniqueOrdered } from './lib/attribute_utils'; function readLangTemplate(): string { - return fs.readFileSync('./pattern/arkts_component_decl.pattern', 'utf8') + return fs.readFileSync('./pattern/arkts_component_decl.pattern', 'utf8'); } function extractSignatureComment( @@ -34,19 +34,19 @@ function extractSignatureComment( const commentText = sourceFile.text - .slice(jsDoc.getStart(sourceFile), jsDoc.getEnd()) + .slice(jsDoc.getStart(sourceFile), jsDoc.getEnd()); return commentText.split('\n').map((l, index) => { if (index == 0) { return l.trimStart(); } - return ' ' + l.trimStart() - }).join('\n') + return ' ' + l.trimStart(); + }).join('\n'); } interface ComponnetFunctionInfo { sig: string[], - comment: string + comment: string; } interface ComponentPram { @@ -57,13 +57,13 @@ interface ComponentPram { function getAllInterfaceCallSignature(node: ts.InterfaceDeclaration, originalCode: ts.SourceFile, mergeCallSig: boolean = false): Array { const signatureParams: Array = []; - const comments: string[] = [] - const paramList: Array = [] + const comments: string[] = []; + const paramList: Array = []; node.members.forEach(member => { if (ts.isCallSignatureDeclaration(member)) { const currentSignature: string[] = []; - const currentParam: ComponentPram[] = [] + const currentParam: ComponentPram[] = []; const comment = extractSignatureComment(member, originalCode); comments.push(comment); @@ -71,38 +71,38 @@ function getAllInterfaceCallSignature(node: ts.InterfaceDeclaration, originalCod currentSignature.push(param.getText(originalCode)); currentParam.push({ name: (param.name as ts.Identifier).escapedText as string, type: [param.type!.getText(originalCode)], isOptional: !!param.questionToken }); }); - signatureParams.push(currentSignature) - paramList.push(currentParam) + signatureParams.push(currentSignature); + paramList.push(currentParam); } }); - const result: Array = new Array + const result: Array = new Array; if (mergeCallSig) { - const mergedParamList: Array = [] + const mergedParamList: Array = []; paramList.forEach((params, _) => { params.forEach((param, index) => { if (!mergedParamList[index]) { - mergedParamList.push(param) + mergedParamList.push(param); if (index > 0) { (mergedParamList[index] as ComponentPram).isOptional = true; } } else { - mergedParamList[index] = { name: param.name, type: mergeUniqueOrdered(mergedParamList[index].type, param.type), isOptional: mergedParamList[index].isOptional || param.isOptional } + mergedParamList[index] = { name: param.name, type: mergeUniqueOrdered(mergedParamList[index].type, param.type), isOptional: mergedParamList[index].isOptional || param.isOptional }; } - }) - }) + }); + }); const mergedSignature: string[] = []; mergedParamList.forEach((param, index) => { - mergedSignature.push(`${param.name}${param.isOptional ? '?' : ''}: ${param.type.join(' | ')}`) - }) + mergedSignature.push(`${param.name}${param.isOptional ? '?' : ''}: ${param.type.join(' | ')}`); + }); result.push({ sig: mergedSignature, comment: '' - }) + }); } else { for (let i = 0; i < signatureParams.length; i++) { - result.push({ sig: signatureParams[i], comment: comments[i] }) + result.push({ sig: signatureParams[i], comment: comments[i] }); } } return result; @@ -110,15 +110,15 @@ function getAllInterfaceCallSignature(node: ts.InterfaceDeclaration, originalCod function handleComponentInterface(node: ts.InterfaceDeclaration, file: ComponentFile) { const result = getAllInterfaceCallSignature(node, file.sourceFile, !uiconfig.useMemoM3); - const declPattern = readLangTemplate() - const declComponentFunction: string[] = [] + const declPattern = readLangTemplate(); + const declComponentFunction: string[] = []; result.forEach(p => { declComponentFunction.push(declPattern - .replaceAll("%COMPONENT_NAME%", file.componentName) - .replaceAll("%FUNCTION_PARAMETERS%", p.sig?.map(it => `${it}, `).join("") ?? "") - .replaceAll("%COMPONENT_COMMENT%", p.comment)) + .replace(/%COMPONENT_NAME%/g, file.componentName) + .replace(/%FUNCTION_PARAMETERS%/g, p.sig?.map(it => `${it}, `).join("") ?? "") + .replace(/%COMPONENT_COMMENT%/g, p.comment)); }) - return declComponentFunction.join('\n') + return declComponentFunction.join('\n'); } function updateMethodDoc(node: ts.MethodDeclaration): ts.MethodDeclaration { @@ -127,7 +127,7 @@ function updateMethodDoc(node: ts.MethodDeclaration): ts.MethodDeclaration { const paramNameType: Map = new Map(); node.parameters.forEach(param => { paramNameType.set((param.name as ts.Identifier).escapedText!, param.type!); - }) + }); const jsDoc = node.jsDoc as ts.JSDoc[]; const updatedJsDoc = jsDoc.map((doc) => { const updatedTags = (doc.tags || []).map((tag: ts.JSDocTag) => { @@ -137,10 +137,10 @@ function updateMethodDoc(node: ts.MethodDeclaration): ts.MethodDeclaration { tag.tagName, ts.factory.createJSDocTypeExpression(returnType), tag.comment - ) + ); } if (tag.tagName.escapedText === 'param') { - const paramTag = tag as ts.JSDocParameterTag + const paramTag = tag as ts.JSDocParameterTag; return ts.factory.updateJSDocParameterTag( paramTag, paramTag.tagName, @@ -149,15 +149,15 @@ function updateMethodDoc(node: ts.MethodDeclaration): ts.MethodDeclaration { ts.factory.createJSDocTypeExpression(paramNameType.get((paramTag.name as ts.Identifier).escapedText!)!), paramTag.isNameFirst, paramTag.comment - ) + ); } - return tag - }) + return tag; + }); return ts.factory.updateJSDocComment(doc, doc.comment, updatedTags); }); - (node as any).jsDoc = updatedJsDoc + (node as any).jsDoc = updatedJsDoc; } - return node + return node; } function handleOptionalType(paramType: ts.TypeNode, wrapUndefined: boolean = true): ts.TypeNode { @@ -174,7 +174,7 @@ function handleOptionalType(paramType: ts.TypeNode, wrapUndefined: boolean = tru ...(ts.isUnionTypeNode(type) ? type.types : [type]), ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword), ]); - } + }; // Check if the parameter type is Optional if (typeName === 'Optional' && paramType.typeArguments?.length === 1) { @@ -203,7 +203,7 @@ function handleAttributeMember(node: ts.MethodDeclaration): ts.MethodSignature { } else if (ts.isUnionTypeNode(paramType)) { const removeOptionalTypes = paramType.types.map(type => { return handleOptionalType(type, false); - }) + }); // Check if the union type already includes undefined const hasUndefined = removeOptionalTypes.some( type => type.kind === ts.SyntaxKind.UndefinedKeyword @@ -258,7 +258,7 @@ function handleAttributeMember(node: ts.MethodDeclaration): ts.MethodSignature { returnType ); - return methodSignature + return methodSignature; } function handleHeritageClause(node: ts.NodeArray | undefined): ts.HeritageClause[] { @@ -282,7 +282,7 @@ function handleHeritageClause(node: ts.NodeArray | undefined) const newClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, types); heritageClauses.push(newClause); }); - return heritageClauses + return heritageClauses; } function handleAttributeModifier(node: ts.ClassDeclaration, members: ts.MethodSignature[]) { @@ -291,8 +291,8 @@ function handleAttributeModifier(node: ts.ClassDeclaration, members: ts.MethodSi if ((m.name as ts.Identifier).escapedText === 'attributeModifier') { members.splice(members.indexOf(m), 1); } - }) - return + }); + return; } members.push( ts.factory.createMethodSignature( @@ -326,7 +326,7 @@ function handleAttributeModifier(node: ts.ClassDeclaration, members: ts.MethodSi )], ts.factory.createThisTypeNode() ) - ) + ); } function transformComponentAttribute(node: ts.ClassDeclaration): ts.Node[] { @@ -337,16 +337,16 @@ function transformComponentAttribute(node: ts.ClassDeclaration): ts.Node[] { return handleAttributeMember(member); }).filter((member): member is ts.MethodSignature => member !== undefined); - const filetredMethos = removeDuplicateMethods(members) + const filetredMethos = removeDuplicateMethods(members); if (uiconfig.shouldHaveAttributeModifier(node.name!.escapedText as string)) { - handleAttributeModifier(node, filetredMethos) + handleAttributeModifier(node, filetredMethos); } const exportModifier = ts.factory.createModifier(ts.SyntaxKind.ExportKeyword); const delcareModifier = ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword); - const heritageClauses = handleHeritageClause(node.heritageClauses) + const heritageClauses = handleHeritageClause(node.heritageClauses); const noneUIAttribute = ts.factory.createInterfaceDeclaration( [exportModifier, delcareModifier], @@ -355,7 +355,7 @@ function transformComponentAttribute(node: ts.ClassDeclaration): ts.Node[] { heritageClauses, filetredMethos ); - return [noneUIAttribute] + return [noneUIAttribute]; } function getLeadingSpace(line: string): string { @@ -385,10 +385,10 @@ function addAttributeMemo(node: ts.ClassDeclaration, componentFile: ComponentFil const functionSet: Set = new Set(); node.members.forEach(m => { - functionSet.add((m.name! as ts.Identifier).escapedText!) - }) + functionSet.add((m.name! as ts.Identifier).escapedText!); + }); - const updatedCode: string[] = [] + const updatedCode: string[] = []; originalCode.forEach(l => { const name = extractMethodName(l); if (!name) { @@ -396,53 +396,53 @@ function addAttributeMemo(node: ts.ClassDeclaration, componentFile: ComponentFil return; } if (functionSet.has(name)) { - updatedCode.push(getLeadingSpace(l) + "@memo") + updatedCode.push(getLeadingSpace(l) + "@memo"); } updatedCode.push(l); - }) - const attributeName = node.name!.escapedText! - const superInterface = getBaseClassName(node) + }); + const attributeName = node.name!.escapedText!; + const superInterface = getBaseClassName(node); componentFile.appendAttribute(updatedCode.join('\n') .replace(`export declare interface ${attributeName}`, `export declare interface UI${attributeName}`) .replace(`extends ${superInterface}`, `extends UI${superInterface}`) - ) + ); } function isComponentAttribute(node: ts.Node) { if (!(ts.isClassDeclaration(node) && node.name?.escapedText)) { return false; } - return uiconfig.isComponent(node.name.escapedText, 'Attribute') + return uiconfig.isComponent(node.name.escapedText, 'Attribute'); } function isComponentInterface(node: ts.Node) { if (!(ts.isInterfaceDeclaration(node) && node.name?.escapedText)) { return false; } - return uiconfig.isComponent(node.name.escapedText, 'Interface') + return uiconfig.isComponent(node.name.escapedText, 'Interface'); } export function addMemoTransformer(componentFile: ComponentFile): ts.TransformerFactory { return (context) => { const visit: ts.Visitor = (node) => { if (isComponentHerirage(node)) { - addAttributeMemo(node as ts.ClassDeclaration, componentFile) + addAttributeMemo(node as ts.ClassDeclaration, componentFile); } return ts.visitEachChild(node, visit, context); - } - return (sourceFile) => { componentFile.sourceFile = sourceFile; return ts.visitNode(sourceFile, visit) }; - } + }; + return (sourceFile) => { componentFile.sourceFile = sourceFile; return ts.visitNode(sourceFile, visit); }; + }; } export function interfaceTransformer(program: ts.Program, componentFile: ComponentFile): ts.TransformerFactory { return (context) => { const visit: ts.Visitor = (node) => { if (isComponentInterface(node)) { - componentFile.appendFunction(handleComponentInterface(node as ts.InterfaceDeclaration, componentFile)) + componentFile.appendFunction(handleComponentInterface(node as ts.InterfaceDeclaration, componentFile)); return undefined; } if (isComponentHerirage(node)) { - return transformComponentAttribute(node as ts.ClassDeclaration) + return transformComponentAttribute(node as ts.ClassDeclaration); } return ts.visitEachChild(node, visit, context); }; @@ -455,10 +455,10 @@ export function componentInterfaceCollector(program: ts.Program, componentFile: return (context) => { const visit: ts.Visitor = (node) => { if (isComponentAttribute(node)) { - const attributeName = (node as ts.ClassDeclaration).name!.escapedText as string - componentFile.componentName = attributeName.replaceAll('Attribute', ''); + const attributeName = (node as ts.ClassDeclaration).name!.escapedText as string; + componentFile.componentName = attributeName.replace(/Attribute/g, ''); const baseTypes = analyzeBaseClasses(node as ts.ClassDeclaration, componentFile.sourceFile, program); - uiconfig.addComponentAttributeHeritage([attributeName, ...baseTypes]) + uiconfig.addComponentAttributeHeritage([attributeName, ...baseTypes]); } return ts.visitEachChild(node, visit, context); }; -- Gitee