[go: up one dir, main page]

CN110688307B - JavaScript code detection method, device, equipment and storage medium - Google Patents

JavaScript code detection method, device, equipment and storage medium Download PDF

Info

Publication number
CN110688307B
CN110688307B CN201910846579.6A CN201910846579A CN110688307B CN 110688307 B CN110688307 B CN 110688307B CN 201910846579 A CN201910846579 A CN 201910846579A CN 110688307 B CN110688307 B CN 110688307B
Authority
CN
China
Prior art keywords
statement
javascript code
code
variable
javascript
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201910846579.6A
Other languages
Chinese (zh)
Other versions
CN110688307A (en
Inventor
陈志城
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guoxin Jinhong Information Consulting Co ltd
Shenzhen Lian Intellectual Property Service Center
Original Assignee
Guoxin Jinhong Information Consulting Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guoxin Jinhong Information Consulting Co ltd filed Critical Guoxin Jinhong Information Consulting Co ltd
Priority to CN201910846579.6A priority Critical patent/CN110688307B/en
Publication of CN110688307A publication Critical patent/CN110688307A/en
Application granted granted Critical
Publication of CN110688307B publication Critical patent/CN110688307B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/3668Testing of software
    • G06F11/3672Test management

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The application relates to the field of code quality tools, and provides a JavaScript code detection method, a JavaScript code detection device, javaScript code detection equipment and a JavaScript code storage medium, wherein the JavaScript code detection method comprises the following steps: acquiring a JavaScript code, and constructing an abstract syntax tree corresponding to the JavaScript code; searching checking rules in the JavaScript code through an automatic script; traversing an if statement in the abstract syntax tree to obtain an if statement of the JavaScript code; respectively setting identification information for variables in each if statement in the JavaScript code; comparing and detecting variables in each row of if sentences in the JavaScript code by using a regular expression and a checking rule to obtain a detection result; if the detection result is failure, returning to the empty set and sending out an abnormal notification. The problem that abnormal variables cannot be resolved under the condition that a plurality of if exist in JavaScript codes is solved.

Description

JavaScript code detection method, device, equipment and storage medium
Technical Field
The present application relates to the field of code quality tools, and in particular, to a method, an apparatus, a device, and a storage medium for detecting JavaScript codes.
Background
With the rapid development of internet technology, terminals such as mobile phones, tablet computers, personal computers and the like are increasingly popular, various systems depending on the operation of the terminals are generated, people depend on various systems more and more in work and life, and higher requirements are put on functions which can be provided by the systems. Along with the improvement and enhancement of the functions of the system, the complexity of the codes of the system is increased, so that the stability and reliability of the system are reduced, and various abnormal states of the system can occur in the running process. In order to track and find the exact location of the system's anomaly, it is often necessary to obtain anomaly information in the system, and locate the anomaly-occurring variables in the system based on the anomaly information, in order to implement the anomaly-occurring variables based on code.
In the existing JavaScript analysis engine mechanism, if multiple ors exist in an if statement, if the previous judgment statement is satisfied, the analysis engine will not continue to analyze the following conditional statement. In the related art, when locating an abnormal variable according to abnormal information, it is necessary to acquire a running log of a system in operation, analyze the running log by using a log analysis tool, and locate the abnormal variable in the running log. When the operation log of the system is obtained, all operation logs generated in a time period in which abnormality is likely to occur need to be obtained, and because the data volume of the operation log generated in the operation process of the system is large, a large amount of workload is generated when the operation log is analyzed, and the abnormal variables cannot be positioned in real time, so that the intelligence is low.
Disclosure of Invention
The application provides a method for developing a function through a packaged page, which can solve the problems that the page development efficiency is too low and quick-maturing iteration cannot be performed on a version in the prior art.
In a first aspect, the present application provides a JavaScript code detection method, including:
and acquiring JavaScript codes, and constructing an abstract syntax tree corresponding to the JavaScript codes. The abstract syntax tree is created by a syntax analyzer. The grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code.
Checking rules in JavaScript code are looked up through an automation script. The checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable.
And traversing the if statement in the abstract syntax tree to obtain the if statement of the JavaScript code.
And setting identification information for variables in each if statement in the JavaScript code respectively.
And comparing and detecting the variable in each line if statement in the JavaScript code by using the regular expression and the check rule to obtain a detection result.
And if the detection result is successful, returning an array and updating the attribute of the regular expression object and the regular expression object.
Or if the detection result is failure, returning to the empty set and sending out an abnormal notification.
In some possible designs, the obtaining the if statement of the JavaScript code by traversing the if statement in the abstract syntax tree includes:
and traversing all objects of the abstract syntax tree circularly, and if the objects are equal to the if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement. The array is used for storing the state of the JavaScript code. The state includes an attribute of the code.
Searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
In some possible designs, the variable identification information includes at least one of: the method comprises the steps of detecting function names of functions of the mark variable assignment statement of JavaScript, line numbers of the mark variable assignment statement of JavaScript code, file names of source files of the mark variable assignment statement of JavaScript code.
In some possible designs, the exception notification refers to a notification generated by combining a code line number, an exception variable, the exception information, and an exception flag in a fixed format.
In some possible designs, the exception flag refers to a 32-bit binary number for detecting code exceptions, each bit of the binary number representing an exception state conveying each code run.
After the comparison detection is completed, a 32-bit 2-ary number is returned.
If the ith bit is 1, the code of the (i+1) th function is determined to operate normally.
If the ith bit is 0, determining that the code operation of the (i+1) th function is abnormal. And i is an integer of 0 or more and 32 or less.
In some possible designs, the specification categories include at least: naming convention, claim convention, annotation convention, constant convention, bean definition convention, transaction convention, script convention, log convention, exception handling convention, junit convention, error class convention, warning class convention, and hint class convention.
In some possible designs, after the returning the empty set, the method further comprises:
sending out indication information; the instruction information is used for indicating the detection result of each if statement in the JavaScript code. .
In a second aspect, the present application provides an apparatus for detecting JavaScript code, having a function of implementing a method for JavaScript code detection corresponding to the first aspect. The functions may be implemented by hardware, or may be implemented by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the functions described above, which may be software and/or hardware.
The device for detecting JavaScript code includes:
the input/output module is used for acquiring JavaScript codes;
and the processing module is used for constructing an abstract syntax tree corresponding to the JavaScript code. The abstract syntax tree is created by a syntax analyzer. The grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code. Checking rules in JavaScript code are looked up through an automation script. The checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable. And traversing the if statement in the abstract syntax tree to obtain the if statement of the JavaScript code. And setting identification information for variables in each if statement in the JavaScript code respectively. Comparing and detecting variables in each row of if sentences in the JavaScript code by using a regular expression and the checking rule to obtain a detection result;
and if the detection result is successful, returning an array through the input/output module and updating the attribute of the regular expression object and the regular expression object;
or if the detection result is failure, returning to an empty set and sending out an abnormal notification through the input/output module.
In some possible designs, the processing module is further to:
and traversing all objects of the abstract syntax tree circularly, and if the objects are equal to the if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement. The array is used for storing the state of the JavaScript code. The state includes an attribute of the code.
Searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
In some possible designs, the variable identification information includes at least one of: the method comprises the steps of detecting function names of functions of the mark variable assignment statement of JavaScript, line numbers of the mark variable assignment statement of JavaScript code, file names of source files of the mark variable assignment statement of JavaScript code.
In some possible designs, the exception notification refers to a notification generated by combining a code line number, an exception variable, the exception information, and an exception flag in a fixed format.
In some possible designs, the exception flag refers to a 32-bit binary number for detecting code exceptions, each bit of the binary number representing an exception state conveying each code run.
The processing module is further configured to:
after the comparison detection is completed, a 32-bit 2-ary number is returned.
If the ith bit is 1, the code of the (i+1) th function is determined to operate normally.
If the ith bit is 0, determining that the code operation of the (i+1) th function is abnormal. And i is an integer of 0 or more and 32 or less.
In some possible designs, the specification categories include at least: naming convention, claim convention, annotation convention, constant convention, bean definition convention, transaction convention, script convention, log convention, exception handling convention, junit convention, error class convention, warning class convention, and hint class convention.
In some possible designs, the processing module is also used to
Sending out indication information; the instruction information is used for indicating the detection result of each if statement in the JavaScript code.
In yet another aspect, the present application provides a computer device, which includes at least one connected processor, a memory, and an input/output unit, where the memory is configured to store program code, and the processor is configured to invoke the program code in the memory to perform the method described in the above aspects.
In yet another aspect, the application provides a computer storage medium comprising instructions which, when run on a computer, cause the computer to perform the method of the above aspects.
In the existing mechanism, when there are a plurality of or in the if statement, if the previous judgment statement is satisfied, the JavaScript parsing engine mechanism does not continue to parse the following conditional statement. The application provides a method for detecting variable information when an if code is abnormal. The method is characterized in that an abstract syntax tree is traversed, abnormal variables in an if sentence are found, the variables in the if sentence are identified, and if the identified variables are judged to be abnormal through a regular expression and an inspection rule, the abnormal variables are sent to relevant developers. The problem that if statement variables in JavaScript codes are undefined is solved, error reporting prompt is carried out on an developer, and the robustness of the codes is guaranteed.
Drawings
Fig. 1 is a flow chart of a JavaScript code detection method in an embodiment of the application;
fig. 2 is a schematic structural diagram of an apparatus for detecting JavaScript codes according to an embodiment of the present application;
fig. 3 is a schematic structural diagram of a computer device according to an embodiment of the present application.
The achievement of the objects, functional features and advantages of the present application will be further described with reference to the accompanying drawings, in conjunction with the embodiments.
Detailed Description
It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the application. The terms first, second and the like in the description and in the claims and in the above-described figures, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used may be interchanged where appropriate such that the embodiments described herein may be implemented in other sequences than those illustrated or otherwise described herein. Furthermore, the terms "comprises," "comprising," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or modules is not necessarily limited to those listed or explicitly listed or inherent to such process, method, article, or apparatus, but may include other steps or modules that may not be listed or inherent to such process, method, article, or apparatus, the partitioning of such modules by the present application may be by one logical partitioning, and may be implemented by other means, such as a plurality of modules may be combined or integrated in another system, or some features may be omitted, or not implemented.
The application provides a JavaScript code detection method, a JavaScript code detection device, javaScript code detection equipment and a storage medium, which can be used for detecting abnormal variables of if sentences in JavaScript codes.
In order to solve the technical problems, the application mainly provides the following technical scheme:
the application provides a method for detecting variable information when an if code is abnormal. The method comprises the steps of obtaining JavaScript codes and constructing abstract syntax trees corresponding to the JavaScript codes; searching checking rules in the JavaScript code through an automatic script; traversing an if statement in the abstract syntax tree to obtain an if statement of the JavaScript code; respectively setting identification information for variables in each if statement in the JavaScript code; comparing and detecting variables in each row of if sentences in the JavaScript code by using a regular expression and a checking rule to obtain a detection result; if the detection result is failure, returning to the empty set and sending out an abnormal notification.
Referring to fig. 1, the following illustrates a JavaScript code detection method according to the present application, where the method includes:
101. and acquiring JavaScript codes, and constructing an abstract syntax tree corresponding to the JavaScript codes. The JavaScript is an transliteration script language, is a dynamic, prototype-based language, and is built-in with support types. Its interpreter, called JavaScript engine, is an integral part of the browser, which JavaScript is a scripting language for clients and is used on hypertext markup language (Hyper Text Markup Language, HTML) web pages to add dynamic functionality to the HTML web pages. Consists of ECMAScript, document object model and browser object model. The ECMAScript describes the JavaScript syntax and basic object. The document object model describes a method and interface for processing web page content. The browser object model describes a method and interface for interacting with a browser. The JavaScript is used to include embedding dynamic text in HTML pages, responding to browser events, reading and writing HTML elements, validating data before it is submitted to the server, detecting browser information for guests, controlling Cookies, and server-side programming based on node. Js technology. The Cookies are one way in which a server or script may maintain information on a client workstation under the HTTP protocol. Cookies are small text files saved by a Web server on a user's browser, which may contain information about the user. The Web site can access Cookie information whenever the user links to the server.
For example, when a website is accessed, the website stores a small text file on your machine, the Cookie records information such as your user ID, password, browsed web page, stay time, etc., and when the website is accessed again, the website reads the Cookie to obtain your relevant information, so that corresponding actions can be performed, such as displaying your slogan on the page, or allowing you to log in directly without inputting ID, password, etc. The "set/view file" may be selected in the "regular" tab of the "tools/Internet options" of the IE to view all cookies saved into the computer. These files are typically named in the user@domain format, with user being your local user name.
The abstract syntax tree is created by a syntax analyzer. The grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code. The data structure refers to a collection of data elements that have one or more specific relationships with each other. The abstract Syntax tree (Abstract Syntax Tree, AST), or simply Syntax tree (syncax tree), is an abstract representation of the source code Syntax structure. It represents the syntax structure of a programming language in the form of a tree, each node on the tree representing a structure in the source code.
Abstract syntax trees are widely used as an intermediate representation form of programs in various fields such as program analysis. A variety of source processing tools, such as a source browser, an intelligent editor, and a language translator, may be conveniently implemented using an abstract syntax tree. The code may be parsed by the abstract syntax tree.
102. Checking rules in JavaScript code are looked up through an automation script.
The checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable.
The check rule refers to a rule for detecting an abnormal variable in an if statement.
And traversing the if statement in the abstract syntax tree to obtain the if statement of the JavaScript code.
Traversing the abstract syntax tree by a depth-first method. The initial state for a given G is that all vertices have not been visited. For example, if a vertex v is selected as the initial departure point in G, the depth-first traversal may be defined as follows: firstly, accessing a departure point v and marking the departure point v as accessed; each adjacency point w of v is then searched in turn starting from v. If w has not been visited, then proceeding depth-first traversal with w as new departure point until all vertices in the graph with path to source point v have been visited. If there are still non-visited vertices in the graph at this time, the above process is repeated with an alternative non-visited vertex as a new source point until all vertices in the graph have been visited.
104. And setting identification information for variables in each if statement in the JavaScript code respectively.
The variables include at least global or local variables. The Local Variable refers to a Variable inside a function, the scope of the Local Variable is limited to the inside of the function, the Local Variable is invalid after leaving the function, and errors are reported when the Local Variable is reused. Global Variable (Global Variable) refers to a Variable defined outside of all functions whose scope is by default the whole program, i.e. all source files. The source file refers to a code file written in storage assembly language or a high-level language.
105. And comparing and detecting the variable in each line if statement in the JavaScript code by using the regular expression and the check rule to obtain a detection result.
The regular expression refers to a regular expression (Regular Expression, regex) for retrieving and replacing text conforming to the rules. Regular expressions refer to a logical formula of string operations, and a "regular string" is formed by using specific characters defined in advance and combinations of the specific characters, and is used for expressing a filtering logic for the string. The following objects can be achieved by regular expressions:
1. whether a given string conforms to the filtering logic of a regular expression.
2. The specific part that we want can be obtained from the string by regular expression.
106. And if the detection result is successful, returning an array and updating the attribute of the regular expression object and the regular expression object.
Or if the detection result is failure, returning to an empty set (NULL) and sending out an abnormal notification.
The regular expression is a logic formula for operating the character string and the special characters, namely, a "regular character string" is formed by a plurality of specific characters defined in advance and the combination of the specific characters, and the "regular character string" is used for expressing a filtering logic for the character string. A regular expression is a text pattern that describes one or more strings to be matched when searching text.
Taking an analysis array as an example, the definition structure is in a [ subscript variable name ] format, and when the subscript variable name ] target variable is not judged in a value range before use. According to the inspection rules, [ subscript variable name ] satisfies the following format:
[ beginning with letters or underlines beginning with the second digit consisting of letters, underlines, numbers ]
For example, scanning vEveryKom index code sentence, according to the format of index variable name and through regular expression, index character string meets the word rule of "first second digit of letter or underline is formed by letter, underline, number, etc., and also meets the rule of symbol following it, so the segmented index variable is index.
After the comparison detection is finished, the abnormal information of the variables is stored in a database and summarized, and the file information of the tandem assembly is presented by using a monitoring log. The anomaly information of the recorded variables includes at least: at least one of checking path, code name to be checked, error line number and error description. And counting and displaying information of the monitoring log through a visual chart. The visual chart includes at least a bar chart, a histogram, a line chart, and a pie chart. The visual chart is selected by selecting the visual chart type, and any visual chart or a plurality of visual charts can be saved and displayed. The integral statistical report function is added, and the variable anomaly problem easily occurring in the JavaScript code is fed back more intuitively.
In the above embodiment, the abstract syntax tree is traversed to find out the abnormal variable in the if statement, identify the variable in the if statement, and if the identified variable is abnormal according to the regular expression and the inspection rule, send an abnormality notification to the relevant developer. The problem that if statement variables in JavaScript codes are undefined is solved, error reporting prompt is carried out on an developer, and the robustness of the codes is guaranteed. The robustness refers to the processing power of the software for input situations outside of the specification requirements. A robust system has the capability of judging that an input outside the specification requirements does not meet the specification requirements and has a reasonable processing mode.
In some embodiments, the obtaining the if statement of the JavaScript code by traversing the if statement in the abstract syntax tree comprises:
and traversing all objects of the abstract syntax tree circularly, and if the objects are equal to the if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement. The array is used for storing the state of the JavaScript code. The state includes an attribute of the code.
Searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
In the embodiment, the method can search the if statement in the JavaScript code and identify the code containing the if statement.
In some embodiments, the variable identification information includes at least one of: the method comprises the steps of detecting function names of functions of the mark variable assignment statement of JavaScript, line numbers of the mark variable assignment statement of JavaScript code, file names of source files of the mark variable assignment statement of JavaScript code.
In the above embodiment, the identification information can better find that an abnormality exists in a variable, for example, a sinx function x variable indicated as 101 st line in the source program file. For example, if the undefined problem exists in the A variable in the code line number 110, the variable flag information is 32768, and if the fixed format is error X flag information-X abnormal information exists in the X-th code line number, the abnormal information is reminded of the undefined problem in the A variable in the error 32768-110.
In some embodiments, the exception notification is a notification generated by combining a code line number, an exception variable, the exception information, and an exception flag in a fixed format.
In the above embodiment, when the abnormality code is determined, the abnormality notification may be generated based on the abnormality code, the abnormality information, and the abnormality data, so that the developer is notified based on the abnormality notification.
For example, when detecting that there are three variables x, y and z in the code line number 555, respectively, where the variables are undefined, the variable is repeatedly defined, and the local variable is used in the global domain, the variable flag information is 32768, 12668 and 654, respectively, if the fixed format is error XX flag information-there is XX abnormal information in the XX line number, the abnormal information is reminded that there is undefined problem for the x variable in the 32768-555 th line.
And after the exception processing of the x variable is completed, detecting again, wherein the exception information is reminded that the y variable in the error 12668-555 th row has the problem of repeated variable definition.
After the exception processing of the x variable and the y variable is completed, the exception information is reminded that the z variable in the error 654-555 th row has the problem of using the local variable in the global domain.
In some embodiments, the exception flag refers to a 32-bit binary number for detecting code exceptions, where each bit of the binary number represents an exception state that conveys each code run.
After the comparison detection is completed, a 32-bit 2-ary number is returned.
If the ith bit is 1, the code of the (i+1) th function is determined to operate normally.
If the ith bit is 0, determining that the code operation of the (i+1) th function is abnormal. And i is an integer of 0 or more and 32 or less.
In the above embodiment, taking the variable type definition error as an example, the 0 th bit of the 2 nd number represents the variable type definition error. After executing the whole flow, returning a 32-bit 2-system number to detect the execution state of each function, if the 0 th bit is 1, the variable type definition is correct, and if the 0 th bit is 0, the variable type definition is wrong, and judging whether the variable type definition has a problem or not by the method.
In some embodiments, the specification categories include at least: naming convention, claim convention, annotation convention, constant convention, bean definition convention, transaction convention, script convention, log convention, exception handling convention, junit convention, error class convention, warning class convention, and hint class convention.
In the above embodiment, the above specifications establish the inspection rule of the variables. Such as duplicate definitions of variables, non-initialization of variables, non-use of character variable types, and non-definition of variables, etc. The naming specification refers to naming functions, naming variables and naming files, and consistency rules are the most important of naming management; comprising the following steps: all lower cases of project names, all lower cases of package names, all upper cases of class names, variable names, all lower cases of method names and all upper cases of constant names; the naming convention further includes: names can only consist of letters, numbers, underlining, $symbols, cannot start with numbers, names cannot use keywords in JAVA, and it is not always allowed that chinese and pinyin naming occur. The annotation specifications include copyright annotations, class annotations (Class), constructor annotations (Constructor), methods annotations (Methods), code Block annotations (Block), single sentence annotations, and field name annotations. The bean definition specification includes the means of operation that the class must use public decoration, must guarantee a common non-parametric constructor, contain attributes. The Junit specification includes: naming definition specification of test class, naming definition specification of test case, package name definition specification of test program and naming specification of variable.
The variable names of the test program are all in a case-by-case mixed mode, the first letter of the first word is lowercase, and the first letter of the following word is uppercase. For example: maxValue, setMobileBind and GetMobilebind et al. When the name is inconsistent with the rule, if the first letter of max of maxValue in maxValue is not capitalized, searching out max according to the checking rule and changing the max into maxValue.
In some embodiments, after the returning the empty set, the method further comprises:
sending out indication information; the instruction information is used for indicating the detection result of each if statement in the JavaScript code.
In the above embodiment, the variable information indicating the abnormality and the responsible person for the abnormality may be used to realize that different abnormality information is transferred to different responsible persons. For example, the responsibility of the variable information of the first abnormality is A, the responsibility of the variable information of the second abnormality is B, the contact information of A is configured with the variable information of the first abnormality through configuration, and the contact information of B is configured with the variable information of the second abnormality through configuration, so that the association of the variable information of the first abnormality, the contact information of A and the contact information of B with the variable information of the second abnormality are completed, and when a task is in error, the abnormal task is processed more efficiently by indicating who knows the responsibility of the specific task error.
A schematic structural diagram of an apparatus 20 for detecting JavaScript code as shown in fig. 2 is applicable to JavaScript code detection. The device for detecting JavaScript codes in the embodiment of the application can realize the steps corresponding to the method for detecting JavaScript codes executed in the embodiment corresponding to fig. 1. The function implemented by the device 20 for detecting JavaScript codes may be implemented by hardware, or may be implemented by executing corresponding software by hardware. The hardware or software includes one or more modules corresponding to the functions described above, which may be software and/or hardware. The means for detecting JavaScript code may include an input/output module 201 and a processing module 202, and the functional implementation of the processing module 202 and the input/output module 201 may refer to the operations performed in the embodiment corresponding to fig. 1, which are not described herein. The input-output module 201 may be used to control the input, output and acquisition operations of the input-output module 201.
In some embodiments, the input-output module 201 may be configured to obtain JavaScript code.
The processing module 202 may be configured to construct an abstract syntax tree corresponding to the JavaScript code. The abstract syntax tree is created by a syntax analyzer. The grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code. Checking rules in JavaScript code are looked up through an automation script. The checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable. And traversing the if statement in the abstract syntax tree to obtain the if statement of the JavaScript code. And setting identification information for variables in each if statement in the JavaScript code respectively. And comparing and detecting the variable in each line if statement in the JavaScript code by using the regular expression and the check rule to obtain a detection result.
If the detection result is successful, an array is returned through the input/output module 201 and the attribute of the regular expression object and the regular expression object are updated.
Or if the detection result is failure, returning to the empty set and sending out an abnormal notification through the input/output module 201.
In some embodiments, the processing module 202 is further configured to:
and traversing all objects of the abstract syntax tree circularly, and if the objects are equal to the if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement. The array is used for storing the state of the JavaScript code. The state includes an attribute of the code.
Searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
In some embodiments, the variable identification information includes at least one of: the method comprises the steps of detecting function names of functions of the mark variable assignment statement of JavaScript, line numbers of the mark variable assignment statement of JavaScript code, file names of source files of the mark variable assignment statement of JavaScript code.
In some embodiments, the exception notification is a notification generated by combining a code line number, an exception variable, the exception information, and an exception flag in a fixed format.
In some embodiments, the exception flag refers to a 32-bit binary number for detecting code exceptions, where each bit of the binary number represents an exception state that conveys each code run.
The processing module 202 is further configured to:
after the comparison detection is completed, a 32-bit 2-ary number is returned.
If the ith bit is 1, the code of the (i+1) th function is determined to operate normally.
If the ith bit is 0, determining that the code operation of the (i+1) th function is abnormal. And i is an integer of 0 or more and 32 or less.
In some embodiments, the specification categories include at least: naming convention, claim convention, annotation convention, constant convention, bean definition convention, transaction convention, script convention, log convention, exception handling convention, junit convention, error class convention, warning class convention, and hint class convention.
In some embodiments, the processing module 202 is further configured to:
sending out indication information; the instruction information is used for indicating the detection result of each if statement in the JavaScript code.
The creation means in the embodiment of the present application are described above from the point of view of modularized functional entities, and the following describes a computer device from the point of view of hardware, as shown in fig. 3, which includes: a processor, a memory, an input output unit (which may also be a transceiver, not identified in fig. 3) and a computer program stored in the memory and executable on the processor. For example, the computer program may be a program corresponding to the JavaScript code detection method in the embodiment corresponding to fig. 1. For example, when the computer device implements the function of the apparatus for detecting JavaScript code 20 shown in fig. 2, the processor implements each step in the method for detecting JavaScript code performed by the apparatus for detecting JavaScript code 20 in the embodiment corresponding to fig. 2 described above when executing the computer program. Alternatively, the processor may implement the functions of each module in the apparatus 20 for detecting JavaScript code according to the embodiment corresponding to fig. 2 when executing the computer program. For another example, the computer program may be a program corresponding to the JavaScript code detection method in the embodiment corresponding to fig. 1.
The processor may be a central processing unit (Central Processing Unit, CPU), other general purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), off-the-shelf programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like that is a control center of the computer device, connecting various parts of the overall computer device using various interfaces and lines.
The memory may be used to store the computer program and/or modules, and the processor may implement various functions of the computer device by running or executing the computer program and/or modules stored in the memory, and invoking data stored in the memory. The memory may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program (such as a sound playing function, an image playing function, etc.) required for at least one function, and the like; the storage data area may store data (such as audio data, video data, etc.) created according to the use of the cellular phone, etc. In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as a hard disk, memory, plug-in hard disk, smart Media Card (SMC), secure Digital (SD) Card, flash Card (Flash Card), at least one disk storage device, flash memory device, or other volatile solid-state storage device.
The input-output unit may be replaced by a receiver and a transmitter, and may be the same or different physical entities. Are the same physical entities and may be collectively referred to as input/output units. The input and output may be a transceiver.
The memory may be integrated in the processor or may be provided separately from the processor.
From the above description of the embodiments, it will be clear to those skilled in the art that the above-described embodiment method may be implemented by means of software plus a necessary general hardware platform, but of course may also be implemented by means of hardware, but in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present application may be embodied essentially or in a part contributing to the prior art in the form of a software product stored in a storage medium (e.g. ROM/RAM), comprising instructions for causing a terminal (which may be a mobile phone, a computer, a server or a network device, etc.) to perform the method according to the embodiments of the present application.
While the embodiments of the present application have been described above with reference to the drawings, the present application is not limited to the above-described embodiments, which are merely illustrative and not restrictive, and many modifications may be made thereto by those of ordinary skill in the art without departing from the spirit of the present application and the scope of the appended claims, which are to be accorded the full scope of the present application as defined by the following description and drawings, or by any equivalent structures or equivalent flow changes, or by direct or indirect application to other relevant technical fields.

Claims (9)

1. A method for JavaScript code detection, comprising:
acquiring a JavaScript code, and constructing an abstract syntax tree corresponding to the JavaScript code; the abstract syntax tree is created by a syntax analyzer; the grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code;
searching checking rules in the JavaScript code through an automatic script; the checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable;
obtaining an if statement of the JavaScript code by traversing the if statement in the abstract syntax tree;
respectively setting identification information for variables in each if statement in the JavaScript code;
comparing and detecting variables in each row of if sentences in the JavaScript code by using a regular expression and the checking rule to obtain a detection result;
if the detection result is successful, returning an array and updating the attribute of the regular expression object and the regular expression object;
or if the detection result is failure, returning to the empty set and sending out an abnormal notification;
the step of obtaining the if statement of the JavaScript code by traversing the if statement in the abstract syntax tree comprises the following steps:
circularly traversing all objects of the abstract syntax tree, and if the objects are equal to an if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement; the array is used for storing the state of the JavaScript code; the state includes an attribute of the code;
searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
2. The method of claim 1, wherein the variable identification information comprises at least one of: the method comprises the steps of detecting function names of functions of the mark variable assignment statement of JavaScript, line numbers of the mark variable assignment statement of JavaScript code, file names of source files of the mark variable assignment statement of JavaScript code.
3. The method of claim 1, wherein the exception notification is a notification generated by combining a code line number, an exception variable, exception information, and an exception flag in a fixed format.
4. A method according to claim 3, wherein the exception flag is a 32-bit binary number for detecting code exceptions, each bit of the binary number representing an exception state for each code run;
after the comparison detection is finished, returning a 32-bit 2-system number;
if the ith bit is 1, determining that the code of the (i+1) th function operates normally;
if the ith bit is 0, determining that the code operation of the (i+1) th function is abnormal; and i is an integer of 0 or more and 32 or less.
5. The method according to claim 1, wherein the specification categories include at least: naming convention, claim convention, annotation convention, constant convention, bean definition convention, transaction convention, script convention, log convention, exception handling convention, junit convention, error class convention, warning class convention, and hint class convention.
6. The method of claim 1, wherein after the returning of the empty set, the method further comprises:
sending out indication information; the instruction information is used for indicating the detection result of each if statement in the JavaScript code.
7. An apparatus for detecting JavaScript code, the apparatus comprising:
the input/output module is used for acquiring JavaScript codes;
the processing module is used for constructing an abstract syntax tree corresponding to the JavaScript code; the abstract syntax tree is created by a syntax analyzer; the grammar analyzer refers to a component of a compiler or an interpreter, and is used for grammar checking and constructing a data structure according to variables in the JavaScript code; searching checking rules in the JavaScript code through an automatic script; the checking rule refers to a rule for generating an abnormal variable in the detection if statement through the applicable scene, preconditions, standard types and judgment standards of the variable; obtaining an if statement of the JavaScript code by traversing the if statement in the abstract syntax tree; respectively setting identification information for variables in each if statement in the JavaScript code; comparing and detecting variables in each row of if sentences in the JavaScript code by using a regular expression and the checking rule to obtain a detection result;
if the detection result is successful, returning an array through the input/output module and updating the attribute of the regular expression object and the regular expression object;
or if the detection result is failure, returning an empty set through the input/output module and sending out an abnormal notification;
the step of obtaining the if statement of the JavaScript code by traversing the if statement in the abstract syntax tree comprises the following steps:
circularly traversing all objects of the abstract syntax tree, and if the objects are equal to an if character string through object name judgment and if the matching array contains an if, determining that the JavaScript code contains an if statement; the array is used for storing the state of the JavaScript code; the state includes an attribute of the code;
searching whether a variable exists in an if statement in the JavaScript code, if so, judging that the if statement contains the variable and identifying the if statement.
8. A computer device, the computer device comprising:
at least one processor, memory, and input output unit;
wherein the memory is for storing program code and the processor is for invoking the program code stored in the memory to perform the method of any of claims 1-6.
9. A computer storage medium comprising instructions which, when run on a computer, cause the computer to perform the method of any of claims 1-6.
CN201910846579.6A 2019-09-09 2019-09-09 JavaScript code detection method, device, equipment and storage medium Active CN110688307B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910846579.6A CN110688307B (en) 2019-09-09 2019-09-09 JavaScript code detection method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910846579.6A CN110688307B (en) 2019-09-09 2019-09-09 JavaScript code detection method, device, equipment and storage medium

Publications (2)

Publication Number Publication Date
CN110688307A CN110688307A (en) 2020-01-14
CN110688307B true CN110688307B (en) 2023-11-17

Family

ID=69108064

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910846579.6A Active CN110688307B (en) 2019-09-09 2019-09-09 JavaScript code detection method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN110688307B (en)

Families Citing this family (10)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111427786A (en) * 2020-03-25 2020-07-17 中国建设银行股份有限公司 Abnormal Full GC problem detection method and device
CN111562910B (en) * 2020-05-08 2023-06-02 上海泛微网络科技股份有限公司 Packaging method and related equipment
CN111881059B (en) * 2020-08-10 2023-10-27 网易(杭州)网络有限公司 Code file detection method and device and electronic equipment
CN112015430B (en) * 2020-09-07 2024-08-16 平安国际智慧城市科技股份有限公司 JavaScript code translation method, device, computer equipment and storage medium
CN112363832B (en) * 2020-11-10 2023-07-28 中国平安人寿保险股份有限公司 Ignite-based distributed data processing method and device and computer equipment
CN112540811B (en) * 2020-12-15 2022-03-18 平安科技(深圳)有限公司 Cache data detection method and device, computer equipment and storage medium
CN114968184B (en) * 2021-02-25 2024-11-01 中国电信股份有限公司 Script frame recognition method and device and computer readable storage medium
CN114579203B (en) * 2022-03-07 2024-03-15 网易(杭州)网络有限公司 Script file checking method and device, electronic equipment and storage medium
CN115344297A (en) * 2022-08-08 2022-11-15 青岛海尔科技有限公司 Configuration data searching method, storage medium and electronic device
CN117439997B (en) * 2023-12-20 2024-03-08 天津华来科技股份有限公司 Method for constructing general P2P communication architecture based on modularized design

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
KR101051600B1 (en) * 2010-03-29 2011-07-22 주식회사 소프트 포 소프트 Code Inspection Execution System
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN105975392A (en) * 2016-04-29 2016-09-28 国家计算机网络与信息安全管理中心 Duplicated code detection method and device based on abstract syntax tree
CN107678957A (en) * 2017-09-25 2018-02-09 中国银行股份有限公司 A kind of Net silver code automatic checking method and device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
KR101051600B1 (en) * 2010-03-29 2011-07-22 주식회사 소프트 포 소프트 Code Inspection Execution System
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN105975392A (en) * 2016-04-29 2016-09-28 国家计算机网络与信息安全管理中心 Duplicated code detection method and device based on abstract syntax tree
CN107678957A (en) * 2017-09-25 2018-02-09 中国银行股份有限公司 A kind of Net silver code automatic checking method and device

Also Published As

Publication number Publication date
CN110688307A (en) 2020-01-14

Similar Documents

Publication Publication Date Title
CN110688307B (en) JavaScript code detection method, device, equipment and storage medium
US12164913B2 (en) Automating identification of code snippets for library suggestion models
US12032475B2 (en) Automating identification of test cases for library suggestion models
CN112015430B (en) JavaScript code translation method, device, computer equipment and storage medium
US10261984B2 (en) Browser and operating system compatibility
US11042467B2 (en) Automated searching and identification of software patches
CN109145235B (en) Method and device for analyzing webpage and electronic equipment
CN113961192A (en) Code data processing method and device and electronic equipment
CN115408453A (en) Configured report generation method and device, computer equipment and storage medium
CN111159045A (en) Compatibility problem detection method, device and storage medium
CN113419738A (en) Interface document generation method and device and interface management equipment
CN114398138B (en) Interface generation method, device, computer equipment and storage medium
CN115577032A (en) Method, device, terminal and storage medium for converting JSON (Java Server object notation) into SQL (structured query language)
CN118113399A (en) Firmware configuration page generation method and device, computer equipment and storage medium
CN117667716A (en) Page testing method and device and electronic equipment
CN113408250B (en) Project file processing method and device
CN116432176A (en) Web malicious program detection method and system based on cross-language semantic analysis
CN114895914A (en) Method, device, electronic device and storage medium for generating log output code
CN108733353B (en) Interface construction method and device
CN118606354B (en) Visualization method and device for program execution scheme, electronic equipment and storage medium
CN119248239B (en) Code generation method, device, equipment and medium
CN118445198A (en) A method, device, terminal device and storage medium for determining air space
CN117608719A (en) Page element searching method and device, electronic equipment and medium
CN120492684A (en) SQL logic detection and optimization method, device, equipment and medium
CN119621587A (en) Generation method, test method, electronic device, storage medium and program product

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right

Effective date of registration: 20231024

Address after: Room 2, Floor 11, Unit 2, Building 2, Baiyi South Bank, south of Binhe Avenue, Liuwu Avenue, Liuwu New District, Lhasa, Xizang Autonomous Region, 850000

Applicant after: Guoxin Jinhong Information Consulting Co.,Ltd.

Address before: 518000 Room 202, block B, aerospace micromotor building, No.7, Langshan No.2 Road, Xili street, Nanshan District, Shenzhen City, Guangdong Province

Applicant before: Shenzhen LIAN intellectual property service center

Effective date of registration: 20231024

Address after: 518000 Room 202, block B, aerospace micromotor building, No.7, Langshan No.2 Road, Xili street, Nanshan District, Shenzhen City, Guangdong Province

Applicant after: Shenzhen LIAN intellectual property service center

Address before: Room 201, building a, No.1 Qianwan 1st Road, Shenzhen Hong Kong cooperation zone, Shenzhen, Guangdong Province (settled in Shenzhen Qianhai business secretary Co., Ltd.)

Applicant before: PING AN PUHUI ENTERPRISE MANAGEMENT Co.,Ltd.

TA01 Transfer of patent application right
GR01 Patent grant
GR01 Patent grant