Method and apparatus for identifying global variables in public javascript files

By generating an abstract syntax tree and converting it into a JSON file, global variables in JavaScript files can be identified, solving the problem of difficulty in identifying referenced global variables in existing technologies and improving accuracy and efficiency.

CN116225454BActive Publication Date: 2026-03-24CHINA CONSTRUCTION BANK +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-04-03
Publication Date
2026-03-24

AI Technical Summary

Technical Problem

Existing technologies struggle to accurately identify whether global variables in JavaScript files are referenced by other files. This can lead to modifications to global variables affecting the normal display of other pages, and manual identification is costly.

Method used

By scanning the codebase, an abstract syntax tree is generated and converted into a JSON file. Based on the operations of declared variables, paragraphs in the JSON file are filtered out, and the JSON file is recursively traversed to determine which files contain global variables.

Benefits of technology

It achieves accurate identification of global variables, avoids affecting other pages when modifying global variables, and significantly reduces the cost of manual identification.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116225454B_ABST
    Figure CN116225454B_ABST
Patent Text Reader

Abstract

The application discloses a method and device for identifying global variables in public JavaScript files, and is applied to the technical field of software, and the method comprises the following steps: scanning a code library and extracting all public JavaScript files; generating a JSON file corresponding to each public JavaScript file based on an abstract syntax tree; screening out paragraphs corresponding to variable declaration operations in the JSON file based on the variable declaration operations; and recursively traversing all the JSON files, and determining global variables, public JavaScript files where the global variables are located and public JavaScript files referring to the global variables according to the structure of the paragraphs corresponding to the variable declaration operations in the JSON files. The application can accurately identify in which files the global variables in the public JavaScript files appear.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of software design, in particular to a method and device for identifying global variables in a public JavaScript file. BACKGROUND

[0002] This section is intended to provide background or context to the embodiments of the application recited in the claims. The description herein does not constitute admission that the prior art is prior art nor does it constitute an admission of any description in this section as prior art to an application described herein and / or in another application also owned by the applicant of the present application.

[0003] JS is a kind of front-end development technology, in the development process, in order to make the development simple, improve code reusability, make the common functional code block decoupling. Development engineer usually write public method in JS file, which is referenced by a large number of HTML or JSP files. In the development process, engineers often modify the common JS according to some needs, especially the global variables in the common JS. Due to the characteristics of JS language, if a statement runs error, the following language cannot continue to run. In this case, if the code of other HTML or JSP files referencing the common JS is not adjusted, the pages will have code error, and the pages may not be normally displayed. How to identify the global variables in the public JS file becomes particularly critical.

[0004] Common technical means include code scanning such as Sonar, but only code errors in public files can be identified, or whether the variable has been used in the current file, and the global variable cannot be identified whether it is referenced by other files, and the file name cannot be identified. The global variable introduced by other files cannot effectively solve the page loading error. More is that the developer pays attention to coding specification when writing code, and searches the code globally before modifying the global variable in the public JS to confirm that there is no other reference before modifying. Or do adaptive logic in the public code block to ensure that the modified public variable does not affect the display of other pages.

[0005] Due to the different coding habits of development engineers, in large projects, it is actually difficult to manage the public variables in the public JS. In addition, if the public JS is referenced by a large number of files, the work of manual global search and modification is too large, it is difficult to identify all the referenced files, and it is not conducive to ensuring the accuracy of the code. SUMMARY

[0006] The embodiments of the present application provide a method for identifying global variables in a public JavaScript file, which is used to check the JS code and accurately identify the global variables in the public JS file in which files appear, so as to prevent the modification of the global variables from affecting other pages. The method comprises the following steps:

[0007] scan the code base to extract all the common JavaScript files;

[0008] generate a JSON file corresponding to each common JavaScript file based on the abstract syntax tree;

[0009] filter out the paragraph corresponding to the operation of declaring variables in the JSON file based on the operation of declaring variables;

[0010] recursively traverse all the JSON files, and determine the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file referring to the global variable according to the structure of the paragraph corresponding to the operation of declaring variables in the JSON file.

[0011] The embodiment of the application also provides a device for identifying global variables in common JavaScript files, which is used for checking JS code and accurately identifying global variables in common JS files in which files the global variables appear, so as to prevent the global variables from affecting other pages when the global variables are modified.

[0012] The file extraction module is configured to scan the code base to extract all the common JavaScript files;

[0013] The JSON file generation module is configured to generate a JSON file corresponding to each common JavaScript file based on the abstract syntax tree;

[0014] The JSON file processing module is configured to filter out the paragraph corresponding to the operation of declaring variables in the JSON file based on the operation of declaring variables;

[0015] The reference file obtaining module is configured to recursively traverse all the JSON files, and determine the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file referring to the global variable according to the structure of the paragraph corresponding to the operation of declaring variables in the JSON file.

[0016] The embodiment of the application also provides a computer device, which comprises a memory, a processor, and a computer program stored in the memory and capable of running on the processor, and the processor implements the method for identifying global variables in common JavaScript files when executing the computer program.

[0017] The embodiment of the application also provides a computer readable storage medium, which stores a computer program, and the computer program implements the method for identifying global variables in common JavaScript files when executed by a processor.

[0018] The embodiment of the present application further provides a computer program product, which comprises a computer program, and the computer program is executed by a processor to realize the method for identifying the global variable in the common JavaScript file.

[0019] In the embodiment of the present application, the code library is scanned, and all common JavaScript files are extracted; based on an abstract syntax tree, a JSON file corresponding to each common JavaScript file is generated; based on a declaration variable operation, a paragraph corresponding to the declaration variable operation in the JSON file is screened out; all JSON files are recursively traversed, and according to the structure of the paragraph corresponding to the declaration variable operation in the JSON file, the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file referring to the global variable are determined. Compared with the technical solution that the code scanning such as Sonar in the prior art cannot identify whether the global variable is referred to by other files, the global variable of each common JavaScript file can be identified, and at the same time, by recursively traversing all JSON files, according to the structure of the paragraph corresponding to the declaration variable operation in the JSON file, the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file referring to the global variable, that is, the global variable in the common JavaScript file appears in which JavaScript file, so as to prevent the global variable from affecting other pages when the global variable is modified. BRIEF DESCRIPTION OF DRAWINGS

[0020] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or the prior art description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor. In the drawings:

[0021] Figure 1 The flowchart of the method for identifying the global variable in the common JavaScript file in the embodiment of the present application;

[0022] Figure 2 The schematic diagram of the JSON format of the exampleA file before processing in the embodiment of the present application;

[0023] Figure 3 The schematic diagram of the JSON format of the exampleA file after processing in the embodiment of the present application;

[0024] Figure 4 The schematic diagram of the JSON format of the exampleB file after processing in the embodiment of the present application;

[0025] Figure 5 Figure 1 is a schematic diagram of an apparatus for identifying global variables in a common JavaScript file in an embodiment of the present application.

[0026] Figure 6 Figure 2 is a schematic diagram of a computer device in an embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to make the objectives, technical solutions and advantages of the embodiments of the present application clearer, the embodiments of the present application are further described in detail below with reference to the accompanying drawings. Herein, the schematic embodiments of the present application and the descriptions thereof are used to explain the present application but not to limit the present application.

[0028] First, the terms involved in the present application are explained.

[0029] JavaScript: abbreviated as JS, is a lightweight, interpreted or just-in-time compiled programming language with first-class functions. JS is mainly used for the development of web pages, is a dynamic scripting language based on prototype programming and multi-paradigm, and supports object-oriented, imperative and declarative (such as functional programming) styles.

[0030] Abstract Syntax Tree: abbreviated as AST, is a tree-like representation of the abstract syntax structure of source code, and each node on the tree represents a structure in the source code. In JavaScript, any object (variable, function, expression, etc.) can be converted into a syntax tree in the form of JSON.

[0031] Global variable: a variable defined outside the function body is called a global variable, and the global variable can be used in all functions. If the global variable itself is modified, all references to the variable will change accordingly.

[0032] Figure 1 Figure 2 is a flowchart of a method for identifying global variables in a common JavaScript file in an embodiment of the present application, comprising:

[0033] Step 101: scanning the code base to extract all common JavaScript files;

[0034] Step 102: generating a JSON file corresponding to each common JavaScript file based on an abstract syntax tree;

[0035] Step 103: based on the operation of declaring a variable, filtering out the paragraph corresponding to the operation of declaring a variable in the JSON file;

[0036] Step 104, recursively traversing all JSON files, determining the global variable, the public JavaScript file where the global variable is located, the public JavaScript file referring to the global variable according to the structure of the paragraph corresponding to the operation of declaring the variable in the JSON file.

[0037] In the embodiment of the application, compared with the prior art, the global variable of each public JavaScript file can be identified, and the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file referring to the global variable can be determined by recursively traversing all JSON files according to the structure of the paragraph corresponding to the operation of declaring the variable in the JSON file and the variable name in the JSON file, so that the global variable is prevented from affecting other pages when being modified.

[0038] It should be noted that the application is based on the compiling mechanism of a JS file, and the AST abstract syntax tree is used to parse the JS file. By processing and parsing the generated JSON file, a general method for identifying variables in a public JS file is provided, and the embodiment of the application takes ECMAScript 5 as an example.

[0039] In step 101, the code library is scanned, and all public JavaScript files are extracted.

[0040] In the embodiment of the application, the global variable of each public JavaScript file can be identified, and the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file referring to the global variable can be determined by recursively traversing all JSON files according to the structure of the paragraph corresponding to the operation of declaring the variable in the JSON file and the variable name in the JSON file, so that the global variable is prevented from affecting other pages when being modified.

[0041] In step 102, based on the abstract syntax tree, a JSON file corresponding to each public JavaScript file is generated.

[0042] In an embodiment, after generating the JSON file corresponding to each public JavaScript file, the method further includes: adding a file name field nameId to each JSON file and assigning a value; and the file name of the JSON file is used to recursively traverse all JSON files to determine the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file referring to the global variable.

[0043] Wherein, Abstract Syntax Tree: abbreviation AST, is the tree-like representation of the abstract syntax structure of the source code, each node on the tree represents a structure in the source code. In JavaScript, any object (variable, function, expression, etc.) can be converted into a syntax tree in the form of JSON. Then, the embodiment of the application generates a JSON file corresponding to each public JavaScript file, which can facilitate subsequent calling, and after adding the file name field and assigning a value, file duplication can be prevented.

[0044] In an embodiment, after generating the JSON file corresponding to each public JavaScript file, it further includes:

[0045] adding a unique identification field to each JSON file and assigning a value;

[0046] After determining that the current JSON file is a JSON file that references global variables in the JSON file, it further includes:

[0047] outputting the value of the unique identification field of the current JSON file and displaying it.

[0048] The JSON features generated by the abstract syntax tree include information of all variables and functions in the generated JSON tree, such as reference line number, return value, start and end character position, etc. But it does not include file path information and cannot provide a unique identification to ensure the uniqueness of the JSON, which is not conducive to subsequent traversal of each JSON file to determine whether there are referenced variables. The application generates a unique JSON file corresponding to each public JavaScript file code by parsing the public JavaScript file code, and then compares the JSON files with each other to identify whether they contain global variables. It can more accurately identify whether a variable in a file is referenced by other files from the root, and further identify global variables.

[0049] In an embodiment, the value of the unique identification field is the absolute path absolutePath of the JSON file.

[0050] In step 103, based on the variable declaration operation, the paragraph corresponding to the variable declaration operation in the JSON file is filtered out;

[0051] In an embodiment, based on the variable declaration operation, the paragraph corresponding to the variable declaration operation in the JSON file is filtered out, including:

[0052] In the paragraph of the body field in the JSON file, only the value of the type field is VariableDeclaration, and the value of the kind field is var. The above description is the operation of declaring a variable.

[0053] In step 104, recursively traverse all JSON files, and determine the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file that references the global variable according to the structure of the paragraph corresponding to the operation of declaring a variable in the JSON file.

[0054] In an embodiment, recursively traversing all JSON files, and determining the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file that references the global variable according to the structure of the paragraph corresponding to the operation of declaring a variable in the JSON file, includes:

[0055] Determining the variable name in the selected paragraph in the JSON file;

[0056] Recursively traversing all JSON files, and determining the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file that references the global variable according to the structure of the paragraph corresponding to the operation of declaring a variable in the JSON file and the variable name in the JSON file.

[0057] In an embodiment, determining the variable name in the selected paragraph in the JSON file includes:

[0058] Determining the id paragraph in the paragraph where the value of each type field is VariableDeclaration;

[0059] Determining the value of the name field in the id paragraph as the variable name.

[0060] In an embodiment, recursively traversing all JSON files, and determining the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file that references the global variable according to the structure of the paragraph corresponding to the operation of declaring a variable in the JSON file and the variable name in the JSON file, includes:

[0061] Recursive traversal of all JSON files, if the name field in the object paragraph in the init paragraph in the current JSON file contains the file name of another JSON file, and the name field of the property paragraph in the init paragraph is the variable name in the other JSON file, the variable name in the other JSON file is determined to be a global variable, the public JavaScript file corresponding to the other JSON file is determined to be the global variable, and the public JavaScript file corresponding to the current JSON file is determined to be the public JavaScript file referring to the global variable.

[0062] A specific embodiment is given below to illustrate the specific application of the method proposed in the embodiments of the present application.

[0063] Referring to step 101, the code library is scanned to extract all public JavaScript files, for example, the public JavaScript files are exampleA and exampleB. Among them, exampleA contains a global variable var a=10. It is assumed that the exampleB file refers to the public variable a, that is, var b=exampleA.a.

[0064] Referring to step 102, based on the abstract syntax tree, the JSON file corresponding to each public JavaScript file is generated, wherein the JSON file corresponding to exampleA is shown in Figure 2 .

[0065] Figure 2 For the parsing result of the variable var a in the exampleA.js file, the parsing result of the variable is “kind: var”, and only the body in which “kind: var” is located in the JSON needs to be concerned, and other bodies of the same level do not need to be concerned. Extracting all bodies in which “kind: var” is located is the total variable of the file.

[0066] A file name field is added to each JSON file and is assigned a value, and a unique identification field is added and is assigned a value, the processed JSON file corresponding to exampleA is shown in Figure 3 , and the processed JSON file corresponding to exampleB is shown in Figure 4 It can be seen that the JSON file is a tree structure, which is convenient for parsing and extracting content. Among them Figure 3 , the added file name field is assigned a value: “nameId”: “exampleA”, and the added unique identification field is assigned a value: “absolutePath”: “F:\code\exampleA.js”, Figure 4Similar. absolutePath absolute path is also as the unique primary key of json, to prevent the same name file distinction error.

[0067] Referring to step 103, the JSON file needs to be processed, including: only retaining the paragraph with the value of the type field being VariableDeclaration and the value of the kind field being var in the body field in the JSON file; determining the id paragraph in each paragraph with the value of the type field being VariableDeclaration; and determining the value of the name field in the id paragraph as a variable name. Figure 3 In exampleA, the variable name a can be identified, Figure 4 In exampleB, the variable name b can be identified.

[0068] Referring to step 104, all JSON files are recursively traversed, and the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file referring to the global variable are determined according to the structure of the paragraph corresponding to the operation of declaring the variable in the JSON file and the variable name in the JSON file, and specifically including: recursively traversing all JSON files, if the name field in the object paragraph in the init paragraph in the current JSON file contains the file name of another JSON file, and the name field of the property paragraph in the init paragraph is the variable name in the other JSON file, it is determined that the variable name in the other JSON file is a global variable, it is determined that the common JavaScript file corresponding to the other JSON file is a common JavaScript file where the global variable is located, and it is determined that the common JavaScript file corresponding to the current JSON file is a common JavaScript file referring to the global variable, see Figure 4 , Figure 4 For exampleB file, the variable a of exampleA file is referenced, which is manifested as that the name value of object in "init" contains exampleA, and the name value of property is a, which indicates that the variable a of exampleA file is referenced in exampleB file.

[0069] After generating the JSON by using the above method, it can be compared and identified whether a file contains a variable of another file. By recursively traversing, the global variable of the common file or all code can be identified.

[0070] In summary, the method provided by the embodiment of the application has the following beneficial effects:

[0071] First, based on the AST generated JSON, the format of the processed JSON, and the method of identifying public variables based on this.

[0072] Second, all referenced public variables in the code can be identified, even if the variable is at a deep level in the current JS, there will be no omission.

[0073] Third, if the amount of code is very large, this method can be used to more quickly, more accurately and more completely identify which files the global variable is referenced in, which can greatly save the cost of manual identification.

[0074] The embodiment of the application also proposes a device for identifying global variables in public JavaScript files, which has a similar principle to the method of identifying global variables in public JavaScript files, which will not be repeated here.

[0075] Figure 5 The schematic diagram of the device for identifying global variables in public JavaScript files in the embodiment of the application comprises:

[0076] The file extraction module 501 is used to scan the code library and extract all public JavaScript files;

[0077] The JSON file generation module 502 is used to generate a JSON file corresponding to each public JavaScript file based on the abstract syntax tree;

[0078] The JSON file processing module 503 is used to filter out the paragraph corresponding to the variable declaration operation in the JSON file based on the variable declaration operation;

[0079] The reference file obtaining module 504 is used to recursively traverse all JSON files, determine the global variable, the public JavaScript file where the global variable is located, and the public JavaScript file that references the global variable according to the structure of the paragraph corresponding to the variable declaration operation in the JSON file.

[0080] In an embodiment, the JSON file generation module is also used to:

[0081] After generating the JSON file corresponding to each public JavaScript file, a unique identification field is added to each JSON file and assigned a value;

[0082] The reference file obtaining module is also used to:

[0083] After determining that the current JSON file is a JSON file that references the global variable in the JSON file, the value of the unique identification field of the current JSON file is output and displayed.

[0084] In an embodiment, the value of the unique identification field is an absolute path of the JSON file.

[0085] In an embodiment, the JSON file processing module is specifically configured to:

[0086] In the body field in the JSON file, only the paragraph whose type field value is VariableDeclaration and kind field value is var is reserved.

[0087] In an embodiment, the reference file obtaining module is specifically configured to:

[0088] determining the variable name in the selected paragraph in the JSON file;

[0089] recursively traversing all the JSON files, and determining the global variable, the common JavaScript file where the global variable is located, and the common JavaScript file that references the global variable according to the structure of the paragraph corresponding to the operation of declaring the variable in the JSON file and the variable name in the JSON file.

[0090] In an embodiment, the JSON file processing module is specifically configured to:

[0091] determining the id paragraph in each paragraph whose type field value is VariableDeclaration;

[0092] determining the variable name in the name field in the id paragraph.

[0093] In an embodiment, the JSON file processing module is further configured to:

[0094] after generating the JSON file corresponding to each common JavaScript file, adding a file name field to each JSON file and assigning a value to the file name field;

[0095] The reference file obtaining module is specifically configured to:

[0096] recursively traversing all the JSON files, and if the name field in the object paragraph in the init paragraph in the current JSON file contains the file name of another JSON file, and the name field in the property paragraph in the init paragraph is the variable name in the other JSON file, determining that the variable name in the other JSON file is a global variable, determining that the common JavaScript file corresponding to the other JSON file is a common JavaScript file where the global variable is located, and determining that the common JavaScript file corresponding to the current JSON file is a common JavaScript file that references the global variable.

[0097] In summary, the device provided by the embodiment of the application has the following beneficial effects:

[0098] First, based on the AST, the JSON is processed, the format of the processed JSON is identified, and the public variable is identified based on the format.

[0099] Second, all the referenced public variables in the code can be identified, and even if the variable is at a deep level in the current JS, there will be no omission.

[0100] Third, if the code amount is very large, the global variable can be identified in which files are referenced more quickly, more accurately and more completely by the method, and the manual identification cost can be greatly saved.

[0101] The embodiment of the application also provides a computer device, Figure 6 The computer device 600 includes a memory 610, a processor 620, and a computer program 630 stored in the memory 610 and capable of running on the processor 620, and the processor 620 implements the method for identifying the global variable in the public JavaScript file when the computer program 630 is executed.

[0102] The embodiment of the application also provides a computer device, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor, and the processor implements the method for identifying the global variable in the public JavaScript file when the computer program is executed.

[0103] The embodiment of the application also provides a computer readable storage medium, the computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method for identifying the global variable in the public JavaScript file.

[0104] The embodiment of the application also provides a computer program product, the computer program product includes a computer program, and the computer program is executed by the processor to implement the method for identifying the global variable in the public JavaScript file.

[0105] Those skilled in the art should understand that the embodiments of the application can be provided as a method, a system, or a computer program product. Therefore, the application can be in the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the application can be in the form of a computer program product implemented on one or more computer usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer usable program code.

[0106] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart block or blocks. Figure 1 one or more flow or blocks Figure 1 one or more flow or blocks

[0107] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instructions which implement the function specified in the flowchart block or blocks. Figure 1 one or more flow or blocks Figure 1 one or more flow or blocks

[0108] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart block or blocks. Figure 1 one or more flow or blocks Figure 1 one or more flow or blocks

[0109] The above-described specific embodiments, the purpose, technical solutions and advantages of the present application are further described in detail, it should be understood that the above-described only for the specific embodiments of the present application has, and is not used to limit the scope of protection of the present application, any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application, should be included within the scope of protection of the present application.

Claims

1. A method for identifying global variables in a public JavaScript file, characterized in that, include: Scan the codebase and extract all public JavaScript files; Based on the abstract syntax tree, generate a JSON file corresponding to each public JavaScript file; Based on the variable declaration operations, filter out the paragraphs in the JSON file that correspond to the variable declaration operations; Determine the variable names in the selected paragraphs of the JSON file; Recursively traverse all JSON files. If the name field of the object section in the init section of the current JSON file contains the filename of another JSON file, and the name field of the property section in the init section is the variable name in that other JSON file, then the variable name in that other JSON file is determined to be a global variable. The public JavaScript file corresponding to that other JSON file is determined to be the public JavaScript file where the global variable is located. Finally, the public JavaScript file corresponding to the current JSON file is determined to be the public JavaScript file that references the global variable.

2. The method as described in claim 1, characterized in that, After generating the JSON file corresponding to each public JavaScript file, the following is also included: Add a unique identifier field to each JSON file and assign a value to it; After determining that the current JSON file is a JSON file that references global variables in a JSON file, the following is also included: Output and display the value of the unique identifier field in the current JSON file.

3. The method as described in claim 2, characterized in that, The value of the unique identifier field is the absolute path to the JSON file.

4. The method as described in claim 1, characterized in that, Based on the variable declaration operations, filter out the paragraphs in the JSON file corresponding to the variable declaration operations, including: In the JSON file, only the paragraphs whose type field value is VariableDeclaration and whose kind field value is var are retained in the body field.

5. The method as described in claim 1, characterized in that, Determine the variable names in the selected paragraphs of the JSON file, including: Determine the value of each type field to be the id paragraph within the VariableDeclaration paragraph; Ensure that the value of the name field in the id paragraph is the variable name.

6. The method as described in claim 5, characterized in that, After generating the JSON file corresponding to each public JavaScript file, the following is also included: Add a filename field to each JSON file and assign a value to it.

7. A device for identifying global variables in a public JavaScript file, characterized in that, include: The file extraction module is used to scan the code repository and extract all public JavaScript files. The JSON file generation module is used to generate a JSON file corresponding to each public JavaScript file based on the abstract syntax tree; The JSON file processing module is used to filter out the paragraphs corresponding to the operations on declared variables in a JSON file based on the operations on declared variables. The reference file retrieval module is used to determine the variable names in the selected paragraphs of the JSON file. It recursively traverses all JSON files. If the name field of the object paragraph in the init paragraph of the current JSON file contains the filename of another JSON file, and the name field of the property paragraph in the init paragraph is the variable name in that other JSON file, it determines that the variable name in that other JSON file is a global variable. It also determines that the public JavaScript file corresponding to that other JSON file is the public JavaScript file where the global variable is located, and determines that the public JavaScript file corresponding to the current JSON file is the public JavaScript file that references the global variable.

8. The apparatus as claimed in claim 7, characterized in that, The JSON file generation module is also used for: After generating the corresponding JSON file for each public JavaScript file, add a unique identifier field to each JSON file and assign a value to it; The reference file retrieval module is also used for: After determining that the current JSON file is a JSON file that references global variables in the JSON file, output and display the value of the unique identifier field of the current JSON file.

9. The apparatus as claimed in claim 8, characterized in that, The value of the unique identifier field is the absolute path to the JSON file.

10. The apparatus as claimed in claim 9, characterized in that, The JSON file processing module is specifically used for: In the JSON file, only the paragraphs whose type field value is VariableDeclaration and whose kind field value is var are retained in the body field.

11. The apparatus as claimed in claim 8, characterized in that, The module for obtaining referenced files is specifically used for: Determine the value of each type field to be the id paragraph within the VariableDeclaration paragraph; Ensure that the value of the name field in the id paragraph is the variable name.

12. The apparatus as claimed in claim 11, characterized in that, The JSON file generation module is also used for: After generating the corresponding JSON file for each public JavaScript file, add a filename field to each JSON file and assign a value to it.

13. A computer 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 according to any one of claims 1 to 6.

14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method of any one of claims 1 to 6.

15. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method for calling binary element in Javascript

    CN104423932A

  • Storage configuration method, device and equipment for VUE application program data and storage medium

    CN114237616A