Code detection method and device, equipment and storage medium

CN116185805BActive Publication Date: 2026-09-11BEIJING YOUZHUJU NETWORK TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111419701.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-26
Publication Date
2026-09-11
Estimated Expiration
2041-11-26

AI Technical Summary

Technical Problem

[0003]目前,用于评价代码的维度包括如重复代码检测以及圈复杂度检测等等,然而,现有的基于上述维度的检测方案过于笼统简单,难以精准地定位代码问题,需要改进

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116185805B_ABST
    Figure CN116185805B_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure disclose a code detection method, device and equipment and a storage medium. The method comprises: converting a target source code file to be detected into a target abstract syntax tree, and detecting a reference declaration statement in the target abstract syntax tree; when a first variable corresponding to a first reference declaration statement is detected to correspond to a preset debugging tool, detecting a new object statement in the target abstract syntax tree; and when a second variable corresponding to a first new object statement is detected to be the same as the first variable, determining that the target source code file includes debugging code corresponding to the preset debugging tool. By using the above technical solution, the debugging code left in the target source code file can be accurately located, so that the developer can find and delete the debugging code in time before the target source code file is put into use, thereby avoiding the influence of redundant debugging code on the normal use of the target source code file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of computer technology, and in particular to code detection methods, apparatus, devices, and storage media. Background Technology

[0002] Code inspection is of great importance in software development and business development iteration. Poor code can affect software performance and lead to a vicious cycle in the entire business development iteration. Therefore, it is necessary to adopt effective inspection methods to inspect the code.

[0003] Currently, dimensions used to evaluate code include duplicate code detection and cyclomatic complexity detection. However, existing detection schemes based on these dimensions are too general and simple, making it difficult to accurately locate code problems, and need to be improved. Summary of the Invention

[0004] This disclosure provides code detection methods, apparatus, storage media, and devices, which can optimize existing code detection schemes.

[0005] In a first aspect, embodiments of this disclosure provide a code detection method, including:

[0006] The target source code file to be detected is converted into a target abstract syntax tree, and the reference declaration statements in the target abstract syntax tree are detected.

[0007] When it is detected that the first variable corresponding to the first reference declaration statement corresponds to the preset debugging tool, the new object statement in the target abstract syntax tree is detected;

[0008] When it is detected that the second variable corresponding to the first newly created object statement is the same as the first variable, it is determined that the target source code file includes the debugging code corresponding to the preset debugging tool.

[0009] Secondly, embodiments of this disclosure provide a code detection device, including:

[0010] The reference declaration statement detection module is used to convert the target source code file to be detected into a target abstract syntax tree, and to detect the reference declaration statements in the target abstract syntax tree;

[0011] A new object statement detection module is used to detect new object statements in the target abstract syntax tree when the first variable corresponding to the first reference declaration statement is detected to correspond to a preset debugging tool.

[0012] The debugging code detection module is used to determine that the target source code file contains debugging code corresponding to the preset debugging tool when it detects that the second variable corresponding to the first newly created object statement is the same as the first variable.

[0013] Thirdly, embodiments of this disclosure provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code detection method provided in embodiments of this disclosure.

[0014] Fourthly, embodiments of this disclosure provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the code detection method provided in embodiments of this disclosure.

[0015] The code detection scheme provided in this embodiment converts the target source code file to be detected into a target abstract syntax tree (AST), and detects the reference declaration statements in the AST. When a first variable corresponding to a first reference declaration statement is detected to correspond to a preset debugging tool, the creation of new objects statements in the AST is detected. When a second variable corresponding to a first creation of new objects statement is detected to be the same as the first variable, it is determined that the target source code file contains debugging code corresponding to the preset debugging tool. By adopting the above technical solution, when detecting the code in the target source code file, by detecting the reference declaration statements and creation of new objects statements in the AST, residual debugging code in the target source code file can be accurately located. This facilitates developers to discover and delete debugging code in a timely manner before the target source code file is put into use, avoiding redundant debugging code from affecting the normal use of the target source code file, and promoting a virtuous cycle of code development iteration. Attached Figure Description

[0016] Figure 1 A flowchart illustrating a code detection method provided in an embodiment of this disclosure;

[0017] Figure 2 A flowchart illustrating yet another code detection method provided in this disclosure embodiment;

[0018] Figure 3 A structural block diagram of a code detection device provided in an embodiment of this disclosure;

[0019] Figure 4 This is a structural block diagram of an electronic device provided in an embodiment of the present disclosure. Detailed Implementation

[0020] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.

[0021] It should be understood that the steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of this disclosure is not limited in this respect.

[0022] The term "comprising" and its variations as used herein are open-ended inclusions, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.

[0023] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are used only to distinguish different devices, modules or units, and are not used to limit the order of functions performed by these devices, modules or units or their interdependencies.

[0024] It should be noted that the terms "a" and "a plurality of" used in this disclosure are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".

[0025] The names of messages or information exchanged between multiple devices in the embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of such messages or information.

[0026] In the following embodiments, each embodiment provides optional features and examples. The features described in the embodiments can be combined to form multiple optional solutions. Each numbered embodiment should not be regarded as only one technical solution.

[0027] Figure 1 This is a flowchart illustrating a code detection method provided in an embodiment of the present disclosure. The method can be executed by a code detection device, which can be implemented in software and / or hardware, and is generally integrated into an electronic device, specifically into the code detection tool of the electronic device. The electronic device can include, for example, a computer, a server, or a mobile device (such as a mobile phone or tablet computer), and is not specifically limited thereto.

[0028] like Figure 1 As shown, the method includes:

[0029] Step 101: Convert the target source code file to be detected into a target abstract syntax tree, and detect the reference declaration statements in the target abstract syntax tree.

[0030] In this embodiment of the disclosure, the target source code file can be understood as the source code file that needs to be inspected, and the source of the target source code file is not limited. For example, after writing the source code file, the developer can publish the source code file to a code repository, such as a GitLab repository, and obtain the target source code file from the code repository; the target source code file can also be obtained by importing or copying and pasting.

[0031] In some development environments, it's often necessary to write debugging code to test certain functionalities. Different testing purposes correspond to different debugging codes. Before deployment, this debugging code needs to be deleted. However, due to various reasons, it may not be deleted in time, resulting in redundant debugging code in the source code files, affecting the code quality. Legacy debugging code can be understood as code written during debugging but missed due to not being deleted in time.

[0032] For example, an Abstract Syntax Tree (AST), or simply a syntax tree, is an abstract representation of the syntactic structure of source code. It can be used to describe the composition, state, or location of code segments and other related information.

[0033] In this embodiment, the target source code file to be detected is first converted into a target abstract syntax tree, which facilitates the detection of various types of statements within it. In this step, reference declaration statements can be detected. A reference declaration statement can be, for example, an ImportDeclaration statement, which indicates a reference to an object. When debugging is required, it is generally necessary to reference the debugging tools used; therefore, detecting reference declaration statements can determine whether a debugging tool is referenced in the target source code file.

[0034] Step 102: When the first variable corresponding to the first reference declaration statement is detected to correspond to the preset debugging tool, the newly created object statement in the target abstract syntax tree is detected.

[0035] For example, the default debugging tool can be a tool used for code debugging, which may include vConsole, console class, alert class, and debugger class, etc. Among them, vConsole can be used for debugging front-end development code for mobile terminal web pages. Currently, there is no corresponding detection method for this debugging tool.

[0036] For example, the currently detected reference declaration statement can be recorded as the first reference declaration statement. The first variable corresponding to the first reference declaration statement can be understood as the object to be referenced. When the reference object of the first reference declaration statement is a preset debugging tool, the first variable can be cached, and then the newly created object statement can be detected to determine whether the target source code file uses a newly created preset debugging tool for code debugging, that is, whether there is debugging code corresponding to the preset debugging tool. The newly created object statement can be, for example, a NewExpression statement.

[0037] Step 103: When it is detected that the second variable corresponding to the first newly created object statement is the same as the first variable, it is determined that the target source code file includes the debugging code corresponding to the preset debugging tool.

[0038] For example, the currently detected new object statement can be recorded as the first new object statement. The second variable corresponding to the first new object statement can be understood as the object to be created. If the second variable is the same as the first variable, it means that there is debugging code corresponding to the preset debugging tool in the target source code file.

[0039] The code detection scheme provided in this embodiment converts the target source code file to be detected into a target abstract syntax tree (AST), and detects the reference declaration statements in the AST. When a first variable corresponding to a first reference declaration statement is detected to correspond to a preset debugging tool, the creation of new objects statements in the AST is detected. When a second variable corresponding to a first creation of new objects statement is detected to be the same as the first variable, it is determined that the target source code file contains debugging code corresponding to the preset debugging tool. By adopting the above technical solution, when detecting the code in the target source code file, by detecting the reference declaration statements and creation of new objects statements in the AST, residual debugging code in the target source code file can be accurately located. This facilitates developers to discover and delete debugging code in a timely manner before the target source code file is put into use, avoiding redundant debugging code from affecting the normal use of the target source code file, and promoting a virtuous cycle of code development iteration.

[0040] In this disclosure, code detection can specifically be code quality detection. In some embodiments, the method may further include: detecting code duplication in the target source code file. Code duplication detection is an important dimension of code quality detection. If duplicate code is allowed to be scattered everywhere, when code in one place needs to be modified due to business iterations, code in other places will also need to be modified, affecting maintenance efficiency. In the prior art, the focus is often only on component-level duplication detection. However, when developers are developing code, they may directly copy large blocks of existing code and simply modify function names or variables. In this case, although the function names or variables have changed, the functionality that the code can achieve has not changed, increasing the amount and complexity of code. In addition, there may be division of labor in development. Different developers may be responsible for parts that have the same development requirements, and different developers may have different coding habits, which may lead to differences in the code snippets written, but the functions are basically the same, which also increases the amount and complexity of code. For the above situations, the detection methods in the prior art cannot detect them.

[0041] In some embodiments, the method may further include: determining a first abstract syntax tree corresponding to a first function code fragment in the target abstract syntax tree, and a second abstract syntax tree corresponding to a second function code fragment in the target abstract syntax tree; performing a repetition analysis on the statement types in the first abstract syntax tree and the second abstract syntax tree; and determining the detection result of the target source code file in the code repetition dimension based on the repetition analysis result.

[0042] In this embodiment of the disclosure, duplication detection can be performed at the function level. For example, a function code snippet can be understood as a code snippet based on a function. A single function code snippet generally contains one function, or it can be understood as a function snippet containing the body of the same function. The number of target source code files can be one or more. If there is only one target source code file, the first function code snippet and the second function code snippet can be two function code snippets from the same target source code file; if there are multiple target source code files, the first function code snippet and the second function code snippet can be two function code snippets from the same target source code file, or they can be function code snippets from different target source code files. It should be noted that the first function code snippet and the second function code snippet can be function code snippets at any or a specified position in the corresponding target source code file, and there is no specific limitation.

[0043] For example, the statement types of each statement in the syntax tree corresponding to the function code fragment can be traversed sequentially. Statement types can be used to represent the purpose or function of a statement, and can include, for example, function declarations, variable declarations, or code block declarations.

[0044] For example, when performing repetition analysis on statement types, the analysis method can be determined based on the actual detection requirements. For instance, the number of statements of the same type can be counted. For each statement type, the number of statements corresponding to the first function code segment and the number of statements corresponding to the second function code segment can be compared. If they are both the same, it indicates a certain degree of repetition. Alternatively, in addition to comparing the number of statements of the same type, the position (e.g., line number) of each statement of the same type within its respective function code segment can be further compared. If the positions are also the same, it indicates a high degree of repetition.

[0045] For example, the degree of repetition in the repetition analysis results can be quantified, and the quantified result can be compared with a corresponding threshold to determine the detection results of the first and second function code fragments in the code repetition dimension. For instance, if the repetition degree is greater than 95%, the first and second function code fragments are determined to be repetitive function code fragments.

[0046] It should be noted that the repeated function code snippets here do not necessarily mean that the first and second function code snippets are completely identical. Rather, it can be considered that, under the current repetition detection scale, the first and second function code snippets are considered to be repeated, indicating a problem with the code repetition dimension, which means there is room for improvement.

[0047] For example, a target source code file typically contains multiple function code snippets. The detection result of the target source code file in the code duplication dimension can be determined based on the duplication analysis results of every two function code snippets.

[0048] By adopting the above technical solution, when detecting the code duplication dimension in the target source code file, the duplication of statement types in two function code segments can be analyzed on a function code segment basis, thereby realizing function-level duplication detection. This refines the detection granularity while ensuring detection efficiency. Moreover, function-level duplication detection has more practical reference significance and value for improving code quality, which is conducive to forming a virtuous cycle of code development iteration.

[0049] In some embodiments, a repetition analysis is performed on the statement types in the first abstract syntax tree and the second abstract syntax tree, including: extracting first statement type information from the first abstract syntax tree and extracting second statement type information from the second abstract syntax tree, wherein the statement type information includes statement type identifiers and the order of the statement type identifiers; comparing the first statement type information and the second statement type information, and if the first statement type information and the second statement type information are consistent, then the first function code fragment and the second function code fragment are determined to be duplicate function code fragments. The advantage of this approach is that it allows for convenient and accurate determination of the statement type of each statement in a function code fragment, and enables reasonable analysis of the repetition of the function code fragment from both the statement type and the order in which the statement types appear.

[0050] After determining the abstract syntax tree (API) corresponding to the function code fragment, the statement type information can be determined by searching for statement type keywords (such as `type`). For example, the statement type identifier can be a statement type name or code, and the order of the statement type identifiers can be understood as the order in which they appear in the corresponding API. For instance, if the first statement type information is type A, type B, type C, type B, and type D, and the second statement type information is type A, type B, type C, type D, and type B, the number of statements with the same statement type is the same, but the order of the statement type identifiers is not completely consistent. In this case, the first and second statement type information are inconsistent. Optionally, if the first and second statement type information are inconsistent, then it is determined that the first and second function code fragments are not duplicate function code fragments.

[0051] In some embodiments, comparing the first statement type information and the second statement type information, and determining that the first function code segment and the second function code segment are duplicate function code segments if they are consistent, includes: calculating a first hash value corresponding to the first statement type information, and calculating a second hash value corresponding to the second statement type information; determining whether the first hash value and the second hash value are equal, and if they are equal, determining that the first function code segment and the second function code segment are duplicate function code segments. The advantage of this approach is that using hash value calculation simplifies the process of comparing statement type information and improves detection efficiency.

[0052] Optionally, the statement type information can be a list of statement type identifiers. This simplifies the representation of statement type information, reduces the difficulty of hash calculation, and improves computational efficiency.

[0053] For example, all statement types within the function body of the abstract syntax tree are traversed sequentially. The resulting statement types are arranged into a list, and then a hash calculation is performed on this list to generate a unique value, also known as a hash value. The specific hash algorithm used is not limited and can be set according to actual needs. By comparing the first hash value with the second hash value, it is possible to quickly and accurately determine whether the first statement type information and the second statement type information are consistent, thereby determining whether the first function code snippet and the second function code snippet are duplicate function code snippets.

[0054] In some embodiments, the method may further include: obtaining corresponding detection rules from detection plugins corresponding to preset detection dimensions; detecting the target source code file according to the obtained detection rules; and determining the detection result for the corresponding detection dimension based on the detection result. The advantage of this setup is that the plugin mechanism allows business users to customize corresponding detection rules according to their actual needs, enriching the detection methods and making the detection methods more aligned with the actual needs of the business users, thus making code detection more flexible and targeted.

[0055] Optionally, the detection rules are determined based on the detection rule code written by the business side according to the preset plugin writing specifications. The preset detection dimensions include at least one of potential vulnerabilities, maintainability, best practices, style files, and web page files.

[0056] For example, a plugin mechanism can be provided. If a business user has new rules, they can write their own detection rule code and place it in the provided plugin. For instance, an editor can be provided on the front end, allowing users to submit their own rule code. After the user submits the code, the submitted rules are automatically or manually added to the detection plugin.

[0057] For example, a potential vulnerability can be understood as a potential bug, and the types of potential vulnerabilities and corresponding detection methods can be defined. Maintainability can be understood as the ease with which source code files can be fixed and improved, and quantitative standards for maintainability and corresponding detection methods can be defined. Best practices can be understood as pre-agreed best writing methods, and optimal writing methods and corresponding detection methods can be defined for different functions or components. Style files may include, for example, Lesser Style Sheets or Syntactically Awesome Stylesheets (Sass, also known as SCSS) files, and quality quantification standards and corresponding detection methods can be defined for each style file. Web page files may specifically be Hypertext Markup Language (HTML) files, and quality quantification standards and corresponding detection methods can be defined for HTML files. Regarding duplication, detection rules different from those described above can be defined. In practical applications, one duplication dimension detection can be selected according to the actual situation, or two or more detection rules can be combined to obtain more accurate detection results. Cyclomatic complexity is also a standard used to measure code quality. The calculation method and corresponding evaluation criteria, such as a cyclomatic complexity threshold, can be defined. For legacy debugging code, the types of debugging code to be checked and the corresponding detection methods can be defined.

[0058] In some embodiments, the method further includes: determining a third function code fragment from the target source code file; determining whether the number of lines of code corresponding to the third function code fragment is less than a preset line count threshold, wherein the preset line count threshold matches the business scenario corresponding to the target source code file; if so, performing cyclomatic complexity dimension code detection on the third function code fragment. Optionally, if not, prohibiting cyclomatic complexity dimension code detection on the third function code fragment. The advantage of this setting is that cyclomatic complexity dimension detection can be performed more reasonably and specifically according to the business scenario. It should be noted that the third function code fragment can be any or specified function code fragment in the target source code file, and can be the same function code fragment as the first function code fragment or the second function code fragment.

[0059] In some business scenarios, such as e-commerce, there are often large function components, meaning they typically have many lines of code and complex internal logic. This leads to high cyclomatic complexity detected, but usually, there is little room for optimization. Therefore, detection is not very meaningful and may even slow down the detection process. In this embodiment, some large function code segments can be exempted based on the business scenario, effectively improving code detection efficiency. If the exemption conditions are not met, cyclomatic complexity detection can be performed to ensure the accuracy of the detection.

[0060] In some embodiments, the cyclomatic complexity detection of the third function code fragment includes: traversing the statements in the third function code fragment, incrementing the cyclomatic complexity parameter value when a preset statement is detected; after the traversal is complete, determining whether the current cyclomatic complexity parameter value is greater than a preset cyclomatic complexity threshold; if it is greater, then determining that the cyclomatic complexity of the third function code fragment has a problem. The advantage of this setup is that the cyclomatic complexity can be calculated accurately. The preset statement can be set according to actual needs, and may include conditional statements or loop statements, etc.

[0061] In some embodiments, the method further includes: when a target problem is detected in the detection result corresponding to a specified detection dimension, querying a preset correspondence relationship based on the target problem, wherein the preset correspondence relationship includes a correspondence relationship between code problems and solution suggestion information; determining the corresponding target solution suggestion information based on the query result, and displaying the target solution suggestion information. The advantage of this setup is that it allows for the pre-establishment of correspondence relationships between different problems and corresponding solutions. When a code problem is detected, querying this correspondence relationship displays the corresponding solution suggestion to the user, helping the user accurately locate the problem and quickly and effectively address it, improving the code quality of the target source code file and overall improving development efficiency.

[0062] Figure 2 This is a flowchart illustrating another code detection method provided in this disclosure. This disclosure optimizes the various optional solutions in the above embodiments. Specifically, the method includes the following steps:

[0063] Step 201: Obtain the first function code fragment and the second function code fragment from the target source code file to be detected.

[0064] For example, there may be multiple target source code files, and there are usually multiple function code snippets in the same target source code file. In order to ensure the comprehensiveness of the detection, the duplication dimension can be detected for every two different function code snippets. The target source code file can be divided into units of functions, and multiple function code snippets can be extracted. For ease of explanation, every two function code snippets that need to be compared are referred to as the first function code snippet and the second function code snippet, respectively.

[0065] Alternatively, instead of splitting, certain rules can be used to restrict access to statements within a function code snippet. For example, access to function statements can be implemented based on rules in Eslint.

[0066] For example, the first and second function code segments can be determined according to the order in which they appear in the target source code file. For instance, the first function code segment to appear can be considered the first function code segment, the second function code segment can be considered the second function code segment, and subsequent function code segments can be considered new second function code segments. All previously appearing function code segments can be considered first function code segments; that is, each time a function code segment appears, it is compared with the previously appearing function code segments.

[0067] In this embodiment, the target source code file can be detected across eight dimensions, including repetition rate, cyclomatic complexity, legacy debugging code, potential vulnerabilities, maintainability, best practices, style files, and web page files. It should be noted that only some of these dimensions can be used; the specific dimensions to be detected can be set according to actual needs. The order of detection between different dimensions is not limited. This embodiment is only for illustrative purposes. For example, step 207 can be executed before step 201, or steps 207 and 201 can be executed in parallel.

[0068] Step 202: Convert the first function code fragment into the corresponding first abstract syntax tree, and convert the second function code fragment into the corresponding second abstract syntax tree.

[0069] For example, when converting function code snippets into corresponding abstract syntax trees (ASTs), the conversion can be performed on the two function code snippets to be compared at the moment. Then, subsequent steps such as extracting statement type information and calculating hash values ​​are performed. After obtaining the corresponding duplication analysis results, the same operations are then performed on the next two function code snippets to be compared. Optionally, the abstract syntax trees of all function code snippets to be compared can be checked first, followed by the extraction of statement type information. Furthermore, a parallel approach can be used to perform the relevant operations on each pair of function code snippets to be compared separately, improving detection efficiency.

[0070] It should be noted that for a certain function code snippet, if the hash value of the corresponding statement type information has already been calculated, then it is not necessary to perform operations such as abstract syntax tree conversion again.

[0071] Step 203: Extract the first statement type information from the first abstract syntax tree and extract the second statement type information from the second abstract syntax tree, wherein the statement type information is a list of statement type identifiers containing statement type identifiers and the order of statement type identifiers.

[0072] For example, the statement type identifier can be a statement type name. A list of statement type names can be created for each abstract syntax tree. For the current abstract syntax tree, a search is performed for statement type keywords (such as type). For each type found, the statement type name after type is added to the end of the corresponding list of statement type names. After traversal, the list of statement type names can be obtained.

[0073] Step 204: Calculate the first hash value corresponding to the first statement type information, and calculate the second hash value corresponding to the second statement type information.

[0074] Step 205: Determine whether the first function code segment and the second function code segment are duplicate function code segments based on whether the first hash value and the second hash value are equal.

[0075] For example, the calculated hash value can be stored, and when a new hash value is calculated, it can be compared with the stored hash value. If it is the same as any stored hash value, it can be determined that there is a duplicate function code segment.

[0076] Step 206: Determine the detection result of the target source code file in the code duplication dimension based on whether duplicate function code snippets are detected.

[0077] For example, if duplicate function code snippets are detected, it can be assumed that the target source code file has a problem in the code duplication dimension. Duplicate function code snippets can be marked to facilitate further processing by developers and improve code quality.

[0078] Step 207: Detect the reference declaration statements in the abstract syntax tree corresponding to the target source code file. When the first variable corresponding to the first reference declaration statement is detected to correspond to the preset debugging tool, detect the new object statement in the target source code file. When the second variable corresponding to the first new object statement is detected to be the same as the first variable, determine that the target source code file includes the debugging code corresponding to the preset debugging tool.

[0079] For example, the target source code file can be converted into the corresponding abstract syntax tree. If the conversion has already been completed in the previous steps, then it is not necessary to perform the conversion again here.

[0080] For example, the default debugging tool could be vConsole, the reference declaration statement could be an ImportDeclaration statement, and the new object statement could be a NewExpression statement. When an ImportDeclaration statement is detected, if the first variable corresponding to the statement belongs to vConsole, then the first variable is cached. When a NewExpression statement is detected later, it is determined whether the second variable corresponding to the statement belongs to vConsole. If it does, and it is equal to the first variable, then it can be assumed that there is debugging code corresponding to the vConsole debugging tool, that is, there is legacy debugging code.

[0081] Of course, it can also be used to detect other types of debugging code, such as console, alert, and debugger classes.

[0082] Step 208: Obtain the third function code snippet from the target source code file. If the number of lines of code corresponding to the third function code snippet is less than or equal to the preset line number threshold, then traverse the statements in the third function code snippet. When the preset statement is detected, increment the parameter value of cyclomatic complexity. After the traversal is completed, determine whether the current parameter value of cyclomatic complexity is greater than or equal to the preset cyclomatic complexity threshold to determine if there is a problem with the cyclomatic complexity of the third function code snippet.

[0083] For example, each function code snippet in the target source code file can be sequentially identified as the third function code snippet. In e-commerce scenarios, some larger function code snippets can be exempted from cyclomatic complexity detection, with a preset line count threshold, for example, 300 lines.

[0084] If the current third function code snippet corresponds to 300 lines of code or less, cyclomatic complexity can be checked. Specifically, the initial value of the cyclomatic complexity parameter can be 1. The function is entered, and the statements in the function body are traversed. Each time the following statements are detected, such as CatchClause, ConditionalExpression, LogicalExpression, ForStatement, ForInStatement, ForOfStatement, IfStatement, SwitchCase, WhileStatement, DoWhileStatement, and AssignmentExpression, the cyclomatic complexity parameter value is incremented by 1. After the traversal is completed and the function is exited, the final value of the cyclomatic complexity parameter value is obtained. If the final value is greater than the preset cyclomatic complexity threshold (e.g., 10), the cyclomatic complexity is considered to be too high and there is a problem.

[0085] Step 209: Obtain the corresponding detection rules from the detection plugins corresponding to the preset detection dimensions, perform detection on the target source code file according to the obtained detection rules, and determine the detection results of the corresponding detection dimensions based on the detection results.

[0086] For example, the preset detection dimensions may include some or all of the above eight detection dimensions. Each detection dimension generally has existing detection rules. When a detection plugin exists for a certain preset detection dimension, the corresponding detection rules can be obtained from the plugin for further quality checks. The detection rules are determined based on the detection rule code written by the business side according to the preset plugin writing specifications.

[0087] Step 210: If a target problem is detected in the detection result corresponding to the specified detection dimension, query the preset corresponding relationship according to the target problem, determine the corresponding target solution suggestion information according to the query result, and display the target solution suggestion information.

[0088] For example, a code inspection tool may include a front-end visual interface where users can view the inspection results for each inspection dimension. When problems are found in the inspection results, the interface can display corresponding target solution suggestions in the form of text and / or illustrations. To facilitate understanding, it can also display actual operation examples corresponding to the target solution suggestions to help users quickly resolve code quality issues.

[0089] The code detection method provided in this disclosure can detect target source code files in multiple detection dimensions. The detection methods of each detection dimension can take into account detection accuracy, detection efficiency, detection targeting and detection flexibility, etc., to comprehensively detect the code of the source code file. When code problems are detected, corresponding solution suggestions are displayed to the user, which helps the user to accurately locate the problem and quickly and effectively deal with the problem, improve the code quality of the target source code file, and improve the overall development efficiency.

[0090] Figure 3 This is a structural block diagram of a code detection device provided in an embodiment of the present disclosure. The device can be implemented by software and / or hardware, and is generally integrated into an electronic device. It can perform code detection by executing a code detection method. Figure 3 As shown, the device includes:

[0091] The reference declaration statement detection module 301 is used to convert the target source code file to be detected into a target abstract syntax tree, and to detect the reference declaration statements in the target abstract syntax tree;

[0092] The newly created object statement detection module 302 is used to detect newly created object statements in the target abstract syntax tree when the first variable corresponding to the first reference declaration statement is detected to correspond to a preset debugging tool.

[0093] The debugging code detection module 303 is used to determine that the target source code file includes debugging code corresponding to the preset debugging tool when it detects that the second variable corresponding to the first newly created object statement is the same as the first variable.

[0094] The code detection device provided in this embodiment converts the target source code file to be detected into a target abstract syntax tree (API). It then detects reference declaration statements within the API. When a first variable corresponding to a first reference declaration statement is detected to correspond to a preset debugging tool, the device detects new object statements within the API. When a second variable corresponding to a first new object statement is detected to be identical to the first variable, it determines that the target source code file contains debugging code corresponding to the preset debugging tool. By employing this technical solution, when detecting code in a target source code file, by detecting reference declaration statements and new object statements in the API, residual debugging code in the target source code file can be accurately located. This facilitates developers in promptly identifying and deleting debugging code before the target source code file is put into use, preventing redundant debugging code from affecting the normal use of the target source code file and promoting a virtuous cycle of code development iteration.

[0095] Optionally, the device may also include:

[0096] The syntax tree determination module is used to determine the first abstract syntax tree corresponding to the first function code segment in the target abstract syntax tree, and the second abstract syntax tree corresponding to the second function code segment in the target abstract syntax tree;

[0097] The statement type extraction module is used to extract the first statement type information in the first abstract syntax tree and the second statement type information in the second abstract syntax tree, wherein the statement type information includes statement type identifiers and the order of statement type identifiers;

[0098] The repetition analysis module is used to compare the first statement type information and the second statement type information. If the first statement type information and the second statement type information are consistent, then the first function code fragment and the second function code fragment are determined to be repetitive function code fragments.

[0099] Optionally, the repetition analysis module is specifically used to calculate the first hash value corresponding to the first statement type information and the second hash value corresponding to the second statement type information; determine whether the first hash value and the second hash value are equal; if they are equal, then determine that the first function code segment and the second function code segment are duplicate function code segments.

[0100] Optionally, the device may also include:

[0101] The detection rule acquisition module is used to acquire the corresponding detection rules from the detection plugins corresponding to the preset detection dimensions;

[0102] The detection result determination module is used to detect the target source code file according to the acquired detection rules, and determine the detection result of the corresponding detection dimension based on the detection result.

[0103] Optionally, the device may also include:

[0104] A code snippet determination module is used to determine a third function code snippet from the target source code file;

[0105] The cyclomatic complexity detection module is used to determine whether the number of lines of code corresponding to the third function code fragment is less than a preset line number threshold, wherein the preset line number threshold matches the business scenario corresponding to the target source code file; if so, the third function code fragment is subjected to cyclomatic complexity dimension code detection.

[0106] Optionally, the step of detecting the cyclomatic complexity dimension of the third function code fragment includes:

[0107] The statements in the third function code snippet are traversed, and the parameter value of cyclomatic complexity is incremented when a preset statement is detected;

[0108] After the traversal is completed, it is determined whether the current parameter value of the cyclomatic complexity is greater than the preset cyclomatic complexity threshold. If it is greater, it is determined that there is a problem with the cyclomatic complexity of the third function code segment.

[0109] Optionally, the device may also include:

[0110] The solution suggestion query module is used to query a preset correspondence based on the target problem when a target problem is detected in the detection result corresponding to a specified detection dimension. The preset correspondence includes the correspondence between code problems and solution suggestion information.

[0111] The solution suggestion display module is used to determine the corresponding target solution suggestion information based on the query results and display the target solution suggestion information.

[0112] The following is for reference. Figure 4 This document illustrates a structural schematic diagram of an electronic device 400 suitable for implementing embodiments of the present disclosure. The electronic devices in these embodiments may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 4 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.

[0113] like Figure 4 As shown, electronic device 400 may include a processing device (e.g., a central processing unit, a graphics processor, etc.) 401, which can perform various appropriate actions and processes according to a program stored in read-only memory (ROM) 402 or a program loaded from storage device 408 into random access memory (RAM) 403. RAM 403 also stores various programs and data required for the operation of electronic device 400. Processing device 401, ROM 402, and RAM 403 are interconnected via bus 404. Input / output (I / O) interface 405 is also connected to bus 404.

[0114] Typically, the following devices can be connected to I / O interface 405: input devices 406 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 407 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 408 including, for example, magnetic tapes, hard disks, etc.; and communication devices 409. Communication device 409 allows electronic device 400 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 4An electronic device 400 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.

[0115] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication device 409, or installed from storage device 408, or installed from ROM 402. When the computer program is executed by processing device 401, it performs the functions defined in the methods of embodiments of this disclosure.

[0116] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in connection with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.

[0117] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.

[0118] The aforementioned computer-readable medium carries one or more programs. When the aforementioned one or more programs are executed by the electronic device, the electronic device causes the following: it converts the target source code file to be detected into a target abstract syntax tree (AST), and detects the reference declaration statements in the AST; when it detects that the first variable corresponding to the first reference declaration statement corresponds to a preset debugging tool, it detects the new object statement in the AST; when it detects that the second variable corresponding to the first new object statement is the same as the first variable, it determines that the target source code file includes the debugging code corresponding to the preset debugging tool.

[0119] Computer program code for performing the operations of this disclosure can be written in one or more programming languages ​​or a combination thereof, including but not limited to object-oriented programming languages ​​such as Java, Smalltalk, and C++, as well as conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0120] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0121] The modules described in the embodiments of this disclosure can be implemented in software or hardware. The names of the modules do not necessarily limit the module itself; for example, a reference declaration statement detection module can also be described as "a module that converts the target source code file to be detected into a target abstract syntax tree and detects reference declaration statements in the target abstract syntax tree."

[0122] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: Field Programmable Gate Arrays (FPGAs), Application-Specific Integrated Circuits (ASICs), Application Standard Products (ASSPs), System-on-Chip (SoCs), Complex Programmable Logic Devices (CPLDs), and so on.

[0123] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0124] According to one or more embodiments of this disclosure, a code detection method is provided, comprising:

[0125] The target source code file to be detected is converted into a target abstract syntax tree, and the reference declaration statements in the target abstract syntax tree are detected.

[0126] When it is detected that the first variable corresponding to the first reference declaration statement corresponds to the preset debugging tool, the new object statement in the target abstract syntax tree is detected;

[0127] When it is detected that the second variable corresponding to the first newly created object statement is the same as the first variable, it is determined that the target source code file includes the debugging code corresponding to the preset debugging tool.

[0128] Furthermore, it also includes:

[0129] Determine the first abstract syntax tree corresponding to the first function code fragment in the target abstract syntax tree, and the second abstract syntax tree corresponding to the second function code fragment in the target abstract syntax tree;

[0130] Extract the first statement type information from the first abstract syntax tree and the second statement type information from the second abstract syntax tree, wherein the statement type information includes statement type identifiers and the order of statement type identifiers;

[0131] The first statement type information and the second statement type information are compared. If the first statement type information and the second statement type information are consistent, then the first function code fragment and the second function code fragment are determined to be duplicate function code fragments.

[0132] Furthermore, the comparison of the first statement type information and the second statement type information, and the determination that the first function code segment and the second function code segment are duplicate function code segments if the first statement type information and the second statement type information are consistent, includes:

[0133] Calculate the first hash value corresponding to the first statement type information, and calculate the second hash value corresponding to the second statement type information;

[0134] Determine whether the first hash value and the second hash value are equal. If they are equal, then determine that the first function code segment and the second function code segment are duplicate function code segments.

[0135] Furthermore, it also includes:

[0136] Obtain the corresponding detection rules from the detection plugin corresponding to the preset detection dimensions;

[0137] The target source code file is detected according to the obtained detection rules, and the detection results of the corresponding detection dimensions are determined based on the detection results.

[0138] Furthermore, it also includes:

[0139] Determine the third function code fragment from the target source code file;

[0140] Determine whether the number of lines of code corresponding to the third function code fragment is less than a preset line number threshold, wherein the preset line number threshold matches the business scenario corresponding to the target source code file;

[0141] If so, then perform code detection on the third function code snippet using the cyclomatic complexity dimension.

[0142] Furthermore, the code detection of the third function code fragment in terms of cyclomatic complexity includes:

[0143] The statements in the third function code snippet are traversed, and the parameter value of cyclomatic complexity is incremented when a preset statement is detected;

[0144] After the traversal is completed, it is determined whether the current parameter value of the cyclomatic complexity is greater than the preset cyclomatic complexity threshold. If it is greater, it is determined that there is a problem with the cyclomatic complexity of the third function code segment.

[0145] Furthermore, it also includes:

[0146] If a target problem is detected in the detection result corresponding to a specified detection dimension, a preset correspondence is queried based on the target problem. The preset correspondence includes the correspondence between code problems and solution suggestion information.

[0147] Based on the query results, determine the corresponding target solution suggestion information and display the target solution suggestion information.

[0148] According to one or more embodiments of this disclosure, a code detection apparatus is provided, comprising:

[0149] The reference declaration statement detection module is used to convert the target source code file to be detected into a target abstract syntax tree, and to detect the reference declaration statements in the target abstract syntax tree;

[0150] A new object statement detection module is used to detect new object statements in the target abstract syntax tree when the first variable corresponding to the first reference declaration statement is detected to correspond to a preset debugging tool.

[0151] The debugging code detection module is used to determine that the target source code file contains debugging code corresponding to the preset debugging tool when it detects that the second variable corresponding to the first newly created object statement is the same as the first variable.

[0152] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.

[0153] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.

[0154] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.

Claims

1. A code detection method, characterized in that, include: The target source code file to be detected is converted into a target abstract syntax tree, and the reference declaration statements in the target abstract syntax tree are detected. When the first variable corresponding to the first reference declaration statement is detected to correspond to the preset debugging tool, the newly created object statement in the target abstract syntax tree is detected, wherein the first reference declaration statement is the currently detected reference declaration statement, and the first variable corresponding to the first reference declaration statement is the reference object corresponding to the first reference declaration statement; When it is detected that the second variable corresponding to the first newly created object statement is the same as the first variable, it is determined that the target source code file includes the debugging code corresponding to the preset debugging tool; Among them, detecting that the first variable corresponding to the first reference declaration statement corresponds to the preset debugging tool includes at least one of the following: The first variable corresponding to the first reference declaration statement was detected to be a preset debugging tool; and, The first variable corresponding to the first reference declaration statement was detected to be a variable belonging to the preset debugging tool.

2. The method according to claim 1, characterized in that, The method further includes: Determine the first abstract syntax tree corresponding to the first function code fragment in the target abstract syntax tree, and the second abstract syntax tree corresponding to the second function code fragment in the target abstract syntax tree; Extract the first statement type information from the first abstract syntax tree and the second statement type information from the second abstract syntax tree, wherein the statement type information includes statement type identifiers and the order of statement type identifiers; The first statement type information and the second statement type information are compared. If the first statement type information and the second statement type information are consistent, then the first function code fragment and the second function code fragment are determined to be duplicate function code fragments.

3. The method according to claim 2, characterized in that, The step of comparing the first statement type information and the second statement type information, and determining that the first function code segment and the second function code segment are duplicate function code segments if the first statement type information and the second statement type information are consistent, includes: Calculate the first hash value corresponding to the first statement type information, and calculate the second hash value corresponding to the second statement type information; Determine whether the first hash value and the second hash value are equal. If they are equal, then determine that the first function code segment and the second function code segment are duplicate function code segments.

4. The method according to claim 1, characterized in that, Also includes: Obtain the corresponding detection rules from the detection plugin corresponding to the preset detection dimensions; The target source code file is detected according to the obtained detection rules, and the detection results of the corresponding detection dimensions are determined based on the detection results.

5. The method according to claim 1, characterized in that, Also includes: Determine the third function code fragment from the target source code file; Determine whether the number of lines of code corresponding to the third function code fragment is less than a preset line number threshold, wherein the preset line number threshold matches the business scenario corresponding to the target source code file; If so, then perform code detection on the third function code snippet using the cyclomatic complexity dimension.

6. The method according to claim 5, characterized in that, The code detection of the third function code fragment in terms of cyclomatic complexity includes: The statements in the third function code snippet are traversed, and the parameter value of cyclomatic complexity is incremented when a preset statement is detected; After the traversal is completed, it is determined whether the current parameter value of the cyclomatic complexity is greater than the preset cyclomatic complexity threshold. If it is greater, it is determined that there is a problem with the cyclomatic complexity of the third function code segment.

7. The method according to any one of claims 1-6, characterized in that, Also includes: If a target problem is detected in the detection result corresponding to a specified detection dimension, a preset correspondence is queried based on the target problem. The preset correspondence includes the correspondence between code problems and solution suggestion information. Based on the query results, determine the corresponding target solution suggestion information and display the target solution suggestion information.

8. A code detection device, characterized in that, include: The reference declaration statement detection module is used to convert the target source code file to be detected into a target abstract syntax tree, and to detect the reference declaration statements in the target abstract syntax tree; A new object statement detection module is used to detect new object statements in the target abstract syntax tree when the first variable corresponding to the first reference declaration statement corresponds to a preset debugging tool. The first reference declaration statement is the currently detected reference declaration statement, and the first variable corresponding to the first reference declaration statement is the reference object corresponding to the first reference declaration statement. The debugging code detection module is used to determine that the target source code file contains debugging code corresponding to the preset debugging tool when it is detected that the second variable corresponding to the first newly created object statement is the same as the first variable; Among them, detecting that the first variable corresponding to the first reference declaration statement corresponds to the preset debugging tool includes at least one of the following: The first variable corresponding to the first reference declaration statement was detected to be a preset debugging tool; and, The first variable corresponding to the first reference declaration statement was detected to be a variable belonging to the preset debugging tool.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method as described in any one of claims 1-7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.

Citation Information

Patent Citations

  • Method and device for detecting similarity of source codes

    CN101315599A

  • A method and apparatus for providing front-end pages

    CN109145535A