+

CN115951890A - Method, system and device for code conversion between different front-end frames - Google Patents

Method, system and device for code conversion between different front-end frames Download PDF

Info

Publication number
CN115951890A
CN115951890A CN202310227287.0A CN202310227287A CN115951890A CN 115951890 A CN115951890 A CN 115951890A CN 202310227287 A CN202310227287 A CN 202310227287A CN 115951890 A CN115951890 A CN 115951890A
Authority
CN
China
Prior art keywords
code
syntax tree
abstract syntax
matching
match
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202310227287.0A
Other languages
Chinese (zh)
Other versions
CN115951890B (en
Inventor
请求不公布姓名
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Chengdu Shuzhilian Technology Co Ltd
Original Assignee
Chengdu Shuzhilian Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Chengdu Shuzhilian Technology Co Ltd filed Critical Chengdu Shuzhilian Technology Co Ltd
Priority to CN202310227287.0A priority Critical patent/CN115951890B/en
Publication of CN115951890A publication Critical patent/CN115951890A/en
Application granted granted Critical
Publication of CN115951890B publication Critical patent/CN115951890B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Document Processing Apparatus (AREA)

Abstract

本发明公开了一种不同前端框架间的代码转换方法及系统及装置,涉及前端技术领域,该方法包括获取源文件组件类型并通过匹配的方式筛选出第一文件组件和第二文件组件,对第一文件组件解析得到模板块、代码块和样式块,对代码块解析得到第一抽象语法树,对第一抽象语法树遍历得到第二抽象语法树,对模板块遍历得到第三抽象语法树,合并第二抽象语法树和第三抽象语法树得到第四抽象语法树,基于第四抽象语法树生成第一代码并与样式块的代码合并,完成转换;对第二文件组件解析得到第五抽象语法树,对第五抽象语法树遍历得到第六抽象语法树,基于第六抽象语法树生成第二代码,完成转换。通过本发明能够提高不同前端框架间代码转换率。

Figure 202310227287

The invention discloses a code conversion method, system and device between different front-end frameworks, and relates to the field of front-end technology. The method includes acquiring the source file component type and screening out the first file component and the second file component by matching. The first file component is parsed to obtain the template block, code block and style block, the code block is parsed to obtain the first abstract syntax tree, the first abstract syntax tree is traversed to obtain the second abstract syntax tree, and the template block is traversed to obtain the third abstract syntax tree , merge the second abstract syntax tree and the third abstract syntax tree to obtain the fourth abstract syntax tree, generate the first code based on the fourth abstract syntax tree and merge it with the code of the style block, and complete the conversion; parse the second file component to obtain the fifth The abstract syntax tree traverses the fifth abstract syntax tree to obtain the sixth abstract syntax tree, generates the second code based on the sixth abstract syntax tree, and completes the conversion. Through the present invention, the code conversion rate between different front-end frameworks can be improved.

Figure 202310227287

Description

一种不同前端框架间的代码转换方法及系统及装置Code conversion method, system and device between different front-end frameworks

技术领域technical field

本发明涉及前端技术领域,具体地,涉及一种不同前端框架间的代码转换方法及系统及装置。The present invention relates to the field of front-end technology, in particular, to a code conversion method, system and device between different front-end frameworks.

背景技术Background technique

在现代web前端开发项目中,除极少数还在使用前后端不分离的开发模式外,几乎都是使用前端三大框架来开发web应用,三大框架分别是React、Vue和Angular,而国内大多数公司使用最多的是React和Vue框架,Vue作为框架中的后来者,有借鉴React和Angular的地方,所以Vue和React、Angular之间源码转换是可行且难度不高的,但在转换各种语法写法层面上重复繁琐,人工手动只能转少量的代码。目前还有一种针对React和Vue框架代码互转的方案是让不同框架共存一个项目里,可以不转换源码来使用多个框架,但项目整体不易于维护,对于要统一技术栈的项目来说不适用。因此如何解决不同前端框架组件之间的移植问题是很有意义的,现有技术中对于React和Vue框架代码之间的互转,是通过将Vue2单文件源码通过Vue官方提供的插件vue-compiler-sfc转换为AST(抽象语法树),再修改所得的AST使其符合React类组件的语法,然后再使用修改后的AST生成转换成React组件的源码。但上述框架间代码互转的方法无法处理一些框架独特语法的互转和用户自定义命令的互转,存在一定的局限性,对含有用户自定义命令和独特语法较多的框架代码互转整体效果较差。In modern web front-end development projects, except for a very few that still use the development model that does not separate the front and back ends, almost all of them use the three front-end frameworks to develop web applications. The three frameworks are React, Vue, and Angular. Most companies use the React and Vue frameworks the most. As a latecomer in the framework, Vue has some references to React and Angular. Therefore, source code conversion between Vue, React and Angular is feasible and not difficult. However, when converting various The grammar and writing level is repetitive and cumbersome, and only a small amount of code can be transferred manually. At present, there is another solution for code conversion between React and Vue frameworks, which is to allow different frameworks to coexist in one project. You can use multiple frameworks without converting the source code, but the project as a whole is not easy to maintain. It is not suitable for projects that need to unify the technology stack. Be applicable. Therefore, how to solve the problem of transplantation between different front-end framework components is very meaningful. In the existing technology, the conversion between React and Vue framework code is through the Vue2 single-file source code through the plug-in vue-compiler officially provided by Vue. -sfc is converted to AST (Abstract Syntax Tree), and then the resulting AST is modified to conform to the grammar of the React class component, and then the modified AST is used to generate the source code converted into the React component. However, the above-mentioned method of inter-framework code conversion cannot handle the mutual conversion of some frameworks' unique syntax and user-defined commands, and there are certain limitations. The effect is poor.

发明内容Contents of the invention

为提高不同前端框架间的代码转换率,本发明提供了一种不同前端框架间的代码转换方法,所述方法包括以下步骤:In order to improve the code conversion rate between different front-end frameworks, the present invention provides a code conversion method between different front-end frameworks, said method comprising the following steps:

S1.获取源文件组件的类型;S1. Obtain the type of the source file component;

S2.将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行步骤S3,若匹配失败,则执行步骤S9;S2. Match the type of the source file component with the first preset file type, if the match is successful, name the source file component as the first file component and execute step S3, if the match fails, execute step S9 ;

S3.对所述第一文件组件进行解析得到模板块、代码块和样式块;S3. Parse the first file component to obtain a template block, a code block, and a style block;

S4.对所述代码块进行解析得到第一抽象语法树;S4. Analyzing the code block to obtain a first abstract syntax tree;

S5.对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;S5. Traversing the first abstract syntax tree to obtain a second abstract syntax tree includes obtaining the node type in the first abstract syntax tree and matching it with the first preset node type. If the match is successful, use the first The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match;

S6.对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;S6. Traversing the template block to obtain the third abstract syntax tree includes obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the match is successful, using the third rule pair to match Successful nodes are processed, and if the matching fails, the user sets the corresponding fourth rule for all matching failed nodes to process;

S7.合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;S7. Merging the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree;

S8.基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;S8. Based on the fourth abstract syntax tree, generate a first code, merge the first code with the code of the style block, and obtain the file code of the first target component;

S9.将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行步骤S10,若匹配失败,则退出;S9. Match the type of the source file component with the second preset file type, if the matching is successful, name the source file component as the second file component and execute step S10, and exit if the matching fails;

S10.对所述第二文件组件进行解析得到第五抽象语法树;S10. Analyzing the second file component to obtain a fifth abstract syntax tree;

S11.对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;S11. Traversing the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes obtaining the node type in the fifth abstract syntax tree and matching it with the third preset node type. If the match is successful, use the fifth abstract syntax tree. The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match;

S12.基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。S12. Based on the sixth abstract syntax tree, generate a second code, where the second code is a file code of a second target component.

发明原理:首选获取源文件组件的类型并与第一预设文件类型进行匹配,筛选出第一文件组件,然后对第一文件组件进行解析得到模板块、代码块和样式块,其中模板块为第一文件组件的模板代码,代码块为第一文件组件的JS代码(JavaScript简称“JS”,是一种具有函数优先的轻量级、解释型或即时编译型的编程语言。),样式块为第一文件组件的样式代码。对代码块进行解析得到第一抽象语法树(抽象语法树是源代码的抽象语法结构的树状表示,树上的每个节点都表示源代码中的一种结构,也就是将js代码进行结构化的转化,转化为一种数据结构。),然后对第一抽象语法树进行遍历得到第二抽象语法树,框架间在进行代码互转时,对于框架的常规语法可以采用现有的第一规则进行处理,但对于框架间的独特语法就需要用户设置不同的第二规则进行处理,对于框架的内置指令可以采用现有的第三规则进行处理,但对于自定义指令就需要用户设置不同的第四规则进行处理。再对模板块进行遍历得到第三抽象语法树,对于框架的内置hooks(钩子函数)可以采用现有第五规则进行处理,但对于框架的自定义hooks就需要用户设置不同的第六规则进行处理。将第二抽象语法树和第三抽象语法树进行合并得到第四抽象语法树,利用第四抽象语法树生成第一代码(对第四抽象语法树进行深度遍历,构建可以表示转换后代码的字符串,再通过代码生成工具转换成代码),由于样式块的代码对于所有组件都是通用的,因此直接将第一代码和样式块的代码进行合并得到第一目标组件的文件代码。通过将源文件组件的类型与第二预设文件类型进行匹配,筛选出第二文件组件,并对第二文件组件进行解析得到第五抽象语法树,再对第五抽象语法树遍历得到第六抽象语法树,利用第六抽象语法树生成第二代码(对第六抽象语法树进行深度遍历,构建可以表示转换后代码的字符串,再通过代码生成工具转换成代码),第二代码就为第二目标组件的文件代码。通过上述方法,可以提高不同前端框架间代码的整体转换率。Invention principle: first obtain the type of the source file component and match it with the first preset file type, filter out the first file component, and then parse the first file component to obtain the template block, code block and style block, where the template block is The template code of the first file component, the code block is the JS code of the first file component (JavaScript is referred to as "JS", which is a lightweight, interpreted or just-in-time compiled programming language with function priority.), style block Style code for the first file component. Parse the code block to get the first abstract syntax tree (the abstract syntax tree is a tree representation of the abstract syntax structure of the source code, each node on the tree represents a structure in the source code, that is, the js code is structured transformation into a data structure.), and then traverse the first abstract syntax tree to obtain the second abstract syntax tree. When the code is transferred between the frameworks, the existing first abstract syntax tree can be used for the regular syntax of the framework. However, for the unique syntax between frameworks, users need to set different second rules for processing. For the built-in instructions of the framework, the existing third rules can be used for processing, but for custom instructions, users need to set different ones. The fourth rule is processed. Then traverse the template block to get the third abstract syntax tree. For the built-in hooks (hook functions) of the framework, the existing fifth rule can be used for processing, but for the custom hooks of the framework, the user needs to set a different sixth rule for processing . Merge the second abstract syntax tree and the third abstract syntax tree to obtain the fourth abstract syntax tree, and use the fourth abstract syntax tree to generate the first code (deeply traverse the fourth abstract syntax tree to construct characters that can represent the converted code string, and then converted into code by a code generation tool), since the code of the style block is common to all components, the first code and the code of the style block are directly merged to obtain the file code of the first target component. By matching the type of the source file component with the second preset file type, the second file component is screened out, and the second file component is parsed to obtain the fifth abstract syntax tree, and then the fifth abstract syntax tree is traversed to obtain the sixth Abstract syntax tree, use the sixth abstract syntax tree to generate the second code (deeply traverse the sixth abstract syntax tree, construct a string that can represent the converted code, and then convert it into code through the code generation tool), the second code is The file code of the second target component. Through the above method, the overall conversion rate of codes between different front-end frameworks can be improved.

优选的,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理包括:Preferably, if the matching fails, the user sets the corresponding second rule for all nodes that fail to match to process including:

a1.获取匹配失败对应节点的处理结果;a1. Obtain the processing result of the node corresponding to the matching failure;

a2.判断所述处理结果是否成功,若是,则执行步骤a3,若否,则执行步骤a4;a2. judging whether the processing result is successful, if so, execute step a3, if not, execute step a4;

a3.将匹配失败对应节点的节点类型添加到所述第一预设节点类型中,将匹配失败对应节点的第二规则添加到所述第一规则中;a3. adding the node type of the node corresponding to the matching failure to the first preset node type, and adding the second rule of the corresponding node of the matching failure to the first rule;

a4.为匹配失败对应节点设置第七规则进行处理;a4. Set the seventh rule for the matching failure corresponding node for processing;

a5.循环步骤a1-a4,直到所有匹配失败对应节点的处理结果都成功。a5. Repeat steps a1-a4 until the processing results of all matching failure nodes are successful.

其中,由于是用户设置第二规则对节点进行处理,因此存在处理结果不成功的情况,通过获取节点的处理结果,判断该处理结果是否成功,如果成功,则将该节点对应的节点类型及第二规则分别加入到第一预设节点类型和第一规则中,这样在第二次遇到该节点时,可以直接使用第一规则进行处理,提高转换效率;但如果处理结果失败,则需要为该节点重新设置第七规则进行处理,直到该节点的处理结果为成功,再将节点对应的节点类型和成功处理该节点的规则加入到第一预设节点类型和第一规则中。Among them, because the user sets the second rule to process the node, there is a case that the processing result is not successful. By obtaining the processing result of the node, it is judged whether the processing result is successful. If it is successful, the node type corresponding to the node and the first The second rule is added to the first preset node type and the first rule respectively, so that when the node is encountered for the second time, the first rule can be directly used for processing to improve the conversion efficiency; but if the processing result fails, it needs to be The node resets the seventh rule for processing until the processing result of the node is successful, and then adds the node type corresponding to the node and the rules for successfully processing the node to the first preset node type and the first rule.

优选的,所述方法还包括,获取预设前端框架组件的版本信息,基于所述版本信息,获取所述预设前端框架组件的框架信息,基于所述框架信息,对所述第一预设节点类型、所述第一规则、所述第二预设节点类型、所述第三规则、所述第三预设节点类型和所述第四规则进行更新。Preferably, the method further includes acquiring version information of a preset front-end framework component, based on the version information, acquiring framework information of the preset front-end framework component, and based on the framework information, configuring the first preset The node type, the first rule, the second preset node type, the third rule, the third preset node type and the fourth rule are updated.

其中,由于不同前端框架的版本会一直更新,更新前后的框架也会存在差异,因此通过获取预设框架的版本信息和框架信息,来对框架间代码转换的规则进行更新,使该方法同样也能处理更新后前端框架间的代码互转问题。Among them, since the versions of different front-end frameworks will be updated all the time, there will be differences between the frameworks before and after the update. Therefore, by obtaining the version information and framework information of the preset framework, the rules of code conversion between the frameworks are updated, so that this method is also It can handle the code transfer between the updated front-end frameworks.

优选的,若匹配成功,则使用第一规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干第一预设函数进行转换处理得到对应若干第一函数,所有所述第一函数均满足第一目标组件的第一语法。其中,由于不同前端框架间的写法不同,因此对匹配成功的节点使用第一规则进行处理得到的若干第一函数需要满足第一目标组件的第一语法,否则,经过转换后的若干第一函数无法在第一目标组件中正常使用。Preferably, if the matching is successful, using the first rule to process the successfully matched nodes includes, based on the depth-first traversal method, converting several first preset functions in the successfully matched nodes to obtain corresponding several first functions, All of the first functions satisfy the first syntax of the first target component. Among them, due to the different writing methods between different front-end frameworks, several first functions obtained by processing successfully matched nodes using the first rule need to meet the first grammar of the first target component, otherwise, several first functions after conversion Doesn't work properly in the first target component.

优选的,若匹配成功,则使用第三规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干内置指令进行转换。其中,由于不同前端框架间的写法不同,因此将第一文件组件中若干内置指令进行转换是为了让第一目标组件能实现与若干内置指令相同的功能,同时经过转换后的若干内置指令满足第一目标组件的写法。Preferably, if the matching is successful, using the third rule to process the successfully matching node includes, based on the depth-first traversal method, converting several built-in instructions in the successfully matching node. Among them, due to the different writing methods between different front-end frameworks, the purpose of converting several built-in instructions in the first file component is to enable the first target component to achieve the same functions as several built-in instructions, and at the same time, the converted several built-in instructions satisfy the first The writing method of a target component.

优选的,基于所述第四抽象语法树,生成第一代码并与所述样式块的代码进行合并,得到第一目标组件的文件代码包括:基于第一预设代码生成工具,对所述第四抽象语法树进行处理得到第一代码字符串,将所述第一代码字符串写入第一预设后缀文件中得到所述第一代码,将所述样式块的代码字符串写入第二预设后缀文件中得到第三代码,所述第一代码和所述第三代码合并得到所述第一目标组件的文件代码。Preferably, based on the fourth abstract syntax tree, generating the first code and merging it with the code of the style block to obtain the file code of the first target component includes: based on the first preset code generation tool, generating the first code Four abstract syntax trees are processed to obtain the first code string, and the first code string is written into the first preset suffix file to obtain the first code, and the code string of the style block is written into the second The third code is obtained from the preset suffix file, and the first code and the third code are combined to obtain the file code of the first target component.

其中,样式块的代码用来设计网页或者文件的样式,对不同框架组件来说都通用,因此在进行不同框架代码转换时不需要转换样式块的代码,而基于第四抽象语法树生成的第一代码字符串写入第一预设后缀文件和样式块的代码字符串写入第二预设后缀文件,一是为了代码规范和方便阅读,二是为了提示编译器进行编译。Among them, the code of the style block is used to design the style of the web page or file, which is common to different framework components, so it is not necessary to convert the code of the style block when performing code conversion of different frameworks, and the first generated based on the fourth abstract syntax tree A code string is written into the first preset suffix file and a code string of the style block is written into the second preset suffix file. One is for code specification and easy reading, and the other is for prompting the compiler to compile.

优选的,若匹配成功,则使用第五规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干第二预设函数进行改写得到若干改写结果,所有所述改写结果均满足所述第二目标组件的第二语法。其中,不同前端框架写法不同,对于相同功能所实现的方式也存在差异,因此将匹配成功的节点中的若干第二预设函数进行改写得到若干改写结果,所有所述改写结果均满足所述第二目标组件的第二语法,即若干第二预设函数在第一文件组件实现的功能经过改写后在第二目标组件中也能同样实现。Preferably, if the matching is successful, using the fifth rule to process the successfully matching nodes includes, based on the depth-first traversal method, rewriting several second preset functions in the successfully matching nodes to obtain several rewriting results, all of which The rewriting results all satisfy the second syntax of the second target component. Among them, different front-end frameworks have different writing methods, and there are also differences in the way of realizing the same function. Therefore, several second preset functions in the successfully matched nodes are rewritten to obtain several rewriting results, and all the rewriting results satisfy the first The second syntax of the second target component, that is, several second preset functions can also be implemented in the second target component after rewriting the functions realized by the first file component.

优选的,基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码包括:基于第二预设代码生成工具,对所述第六抽象语法树进行处理得到第二代码字符串,将所述第二代码字符串写入第三预设后缀文件中得到所述第二代码,所述第二代码为所述第二目标组件的文件代码。其中,将第二代码字符串写入第三预设后缀文件中,一是为了让代码规范和方便阅读,二是提示编译器进行编译。Preferably, generating the second code based on the sixth abstract syntax tree, the second code being the file code of the second target component includes: performing the sixth abstract syntax tree on the basis of the second preset code generation tool A second code string is obtained through processing, and the second code string is written into a third preset suffix file to obtain the second code, and the second code is the file code of the second target component. Among them, the second code string is written into the third preset suffix file, one is to make the code standard and easy to read, and the other is to prompt the compiler to compile.

为提高不同前端框架间的代码转换效果,本发明还提供了一种不同前端框架间的代码转换系统,所述系统包括:In order to improve the code conversion effect between different front-end frameworks, the present invention also provides a code conversion system between different front-end frameworks, said system comprising:

获取单元,用于获取源文件组件的类型;Acquisition unit, used to obtain the type of the source file component;

第一匹配单元,用于将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行第一解析单元,若匹配失败,则执行第二匹配单元;The first matching unit is configured to match the type of the source file component with the first preset file type, if the match is successful, name the source file component as the first file component and execute the first parsing unit, if If the matching fails, execute the second matching unit;

第一解析单元,用于对所述第一文件组件进行解析得到模板块、代码块和样式块;A first parsing unit, configured to parse the first file component to obtain a template block, a code block, and a style block;

第二解析单元,用于对所述代码块进行解析得到第一抽象语法树;a second parsing unit, configured to parse the code block to obtain a first abstract syntax tree;

第一遍历单元,用于对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;The first traversal unit, configured to traverse the first abstract syntax tree to obtain the second abstract syntax tree includes, obtaining the node type in the first abstract syntax tree and matching with the first preset node type, if the matching is successful , the first rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match;

第二遍历单元,用于对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;The second traversal unit, configured to traverse the template block to obtain a third abstract syntax tree includes: obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the matching is successful, using The third rule processes the nodes that match successfully, and if the match fails, the user sets the corresponding fourth rule for all nodes that fail to match;

合并单元,用于合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;a merging unit, configured to merge the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree;

第一代码生成单元,用于基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;a first code generation unit, configured to generate a first code based on the fourth abstract syntax tree, and combine the first code with the code of the style block to obtain the file code of the first target component;

第二匹配单元,用于将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行第三解析单元,若匹配失败,则退出;The second matching unit is used to match the type of the source file component with the second preset file type, if the match is successful, name the source file component as the second file component and execute the third parsing unit, if If the match fails, exit;

第三解析单元,用于对所述第二文件组件进行解析得到第五抽象语法树;a third parsing unit, configured to parse the second file component to obtain a fifth abstract syntax tree;

第三遍历单元,用于对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;The third traversal unit, configured to traverse the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes, obtaining the node type in the fifth abstract syntax tree and matching with the third preset node type, if the matching is successful , the fifth rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match;

第二代码生成单元,用于基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。The second code generation unit is configured to generate a second code based on the sixth abstract syntax tree, where the second code is the file code of the second target component.

本发明还提供了一种不同前端框架间的代码转换装置,包括存储器、处理器以及存储在所述存储器中并可在所述处理器上运行的计算机程序,所述处理器执行所述计算机程序时实现所述不同前端框架间的代码转换方法的步骤。The present invention also provides a code conversion device between different front-end frameworks, including a memory, a processor, and a computer program stored in the memory and operable on the processor, and the processor executes the computer program The steps of implementing the code conversion method between the different front-end frameworks at the same time.

本发明提供的一个或多个技术方案,至少具有如下技术效果或优点:One or more technical solutions provided by the present invention have at least the following technical effects or advantages:

本发明通过增加对框架间独特语法、自定义指令和自定义hooks的转换方法,从整体上,提高不同前端框架间代码转换率。The present invention improves the conversion rate of codes between different front-end frameworks as a whole by adding conversion methods for unique syntax, self-defined instructions and self-defined hooks between the frameworks.

本发明通过将框架间独特语法、自定义指令和自定义hooks成功转换的方法添加到框架间代码的转换规则中,在后续处理相同的独特语法、自定义指令和自定义hooks时,可以直接利用代码转换规则进行处理,不需要再针对每个独特语法、自定义指令和自定义hooks单独处理,从而在提高代码转换率的同时提高代码转换效率。In the present invention, by adding the method of successfully converting the unique syntax between frames, self-defined instructions and custom hooks into the conversion rules of inter-frame codes, the same unique syntax, custom instructions and custom hooks can be directly used in subsequent processing. The code conversion rules are processed, and there is no need to process each unique syntax, custom instruction and custom hooks separately, so as to improve the code conversion efficiency while improving the code conversion rate.

本发明通过持续获取预设框架的版本信息和框架信息,并对不同前端框架间代码转换的规则进行更新,使本发明能处理框架版本更新后的代码互转问题。The present invention continuously obtains the version information and framework information of the preset framework, and updates the code conversion rules between different front-end frameworks, so that the present invention can deal with the code mutual conversion problem after the framework version is updated.

附图说明Description of drawings

此处所说明的附图用来提供对本发明实施例的进一步理解,构成本发明的一部分,并不构成对本发明实施例的限定;The drawings described here are used to provide a further understanding of the embodiments of the present invention, constitute a part of the present invention, and do not constitute a limitation to the embodiments of the present invention;

图1是本发明中一种不同前端框架间的代码转换方法流程示意图;Fig. 1 is a schematic flow chart of a code conversion method between different front-end frameworks in the present invention;

图2是本发明中一种不同前端框架间的代码转换系统组成示意图。Fig. 2 is a schematic diagram of the composition of a code conversion system between different front-end frameworks in the present invention.

具体实施方式Detailed ways

为了能够更清楚地理解本发明的上述目的、特征和优点,下面结合附图和具体实施方式对本发明进行进一步的详细描述。需要说明的是,在相互不冲突的情况下,本发明的实施例及实施例中的特征可以相互组合。In order to understand the above-mentioned purpose, features and advantages of the present invention more clearly, the present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments. It should be noted that the embodiments of the present invention and the features in the embodiments may be combined with each other under the condition that they do not conflict with each other.

在下面的描述中阐述了很多具体细节以便于充分理解本发明,但是,本发明还可以采用其他不同于在此描述范围内的其他方式来实施,因此,本发明的保护范围并不受下面公开的具体实施例的限制。In the following description, many specific details are set forth in order to fully understand the present invention. However, the present invention can also be implemented in other ways different from the scope of this description. Therefore, the protection scope of the present invention is not limited by the following disclosure. The limitations of specific examples.

实施例一Embodiment one

请参考图1,为本发明中一种不同前端框架间的代码转换方法流程示意图,所述方法包括以下步骤:Please refer to Fig. 1, which is a schematic flow chart of a code conversion method between different front-end frameworks in the present invention, and the method includes the following steps:

S1.获取源文件组件的类型;S1. Obtain the type of the source file component;

S2.将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行步骤S3,若匹配失败,则执行步骤S9;S2. Match the type of the source file component with the first preset file type, if the match is successful, name the source file component as the first file component and execute step S3, if the match fails, execute step S9 ;

S3.对所述第一文件组件进行解析得到模板块、代码块和样式块;S3. Parse the first file component to obtain a template block, a code block, and a style block;

S4.对所述代码块进行解析得到第一抽象语法树;S4. Analyzing the code block to obtain a first abstract syntax tree;

S5.对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;S5. Traversing the first abstract syntax tree to obtain a second abstract syntax tree includes obtaining the node type in the first abstract syntax tree and matching it with the first preset node type. If the match is successful, use the first The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match;

S6.对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;S6. Traversing the template block to obtain the third abstract syntax tree includes obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the match is successful, using the third rule pair to match Successful nodes are processed, and if the matching fails, the user sets the corresponding fourth rule for all matching failed nodes to process;

S7.合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;S7. Merging the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree;

S8.基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;S8. Based on the fourth abstract syntax tree, generate a first code, merge the first code with the code of the style block, and obtain the file code of the first target component;

S9.将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行步骤S10,若匹配失败,则退出;S9. Match the type of the source file component with the second preset file type, if the matching is successful, name the source file component as the second file component and execute step S10, and exit if the matching fails;

S10.对所述第二文件组件进行解析得到第五抽象语法树;S10. Analyzing the second file component to obtain a fifth abstract syntax tree;

S11.对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;S11. Traversing the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes obtaining the node type in the fifth abstract syntax tree and matching it with the third preset node type. If the match is successful, use the fifth abstract syntax tree. The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match;

S12.基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。S12. Based on the sixth abstract syntax tree, generate a second code, where the second code is a file code of a second target component.

其中,将代码解析成抽象语法树(AST)包含词法分析和语法分析两个阶段,词法分析阶段把字符串形式的代码转换为词元流(tokens),词元流类似于AST中的节点,而语法分析阶段则会把词元流转换成AST的形式,同时会把词元流中的信息转换成AST的表述结构。对抽象语法树进行转换包括,接收AST并对其进行深度优先遍历,维护AST树的整体状态,在此过程中对节点进行添加、更新及移除等操作,转换方法的参数为原始AST树和预设的转换规则,返回结果为转换后的AST树。通过抽象语法树生成代码包括,深度优先遍历整个AST,然后构建可以表示转换后代码的字符串,再通过代码生成工具将字符串转换成代码。上述关于代码的解析、抽象语法树的转换和代码的生成均适用于本发明,后续不再具体阐述。Among them, parsing the code into an abstract syntax tree (AST) includes two stages of lexical analysis and syntactic analysis. The lexical analysis stage converts the code in the form of a string into tokens. The token stream is similar to the nodes in the AST. In the grammatical analysis stage, the lexical stream will be converted into the form of AST, and the information in the lexical stream will be converted into the expression structure of AST. The conversion of the abstract syntax tree includes receiving the AST and performing depth-first traversal on it, maintaining the overall state of the AST tree, and performing operations such as adding, updating, and removing nodes during the process. The parameters of the conversion method are the original AST tree and Preset conversion rules, the returned result is the converted AST tree. Generating code through the abstract syntax tree includes traversing the entire AST in depth first, then constructing a string that can represent the converted code, and then converting the string into code through a code generation tool. The above code analysis, abstract syntax tree conversion and code generation are all applicable to the present invention, and will not be described in detail later.

其中,将源文件组件的类型和第一预设文件类型(为.vue的后缀文件)进行匹配,筛选出第一文件组件(Vue组件),然后使用组件解析工具(@vue/compiler-sfc)对第一文件组件进行解析得到模板块(template代码)、代码块(js代码)和样式块(css代码),再使用AST解析工具(@babel/parser)对代码块进行解析得到第一抽象语法树。使用AST遍历操作工具(@babel/traverse)对第一抽象语法树和模板块进行遍历分别得到第二抽象语法树和第三抽象语法树,再将第二抽象语法树和第三抽象语法树进行合并得到第四抽象语法树(按照React+JSX语法进行合并,JSX是一种JavaScript的语法扩展,全称JavaScript XML,运用于React架构中,是React的标记语言。),对第四抽象语法树使用AST代码生成工具(@babel/generator)生成第一代码,再将第一代码和样式块的代码进行合并得到第一目标组件(React组件)的文件代码,完成第一文件组件到第一目标组件的转换。当源文件组件的类型与第一预设文件类型匹配失败时,将源文件组件的类型和第二预设文件类型(为.jsx或者.tsx后缀的文件)进行匹配,如果匹配失败,就退出,如果匹配成功则筛选出第二文件组件(React组件),使用AST解析工具(@babel/parser)并增加jsx插件对第二文件组件进行解析得到第五抽象语法树,使用AST遍历工具(@babel/traverse)对第五抽象语法树进行遍历操作得到第六抽象语法树,对第六抽象语法树使用AST代码生成工具(@babel/generator)生成第二代码,第二代码就为第二目标组件(Vue组件)的文件代码。Among them, match the type of the source file component with the first preset file type (the suffix file of .vue), filter out the first file component (Vue component), and then use the component parsing tool (@vue/compiler-sfc) Parse the first file component to get the template block (template code), code block (js code) and style block (css code), and then use the AST parsing tool (@babel/parser) to parse the code block to get the first abstract syntax Tree. Use the AST traversal operation tool (@babel/traverse) to traverse the first abstract syntax tree and the template block to obtain the second abstract syntax tree and the third abstract syntax tree respectively, and then perform the second abstract syntax tree and the third abstract syntax tree Merge to get the fourth abstract syntax tree (merge according to React+JSX syntax, JSX is a JavaScript syntax extension, the full name is JavaScript XML, which is used in the React architecture and is the markup language of React.), use the fourth abstract syntax tree The AST code generation tool (@babel/generator) generates the first code, and then merges the first code and the code of the style block to obtain the file code of the first target component (React component), and completes the first file component to the first target component conversion. When the type of the source file component fails to match the first preset file type, match the type of the source file component with the second preset file type (files with .jsx or .tsx suffixes), and exit if the matching fails , if the match is successful, filter out the second file component (React component), use the AST parsing tool (@babel/parser) and add the jsx plug-in to parse the second file component to get the fifth abstract syntax tree, use the AST traversal tool (@babel/parser) babel/traverse) traverse the fifth abstract syntax tree to obtain the sixth abstract syntax tree, and use the AST code generation tool (@babel/generator) to generate the second code for the sixth abstract syntax tree, and the second code is the second target The file code of the component (Vue component).

其中,对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,基于深度优先遍历方法,将匹配成功的节点中的若干第一预设函数(如ref、reactive、props、computed、watch以及生命周期钩子函数)进行转换处理得到对应若干第一函数(如ref对应useRef函数、reactive对应useReactive函数、props对应useProps函数、computed对应useComputed函数以及watch对应useWatch函数),所有所述第一函数均满足第一目标组件的第一语法(如React组件的Hooks语法)。若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理包括,a1.获取匹配失败对应节点的处理结果;a2.判断所述处理结果是否成功,若是,则执行步骤a3,若否,则执行步骤a4;a3.将匹配失败对应节点的节点类型添加到所述第一预设节点类型中,将匹配失败对应节点的第二规则添加到所述第一规则中;a4.为匹配失败对应节点设置第七规则进行处理;a5.循环步骤a1-a4,直到所有匹配失败对应节点的处理结果都成功。由于在进行框架转换时存在部分框架独特语法无法使用统一规则进行处理,而与第一预设节点类型匹配失败的节点则对应这部分独特语法,用户通过设置第二规则来处理匹配失败对应的节点,但在处理这些节点时可能存在处理失败的情况(如对该节点包含的独特语法处理后不满足目标组件的语法),所以需要判断处理结果是否成功,如果不成功,则需要设置第七规则(新的规则)来处理这些节点,直到所有匹配失败对应的节点处理结果都为成功,然后再将匹配失败的节点类型添加到第一预设节点类型中,将成功处理匹配失败节点的规则添加到第一规则中。本发明通过第四规则处理第三抽象语法树中与第二预设节点类型匹配失败对应的节点,是对应第一文件组件中一些自定义指令无法通过统一规则进行处理,通过第六规则处理第五抽象语法树中与第三预设节点类型匹配失败对应的节点,是对应第二文件组件中一些自定义hooks无法通过统一规则进行处理,其具体处理方式都可参考上述利用第二规则处理第一抽象语法树中与第一预设节点类型匹配失败对应的节点,本发明不再展开阐述。Wherein, traversing the first abstract syntax tree to obtain the second abstract syntax tree includes obtaining the node type in the first abstract syntax tree and matching with the first preset node type, and if the matching is successful, using the first The rules process the successfully matched nodes. Based on the depth-first traversal method, several first preset functions (such as ref, reactive, props, computed, watch, and life cycle hook functions) in the successfully matched nodes are converted and processed to obtain corresponding Several first functions (such as useRef function corresponding to ref, useReactive function corresponding to reactive, useProps function corresponding to props, useComputed function corresponding to computed, and useWatch function corresponding to watch), all of the first functions satisfy the first syntax of the first target component (such as Hooks syntax for React components). If the matching fails, the user sets the corresponding second rule for all matching failed nodes to process, including: a1. Obtaining the processing result of the node corresponding to the matching failure; a2. Judging whether the processing result is successful, if so, then execute step a3, If not, then execute step a4; a3. Add the node type of the node corresponding to the matching failure to the first preset node type, and add the second rule of the node corresponding to the matching failure to the first rule; a4. Set the seventh rule for the matching failure corresponding nodes for processing; a5. Repeat steps a1-a4 until the processing results of all matching failure corresponding nodes are successful. Since there are some frame unique grammars that cannot be processed by unified rules during frame conversion, and nodes that fail to match the first preset node type correspond to this part of unique grammars, the user handles the nodes corresponding to matching failures by setting the second rule , but there may be processing failures when processing these nodes (such as the unique grammar contained in the node does not satisfy the grammar of the target component after processing), so it is necessary to judge whether the processing result is successful, if not, you need to set the seventh rule (new rules) to process these nodes until all the node processing results corresponding to the matching failures are successful, and then add the node type of the matching failure to the first preset node type, and add the rules for successfully processing the matching failure nodes into the first rule. The present invention uses the fourth rule to process the nodes in the third abstract syntax tree that fail to match the second preset node type, because some self-defined instructions in the first file component cannot be processed by unified rules, and the sixth rule is used to process the first node. The node corresponding to the failure to match the third preset node type in the fifth abstract syntax tree corresponds to some custom hooks in the second file component that cannot be processed by unified rules. For the specific processing method, please refer to the above-mentioned process using the second rule The nodes in an abstract syntax tree that fail to match the first preset node type will not be elaborated in the present invention.

其中,对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,基于深度优先遍历方法,将匹配成功的节点中的若干内置指令(如v-if、v-model以及v-for等指令)进行转换,例如在Vue组件的模板块中想要将数组中的数据进行循环的时候可以直接使用封装好的v-for指令进行实现,但在React组件中并没有这种指令,React想要实现列表的循环需要使用map()函数来实现,模板块中的若干内置指令在React组件中都有对应的实现方法,具体可参考v-for指令的转换方法,本发明不再过多阐述。Wherein, traversing the template block to obtain the third abstract syntax tree includes obtaining the node type in the third abstract syntax tree and matching with the second preset node type, and if the matching is successful, using the third rule to match The successful node is processed, and based on the depth-first traversal method, several built-in instructions (such as v-if, v-model, and v-for instructions) in the successfully matched node are converted, for example, in the template block of the Vue component. When you want to loop the data in the array, you can directly use the packaged v-for instruction to implement it, but there is no such instruction in the React component. React needs to use the map() function to implement the loop of the list. Several built-in instructions in the template block have corresponding implementation methods in the React component. For details, please refer to the conversion method of the v-for instruction, which will not be elaborated in the present invention.

其中,对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,基于深度优先遍历方法,将匹配成功的节点中的若干第二预设函数(如useState、useEffect和useMemo等内置hooks)进行改写得到若干改写结果,所有所述改写结果均满足所述第二目标组件的第二语法(Composition API语法),例如在React组件中,useState的基本原理是通过把useState Hooks的相关信息存储在对应的函数组件的fiber节点(fiber是一种数据结构,每个fiber节点的内部都保存了dom相关信息和fiber树相关引用等信息)上,而在Vue组件中要实现同useState相同的功能,则需要将useStateHooks的相关信息存储在对应的函数组件的实例对象上。上述举例只是起到解释说明的作用,针对具体的第二预设函数按照第三预设规则进行改写可参考useState Hooks的改写方法,本发明不再过多阐述。Wherein, traversing the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes obtaining the node type in the fifth abstract syntax tree and matching with the third preset node type, and if the matching is successful, using the fifth abstract syntax tree The rules process the successfully matched nodes. Based on the depth-first traversal method, rewrite several second preset functions (such as built-in hooks such as useState, useEffect, and useMemo) in the successfully matched nodes to obtain several rewritten results. All the rewritten The results all meet the second syntax (Composition API syntax) of the second target component. For example, in the React component, the basic principle of useState is to store the relevant information of useState Hooks in the fiber node of the corresponding function component (fiber is a A data structure, each fiber node internally stores dom-related information and fiber tree-related references, etc.), and in order to achieve the same function as useState in the Vue component, you need to store the relevant information of useStateHooks in the corresponding On the instance object of the function component. The above examples are just for explanation. For the rewriting of the specific second preset function according to the third preset rule, please refer to the rewriting method of useState Hooks, which will not be elaborated in the present invention.

其中,基于所述第四抽象语法树,生成第一代码并与所述样式块的代码进行合并,得到第一目标组件的文件代码包括:基于第一预设代码生成工具(@babel/generator),对所述第四抽象语法树进行处理得到第一代码字符串,将所述第一代码字符串写入第一预设后缀文件(.jsx或者.tsx后缀的文件)中得到所述第一代码,将所述样式块的代码字符串写入第二预设后缀文件(.css后缀的文件)中得到第三代码,所述第一代码和所述第三代码合并得到所述第一目标组件的文件代码。Wherein, based on the fourth abstract syntax tree, generating the first code and merging it with the code of the style block to obtain the file code of the first target component includes: based on the first preset code generation tool (@babel/generator) , process the fourth abstract syntax tree to obtain the first code string, write the first code string into the first preset suffix file (file with .jsx or .tsx suffix) to obtain the first code, write the code string of the style block into the second preset suffix file (file with .css suffix) to obtain the third code, and combine the first code and the third code to obtain the first target The component's file code.

其中,基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码包括:基于第二预设代码生成工具(@babel/generator),对所述第六抽象语法树进行处理得到第二代码字符串,将所述第二代码字符串写入第三预设后缀文件(.vue后缀的文件)中得到所述第二代码,所述第二代码为所述第二目标组件的文件代码。Wherein, generating the second code based on the sixth abstract syntax tree, the second code being the file code of the second target component includes: based on the second preset code generation tool (@babel/generator), for the first Six abstract syntax trees are processed to obtain the second code string, and the second code string is written into the third preset suffix file (file with .vue suffix) to obtain the second code, and the second code is The file code of the second object component.

其中,所述方法还包括,获取预设前端框架组件的版本信息,基于所述版本信息,获取所述预设前端框架组件的框架信息,基于所述框架信息,对所述第一预设节点类型、所述第一规则、所述第二预设节点类型、所述第三规则、所述第三预设节点类型和所述第四规则进行更新。由于不同前端框架都在不停更新,且更新前后框架间差异较大,例如Vue2.0是选项API(Option API)语法,而Vue3.0为组合式API(Composition API)语法,例如React16.8.0版本开始引入Hooks语法,但React之前的版本没有Hooks语法,因此相同前端框架但不同版本间的代码转换规则也存在差异,所以需要不断对前端框架前的代码转换规则进行更新。Wherein, the method further includes acquiring the version information of the preset front-end framework component, and based on the version information, acquiring the framework information of the preset front-end framework component, and based on the framework information, configuring the first preset node type, the first rule, the second preset node type, the third rule, the third preset node type and the fourth rule are updated. Since different front-end frameworks are constantly being updated, and there are large differences between the frameworks before and after the update, for example, Vue2.0 is an Option API (Option API) syntax, while Vue3.0 is a Composition API (Composition API) syntax, such as React16.8.0 The version began to introduce Hooks syntax, but the previous version of React did not have Hooks syntax, so the code conversion rules of different versions of the same front-end framework are also different, so it is necessary to constantly update the code conversion rules before the front-end framework.

实施例二Embodiment two

请参考图2,为本发明中一种不同前端框架间的代码转换系统组成示意图,所述系统包括:Please refer to Figure 2, which is a schematic diagram of the composition of a code conversion system between different front-end frameworks in the present invention, the system includes:

获取单元,用于获取源文件组件的类型;Acquisition unit, used to obtain the type of the source file component;

第一匹配单元,用于将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行第一解析单元,若匹配失败,则执行第二匹配单元;The first matching unit is used to match the type of the source file component with the first preset file type, if the matching is successful, then name the source file component as the first file component and execute the first parsing unit, if If the matching fails, execute the second matching unit;

第一解析单元,用于对所述第一文件组件进行解析得到模板块、代码块和样式块;A first parsing unit, configured to parse the first file component to obtain a template block, a code block, and a style block;

第二解析单元,用于对所述代码块进行解析得到第一抽象语法树;a second parsing unit, configured to parse the code block to obtain a first abstract syntax tree;

第一遍历单元,用于对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;The first traversal unit, configured to traverse the first abstract syntax tree to obtain the second abstract syntax tree includes, obtaining the node type in the first abstract syntax tree and matching with the first preset node type, if the matching is successful , the first rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match;

第二遍历单元,用于对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;The second traversal unit, configured to traverse the template block to obtain a third abstract syntax tree includes: obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the matching is successful, using The third rule processes the nodes that match successfully, and if the match fails, the user sets the corresponding fourth rule for all nodes that fail to match;

合并单元,用于合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;a merging unit, configured to merge the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree;

第一代码生成单元,用于基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;a first code generation unit, configured to generate a first code based on the fourth abstract syntax tree, and combine the first code with the code of the style block to obtain the file code of the first target component;

第二匹配单元,用于将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行第三解析单元,若匹配失败,则退出;The second matching unit is used to match the type of the source file component with the second preset file type, if the match is successful, name the source file component as the second file component and execute the third parsing unit, if If the match fails, exit;

第三解析单元,用于对所述第二文件组件进行解析得到第五抽象语法树;a third parsing unit, configured to parse the second file component to obtain a fifth abstract syntax tree;

第三遍历单元,用于对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;The third traversal unit, configured to traverse the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes, obtaining the node type in the fifth abstract syntax tree and matching with the third preset node type, if the matching is successful , the fifth rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match;

第二代码生成单元,用于基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。The second code generation unit is configured to generate a second code based on the sixth abstract syntax tree, where the second code is the file code of the second target component.

实施例三Embodiment three

本发明实施例三提供了一种不同前端框架间的代码转换装置,包括存储器、处理器以及存储在所述存储器中并可在所述处理器上运行的计算机程序,所述处理器执行所述计算机程序时实现所述一种不同前端框架间的代码转换方法的步骤。Embodiment 3 of the present invention provides a code conversion device between different front-end frameworks, including a memory, a processor, and a computer program stored in the memory and operable on the processor, and the processor executes the The computer program is the step of implementing the code conversion method between different front-end frameworks.

其中,所述处理器可以是中央处理器(CPU,Central Processing Unit),还可以是其他通用处理器、数字信号处理器(digital signal processor)、专用集成电路(Application Specific Integrated Circuit)、现成可编程门阵列(Fieldprogrammablegate array)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件等。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。Wherein, the processor can be a central processing unit (CPU, Central Processing Unit), and can also be other general-purpose processors, digital signal processors (digital signal processors), application specific integrated circuits (Application Specific Integrated Circuits), off-the-shelf programmable Gate array (Fieldprogrammablegate array) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor, or the processor may be any conventional processor, or the like.

所述存储器可用于存储所述计算机程序和/或模块,所述处理器通过运行或执行存储在所述存储器内的数据,实现发明中一种不同前端框架间的代码转换装置的各种功能。所述存储器可主要包括存储程序区和存储数据区,其中,存储程序区可存储操作系统、至少一个功能所需的应用程序(比如声音播放功能、图像播放功能等)等。此外,存储器可以包括高速随机存取存储器、还可以包括非易失性存储器,例如硬盘、内存、插接式硬盘,智能存储卡,安全数字卡,闪存卡、至少一个磁盘存储器件、闪存器件、或其他易失性固态存储器件。The memory can be used to store the computer programs and/or modules, and the processor implements various functions of a code conversion device between different front-end frameworks in the invention by running or executing data stored in the memory. The memory may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, at least one application required by a function (such as a sound playback function, an image playback function, etc.) and the like. In addition, the memory can include high-speed random access memory, and can also include non-volatile memory, such as hard disk, internal memory, plug-in hard disk, smart memory card, secure digital card, flash memory card, at least one magnetic disk storage device, flash memory device, or other volatile solid-state memory devices.

本发明已对基本概念做了描述,显然,对于本领域技术人员来说,上述详细披露仅仅作为示例,而并不构成对本说明书的限定。虽然此处并没有明确说明,本领域技术人员可能会对本说明书进行各种修改、改进和修正。该类修改、改进和修正在本说明书中被建议,所以该类修改、改进、修正仍属于本说明书示范实施例的精神和范围。The present invention has described the basic concepts. Obviously, for those skilled in the art, the above detailed disclosure is only an example, and does not constitute a limitation to this specification. Although not expressly stated here, those skilled in the art may make various modifications, improvements and corrections to this description. Such modifications, improvements and corrections are suggested in this specification, so such modifications, improvements and corrections still belong to the spirit and scope of the exemplary embodiments of this specification.

同时,本说明书使用了特定词语来描述本说明书的实施例。如“一个实施例”、“一实施例”、和/或“一些实施例”意指与本说明书至少一个实施例相关的某一特征、结构或特点。因此,应强调并注意的是,本说明书中在不同位置两次或多次提及的“一实施例”或“一个实施例”或“一个替代性实施例”并不一定是指同一实施例。此外,本说明书的一个或多个实施例中的某些特征、结构或特点可以进行适当的组合。Meanwhile, this specification uses specific words to describe the embodiments of this specification. For example, "one embodiment", "an embodiment", and/or "some embodiments" refer to a certain feature, structure or characteristic related to at least one embodiment of this specification. Therefore, it should be emphasized and noted that two or more references to "an embodiment" or "an embodiment" or "an alternative embodiment" in different places in this specification do not necessarily refer to the same embodiment . In addition, certain features, structures or characteristics in one or more embodiments of this specification may be properly combined.

此外,本领域技术人员可以理解,本说明书的各方面可以通过若干具有可专利性的种类或情况进行说明和描述,包括任何新的和有用的工序、机器、产品或物质的组合,或对他们的任何新的和有用的改进。相应地,本说明书的各个方面可以完全由硬件执行、可以完全由软件(包括固件、常驻软件、微码等)执行、也可以由硬件和软件组合执行。以上硬件或软件均可被称为“数据块”、“模块”、“引擎”、“单元”、“组件”或“系统”。此外,本说明书的各方面可能表现为位于一个或多个计算机可读介质中的计算机产品,该产品包括计算机可读程序编码。In addition, those skilled in the art will understand that various aspects of this specification can be illustrated and described by several patentable categories or situations, including any new and useful process, machine, product or combination of substances, or any combination of them Any new and useful improvements. Correspondingly, various aspects of this specification may be entirely executed by hardware, may be entirely executed by software (including firmware, resident software, microcode, etc.), or may be executed by a combination of hardware and software. The above hardware or software may be referred to as "block", "module", "engine", "unit", "component" or "system". Additionally, aspects of this specification may be embodied as a computer product comprising computer readable program code on one or more computer readable media.

计算机存储介质可能包含一个内含有计算机程序编码的传播数据信号,例如在基带上或作为载波的一部分。该传播信号可能有多种表现形式,包括电磁形式、光形式等,或合适的组合形式。计算机存储介质可以是除计算机可读存储介质之外的任何计算机可读介质,该介质可以通过连接至一个指令执行系统、装置或设备以实现通讯、传播或传输供使用的程序。位于计算机存储介质上的程序编码可以通过任何合适的介质进行传播,包括无线电、电缆、光纤电缆、RF、或类似介质,或任何上述介质的组合。A computer storage medium may contain a propagated data signal embodying a computer program code, for example, in baseband or as part of a carrier wave. The propagated signal may have various manifestations, including electromagnetic form, optical form, etc., or a suitable combination. A computer storage medium may be any computer-readable medium, other than a computer-readable storage medium, that can be used to communicate, propagate, or transfer a program for use by being coupled to an instruction execution system, apparatus, or device. Program code residing on a computer storage medium may be transmitted over any suitable medium, including radio, electrical cable, fiber optic cable, RF, or the like, or combinations of any of the foregoing.

本说明书各部分操作所需的计算机程序编码可以用任意一种或多种程序语言编写,包括面向对象编程语言如Java、Scala、Smalltalk、Eiffel、JADE、Emerald、C++、C#、VB.NET、Python等,常规程序化编程语言如C语言、Visual Basic、Fortran 2003、Perl、COBOL 2002、PHP、ABAP,动态编程语言如Python、Ruby和Groovy,或其他编程语言等。该程序编码可以完全在用户计算机上运行、或作为独立的软件包在用户计算机上运行、或部分在用户计算机上运行部分在远程计算机运行、或完全在远程计算机或服务器上运行。在后种情况下,远程计算机可以通过任何网络形式与用户计算机连接,比如局域网(LAN)或广域网(WAN),或连接至外部计算机(例如通过因特网),或在云计算环境中,或作为服务使用如软件即服务(SaaS)。The computer program codes required for the operation of each part of this manual can be written in any one or more programming languages, including object-oriented programming languages such as Java, Scala, Smalltalk, Eiffel, JADE, Emerald, C++, C#, VB.NET, Python etc., conventional procedural programming languages such as C language, Visual Basic, Fortran 2003, Perl, COBOL 2002, PHP, ABAP, dynamic programming languages such as Python, Ruby and Groovy, or other programming languages. The program code may run entirely on the user's computer, or as a stand-alone software package, or run partly on the user's computer and partly on a remote computer, or entirely on the remote computer or server. In the latter case, the remote computer can be connected to the user computer through any form of network, such as a local area network (LAN) or wide area network (WAN), or to an external computer (such as through the Internet), or in a cloud computing environment, or as a service Use software as a service (SaaS).

此外,除非权利要求中明确说明,本说明书所述处理元素和序列的顺序、数字字母的使用、或其他名称的使用,并非用于限定本说明书流程和方法的顺序。尽管上述披露中通过各种示例讨论了一些目前认为有用的发明实施例,但应当理解的是,该类细节仅起到说明的目的,附加的权利要求并不仅限于披露的实施例,相反,权利要求旨在覆盖所有符合本说明书实施例实质和范围的修正和等价组合。例如,虽然以上所描述的系统组件可以通过硬件设备实现,但是也可以只通过软件的解决方案得以实现,如在现有的服务器或移动设备上安装所描述的系统。In addition, unless explicitly stated in the claims, the order of processing elements and sequences described in this specification, the use of numbers and letters, or the use of other names are not used to limit the sequence of processes and methods in this specification. While the foregoing disclosure has discussed by way of various examples some embodiments of the invention that are presently believed to be useful, it should be understood that such detail is for illustrative purposes only and that the appended claims are not limited to the disclosed embodiments, but rather, the claims The claims are intended to cover all modifications and equivalent combinations that fall within the spirit and scope of the embodiments of this specification. For example, although the system components described above may be implemented by hardware devices, they may also be implemented by a software-only solution, such as installing the described system on an existing server or mobile device.

同理,应当注意的是,为了简化本说明书披露的表述,从而帮助对一个或多个发明实施例的理解,前文对本说明书实施例的描述中,有时会将多种特征归并至一个实施例、附图或对其的描述中。但是,这种披露方法并不意味着本说明书对象所需要的特征比权利要求中提及的特征多。实际上,实施例的特征要少于上述披露的单个实施例的全部特征。In the same way, it should be noted that in order to simplify the expression disclosed in this specification and help the understanding of one or more embodiments of the invention, in the foregoing description of the embodiments of this specification, sometimes multiple features are combined into one embodiment, drawings or descriptions thereof. This method of disclosure does not, however, imply that the subject matter of the specification requires more features than are recited in the claims. Indeed, embodiment features are less than all features of a single foregoing disclosed embodiment.

针对本说明书引用的每个专利、专利申请、专利申请公开物和其他材料,如文章、书籍、说明书、出版物、文档等,特此将其全部内容并入本说明书作为参考。与本说明书内容不一致或产生冲突的申请历史文件除外,对本说明书权利要求最广范围有限制的文件(当前或之后附加于本说明书中的)也除外。需要说明的是,如果本说明书附属材料中的描述、定义、和/或术语的使用与本说明书所述内容有不一致或冲突的地方,以本说明书的描述、定义和/或术语的使用为准。Each patent, patent application, patent application publication, and other material, such as article, book, specification, publication, document, etc., cited in this specification is hereby incorporated by reference in its entirety. Application history documents that are inconsistent with or conflict with the content of this specification are excluded, and documents (currently or later appended to this specification) that limit the broadest scope of the claims of this specification are also excluded. It should be noted that if there is any inconsistency or conflict between the descriptions, definitions, and/or terms used in the accompanying materials of this manual and the contents of this manual, the descriptions, definitions and/or terms used in this manual shall prevail .

最后,应当理解的是,本说明书中所述实施例仅用以说明本说明书实施例的原则。其他的变形也可能属于本说明书的范围。因此,作为示例而非限制,本说明书实施例的替代配置可视为与本说明书的教导一致。相应地,本说明书的实施例不仅限于本说明书明确介绍和描述的实施例。Finally, it should be understood that the embodiments described in this specification are only used to illustrate the principles of the embodiments of this specification. Other modifications are also possible within the scope of this description. Therefore, by way of example and not limitation, alternative configurations of the embodiments of this specification may be considered consistent with the teachings of this specification. Accordingly, the embodiments of this specification are not limited to the embodiments explicitly introduced and described in this specification.

尽管已描述了本发明的优选实施例,但本领域内的技术人员一旦得知了基本创造性概念,则可对这些实施例作出另外的变更和修改。所以,所附权利要求意欲解释为包括优选实施例以及落入本发明范围的所有变更和修改。While preferred embodiments of the invention have been described, additional changes and modifications to these embodiments can be made by those skilled in the art once the basic inventive concept is appreciated. Therefore, it is intended that the appended claims be construed to cover the preferred embodiment as well as all changes and modifications which fall within the scope of the invention.

显然,本领域的技术人员可以对本发明进行各种改动和变型而不脱离本发明的精神和范围。这样,倘若本发明的这些修改和变型属于本发明权利要求及其等同技术的范围之内,则本发明也意图包含这些改动和变型在内。Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention also intends to include these modifications and variations.

Claims (10)

1.一种不同前端框架间的代码转换方法,其特征在于,所述方法包括以下步骤:1. a code conversion method between different front-end frameworks, it is characterized in that, described method comprises the following steps: S1.获取源文件组件的类型;S1. Obtain the type of the source file component; S2.将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行步骤S3,若匹配失败,则执行步骤S9;S2. Match the type of the source file component with the first preset file type, if the match is successful, name the source file component as the first file component and execute step S3, if the match fails, execute step S9 ; S3.对所述第一文件组件进行解析得到模板块、代码块和样式块;S3. Parse the first file component to obtain a template block, a code block, and a style block; S4.对所述代码块进行解析得到第一抽象语法树;S4. Analyzing the code block to obtain a first abstract syntax tree; S5.对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;S5. Traversing the first abstract syntax tree to obtain a second abstract syntax tree includes obtaining the node type in the first abstract syntax tree and matching it with the first preset node type. If the match is successful, use the first The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match; S6.对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;S6. Traversing the template block to obtain the third abstract syntax tree includes obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the match is successful, using the third rule pair to match Successful nodes are processed, and if the matching fails, the user sets the corresponding fourth rule for all matching failed nodes to process; S7.合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;S7. Merging the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree; S8.基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;S8. Based on the fourth abstract syntax tree, generate a first code, merge the first code with the code of the style block, and obtain the file code of the first target component; S9.将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行步骤S10,若匹配失败,则退出;S9. Match the type of the source file component with the second preset file type, if the matching is successful, name the source file component as the second file component and execute step S10, and exit if the matching fails; S10.对所述第二文件组件进行解析得到第五抽象语法树;S10. Analyzing the second file component to obtain a fifth abstract syntax tree; S11.对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;S11. Traversing the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes obtaining the node type in the fifth abstract syntax tree and matching it with the third preset node type. If the match is successful, use the fifth abstract syntax tree. The rules process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match; S12.基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。S12. Based on the sixth abstract syntax tree, generate a second code, where the second code is a file code of a second target component. 2.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理包括:2. a kind of code conversion method between different front-end frameworks according to claim 1, it is characterized in that, if matching fails, the user then sets the corresponding second rule to all matching failed nodes and handles and comprises: a1.获取匹配失败对应节点的处理结果;a1. Obtain the processing result of the node corresponding to the matching failure; a2.判断所述处理结果是否成功,若是,则执行步骤a3,若否,则执行步骤a4;a2. judging whether the processing result is successful, if so, execute step a3, if not, execute step a4; a3.将匹配失败对应节点的节点类型添加到所述第一预设节点类型中,将匹配失败对应节点的第二规则添加到所述第一规则中;a3. adding the node type of the node corresponding to the matching failure to the first preset node type, and adding the second rule of the corresponding node of the matching failure to the first rule; a4.为匹配失败对应节点设置第七规则进行处理;a4. Set the seventh rule for the matching failure corresponding node for processing; a5.循环步骤a1-a4,直到所有匹配失败对应节点的处理结果都成功。a5. Repeat steps a1-a4 until the processing results of all matching failure nodes are successful. 3.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,所述方法还包括,获取预设前端框架组件的版本信息,基于所述版本信息,获取所述预设前端框架组件的框架信息,基于所述框架信息,对所述第一预设节点类型、所述第一规则、所述第二预设节点类型、所述第三规则、所述第三预设节点类型和所述第四规则进行更新。3. A code conversion method between different front-end frameworks according to claim 1, characterized in that the method further comprises, obtaining version information of preset front-end framework components, and obtaining the preset version information based on the version information Set the framework information of the front-end framework component, based on the framework information, the first preset node type, the first rule, the second preset node type, the third rule, the third preset Set the node type and the fourth rule to be updated. 4.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,若匹配成功,则使用第一规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干第一预设函数进行转换处理得到对应若干第一函数,所有所述第一函数均满足第一目标组件的第一语法。4. A code conversion method between different front-end frameworks according to claim 1, wherein, if the matching is successful, using the first rule to process the successfully matching node includes, based on the depth-first traversal method, matching Several first preset functions in successful nodes are transformed to obtain corresponding several first functions, all of which satisfy the first syntax of the first target component. 5.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,若匹配成功,则使用第三规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干内置指令进行转换。5. A code conversion method between different front-end frameworks according to claim 1, wherein if the matching is successful, using the third rule to process the successfully matching node includes, based on the depth-first traversal method, matching Several built-in directives in the successful node convert. 6.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,基于所述第四抽象语法树,生成第一代码并与所述样式块的代码进行合并,得到第一目标组件的文件代码包括:基于第一预设代码生成工具,对所述第四抽象语法树进行处理得到第一代码字符串,将所述第一代码字符串写入第一预设后缀文件中得到所述第一代码,将所述样式块的代码字符串写入第二预设后缀文件中得到第三代码,所述第一代码和所述第三代码合并得到所述第一目标组件的文件代码。6. A code conversion method between different front-end frameworks according to claim 1, characterized in that, based on the fourth abstract syntax tree, the first code is generated and merged with the code of the style block to obtain the first code The file code of a target component includes: based on the first preset code generation tool, processing the fourth abstract syntax tree to obtain a first code string, and writing the first code string into the first preset suffix file to obtain the first code, write the code string of the style block into the second preset suffix file to obtain the third code, combine the first code and the third code to obtain the first target component file code. 7.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,若匹配成功,则使用第五规则对匹配成功的节点进行处理包括,基于深度优先遍历方法,将匹配成功的节点中的若干第二预设函数进行改写得到若干改写结果,所有所述改写结果均满足所述第二目标组件的第二语法。7. A code conversion method between different front-end frameworks according to claim 1, wherein, if the matching is successful, then using the fifth rule to process the successfully matching node includes, based on the depth-first traversal method, matching Several second preset functions in successful nodes are rewritten to obtain several rewriting results, all of which satisfy the second syntax of the second target component. 8.根据权利要求1所述的一种不同前端框架间的代码转换方法,其特征在于,基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码包括:基于第二预设代码生成工具,对所述第六抽象语法树进行处理得到第二代码字符串,将所述第二代码字符串写入第三预设后缀文件中得到所述第二代码,所述第二代码为所述第二目标组件的文件代码。8. The code conversion method between different front-end frameworks according to claim 1, characterized in that, based on the sixth abstract syntax tree, a second code is generated, and the second code is a file of the second target component The code includes: processing the sixth abstract syntax tree to obtain a second code string based on a second preset code generation tool, and writing the second code string into a third preset suffix file to obtain the first Two codes, the second code is the file code of the second target component. 9.一种不同前端框架间的代码转换系统,其特征在于,所述系统包括:9. A code conversion system between different front-end frameworks, characterized in that the system comprises: 获取单元,用于获取源文件组件的类型;Acquisition unit, used to obtain the type of the source file component; 第一匹配单元,用于将所述源文件组件的类型与第一预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第一文件组件并执行第一解析单元,若匹配失败,则执行第二匹配单元;The first matching unit is configured to match the type of the source file component with the first preset file type, if the match is successful, name the source file component as the first file component and execute the first parsing unit, if If the matching fails, execute the second matching unit; 第一解析单元,用于对所述第一文件组件进行解析得到模板块、代码块和样式块;A first parsing unit, configured to parse the first file component to obtain a template block, a code block, and a style block; 第二解析单元,用于对所述代码块进行解析得到第一抽象语法树;a second parsing unit, configured to parse the code block to obtain a first abstract syntax tree; 第一遍历单元,用于对所述第一抽象语法树进行遍历得到第二抽象语法树包括,获取所述第一抽象语法树中节点类型并与第一预设节点类型进行匹配,若匹配成功,则使用第一规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第二规则进行处理;The first traversal unit, configured to traverse the first abstract syntax tree to obtain the second abstract syntax tree includes, obtaining the node type in the first abstract syntax tree and matching with the first preset node type, if the matching is successful , the first rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding second rule for all nodes that fail to match; 第二遍历单元,用于对所述模板块进行遍历得到第三抽象语法树包括,获取所述第三抽象语法树中节点类型并与第二预设节点类型进行匹配,若匹配成功,则使用第三规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第四规则进行处理;The second traversal unit, configured to traverse the template block to obtain a third abstract syntax tree includes: obtaining the node type in the third abstract syntax tree and matching it with the second preset node type, and if the matching is successful, using The third rule processes the nodes that match successfully, and if the match fails, the user sets the corresponding fourth rule for all nodes that fail to match; 合并单元,用于合并所述第二抽象语法树和所述第三抽象语法树得到第四抽象语法树;a merging unit, configured to merge the second abstract syntax tree and the third abstract syntax tree to obtain a fourth abstract syntax tree; 第一代码生成单元,用于基于所述第四抽象语法树,生成第一代码,将所述第一代码与所述样式块的代码进行合并,得到第一目标组件的文件代码;a first code generation unit, configured to generate a first code based on the fourth abstract syntax tree, and combine the first code with the code of the style block to obtain the file code of the first target component; 第二匹配单元,用于将所述源文件组件的类型与第二预设文件类型进行匹配,若匹配成功,则将所述源文件组件命名为第二文件组件并执行第三解析单元,若匹配失败,则退出;The second matching unit is used to match the type of the source file component with the second preset file type, if the match is successful, name the source file component as the second file component and execute the third parsing unit, if If the match fails, exit; 第三解析单元,用于对所述第二文件组件进行解析得到第五抽象语法树;a third parsing unit, configured to parse the second file component to obtain a fifth abstract syntax tree; 第三遍历单元,用于对所述第五抽象语法树进行遍历得到第六抽象语法树包括,获取所述第五抽象语法树中节点类型并与第三预设节点类型进行匹配,若匹配成功,则使用第五规则对匹配成功的节点进行处理,若匹配失败,用户则对所有匹配失败的节点设置对应的第六规则进行处理;The third traversal unit, configured to traverse the fifth abstract syntax tree to obtain the sixth abstract syntax tree includes, obtaining the node type in the fifth abstract syntax tree and matching with the third preset node type, if the matching is successful , the fifth rule is used to process the nodes that match successfully, and if the match fails, the user sets the corresponding sixth rule for all nodes that fail to match; 第二代码生成单元,用于基于所述第六抽象语法树,生成第二代码,所述第二代码为第二目标组件的文件代码。The second code generation unit is configured to generate a second code based on the sixth abstract syntax tree, where the second code is the file code of the second target component. 10.一种不同前端框架间的代码转换装置,包括存储器、处理器以及存储在所述存储器中并可在所述处理器上运行的计算机程序,其特征在于,所述处理器执行所述计算机程序时实现如权利要求1-7中任意一个所述不同前端框架间的代码转换方法的步骤。10. A code conversion device between different front-end frameworks, comprising a memory, a processor, and a computer program stored in the memory and operable on the processor, wherein the processor executes the computer program The program is the step of realizing the code conversion method between different front-end frameworks as described in any one of claims 1-7.
CN202310227287.0A 2023-03-10 2023-03-10 Code conversion method, system and device between different front-end frameworks Active CN115951890B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310227287.0A CN115951890B (en) 2023-03-10 2023-03-10 Code conversion method, system and device between different front-end frameworks

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310227287.0A CN115951890B (en) 2023-03-10 2023-03-10 Code conversion method, system and device between different front-end frameworks

Publications (2)

Publication Number Publication Date
CN115951890A true CN115951890A (en) 2023-04-11
CN115951890B CN115951890B (en) 2023-05-12

Family

ID=85906933

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310227287.0A Active CN115951890B (en) 2023-03-10 2023-03-10 Code conversion method, system and device between different front-end frameworks

Country Status (1)

Country Link
CN (1) CN115951890B (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116627402A (en) * 2023-07-19 2023-08-22 云筑信息科技(成都)有限公司 Method for realizing custom component configuration in low-code platform based on React
CN116679912A (en) * 2023-04-27 2023-09-01 深圳华为云计算技术有限公司 Code generation method, apparatus, device, storage medium, and computer program
CN118245050A (en) * 2024-03-20 2024-06-25 广州市玄武无线科技股份有限公司 Front end frame assembly automatic conversion method, system, electronic device and storage medium
CN118409793A (en) * 2024-07-01 2024-07-30 太平金融科技服务(上海)有限公司 Code migration method, device, equipment and medium for front-end framework upgrade
CN118502769A (en) * 2024-05-10 2024-08-16 重庆赛力斯凤凰智创科技有限公司 Code file detection alarm method and device, electronic equipment and storage medium

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060230378A1 (en) * 2005-03-30 2006-10-12 Lucent Technologies Inc. Technique for constructing syntax-directed search and modifications in program transformation systems
US20130031536A1 (en) * 2011-07-28 2013-01-31 De Subrato K Apparatus and method for improving the performance of compilers and interpreters of high level programming languages
CN106371829A (en) * 2016-08-24 2017-02-01 北京邮电大学 Modularization thinking-based template framework design method and system
CN110442330A (en) * 2019-07-05 2019-11-12 五八有限公司 List element conversion method, device, electronic equipment and storage medium
CN112988163A (en) * 2021-04-01 2021-06-18 中国工商银行股份有限公司 Intelligent programming language adaptation method and device, electronic equipment and medium
CN113553064A (en) * 2021-07-07 2021-10-26 北京计算机技术及应用研究所 Web front-end code conversion method
CN113760729A (en) * 2021-01-26 2021-12-07 北京沃东天骏信息技术有限公司 A code detection method and device
CN113805881A (en) * 2021-09-18 2021-12-17 上海仪电(集团)有限公司中央研究院 Method and device for converting front end components between different frames

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060230378A1 (en) * 2005-03-30 2006-10-12 Lucent Technologies Inc. Technique for constructing syntax-directed search and modifications in program transformation systems
US20130031536A1 (en) * 2011-07-28 2013-01-31 De Subrato K Apparatus and method for improving the performance of compilers and interpreters of high level programming languages
CN106371829A (en) * 2016-08-24 2017-02-01 北京邮电大学 Modularization thinking-based template framework design method and system
CN110442330A (en) * 2019-07-05 2019-11-12 五八有限公司 List element conversion method, device, electronic equipment and storage medium
CN113760729A (en) * 2021-01-26 2021-12-07 北京沃东天骏信息技术有限公司 A code detection method and device
CN112988163A (en) * 2021-04-01 2021-06-18 中国工商银行股份有限公司 Intelligent programming language adaptation method and device, electronic equipment and medium
CN113553064A (en) * 2021-07-07 2021-10-26 北京计算机技术及应用研究所 Web front-end code conversion method
CN113805881A (en) * 2021-09-18 2021-12-17 上海仪电(集团)有限公司中央研究院 Method and device for converting front end components between different frames

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
FABIO FERREIRA等: "Detecting code smells in React-based Web apps", INFORMATION AND SOFTWARE TECHNOLOGY *
李博: "企业移动端开发框架初步探索", 中华建设 *
樊鼎威: "基于TypeScript的前端MVVM框架的设计与研究", 中国优秀硕士学位论文全文数据库 信息科技辑 *

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116679912A (en) * 2023-04-27 2023-09-01 深圳华为云计算技术有限公司 Code generation method, apparatus, device, storage medium, and computer program
CN116627402A (en) * 2023-07-19 2023-08-22 云筑信息科技(成都)有限公司 Method for realizing custom component configuration in low-code platform based on React
CN116627402B (en) * 2023-07-19 2023-10-20 云筑信息科技(成都)有限公司 Method for realizing custom component configuration in low-code platform based on React
CN118245050A (en) * 2024-03-20 2024-06-25 广州市玄武无线科技股份有限公司 Front end frame assembly automatic conversion method, system, electronic device and storage medium
CN118245050B (en) * 2024-03-20 2024-09-17 广州市玄武无线科技股份有限公司 Front end frame assembly automatic conversion method, system, electronic device and storage medium
CN118502769A (en) * 2024-05-10 2024-08-16 重庆赛力斯凤凰智创科技有限公司 Code file detection alarm method and device, electronic equipment and storage medium
CN118409793A (en) * 2024-07-01 2024-07-30 太平金融科技服务(上海)有限公司 Code migration method, device, equipment and medium for front-end framework upgrade
CN118409793B (en) * 2024-07-01 2024-10-01 太平金融科技服务(上海)有限公司 Code migration method, device, equipment and medium for front-end framework upgrade

Also Published As

Publication number Publication date
CN115951890B (en) 2023-05-12

Similar Documents

Publication Publication Date Title
CN115951890B (en) Code conversion method, system and device between different front-end frameworks
CN103164249B (en) Extension mechanism for script compiler
CN108628635B (en) Method, device, equipment and storage medium for acquiring parameter name and local variable name
CN112100054A (en) A program static analysis method and system for data management and control
US20090328016A1 (en) Generalized expression trees
CN112988163A (en) Intelligent programming language adaptation method and device, electronic equipment and medium
CN113504900A (en) Programming language conversion method and device
CN114513566A (en) Custom network protocol analysis method, system, medium and electronic device
US20180143814A1 (en) Machine-based normalization of machine instructions
CN103034486A (en) Automatic optimization method based on full-system expansion call graph for mobile terminal operation system
Ahmad et al. Leveraging parallel data processing frameworks with verified lifting
CN115509514A (en) A front-end data simulation method, device, equipment and medium
CN112579151A (en) Method and device for generating model file
CN111158665B (en) Code generation method and device, electronic equipment and storage medium
Li et al. Efficient bottom-up synthesis for programs with local variables
US10656926B2 (en) Compact type layouts
Nguyen et al. Extending python for quantum-classical computing via quantum just-in-time compilation
Baziotis et al. Dias: Dynamic Rewriting of Pandas Code
JP5600301B2 (en) System representation and handling technology
CN117971236A (en) Operator analysis method, device, equipment and medium based on lexical and grammatical analysis
CN117170684A (en) File compiling method, device, equipment and storage medium
CN113312038B (en) Implementation method for generating Java code class relay structure
CN114416050B (en) Swift code processing method, device, electronic device and storage medium
CN114675867B (en) A data processing method, device, server and storage medium
Singh Creating Adaptive GraphQL Client for Github Repo Analysis Using Scala Macros

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载