Disclosure of Invention
The invention mainly aims to provide a method and a device for processing JAVASCRIPT variables and computer equipment, and aims to solve the problem of variable promotion in JavaScript.
The invention provides a method for processing JAVASCRIPT variables, which comprises the following steps:
acquiring code data to be detected;
traversing the code data, and detecting target code parameters with set prefixes in the code data;
detecting the variable type of the target code parameter; wherein the variable types include invalid code parameters, reference-only code parameters, and reassigned code parameters;
and processing the target code parameters according to the variable types.
Further, the step of traversing the code data and detecting the target code parameter with the set prefix in the code data includes:
acquiring a preset variable statement in JAVASCRIPT;
acquiring the set prefix according to the variable statement;
and traversing the code data by adopting a sequence traversal method according to the set prefix to obtain the corresponding target code parameter.
Further, the step of detecting the variable type of the object code parameter includes:
sequentially performing declaration testing on each target code parameter in a file corresponding to the code data;
judging whether the operation corresponding to each code data appears in the file or not;
and if the corresponding operation does not occur, determining the corresponding target code parameter as an invalid code parameter.
Further, after the step of determining whether an operation corresponding to each code data occurs in the file, the method further includes:
detecting a declaration operation of the object code parameter of the corresponding operation;
judging whether the declaration operation has the operation of reassigning the target code parameter;
and if the reassignment operation is not carried out, the corresponding target code parameter is determined to be the reference-only code parameter which is only referenced.
Further, after the step of determining whether the declaration operation includes a reassignment operation for the target code parameter, the method further includes:
and if the reassignment operation exists, identifying the corresponding target code parameter as a reassignment code parameter.
Further, the step of acquiring the code data to be detected includes:
acquiring a file to be detected;
standardizing the files to be detected to obtain corresponding temporary files;
and extracting the code data in the temporary file to obtain the code data to be detected.
Further, the step of performing standardization processing on the file to be detected to obtain a corresponding temporary file includes:
acquiring an initial file address in the file to be detected according to the address field in the file to be detected;
splicing the initial file address with a preset address to obtain a corresponding target address;
and updating the target address to the file address of the file to be detected.
The invention also provides a device for processing JAVASCRIPT variables, which comprises:
the acquisition module is used for acquiring code data to be detected;
the traversal module is used for traversing the code data and detecting a target code parameter with a set prefix in the code data;
the detection module is used for detecting the variable type of the target code parameter; wherein the variable types include invalid code parameters, reference-only code parameters, and reassigned code parameters;
and the processing module is used for processing the target code parameters according to the variable types.
The invention also provides a computer device comprising a memory storing a computer program and a processor implementing the steps of any of the above methods when the processor executes the computer program.
The invention also provides a computer-readable storage medium, on which a computer program is stored, which computer program, when being executed by a processor, carries out the steps of the method of any of the above.
The invention has the beneficial effects that: the target code parameters with the set prefixes in the code data are detected in a traversal mode, then the variable types of the target code parameters are detected, and the target code parameters are correspondingly processed according to the variable types, so that the automatic optimization processing of JAVASCRIPT variables is realized, the problem of variable improvement in the research and development process is solved, the code readability is better, and the research and development speed is further accelerated.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that all directional indicators (such as up, down, left, right, front, back, etc.) in the embodiments of the present invention are only used to explain the relative position relationship between the components, the motion situation, etc. in a specific posture (as shown in the drawings), and if the specific posture is changed, the directional indicator is changed accordingly, and the connection may be a direct connection or an indirect connection.
The term "and/or" herein is merely an association describing an associated object, meaning that three relationships may exist, e.g., a and B, may mean: a exists alone, A and B exist simultaneously, and B exists alone.
In addition, the descriptions related to "first", "second", etc. in the present invention are only for descriptive purposes and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. In addition, technical solutions between various embodiments may be combined with each other, but must be realized by a person skilled in the art, and when the technical solutions are contradictory or cannot be realized, such a combination should not be considered to exist, and is not within the protection scope of the present invention.
Referring to fig. 1, the present invention provides a method for processing JAVASCRIPT variables, including:
s1: acquiring code data to be detected;
s2: traversing the code data, and detecting target code parameters with set prefixes in the code data;
s3: detecting the variable type of the target code parameter;
s4: and processing the target code parameters according to the variable types.
As described in step S1, the code data to be detected is obtained by extracting the code data in the file to be detected, and then marking each code data according to the directory address of the code data in the file to be detected, so that the obtained code data can query the location of the code data in the file.
As described in step S2 above, the code data is traversed, and target code parameters with set prefixes in the code data are detected. Since the JAVASCRIPT variables are generally var prefixes, traversal can be performed by using the var prefixes as set prefixes, that is, codes with or without var prefixes in code data are detected, or positions where three letters of var are sequentially arranged are directly detected. And obtaining the corresponding target code parameters.
As described in the above step S3, the variable type of the object code parameter is detected by declaring the object code parameter, detecting whether operations such as assignment and reference occur in the corresponding file, if not, indicating that the object code parameter is an invalid object code parameter, that is, an invalid code parameter, if so, detecting whether the object code parameter is only declared, and then only referencing, and not performing an assignment operation, and if only referencing is performed, considering that the object code parameter is only referencing a code parameter. And if the assignment operation is carried out, the code parameter is considered as the reassigned code parameter.
As described in step S4 above, the target code parameter is processed according to the detected variable type of the target code parameter. For example, if the variable type is an invalid code parameter, the target code parameter may be deleted, and if the variable type is a reference-only code parameter, the target code parameter may be replaced with a set prefix, that is, the var prefix may be modified to a const prefix. If the code parameter is reassigned, the target code parameter is replaced by the preset prefix, namely the var prefix is modified into the let prefix.
In one embodiment, the step S2 of traversing the code data and detecting the target code parameter with the set prefix in the code data includes:
s201: acquiring a preset variable statement in JAVASCRIPT;
s202: acquiring the set prefix according to the variable statement;
s203: and traversing the code data by adopting a sequence traversal method according to the set prefix to obtain the corresponding target code parameter.
As described in the above steps S201-S203, the target code parameter is obtained, and the ES5 syntax in JAVASCRIPT is declared through the variable prefix variable, so that there is a problem of variable promotion, that is, it allows the variable to be accessed without declaration, or declares that before the variable is used later, some novice developers set the variable declaration at will, which results in poor readability of the code, reduces maintainability of the code, and also pollutes global variables, lacks definitions of variables and constants, and therefore requires modifying the prefix of the variable declaration to construct a stable and uniform variable declaration prefix. That is, a preset variable declaration is obtained, for example, the ES5 syntax passes through the var key variable declaration, the set prefix var can be obtained, and then the code data is traversed by a hierarchical traversal method. The method specifically includes creating a queue, detecting a first root node with the highest hierarchy, then removing the first root node from the queue, sequentially changing a second root node which is a subroot node of the first root node according to a set sequence, sequentially detecting a subroot node of each second root node, and repeating the steps to finally complete the variable of code data.
In one embodiment, the step S3 of detecting the variable type of the object code parameter includes:
s301: sequentially performing declaration testing on each target code parameter in a file corresponding to the code data;
s302: judging whether the operation corresponding to each code data appears in the file or not;
s303: and if the corresponding operation does not occur, determining the corresponding target code parameter as an invalid code parameter.
As described above in steps S301-S303, detection of assertion of an invalid code parameter is achieved. Specifically, each object code parameter may be subjected to declaration testing in a corresponding file, whether a corresponding operation such as assignment, reference, and the like occurs in each file is detected, and if not, the object code parameter is an invalid code parameter, and then deletion processing may be performed.
In an embodiment, after the step S302 of determining whether an operation corresponding to each code data occurs in the file, the method further includes:
s3031: detecting a declaration operation of the object code parameter of the corresponding operation;
s3032: judging whether the declaration operation has the operation of reassigning the target code parameter;
s3033: and if the reassignment operation is not carried out, the corresponding target code parameter is determined to be the reference-only code parameter.
As described in the above steps S3031-S3033, the detection of the target code parameter is realized. The declaration operation with the target code parameter is detected first, and because there may be an operation corresponding to the declaration operation in response, it can be detected whether there is a reassignment operation in the corresponding declaration operation, that is, the declaration operation is divided into two types, one is to only refer, and the other needs the target code parameter to participate in calculation, that is, the target code parameter needs to be reassigned, if there is no reassignment operation, the target code parameter is considered to be the only-referring code parameter, and the set prefix in the only-referring code parameter is modified into the first target prefix. If the prefix is set as var prefix, then the prefix is modified to const prefix. Wherein the const prefix is a syntax in ES6 (ECMAScript).
In an embodiment, after the step S3032 of determining whether there is a reassignment operation performed on the target code parameter in the declaration operation, the method further includes:
s30331: and if the reassignment operation exists, identifying the corresponding target code parameter as a reassignment code parameter.
As described in step S30331, if there is a corresponding assignment operation, the corresponding target code parameter is determined as a reassigned code parameter, and the set prefix in the reassigned code parameter is modified to a second target prefix, specifically, if the set prefix is a var prefix, the set prefix can be changed to a let prefix, so that processing of the prefix in the JAVASCRIPT variable is completed, and subsequent operations of the file can be performed based on the new prefix, that is, based on a conventional language code, so that variable promotion caused by the JAVASCRIPT variable does not occur. The let prefix is a syntax in ES6 (ECMAScript).
In one embodiment, the step S4 includes:
s401: detecting the code data after the prefix is modified, and extracting code parameters with a first target prefix or a second target prefix;
s402: and placing the extracted code parameters in a global variable directory of the code data.
As described in the above steps S401 to S402, the code data after the prefix modification is detected, and the code parameter having the first target prefix and the second target prefix is extracted, and for insurance, the code parameter may be moved to the directory of the global variable, so as to avoid polluting the data at the top layer after the modification. In a specific embodiment, the modified let prefix and the const prefix can be transferred to the globalData object under the window object, and all the fields in the globalData object belong to the global variables of the current file. The window objects at the top layer are not polluted, and the global objects of the current page are uniformly written and managed. In some embodiments, if a single variable is used in only one function. The variable declaration can be moved to the top level of the function block for declaration.
In one embodiment, the step S1 of acquiring code data to be detected includes:
s101: acquiring a file to be detected;
s102: standardizing the files to be detected to obtain corresponding temporary files;
s103: and extracting the code data in the temporary file to obtain the code data to be detected.
As described in the foregoing steps S101 to S103, the code data is obtained, and the file to be detected is obtained, where the obtaining may be by obtaining a file (hereinafter, referred to as a js file) in the development project with JAVASCRIPT language, and standardizing the js file, where the standardizing may be by deleting a code in the js file without JAVASCRIPT language, or by converting, supplementing, marking a format of a file in the js file with JAVASCRIPT language. To obtain the code data to be detected.
In an embodiment, the step S102 of performing a normalization process on the file to be detected to obtain a corresponding temporary file includes:
s1021: acquiring an initial file address in the file to be detected according to the address field in the file to be detected;
s1022: splicing the initial file address with a preset address to obtain a corresponding target address;
s1023: and updating the target address to the file address of the file to be detected.
As described in the foregoing steps S1021 to S1023, the address of the file to be detected is standardized, that is, according to the address field in the file to be detected, the corresponding initial file address is obtained, the file address where the js file to be converted is located is obtained, and the obtained file address is spliced with the preset address to generate a complete address. And if the initial file address is not set or is null, retrieving all js files according to the preset address.
Referring to fig. 2, the present invention further provides a device for processing JAVASCRIPT variables, including:
the acquisition module 10 is used for acquiring code data to be detected;
a traversing module 20, configured to traverse the code data, and detect a target code parameter having a set prefix in the code data;
a detection module 30, configured to detect a variable type of the target code parameter; wherein the variable types include invalid code parameters, reference-only code parameters, and reassigned code parameters;
and the processing module 40 is configured to process the target code parameter according to the variable type.
In one embodiment, traverse module 20 includes:
the variable statement acquisition submodule is used for acquiring a preset variable statement in JAVASCRIPT;
a set prefix obtaining submodule for obtaining the set prefix according to the variable statement;
and the traversal submodule is used for traversing the code data by adopting a sequence traversal method according to the set prefix to obtain the corresponding target code parameter.
In one embodiment, the detection module 30 includes:
the declaration testing submodule is used for sequentially performing declaration testing on each target code parameter in a file corresponding to the code data;
the operation judgment submodule is used for judging whether the operation corresponding to each code data appears in the file;
and the identification module is used for identifying the corresponding target code parameter as an invalid code parameter if the corresponding operation does not occur.
In one embodiment, the detection module 30 further includes:
a declaration operation detection submodule for detecting a declaration operation of the object code parameter for which the corresponding operation has occurred;
a reassignment detection submodule, configured to determine whether a reassignment operation is performed on the target code parameter in the declaration operation;
and the reference-only code parameter identification submodule is used for identifying the corresponding target code parameter as the reference-only code parameter if the reassignment operation is not carried out.
In one embodiment, the detection module 30 further includes:
and the reassignment code parameter identification submodule is used for identifying the corresponding target code parameter as a reassignment code parameter if the reassignment operation exists.
In one embodiment, the acquisition module 10 includes:
the file acquisition submodule is used for acquiring a file to be detected;
the standardization processing submodule is used for standardizing the file to be detected to obtain a corresponding temporary file;
and the code data extraction submodule is used for extracting the code data in the temporary file to obtain the code data to be detected.
In one embodiment, the normalization processing sub-module includes:
the acquisition unit is used for acquiring an initial file address in the file to be detected according to the address field in the file to be detected;
the splicing unit is used for splicing the initial file address with a preset address to obtain a corresponding target address;
and the updating unit is used for updating the target address to the file address of the file to be detected.
The invention has the beneficial effects that: the target code parameters with the set prefixes in the code data are detected in a traversal mode, then the variable types of the target code parameters are detected, and the target code parameters are correspondingly processed according to the variable types, so that the automatic optimization processing of JAVASCRIPT variables is realized, the problem of variable improvement in the research and development process is solved, the code readability is better, and the research and development speed is further accelerated.
Referring to fig. 3, a computer device, which may be a server and whose internal structure may be as shown in fig. 3, is also provided in the embodiment of the present application. The computer device includes a processor, a memory, a network interface, and a database connected by a system bus. Wherein the computer designed processor is used to provide computational and control capabilities. The memory of the computer device comprises a storage medium and an internal memory. The storage medium stores an operating system, a computer program, and a database. The memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store various code data and the like. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program can implement the method for processing JAVASCRIPT variables according to any of the above embodiments when executed by a processor. The processor, when executing the computer program, implements the steps of: acquiring code data to be detected; traversing the code data, and detecting target code parameters with set prefixes in the code data; detecting the variable type of the target code parameter; wherein the variable types include invalid code parameters, reference-only code parameters, and reassigned code parameters; and processing the target code parameters according to the variable types.
Those skilled in the art will appreciate that the architecture shown in fig. 3 is only a block diagram of some of the structures associated with the disclosed aspects and is not intended to limit the computing devices to which the disclosed aspects may be applied.
The embodiments of the present application further provide a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the method for processing JAVASCRIPT variables according to any of the embodiments above may be implemented. The computer-readable storage medium, when executed by the one or more processors, causes the one or more processors to perform the steps of: acquiring code data to be detected; traversing the code data, and detecting target code parameters with set prefixes in the code data; detecting the variable type of the target code parameter; wherein the variable types include invalid code parameters, reference-only code parameters, and reassigned code parameters; and processing the target code parameters according to the variable types.
It will be understood by those skilled in the art that all or part of the processes of the methods of the above embodiments may be implemented by hardware associated with instructions of a computer program, which may be stored on a non-volatile computer-readable storage medium, and when executed, may include processes of the above embodiments of the methods. Any reference to memory, storage, database, or other medium provided herein and used in the examples may include non-volatile and/or volatile memory. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), double-rate SDRAM (SSRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and bus dynamic RAM (RDRAM).
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, apparatus, article, or method that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, apparatus, article, or method. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, apparatus, article, or method that includes the element.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the scope of the claims of the present invention.