Code detection method and device, electronic equipment and storage medium

By traversing the abstract syntax tree of the code and judging the exception handling mechanism, the system instability caused by code irregularity is solved, and the system stability and resource optimization are achieved.

CN120336151APending Publication Date: 2025-07-18TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410056731.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-15
Publication Date
2025-07-18

AI Technical Summary

Technical Problem

The exception handling mechanism of the code in the prior art is not standardized, resulting in large consumption of system resources and risk of paralysis, affecting system stability.

Method used

By obtaining the abstract syntax tree of the code, traverse each node, and determine whether the target node has exception processing information. If there is no, traverse its ancestor node and judge the type information of the ancestor node. If it does not match the exception detection statement type, then determine the code as an exception code, and generate prompts and correction information.

Benefits of technology

Effectively detect the exception handling mechanism in the code, reduce the risk of system paralysis, improve system operation stability, and reduce resource consumption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120336151A_ABST
    Figure CN120336151A_ABST
Patent Text Reader

Abstract

The invention discloses a code detection method and device, electronic equipment and a storage medium, the method comprises the steps that each node in an abstract syntax tree corresponding to a to-be-detected code is traversed, and each node comprises a parameter of the to-be-detected code and attribute information of the parameter; under the condition that a target node is traversed and attribute information of parameters included in a target child node of the target node does not include exception processing information, a predecessor node of the target node in the abstract syntax tree is traversed, and the target node is used for representing a to-be-executed parameter of the to-be-detected code; when it is determined that the type information of the ancestor node does not include the first preset type information, determining the to-be-detected code as an abnormal code; the first preset type information is used for representing that the type information of the node is an abnormal detection statement type. By utilizing the technical scheme provided by the invention, the abnormal processing mechanism in the code can be effectively detected, the paralysis risk of a system for running the code is reduced, and the running stability of the system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and particularly to a code detection method, device, electronic device, and storage medium. Background Art

[0002] With the rapid development of the software industry and the increasing growth of software requirements, the standardization and stability of development code are crucial. The robustness of the code directly affects the robustness of the system. Abnormal code can cause large consumption of system resources and even prevent the system from running properly. Therefore, during the software program development process, it is necessary to perform abnormal detection on the code to improve the stability of the system operation, reduce the consumption of system resources, and avoid the risk of system paralysis. Summary of the Invention

[0003] The present application provides a code detection method, device, electronic device, and storage medium, which can effectively detect the exception handling mechanism in the code, improve the stability of the system running the code, and reduce the resource consumption of the system running the code.

[0004] On the one hand, the present application provides a code detection method, and the method includes:

[0005] Obtain an abstract syntax tree corresponding to the code to be detected; the abstract syntax tree includes at least two nodes, and each node includes the parameters of the code to be detected and the attribute information of the parameters;

[0006] Traverse each node in the abstract syntax tree;

[0007] When traversing to a target node and the attribute information of the target parameter does not include exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree; the attribute information of the target parameter is the attribute information of the parameter included in the target child node of the target node in the abstract syntax tree, and the target node is used to represent the parameter to be executed of the code to be detected;

[0008] When it is determined that the type information of the ancestor node does not include the first preset type information, determine the code to be detected as abnormal code; the first preset type information is used to represent that the type information of the node is the exception detection statement type.

[0009] Optionally, after determining the code to be detected as abnormal code, the method further includes:

[0010] Generate a first preset prompt message and a first correction message;

[0011] Send the first preset prompt message, the first correction information, and the target location to the terminal, so that the terminal displays the first preset prompt message and the first correction information at the target location in the code to be detected; the target location is determined based on the target node, and the first preset prompt message is used to prompt that there is a first exception handling problem in the code to be detected.

[0012] Optionally, the method further includes:

[0013] When traversing to the target node and the attribute information of the target parameter includes the exception handling information, determine the code to be detected as an abnormal code, and generate a second preset prompt message and a second correction information;

[0014] Send the second preset prompt message, the second correction information, and the target location to the terminal, so that the terminal displays the second preset prompt message and the second correction information at the target location; the target location is determined based on the target node, and the second preset prompt message is used to prompt that there is a second exception handling problem in the code to be detected.

[0015] Optionally, when traversing to the target node and the attribute information of the target parameter does not include the exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree includes:

[0016] When traversing to the target node, traverse the child nodes of the target node in the abstract syntax tree;

[0017] When the type information of the currently traversed child node is the second preset type information, determine the currently traversed child node as the target child node, and the second preset type information is used to represent that the type information of the node is a call expression type;

[0018] When the attribute information of the target parameter included in the target child node does not include the exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

[0019] Optionally, the attribute information includes name information, and when the attribute information of the target parameter included in the target child node does not include the exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree includes:

[0020] When the name information of the target parameter does not include the exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

[0021] Optionally, the target location is determined through the following steps:

[0022] Taking the target node as the root node, determine the target subtree in the abstract syntax tree;

[0023] Determine the position of the target code segment corresponding to the target subtree in the code to be detected as the target position.

[0024] Optionally, before traversing the ancestor nodes of the target node in the abstract syntax tree, the method further includes:

[0025] When the type information of the currently traversed node is the third preset type information, determine the currently traversed node as the target node, and the third preset type information is used to represent that the type information of the node is the waiting expression type.

[0026] On the other hand, a code detection device is provided, and the device includes:

[0027] An acquisition module, configured to acquire an abstract syntax tree corresponding to the code to be detected; the abstract syntax tree includes at least two nodes, and each node includes the parameters of the code to be detected and the attribute information of the parameters;

[0028] A first traversal module, configured to traverse each node in the abstract syntax tree;

[0029] A second traversal module, configured to traverse the ancestor nodes of the target node in the abstract syntax tree when the target node is traversed and the attribute information of the target parameter does not include exception handling information; the attribute information of the target parameter is the attribute information of the parameter included in the target child node of the target node in the abstract syntax tree, and the target node is used to represent the waiting execution parameter of the code to be detected;

[0030] A first exception code determination module, configured to determine the code to be detected as an exception code when it is determined that the type information of the ancestor node does not include the first preset type information; the first preset type information is used to represent that the type information of the node is the exception detection statement type.

[0031] On the other hand, an electronic device for code detection is provided, including: a processor;

[0032] A memory for storing executable instructions of the processor;

[0033] Wherein, the processor is configured to execute the instructions to implement the code detection method described in any one of the above.

[0034] On the other hand, a computer-readable storage medium is provided. When the instructions in the computer-readable storage medium are executed by the processor of the electronic device, the electronic device can execute the code detection method described in any one of the above.

[0035] On the other hand, a computer program product or a computer program is provided. The computer program product or the computer program includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the code detection method provided in the above various alternative implementation manners.

[0036] The code detection method, device, electronic device and storage medium provided in this application have the following technical effects:

[0037] This application obtains an abstract syntax tree corresponding to the code to be detected. The abstract syntax tree includes at least two nodes, and each node includes parameters of the code to be detected and attribute information of the parameters. Each node in the abstract syntax tree is traversed. When the target node is traversed and the attribute information of the parameters included in the target child node of the target node does not include exception handling information, the ancestor nodes of the target node in the abstract syntax tree are traversed, where the target node is used to represent the parameter to be executed of the code to be detected; when it is determined that the type information of the ancestor node does not include the first preset type information, the code to be detected is determined as an abnormal code. The above first preset type information is used to represent that the type information of the node is an exception detection statement type, so as to effectively detect the exception handling mechanism in the code, reduce the paralysis risk of the system running the code, improve the stability of the system operation, and at the same time reduce the resource consumption of the system running the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] In order to more clearly illustrate the technical solutions and advantages in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required to be used in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0039] Figure 1 is a schematic diagram of an application environment of a code detection method shown according to an exemplary embodiment;

[0040] Figure 2 is a schematic flowchart of a code detection method shown according to an exemplary embodiment;

[0041] Figure 3 is a schematic flowchart of traversing ancestor nodes shown according to an exemplary embodiment;

[0042] Figure 4 is a schematic flowchart of a code detection process shown according to an exemplary embodiment;

[0043] Figure 5 is a schematic structural diagram of an abstract syntax tree shown according to an exemplary embodiment;

[0044] Figure 6 is a schematic diagram of a display interface of a first abnormal problem shown according to an exemplary embodiment;

[0045] Figure 7 is a schematic structural diagram of another abstract syntax tree shown according to an exemplary embodiment;

[0046] Figure 8 is a schematic structural diagram of another abstract syntax tree shown according to an exemplary embodiment;

[0047] Figure 9 is a schematic diagram of a display interface of a second abnormal problem shown according to an exemplary embodiment;

[0048] Figure 10 is a schematic structural diagram of a code detection device shown according to an exemplary embodiment;

[0049] Figure 11 is a schematic structural diagram of a server shown according to an exemplary embodiment. Detailed implementation manners

[0050] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.

[0051] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above accompanying drawings are used to distinguish similar objects, and do not necessarily need to describe a specific order or sequence. It should be understood that such used data may be interchanged under appropriate circumstances so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or server including a series of steps or units does not necessarily have to be limited to those clearly listed steps or units, but may include other steps or units not clearly listed or inherent to these processes, methods, products, or devices.

[0052] In the embodiments of the present application, the term "module" or "unit" refers to a computer program with a predetermined function or a part of a computer program, which works together with other related parts to achieve a predetermined goal, and can be implemented in whole or in part by using software, hardware (such as a processing circuit or a memory), or a combination thereof. Similarly, one processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be a part of an overall module or unit that includes the function of the module or unit.

[0053] Please refer to Figure 1 , Figure 1 which is a schematic diagram of an application environment of a code detection method provided by the embodiments of the present application. The application environment may at least include a server 100 and a terminal 200.

[0054] In an alternative embodiment, the server 100 can be used to perform anomaly detection on the code to be detected, and after determining the code to be detected as an abnormal code, generate a preset prompt message, a correction message, and determine the target location, and send them to the terminal 200. Specifically, the server 100 can be an independent physical server, or a server cluster or a distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.

[0055] In an alternative embodiment, the terminal 200 can be used to display the preset prompt message and the correction message at the target location in the code to be detected based on the preset prompt message, the correction message, and the target location sent by the server 100. Specifically, the terminal 200 can include, but is not limited to, electronic devices such as smartphones, desktop computers, tablet computers, laptop computers, smart speakers, digital assistants, augmented reality (AR) / virtual reality (VR) devices, smart wearable devices, vehicle-mounted terminals, smart TVs, etc.; it can also be software running on the above-mentioned electronic devices, such as application programs, applets, etc. The operating systems running on the electronic devices in the embodiments of the present application can include, but are not limited to, Android systems, IOS systems, Linux, Windows, etc.

[0056] In addition, it should be noted that Figure 1 what is shown is only an application environment of a code detection method, and the embodiments of this specification are not limited thereto.

[0057] In the embodiments of this specification, the above-mentioned server 100 and terminal 200 can be directly or indirectly connected through wired or wireless communication methods, and the present application does not make any restrictions here.

[0058] In a network request, various abnormal situations may occur. For example, network connection abnormalities, server abnormalities, Domain Name System (DNS) resolution abnormalities, cross - domain abnormalities, request timeouts, requests aborted by users or browsers, request Uniform Resource Locator (URL) errors, and server - returned error status codes, etc. When processing network requests, requests are usually initiated through the await statement, but there may be a lack of corresponding exception - handling mechanisms for possible errors in the requests, or the exception - handling mechanisms are set irregularly, making the code have a relatively high potential risk. In the case of a request error, it will cause the entire code to be incorrect, and may further lead to the system being unable to run properly. Therefore, the present invention proposes a code detection method to effectively detect the exception - handling mechanisms in the code, reduce the risk of paralysis of the system running the code, and improve the stability of the system operation.

[0059] The following introduces a code detection method of the present application. Figure 2 It is a schematic flowchart of a code detection method provided by an embodiment of the present application. This specification provides method operation steps such as in the embodiment or flowchart, but based on routine or non - creative labor, it may include more or fewer operation steps. The step order listed in the embodiment is only one way among the execution orders of numerous steps and does not represent the only execution order. When actually executed in a system or terminal product, it can be executed in the order shown in the embodiment or the drawing or executed in parallel (for example, in an environment of parallel processors or multi - threaded processing). Specifically, as Figure 2 shown, the above - mentioned method may include:

[0060] S201: Obtain the abstract syntax tree corresponding to the code to be detected.

[0061] In a specific embodiment, the code to be detected may be the code that needs to perform exception detection, such as the code that needs to detect the exception - handling mechanism. The abstract syntax tree may be a tree - like representation of the abstract syntax structure of the code, and the abstract syntax tree can represent the syntax structure of a programming language in a tree - like form. Specifically, the abstract syntax tree may include at least two nodes, and each node of the abstract syntax tree may represent a structure in the code. Each node may include the parameters of the code to be detected and the attribute information (property) of the parameters. Specifically, the parameters of the code to be detected may include syntax elements such as functions, variables, and expressions, and the above - mentioned attribute information may include name information (name) and type information (type), etc.

[0062] Optionally, obtaining the abstract syntax tree corresponding to the code to be detected may include:

[0063] Obtain the code to be detected;

[0064] Parse the code to be detected based on a preset open-source tool to generate an abstract syntax tree.

[0065] In a specific embodiment, the preset open-source tool may include a compiler, a parser, a code inspection tool, etc., such as Espree, Babel, ESLint, etc. Specifically, the preset open-source tool may include a lexical analyzer, a syntax analyzer, and a semantic analyzer. Among them, the lexical analyzer can be used to convert a character stream into a token stream, the syntax analyzer can be used to convert the token stream into a syntax tree, and the semantic analyzer can be used to collect attribute information of identifiers, such as type, kind, storage location, length value, scope, elements, and return value information, etc.

[0066] In practical applications, the code can be first lexically analyzed by an open-source tool to decompose the code into a series of tokens. Among them, a token is the smallest unit in the code and can be a variable name, an operator, a punctuation mark, etc. For example, for the code var x = 1, it will be decomposed into var, x, =, 1, and ; these tokens. Then, syntax analysis is performed to combine the tokens into individual syntax structures (such as variable declarations, function calls, etc.) and organize these syntax structures into an abstract syntax tree. During this process, attribute information of identifiers is collected, and at the same time, the syntax of the code can be checked for correctness, such as whether there are unclosed parentheses or undeclared variables, etc., so as to obtain the abstract syntax tree corresponding to the code.

[0067] S203: Traverse each node in the abstract syntax tree.

[0068] In a specific embodiment, each node in the abstract syntax tree can be traversed based on a preset traversal method. Specifically, the preset traversal method can be depth-first traversal, linear traversal, etc., and the traversal order can be a top-down sequential traversal or a bottom-up reverse traversal.

[0069] S205: When the target node is traversed and the attribute information of the target parameter does not include exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

[0070] In a specific embodiment, the target node can be used to represent the waiting execution parameter of the code to be detected. The attribute information of the target parameter can be the attribute information of the parameter included in the target child node of the target node in the abstract syntax tree. The target child node can be used to represent the call expression parameter of the code to be detected. Specifically, the target node can be an AwaitExpression node, and the target child node can be the method child node of the target node. For example, the target child node can be a CallExpression node. The above waiting execution parameter can be an await expression, and the call expression parameter can be a method call expression in the await expression. The exception handling information can be catch information.

[0071] In practical applications, when traversing to an AwaitExpression node and the attribute information of the parameter of the CallExpression node, which is the method child node of the AwaitExpression node, is not catch information, traverse the upper-level node of the AwaitExpression node in the abstract syntax tree.

[0072] In an alternative embodiment, Figure 3 is a schematic flowchart of traversing ancestor nodes shown according to an exemplary embodiment, as Figure 3 shown. In the case of traversing to the target node and the attribute information of the target parameter not including exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree may include:

[0073] S301: When traversing to the target node, traverse the child nodes of the target node in the abstract syntax tree.

[0074] In a specific embodiment, when traversing to an AwaitExpression node, traverse the child nodes of the AwaitExpression node in the abstract syntax tree.

[0075] S303: When the type information of the currently traversed child node is the second preset type information, determine the currently traversed child node as the target child node.

[0076] In a specific embodiment, the second preset type information can be used to represent that the type information of the node is the call expression type. Specifically, the call expression type can be the CallExpression type, that is, the type information of the target child node is the CallExpression type. Specifically, the type information of the node can be obtained based on the keyword type in the abstract syntax tree.

[0077] S305: When the attribute information of the target parameter included in the target child node does not include exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

[0078] Optionally, when the attribute information of the target parameter included in the target child node does not include exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree may include:

[0079] When the name information of the target parameter does not include exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

[0080] In practical applications, when traversing to an AwaitExpression node, a child CallExpression node with a type information of CallExpression type can be found in the arguments of the AwaitExpression node, and then it is further deeply queried whether the name information (callee.property.name) of the CallExpression node is catch. When the name information is not catch, it indicates that in the corresponding code to be detected, the wait execution statement (await expression statement) does not end with a catch statement, and then the upper-level node of the AwaitExpression node in the abstract syntax tree is traversed again.

[0081] In the above embodiment, when traversing to the target node, first continue to traverse downward, traverse the child nodes of the target node, and when the name information of its child nodes does not include exception handling information, then traverse the upper-level node of the target node, reducing the resource occupation of the server during the traversal process and ensuring the overall performance.

[0082] In an optional embodiment, before traversing the ancestor nodes of the target node in the above abstract syntax tree, the method may further include:

[0083] When the type information of the currently traversed node is the third preset type information, determine the currently traversed node as the target node.

[0084] In a specific embodiment, the third preset type information may be used to represent that the type information of the node is the wait expression type. Specifically, the wait expression type may be the AwaitExpression type, that is, the type information of the target node is the AwaitExpression type.

[0085] S207: When it is determined that the type information of the ancestor node does not include the first preset type information, determine the code to be detected as abnormal code.

[0086] In a specific embodiment, the first preset type information can be used to represent that the type information of the node is an anomaly detection statement type. Specifically, the anomaly detection statement type can be the TryStatement type. When the type information of the node is the TryStatement type, the node is a TryStatement node.

[0087] In a specific embodiment, the abnormal code can be code that does not conform to the code specification and has a relatively high potential risk. For example, when processing a network request, a request can be initiated through an await statement, but there is a lack of corresponding anomaly handling mechanism for possible errors in the request, or the anomaly handling mechanism is set irregularly. In the case of an error in the request, it will cause the entire code to be incorrect, and further may cause the system to fail to run properly.

[0088] In practical applications, various errors may occur in network requests. For example, ① network connection problems: If the user's device is not connected to the Internet, or the network connection is unstable, then the network request may fail. ② Server problems: If the server fails, or the server is under maintenance, then the network request may fail. In addition, if the server's load is too high, it may also cause the request to time out or fail. ③ Domain Name System (DNS) resolution problems: If the DNS server cannot resolve the Uniform Resource Locator (URL) of the request, then the network request may fail. ④ Cross-origin problems: For security reasons, browsers implement the same-origin policy, which restricts requests from different origins. If Cross-Origin Resource Sharing (CORS) is not configured correctly, then cross-origin requests may fail. ⑤ Request timeout: If the processing time of the request exceeds the set timeout time, then the network request may fail. ⑥ The request is aborted by the user or the browser: If the user or the browser aborts the request (for example, the user closes the page, or the browser cancels the request when the page is unloaded), then the network request may fail. ⑦ Request URL error: If the request URL is incorrect (for example, the URL contains invalid characters, or the URL points to a non-existent resource), then the network request may fail. ⑧ The server returns an error status code: If the server returns an error status code (for example, 404 indicates that the resource was not found, and 500 indicates an internal server error), then although the network request itself is successful, the processing result of the request may be a failure.

[0089] In practical applications, traverse the upper nodes of the AwaitExpression node in the abstract syntax tree layer by layer, query the type information of each upper node. When the type information of all upper nodes is not of the TryStatement type, that is, there is no TryStatement node above the AwaitExpression node, it can be indicated that in the corresponding code to be detected, the await expression statement is not wrapped by an exception handling statement (try-catch statement), does not have an exception handling mechanism, does not conform to the code specification, and the corresponding code has a relatively high potential risk and code exception, and corresponding subsequent processing can be performed. When the type information of a certain upper node is of the TryStatement type, this node is a TryStatement node, that is, there is a TryStatement node above the AwaitExpression node, it can be indicated that in the corresponding code to be detected, the await expression statement is wrapped by a try-catch statement, has an exception handling mechanism, conforms to the code specification, and the potential risk of the corresponding code point is relatively low and the code is not abnormal. As long as traversing upward to the TryStatement node and ensuring that there is a TryStatement node above the AwaitExpression node, it can be ensured that the code will not cause the entire code execution error due to the await statement exception, and the upward traversal can be stopped, thereby reducing the resource occupancy of the server during the traversal process and ensuring the overall performance.

[0090] In an optional embodiment, after determining the code to be detected as an abnormal code as described above, the method may further include:

[0091] Generate a first preset prompt message and a first correction message;

[0092] Send the first preset prompt message, the first correction message, and the target position to the terminal, so that the terminal displays the first preset prompt message and the first correction message at the target position in the code to be detected.

[0093] In a specific embodiment, the first preset prompt message can be used to prompt that there is a first exception handling problem in the code to be detected, and the first correction message can be the correction message corresponding to the first exception handling problem. This correction message can include modification suggestions for the abnormal code or the correct coding method. Specifically, the first exception handling problem can be the problem that the await expression statement is not wrapped by a try-catch statement. The target position can be determined based on the target node. Specifically, the target position can be the position of the above-mentioned await expression statement in the code to be detected.

[0094] In practical applications, during the process of the terminal displaying the above prompt information and correction information, the above information can be displayed in the form of annotations. Specifically, multiple display methods can be set. For example, the prompt information and correction information can be continuously displayed at the position where the await expression statement is located, or the prompt information and correction information can be displayed only when the pointing tool (such as a mouse or finger) hovers over the position where the await expression statement is located. Moreover, while displaying the above information, the await expression statement can be highlighted. For example, it can be marked with a line of a higher saturation color below the await expression statement, or the area of the await expression statement can be filled with a color block of a higher saturation color, so that the end user (such as a developer) can correct the abnormal code segment based on the prompt information and correction information.

[0095] In an optional embodiment, the above target position can be determined through the following steps:

[0096] Taking the target node as the root node, determine the target subtree in the abstract syntax tree;

[0097] Determine the position of the target code segment corresponding to the target subtree in the code to be detected as the target position.

[0098] In a specific embodiment, the target subtree can be the subtree in the above abstract syntax tree with the AwaitExpression node as the root node. The target code segment corresponding to the target subtree can be the await expression statement in the above code to be detected, and the target position can be the position where the await expression statement is located in the code to be detected.

[0099] In an optional embodiment, the above method may further include:

[0100] When the target node is traversed and the attribute information of the target parameter includes exception handling information, determine the code to be detected as abnormal code, and generate a second preset prompt information and a second correction information;

[0101] Send the second preset prompt information, the second correction information, and the target position to the terminal, so that the terminal displays the second preset prompt information and the second correction information at the target position.

[0102] In a specific embodiment, the second preset prompt information can be used to prompt that there is a second exception handling problem in the code to be detected. The second exception handling problem can be the problem that the await expression statement ends with a catch statement.

[0103] Specifically, in the case of traversing an AwaitExpression node, a child node CallExpression node with a type information of CallExpression type can be searched in the argument of the AwaitExpression node, and then the name information (callee.property.name) of the CallExpression node can be deeply queried to see if it is "catch". If the name information is "catch", it indicates that in the corresponding code to be detected, the await expression statement ends with a catch statement, which does not conform to the code specification, and the corresponding code has a relatively high potential risk and code anomalies. Then, prompt information and correction information are generated, and the position where the await expression statement is located in the code to be detected is determined based on the subtree with the AwaitExpression node as the root node. The prompt information, correction information, and the above position are sent to the terminal, and the terminal displays the above prompt information and correction information at the position where the await expression statement is located.

[0104] In practical applications, a plugin for code detection can be developed and encapsulated based on the ESLint tool to implement the above code detection method. The above plugin can be obtained by making corresponding configurations for files, rules, parsers, etc. in the ESLint tool, realizing the effective detection of the exception handling mechanism in the code in a lightweight manner, which is convenient for popularization and application.

[0105] In a specific embodiment, as Figure 4 shown, Figure 4 FIG. is a flowchart showing the code detection process according to an exemplary embodiment. Specifically, the server obtains the code to be detected, first performs lexical analysis on the code to be detected through the ESLint tool, decomposes the code to be detected into a series of tokens, and then performs syntax analysis to combine the tokens into individual syntax structures (such as variable declarations, function calls, etc.), and organizes the syntax structures into an abstract syntax tree. In this process, the attribute information of the identifiers is collected, and at the same time, the syntax of the code is checked to see if it is correct, such as whether there are unclosed parentheses, whether there are undeclared variables, etc., so as to obtain the abstract syntax tree corresponding to the code to be detected.

[0106] After that, the server traverses each node in the abstract syntax tree based on the depth-first traversal method. When traversing to any node, it queries the type information of the node. When the type information is of the AwaitExpression type, the corresponding node is an AwaitExpression node. When traversing to an AwaitExpression node, it searches for a child CallExpression node whose type information is of the CallExpression type in the parameter (argument) of the AwaitExpression node, and then deeply queries whether the name information (callee.property.name) of the CallExpression node is "catch". On the one hand, as Figure 5 shown, Figure 5 in the abstract syntax tree, the name information of the CallExpression node is not "catch", and there is no TryStatement node above the AwaitExpression node. When the name information is not "catch", it indicates that in the corresponding code to be detected, the waiting execution statement (await expression statement) does not end with a catch statement. Furthermore, it then traverses layer by layer the upper nodes of the AwaitExpression node in the abstract syntax tree and queries the type information of each upper node.

[0107] In the first case, as Figure 5 shown, when the type information of the upper nodes of the AwaitExpression node is not of the TryStatement type, that is, there is no TryStatement node above the AwaitExpression node, it can be indicated that in the corresponding code to be detected, there is an abnormal problem that the await expression statement is not wrapped by a try-catch statement, lacks an exception handling mechanism, does not conform to the code specification, and the corresponding code has a relatively high potential risk. Then, it generates prompt information and correction information corresponding to the above abnormal problem. For example, as Figure 6As shown, the prompt message is "await is not wrapped by trycatch", and the correction message can be "Refer to try{res = await req()}catch{res = []}", and determine the position of the code segment corresponding to the subtree with the AwaitExpression node as the root node in the abstract syntax tree (i.e., the await expression statement) in the code to be detected as the display position. Furthermore, the background server sends the prompt message, correction message, and display position to the terminal. The terminal floats and displays the prompt message and correction message in the form of comments at the above display position, and marks it with a yellow wavy line below the above display position, so that the terminal user (such as a developer) can correct the abnormal code segment based on the prompt message and correction message.

[0108] In the second case, as Figure 7 shown, when the type information of a certain upper-layer node is of the TryStatement type, that is, there is a TryStatement node above the AwaitExpression node, stop traversing upward, which can indicate that in the corresponding code to be detected, the await expression statement is wrapped by a try-catch statement, has an exception handling mechanism, conforms to the code specification, has a relatively low potential risk at the corresponding code point, and the code is not abnormal.

[0109] On the other hand, as Figure 8 shown, Figure 8 In the boxed area in, property represents the attribute information of the CallExpression node, and name represents the name information. When the name information of the CallExpression node is catch, it indicates that in the corresponding code to be detected, there is an abnormal problem that the await expression statement ends with a catch statement, does not conform to the code specification, the corresponding code has a relatively high potential risk, and the code is abnormal. Furthermore, generate a prompt message and a correction message corresponding to the above abnormal problem. For example, as Figure 9 shown, the prompt message is "It is not recommended to directly follow catch with await", and the correction message can be "Refer to let res; try{res = await req()}catch{res = []}", and determine the position of the code segment corresponding to the subtree with the AwaitExpression node as the root node in the abstract syntax tree in the code to be detected as the display position. Furthermore, the server sends the prompt message, correction message, and display position to the terminal. The terminal floats and displays the prompt message and correction message in the form of comments at the above display position, and marks it with a yellow wavy line below the above display position, so that the terminal user (such as a developer) can correct the abnormal code segment based on the prompt message and correction message.

[0110] As can be seen from the technical solutions provided in the embodiments of this specification above, in this specification, an abstract syntax tree corresponding to the code to be detected is obtained. The abstract syntax tree includes at least two nodes, and each node includes the parameters of the code to be detected and the attribute information of the parameters. Each node in the abstract syntax tree is traversed. When the target node is traversed and the attribute information of the parameters included in the target child node of the target node does not include exception handling information, the ancestor nodes of the target node in the abstract syntax tree are traversed, where the target node is used to represent the parameter to be executed of the code to be detected; when it is determined that the type information of the ancestor node does not include the first preset type information, the code to be detected is determined as an abnormal code, and the above-mentioned first preset type information is used to represent that the type information of the node is an exception detection statement type, so that the effective detection of the exception handling mechanism in the code can be realized, the paralysis risk of the system running the code can be reduced, the stability of the system operation can be improved, and the resource consumption of the system running the code can be reduced.

[0111] The embodiments of the present application also provide a code detection device, as Figure 10 shown. The above device may include:

[0112] An acquisition module 1010, configured to acquire an abstract syntax tree corresponding to the code to be detected; the abstract syntax tree includes at least two nodes, and each node includes the parameters of the code to be detected and the attribute information of the parameters;

[0113] A first traversal module 1020, configured to traverse each node in the abstract syntax tree;

[0114] A second traversal module 1030, configured to traverse the ancestor nodes of the target node in the abstract syntax tree when the target node is traversed and the attribute information of the target parameter does not include exception handling information; the attribute information of the target parameter is the attribute information of the parameter included in the target child node of the target node in the abstract syntax tree, and the target node is used to represent the parameter to be executed of the code to be detected;

[0115] A first abnormal code determination module 1040, configured to determine the code to be detected as an abnormal code when it is determined that the type information of the ancestor node does not include the first preset type information; the first preset type information is used to represent that the type information of the node is an exception detection statement type.

[0116] Optionally, the above device may further include:

[0117] A generation module, configured to generate a first preset prompt message and a first correction message;

[0118] A first sending module, configured to send a first preset prompt message, a first correction message, and a target location to a terminal, so that the terminal displays the first preset prompt message and the first correction message at the target location in the code to be detected; the target location is determined based on a target node, and the first preset prompt message is used to prompt that there is a first exception handling problem in the code to be detected.

[0119] Optionally, the above device may further include:

[0120] A second abnormal code determination module, configured to determine the code to be detected as an abnormal code and generate a second preset prompt message and a second correction message when traversing to the target node and the attribute information of the target parameter includes exception handling information;

[0121] A second sending module, configured to send the second preset prompt message, the second correction message, and the target location to the terminal, so that the terminal displays the second preset prompt message and the second correction message at the target location; the target location is determined based on the target node, and the second preset prompt message is used to prompt that there is a second exception handling problem in the code to be detected.

[0122] Optionally, the second traversal module 1030 may include:

[0123] A first traversal unit, configured to traverse the child nodes of the target node in the abstract syntax tree when traversing to the target node;

[0124] A target child node determination unit, configured to determine the currently traversed child node as a target child node when the type information of the currently traversed child node is the second preset type information, and the second preset type information is used to characterize that the type information of the node is a call expression type;

[0125] A second traversal unit, configured to traverse the ancestor nodes of the target node in the abstract syntax tree when the attribute information of the target parameter included in the target child node does not include exception handling information.

[0126] Optionally, the attribute information includes name information, and the second traversal unit may include:

[0127] A third traversal unit, configured to traverse the ancestor nodes of the target node in the abstract syntax tree when the name information of the target parameter does not include exception handling information.

[0128] Optionally, the above target location may be determined by the following unit:

[0129] A target subtree determination unit, configured to determine a target subtree in the abstract syntax tree with the target node as the root node;

[0130] A target location determination unit, configured to determine the location of the target code segment corresponding to the target subtree in the code to be detected as the target location.

[0131] Optionally, the above device may further include:

[0132] A target node determination module, configured to determine the currently traversed node as the target node when the type information of the currently traversed node is the third preset type information, where the third preset type information is used to characterize that the type information of the node is a wait expression type.

[0133] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment related to the method, and will not be elaborated herein.

[0134] Figure 11 is a hardware structure block diagram of a server for a code detection method provided by an embodiment of the present application. As Figure 11 shown, the server 1100 may vary greatly due to configuration or performance differences, and may include one or more central processing units (CPUs) 1110 (the central processing unit 1110 may include, but is not limited to, a processing device such as a microprocessor MCU or a programmable logic device FPGA), a memory 1130 for storing data, and one or more storage media 1120 for storing application programs 1123 or data 1122 (such as one or more mass storage devices). Among them, the memory 1130 and the storage media 1120 may be transient storage or persistent storage. The program stored in the storage media 1120 may include one or more modules, and each module may include a series of instruction operations on the server. Further, the central processing unit 1110 may be configured to communicate with the storage media 1120 and execute a series of instruction operations in the storage media 1120 on the server 1100. The server 1100 may further include one or more power supplies 1160, one or more wired or wireless network interfaces 1150, one or more input / output interfaces 1140, and / or one or more operating systems 1121, such as Windows ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, and so on.

[0135] The input / output interface 1140 can be used to receive or send data via a network. Specific examples of the above-mentioned network can include a wireless network provided by a communication provider of the server 1100. In one example, the input / output interface 1140 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the input / output interface 1140 can be a Radio Frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0136] Those of ordinary skill in the art can understand that Figure 11 the structure shown is only schematic and does not limit the structure of the above-mentioned electronic device. For example, the server 1100 may also include more or fewer components than Figure 11 shown therein, or have a different configuration from Figure 11 that shown.

[0137] In an exemplary embodiment, there is also provided an electronic device for code detection, including: a processor; a memory for storing instructions executable by the processor; wherein, the processor is configured to execute the instructions to implement the code detection method as in the embodiments of the present disclosure.

[0138] In an exemplary embodiment, there is also provided a computer-readable storage medium, when the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device can execute the code detection method in the embodiments of the present disclosure.

[0139] In an exemplary embodiment, there is also provided a computer program product or a computer program, the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the code detection method provided in the above various alternative implementation manners.

[0140] It can be understood that in the specific implementation manner of the present application, when it comes to user-related data, when the above embodiments of the present application are applied to specific products or technologies, user permission or consent needs to be obtained, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.

[0141] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, storage, database, or other medium used in the embodiments provided in the present application can include non-volatile and / or volatile memories. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM), etc.

[0142] After considering the specification and practicing the invention disclosed herein, those skilled in the art will readily conceive of other embodiments of the present disclosure. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include known common general knowledge or conventional technical means in the technical field not disclosed by the present disclosure. The specification and examples are only illustrative, and the true scope and spirit of the present disclosure are pointed out by the following claims.

[0143] It should be understood that the present disclosure is not limited to the exact structures described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the present disclosure is only limited by the appended claims.

Claims

1. A code detection method, characterized in that, The method includes: Obtaining an abstract syntax tree corresponding to the code to be detected; the abstract syntax tree includes at least two nodes, and each node includes parameters of the code to be detected and attribute information of the parameters; Traversing each node in the abstract syntax tree; When traversing to a target node and the attribute information of the target parameter does not include exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree; the attribute information of the target parameter is the attribute information of the parameter included in the target child node of the target node in the abstract syntax tree, and the target node is used to represent the parameter to be executed in the code to be detected; When it is determined that the type information of the ancestor node does not include the first preset type information, determining the code to be detected as an abnormal code; the first preset type information is used to represent that the type information of the node is an exception detection statement type.

2. The method according to claim 1, characterized in that, After determining the code to be detected as an abnormal code, the method further includes: Generating a first preset prompt message and a first correction message; Sending the first preset prompt message, the first correction message, and the target position to the terminal, so that the terminal displays the first preset prompt message and the first correction message at the target position in the code to be detected; the target position is determined based on the target node, and the first preset prompt message is used to prompt that there is a first exception handling problem in the code to be detected.

3. The method according to claim 1, wherein The method further includes: When traversing to the target node and the attribute information of the target parameter includes the exception handling information, determining the code to be detected as an abnormal code and generating a second preset prompt message and a second correction message; Sending the second preset prompt message, the second correction message, and the target position to the terminal, so that the terminal displays the second preset prompt message and the second correction message at the target position; the target position is determined based on the target node, and the second preset prompt message is used to prompt that there is a second exception handling problem in the code to be detected.

4. The method according to claim 1, wherein The traversing the ancestor nodes of the target node in the abstract syntax tree when traversing to the target node and the attribute information of the target parameter does not include the exception handling information includes: When traversing to the target node, traversing the child nodes of the target node in the abstract syntax tree; When the type information of the currently traversed child node is the second preset type information, determining the currently traversed child node as the target child node, and the second preset type information is used to represent that the type information of the node is a call expression type; When the attribute information of the target parameter included in the target child node does not include the exception handling information, traversing the ancestor nodes of the target node in the abstract syntax tree.

5. The method according to claim 4, characterized in that, The attribute information includes name information, and the traversing the ancestor nodes of the target node in the abstract syntax tree when the attribute information of the target parameter included in the target child node does not include the exception handling information includes: In the case where the name information of the target parameter does not include the exception handling information, traverse the ancestor nodes of the target node in the abstract syntax tree.

6. The method according to claim 1, wherein The target position is determined by the following steps: Taking the target node as the root node, determine the target subtree in the abstract syntax tree; Determine the position of the target code segment corresponding to the target subtree in the code to be detected as the target position.

7. The method according to claim 1, characterized in that Before traversing the ancestor nodes of the target node in the abstract syntax tree, the method further includes: In the case where the type information of the currently traversed node is the third preset type information, determine the currently traversed node as the target node, and the third preset type information is used to represent that the type information of the node is the waiting expression type.

8. A code detection device, characterized in that, The device includes: An acquisition module, configured to acquire an abstract syntax tree corresponding to the code to be detected; the abstract syntax tree includes at least two nodes, and each node includes the parameters of the code to be detected and the attribute information of the parameters; A first traversal module, configured to traverse each node in the abstract syntax tree; A second traversal module, configured to traverse the ancestor nodes of the target node in the abstract syntax tree when the target node is traversed and the attribute information of the target parameter does not include the exception handling information; the attribute information of the target parameter is the attribute information of the parameter included in the target subtree of the target node in the abstract syntax tree, and the target node is used to represent the waiting execution parameter of the code to be detected; A first abnormal code determination module, configured to determine the code to be detected as an abnormal code when it is determined that the type information of the ancestor node does not include the first preset type information; the first preset type information is used to represent that the type information of the node is the abnormal detection statement type.

9. An electronic device for code detection, characterized in that, Including: A processor; A memory for storing executable instructions of the processor; Wherein, the processor is configured to execute the instructions to implement the code detection method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, When the instructions in the computer-readable storage medium are executed by the processor of the electronic device, the electronic device is enabled to execute the code detection method according to any one of claims 1 to 7.