WO2021197273A1 - 冗余代码去除方法、装置、设备与计算机可读存储介质 - Google Patents

冗余代码去除方法、装置、设备与计算机可读存储介质 Download PDF

Info

Publication number
WO2021197273A1
WO2021197273A1 PCT/CN2021/083624 CN2021083624W WO2021197273A1 WO 2021197273 A1 WO2021197273 A1 WO 2021197273A1 CN 2021083624 W CN2021083624 W CN 2021083624W WO 2021197273 A1 WO2021197273 A1 WO 2021197273A1
Authority
WO
WIPO (PCT)
Prior art keywords
file
target
selector
redundant
target style
Prior art date
Application number
PCT/CN2021/083624
Other languages
English (en)
French (fr)
Inventor
黎俊标
林挺
卢道和
Original Assignee
深圳前海微众银行股份有限公司
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 深圳前海微众银行股份有限公司 filed Critical 深圳前海微众银行股份有限公司
Publication of WO2021197273A1 publication Critical patent/WO2021197273A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/71Version control; Configuration management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4434Reducing the memory space required by the program code
    • G06F8/4435Detection or removal of dead or redundant code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating
    • G06F9/44505Configuring for program initiating, e.g. using registry, configuration files
    • G06F9/4451User profiles; Roaming

Definitions

  • This application relates to the technical field of financial technology (Fintech), in particular to methods, devices, equipment and computer-readable storage media for removing redundant codes.
  • the applet specifies the page content through framework conventions, such as wxml (WeiXin Markup Language, a set of label language for frame design) and style files, such as wxss (WeiXin Style Sheets, a set of style language for description wxml component style), and the applet style file (wxss) is a modification and expansion of the traditional h5 style file (css), and the syntax and usage are different. Therefore, the traditional h5 method of removing redundant code is not suitable for small programs. At present, only the redundant code of small programs can be manually screened and removed, which is extremely inefficient. Therefore, there is an urgent need for efficient removal of redundant code of small programs. Methods.
  • the main purpose of this application is to propose a redundant code removal method, device, equipment, and computer-readable storage medium, aiming to realize the efficient removal of redundant code of a small program.
  • the redundant code removal method includes the following steps:
  • the step of determining the target style file and the target page content file includes:
  • the scope of the target style file is global, determine the first temporary file corresponding to the target style file, and determine the first temporary file as the target page content file to characterize based on the first temporary file.
  • the redundant code removal method further includes:
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the functional component corresponding to the target page based on the configuration file;
  • the scope of the target style file is a page
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the component to be filtered corresponding to the target page based on the configuration file;
  • the isolation configuration of the component to be screened is determined, and based on the isolation configuration, the active component corresponding to the target page is determined from the component to be screened.
  • the step of determining the redundant selector of the target pattern file based on the first grammatical structure and the second grammatical structure includes:
  • a redundant selector of the target pattern file is determined.
  • the step of determining a target style file and a target page content file, and generating a first syntax structure corresponding to the target style file, and a second syntax structure corresponding to the target page content file includes:
  • the second source code is parsed, and based on the preset template, a second grammatical structure corresponding to the target page content file is generated.
  • the redundant code removal method further includes:
  • the selector that is not supported by the applet type is determined as a redundant selector.
  • the present application also provides a redundant code removal device, the redundant code removal device including:
  • the first determining module is configured to determine a target style file and a target page content file, and generate a first syntax structure corresponding to the target style file, and a second syntax structure corresponding to the target page content file;
  • a second determining module configured to determine a redundant selector of the target pattern file based on the first grammatical structure and the second grammatical structure
  • the deletion module is used to determine the target style corresponding to the redundant selector and delete the code corresponding to the target style.
  • the first determining module is further configured to:
  • the scope of the target style file is global, determine the first temporary file corresponding to the target style file, and determine the first temporary file as the target page content file to characterize based on the first temporary file.
  • the first determining module is further configured to:
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the functional component corresponding to the target page based on the configuration file;
  • the first determining module is further configured to:
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the component to be filtered corresponding to the target page based on the configuration file;
  • the isolation configuration of the component to be screened is determined, and based on the isolation configuration, the active component corresponding to the target page is determined from the component to be screened.
  • the second determining module is further configured to:
  • a redundant selector of the target pattern file is determined.
  • the first determining module is further configured to:
  • the second source code is parsed, and based on the preset template, a second grammatical structure corresponding to the target page content file is generated.
  • the second determining module is further configured to:
  • the selector that is not supported by the applet type is determined as a redundant selector.
  • the present application also provides a redundant code removal device, which includes a memory, a processor, and a redundant code that is stored on the memory and can run on the processor.
  • a residual code removal program which implements the steps of the redundant code removal method as described above when the redundant code removal program is executed by the processor.
  • the present application also provides a computer-readable storage medium having a redundant code removal program stored on the computer-readable storage medium, and the redundant code removal program is executed by a processor to achieve the above The steps of the redundant code removal method described.
  • the redundant code removal method proposed in this application determines a target style file and a target page content file, and generates a first syntax structure corresponding to the target style file, and a second syntax structure corresponding to the target page content file;
  • the first grammatical structure and the second grammatical structure are used to determine the redundant selector of the target pattern file; the target pattern corresponding to the redundant selector is determined, and the code corresponding to the target pattern is deleted.
  • This application generates a modular grammatical structure from the style file and the page content file respectively. Through the grammatical structure of the two, the redundant selector is determined, so as to determine that the style defined by the redundant selector is a useless style and can be safely deleted. Realize the efficient removal of redundant codes, thereby reducing the code size of small programs.
  • FIG. 1 is a schematic diagram of the device structure of the hardware operating environment involved in the solution of the embodiment of the present application;
  • FIG. 2 is a schematic flowchart of a first embodiment of a method for removing redundant codes according to this application;
  • Figure 3 (a) is a schematic diagram of extracting a first selector in the first embodiment of the redundant code removal method of this application;
  • Figure 3(b) is a schematic diagram of extracting the second selector in the first embodiment of the redundant code removal method of this application.
  • FIG. 1 is a schematic diagram of the device structure of the hardware operating environment involved in the solution of the embodiment of the present application.
  • the device in the embodiment of the present application may be a mobile terminal or a server device.
  • the device may include: a processor 1001, such as a CPU, a network interface 1004, a user interface 1003, a memory 1005, and a communication bus 1002.
  • the communication bus 1002 is used to implement connection and communication between these components.
  • the user interface 1003 may include a display screen (Display) and an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface.
  • the network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface).
  • the memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a magnetic disk memory.
  • the memory 1005 may also be a storage device independent of the aforementioned processor 1001.
  • FIG. 1 does not constitute a limitation on the device, and may include more or fewer components than those shown in the figure, or a combination of certain components, or different component arrangements.
  • a memory 1005 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a redundant code removal program.
  • the operating system is a program that manages and controls redundant code removal equipment and software resources, and supports the operation of network communication modules, user interface modules, redundant code removal programs, and other programs or software; network communication modules are used to manage and control the network Interface 1002:
  • the user interface module is used to manage and control the user interface 1003.
  • the redundant code removal device calls the redundant code removal program stored in the memory 1005 through the processor 1001, and executes the following in each embodiment of the redundant code removal method operate.
  • FIG. 2 is a schematic flowchart of a first embodiment of a redundant code removal method according to this application, and the method includes:
  • Step S10 determining a target style file and a target page content file, and generating a first syntax structure corresponding to the target style file and a second syntax structure corresponding to the target page content file;
  • Step S20 based on the first grammatical structure and the second grammatical structure, determine a redundant selector of the target style file
  • Step S30 Determine the target style corresponding to the redundant selector, and delete the code corresponding to the target style.
  • the redundant code removal method of this embodiment is applied to redundant code removal equipment of financial institutions such as financial institutions or banking systems.
  • the redundant code removal equipment can be a terminal, a robot or a PC device.
  • the redundant code removal equipment To remove the equipment abbreviation. Remove various applications installed on the device, such as WeChat applications, and run small programs, such as WeChat applets, in the applications.
  • WeChat applet is taken as an example. It should be noted that the applet stipulates the binding relationship between the page content wxml and the style file wxss in the applet through framework conventions.
  • wxml is a set of label language designed by the applet framework, combining basic components and The event system can construct the content of the page
  • wxss is a style language used to describe the style of wxml components, that is, wxss is used to determine how the components of wxml should be displayed. Therefore, under normal circumstances, the style in the style file is the style used by the page content file.
  • mini programs are used as the interface of applications (such as WeChat) to directly contact users. They often need to adapt to business changes or user habits and constantly iteratively upgrade. Code changes are very frequent, and in the process of continuous iterative upgrades , It is likely to cause a lot of code redundancy, especially wxss code. After iteration, a lot of wxss not needed by wxml is likely to appear in the applet, that is, after iteration, there are styles that are not used by the page content file in the style file. If the applet is iterated and the screen content displayed on the current interface is upgraded from static to dynamic, the statically displayed style is no longer the style required by the current screen content.
  • the code corresponding to the statically displayed style is redundant Code. Because the relationship between wxss and wxml is not directly bound by hyperlinks like html and css, that is, it cannot be well modularized, so it is difficult to identify useless code in wxss.
  • the target style file and the target page content file are determined first, and the target style file and the target page content file are respectively generated into a modular grammatical structure, so that the selectors of the two can be extracted from them, and then through the selectors of the two, Filter out the selectors that are not used in the target page content file, so as to determine that the style defined by the selector is a useless style, and finally, delete the code corresponding to the useless style, that is, to achieve efficient removal of redundant code.
  • Step S10 determining a target style file and a target page content file, and generating a first syntax structure corresponding to the target style file and a second syntax structure corresponding to the target page content file;
  • the target style file and the target page content file in the applet are first determined, and the target style file is generated into a modular first grammatical structure, and the target page content file A modular second grammatical structure is also generated to facilitate subsequent selection of target style files and target page content files.
  • step S10 includes:
  • Step a1 Determine the target style file and the target page content file, and obtain the first source code of the target style file and the second source code of the target page content file;
  • the removal device first determines the target style file and the target page content file corresponding to the target style file, and then obtains the first source code of the target style file and the second source code of the target page content file respectively.
  • Step a2 parse the first source code, and generate a first grammatical structure corresponding to the target style file based on a preset template;
  • the first source code is parsed. Specifically, the first source code is lexically analyzed, and then grammatically analyzed, and the first grammatical structure is generated according to the preset template.
  • the lexical analysis stage is the string The formal code is converted into a token (Tokens) stream.
  • the grammatical analysis stage is to convert each token stream into grammatical nodes, so that according to the preset template, each grammatical node is pieced together into a complete grammatical structure, that is, the device will be removed.
  • a long string of strings connected by a whole line is parsed into an arrangement of short strings, and then the short strings are filled as nodes into the preset template to obtain the first grammatical structure.
  • the preset template can be tree-shaped Template, that is, the parent node is the root node, and the child nodes are the extendable templates of the branch nodes, and each node corresponds to a grammar node.
  • Step a3 parse the second source code, and generate a second grammatical structure corresponding to the target page content file based on the preset template.
  • the second source code is parsed, and the second grammatical structure corresponding to the target page content file is generated according to the preset template.
  • the specific generation method is similar to the method of generating the first grammatical structure, and will not be repeated here.
  • Step S20 based on the first grammatical structure and the second grammatical structure, determine a redundant selector of the target style file.
  • the first grammatical structure and the second grammatical structure can be compared to determine the redundant selector of the target pattern.
  • step S20 includes:
  • Step b1 extracting a first selector of the target style file based on the first grammatical structure, and extracting a second selector of the target page content file based on the second grammatical structure;
  • the first selector of the target style file is extracted first, and then the second selector of the target page content file is extracted through the second grammatical structure.
  • the selector refers to the role component of the style.
  • the selectors supported by the WeChat applet include class selectors, id selectors, and element selectors.
  • element is an element selection
  • the first selector used in the target style file can be determined in the first syntax structure
  • the second selector used in the target page content file can be determined in the second syntax structure
  • the left window is the wxss code, which is the source code of the target style file
  • the right window is the generated first syntax structure
  • the rectangular box in the right window is the first selector.
  • They are id selector: #index, element selector: view, and class selector: .title and .content.
  • the left window is the wxml code, which is the source code of the target page content file
  • the right window is the generated second syntax structure
  • the rectangular box in the right window is the second selector
  • Step b2 based on the first selector and the second selector, determine the redundant selector of the target style file
  • the process of removing useless wxss is identified by the function unwxss.
  • Step S30 Determine the target style corresponding to the redundant selector, and delete the code corresponding to the target style.
  • the redundant selector of the target pattern file after the redundant selector of the target pattern file is determined, it can be determined that the target pattern defined by the redundant selector is redundant. Therefore, it can be safely deleted. In this case, delete the corresponding target pattern.
  • the code does not affect the normal use of the applet.
  • the style in the style file is defined by the selector. Therefore, after the redundant selector is determined, the target style defined by the redundant selector can be determined backward, and the target style is the redundant style , Can be safely deleted, thereby reducing the code size.
  • a target style file and a target page content file are determined, and a first syntax structure corresponding to the target style file and a second syntax structure corresponding to the target page content file are generated; based on the first syntax structure and the content file
  • the second grammatical structure determines the redundant selector of the target pattern file; determines the target pattern corresponding to the redundant selector, and deletes the code corresponding to the target pattern.
  • This application generates a modular grammatical structure from the style file and the page content file respectively. Through the grammatical structure of the two, the redundant selector is determined, so as to determine that the style defined by the redundant selector is a useless style and can be safely deleted. Realize the efficient removal of redundant codes, thereby reducing the code size.
  • the difference between the second embodiment of the redundant code removal method and the first embodiment of the redundant code removal method is that in the process of determining the target style file and the target page content file, the redundant code removal method further includes:
  • Step c determining the scope of the target style file
  • Step d if the scope of the target style file is global, determine the first temporary file corresponding to the target style file, and determine the first temporary file as the target page content file, based on the first temporary file
  • Temporary files represent page content files of all pages and components in the world.
  • the component module is defined in the applet framework of this embodiment, and the components in the component module contain corresponding configuration files.
  • the configuration files can have different configurations for the scope of the style file bound to the current component, that is, the style files have Different scopes (scope, usability code range), specifically, the modules that contain wxss code in the applet are: app (global), page (page) and component (component), app's wxss (ie app.wxss file) will Affects the entire applet, which is the global style; the wxss of the page will affect the page and its called components, which is the page style; the wxss of the component only affects this component, which is the component style.
  • the wxss influence in the applet is app -> page -> component, that is, the global style affects the pages and components of the entire applet, the page style affects this page and the components called by this page, and the component style affects this component.
  • the scope of the target style file is determined first, and then the target page content file is determined, thereby further improving the accuracy of determining the target page content file.
  • Step c Determine the scope of the target style file.
  • the scope of the target style file is determined first, and specifically according to the definition of the target style file, the scope of the target style file is determined, that is, style files with multiple scopes are defined in the applet framework, each Style files have their corresponding scopes, where the scope includes global, page, and component.
  • Step d if the scope of the target style file is global, determine the first temporary file corresponding to the target style file, and determine the first temporary file as the target page content file, based on the first temporary file
  • Temporary files represent page content files of all pages and components in the world.
  • the target style file is a global style file
  • the applet stipulates that the file named app.wxss in the root directory of the code package is used as the public style file of the applet.
  • the defined style will be applied to all wxml files in the applet, that is, the app.wxss file is a global style. File, so to identify the useless wxss in app.wxss, you need to generate a temporary file that contains all the page content of the applet.
  • unwxss wxml of all pages and components, wxss of app
  • unwxss first temporary file, target style file
  • the redundant code removal method further includes:
  • Step e If the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the functional component corresponding to the target page based on the configuration file;
  • the configuration file of the target page corresponding to the target style file is read, that is, the target page's configuration file that the target style file acts on.
  • the configuration file and then through the configuration file, determine the role component corresponding to the target page. This is because the page style will affect the page and the components it calls. Therefore, it is necessary to determine the role component affected by the target style file through the configuration file.
  • step e includes:
  • Step e1 If the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the component to be filtered corresponding to the target page based on the configuration file;
  • the target style file is a page style file
  • the target page to which the target style file functions is determined, and the configuration file of the target page corresponding to the target style file is read ,
  • the components to be filtered corresponding to the target page according to the configuration file that is, determining all the components related to the target page.
  • Step e2 Determine the isolation configuration of the component to be screened, and based on the isolation configuration, determine the active component corresponding to the target page from the component to be screened.
  • the isolation configuration includes absolute isolation, one-way isolation, and non-isolation.
  • Absolute isolation refers to non-influence with the outside of the component
  • one-way isolation refers to the component being affected by the page, but The page is not affected by the component
  • non-isolation means that the component is affected by the page, and the page is also affected by the component.
  • the isolation configuration the active components corresponding to the target page are filtered out from the components to be filtered, that is, the components that are affected by the target page, that is, the isolation configuration is one-way isolation and non-isolation components.
  • the applet stipulates that each page can only have one style file with the same name and in the same directory. If the page content file of a page is /pages/home.wxml, its corresponding style file is /pages/home.wxss. Next, read the usingComponents array field in the configuration file of the current page (such as /pages/home.json) to get all the components used in the current page, that is, the components to be filtered.
  • the content of the field is ["/components/modal", “/Components/msg”], then read the styleIsolation field in the component configuration files /components/modal.json and /components/msg.json in turn to obtain the isolation configuration of the component. If the value is isolated, it is determined that the target style file is not Will affect the component; if the value is apply-shared, it is determined that the target style file will affect the component.
  • Step f Determine a second temporary file corresponding to the target style file, and determine the second temporary file as a target page content file to characterize the page content file of the target page based on the second temporary file, and The page content file of the active component.
  • the target style file is a page
  • the target style file is a page style file
  • First determine the page content file of the corresponding target page and the page content file of the component affected by the target page and then package these page content files into a temporary file, which is the target page content file.
  • unwxss (wxml of a page + component.wxml of all components affected by the front page style, wxss of a page), that is, unwxss (second temporary file, Target style file).
  • the target style file and the target page content file need to be correctly identified, that is, the removal process is both The same, the difference is that the compared wxml code is different from the wxss code. Therefore, in the removal process, the scope of the target style needs to be determined, so as to determine the correct target page content file, so that the correct redundant code can be found later to achieve redundancy. Correct removal of the remaining code.
  • the difference between the third embodiment of the redundant code removal method and the first and second embodiments of the redundant code removal method is that in the process of determining the redundancy selector of the target pattern file, the redundant code removal method further includes:
  • Step g Determine the applet type corresponding to the target style file, and determine the selector list corresponding to the applet type;
  • Step h Determine, based on the selector list, whether the target style file has a selector that is not supported by the applet type;
  • Step i if it exists, determine the selector that is not supported by the applet type as a redundant selector.
  • This embodiment does not support many selectors due to the limitation of the mini program project itself, that is, the mini program can only support defined selectors. For unsupported selectors, if they are used in the target style file, they can be considered as selectors. It is a redundant selector.
  • Step g Determine the applet type corresponding to the target style file, and determine the selector list corresponding to the applet type.
  • the applet type corresponding to the target style file
  • determine the selector list corresponding to the applet type that is, through the applet type
  • determine which selectors the applet supports such as the applet
  • the program type is WeChat Mini Program
  • the supported selectors include class selectors, id selectors, and element selectors. That is, the selector list includes class selectors, id selectors, and element selectors.
  • Step h based on the selector list, determine whether the target style file has a selector that is not supported by the applet type.
  • the target style file has selectors that are not supported by the current applet, that is, the selectors in the target style file and the selector list Compare the selectors to determine whether there are selectors that are not in the selector list in the target style file.
  • Step i if it exists, determine the selector that is not supported by the applet type as a redundant selector.
  • the selector is determined to be a redundant selector. This is because the selector is not in the selector list. Even if the target style file is used, it cannot be executed in the current applet. Therefore, it is considered as a redundant selector.
  • Components and pages referencing components cannot use id selector (#a), attribute selector ([a]) and tag name selector. That is, when the scope of the target style file is a component, it is also necessary to determine whether the component in the target style file and the page referencing the component use the id selector, attribute selector, and tag name selector. If they are used, these choices are considered The device is a redundant selector.
  • descendant selectors in components and pages referencing components may cause unexpected behavior in some extreme cases. That is, when the scope of the target style file is a component, it is also necessary to determine whether the component in the target style file and the page referencing the component use descendant selectors. If used, these selectors are considered redundant selectors.
  • the child element selector (.a>.b) can only be used between the view component and its child nodes, which may cause unexpected situations when used for other components. That is, when the scope of the target style file is a component, it is determined whether any component other than the view component uses the child element selector. If so, the selector used by the component is a redundant selector.
  • the applet of this embodiment does not support selectors other than the selector list. Therefore, in the process of determining the redundant selector, it can be judged whether the selector is in the selector list. If not, it is a redundant selector. In addition, the selectors that are additionally specified for hits are also determined as redundant selectors, so that redundant codes can be accurately removed later, and the accuracy of redundant codes removal is improved.
  • the application also provides a redundant code removal device.
  • the redundant code removal device of this application includes:
  • the first determining module is configured to determine a target style file and a target page content file, and generate a first syntax structure corresponding to the target style file, and a second syntax structure corresponding to the target page content file;
  • a second determining module configured to determine a redundant selector of the target pattern file based on the first grammatical structure and the second grammatical structure
  • the deletion module is used to determine the target style corresponding to the redundant selector and delete the code corresponding to the target style.
  • the first determining module is further configured to:
  • the scope of the target style file is global, determine the first temporary file corresponding to the target style file, and determine the first temporary file as the target page content file to characterize based on the first temporary file.
  • the first determining module is further configured to:
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the functional component corresponding to the target page based on the configuration file;
  • the first determining module is further configured to:
  • the scope of the target style file is a page, read the configuration file of the target page corresponding to the target style file, and determine the component to be filtered corresponding to the target page based on the configuration file;
  • the isolation configuration of the component to be screened is determined, and based on the isolation configuration, the active component corresponding to the target page is determined from the component to be screened.
  • the second determining module is further configured to:
  • a redundant selector of the target pattern file is determined.
  • the first determining module is further configured to:
  • the second source code is parsed, and based on the preset template, a second grammatical structure corresponding to the target page content file is generated.
  • the second determining module is further configured to:
  • the selector that is not supported by the applet type is determined as a redundant selector.
  • the application also provides a computer-readable storage medium.
  • a redundant code removal program is stored on the computer-readable storage medium of the present application, and the redundant code removal program implements the steps of the redundant code removal method as described above when the redundant code removal program is executed by a processor.
  • the method implemented when the redundant code removal program running on the processor is executed can refer to each embodiment of the redundant code removal method of the present application, which will not be repeated here.
  • the technical solution of this application essentially or the part that contributes to the existing technology can be embodied in the form of a software product, and the computer software product is stored in a storage medium (such as ROM/RAM) as described above. , Magnetic disks, optical disks), including several instructions to make a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) execute the method described in each embodiment of the present application.
  • a terminal device which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Security & Cryptography (AREA)
  • Stored Programmes (AREA)

Abstract

本申请公开了一种冗余代码去除方法,包括:确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。本申请还公开了一种冗余代码去除装置、设备和计算机可读存储介质。

Description

冗余代码去除方法、装置、设备与计算机可读存储介质
本申请要求2020年4月3日申请的,申请号为202010262738.0,名称为“冗余代码去除方法、装置、设备与计算机可读存储介质”的中国专利申请的优先权,在此将其全文引入作为参考。
技术领域
本申请涉及金融科技(Fintech)技术领域,尤其涉及冗余代码去除方法、装置、设备与计算机可读存储介质。
背景技术
近年来,随着金融科技(Fintech),尤其是互联网金融的不断发展,小程序技术被引入银行等金融机构的日常服务中。在银行等金融机构的日常服务过程中,为满足业务需要,常常需要开发一些不需要下载即可使用的应用——小程序。为适应业务的变更,或者用户的使用习惯,需要对小程序不断迭代升级,在不断升级的过程中,很容易造成代码的大量冗余,因此,需要对冗余代码进行去除。
传统h5中是通过超链接方式把页面内容html(HyperText Markup Language,超文本标记语言)和对应的样式文件css(Cascading Style Sheets,层叠样式表)进行直接绑定,因此,通过页面中的超链接即可获取到所有作用于本页面的样式文件,从而筛选出未作用于本页面的样式文件所对应的代码即为冗余代码。
而小程序是通过框架约定的方式来规定页面内容,如,wxml(WeiXin Markup Language,框架设计的一套标签语言)和样式文件,如,wxss(WeiXin Style Sheets,一套样式语言,用于描述wxml的组件样式)的绑定关系的,并且,小程序的样式文件(wxss)是对传统h5样式文件(css)的修改和扩充,语法和使用方式并不相同。因此,传统h5去除冗余代码的方式并不适用小程序,目前仅能人工对小程序的冗余代码进行筛选去除,效率极其低下,因此,亟需一种对小程序冗余代码进行高效去除的方法。
技术问题
本申请的主要目的在于提出一种冗余代码去除方法、装置、设备与计算机可读存储介质,旨在实现小程序冗余代码的高效去除。
技术解决方案
为实现上述目的,本申请提供一种冗余代码去除方法,所述冗余代码去除方法包括如下步骤:
确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;
确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
在一实施例中,所述确定目标样式文件和目标页面内容文件的步骤包括:
确定所述目标样式文件的作用域;
若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
在一实施例中,所述确定所述目标样式文件的作用域的步骤之后,所述冗余代码去除方法还包括:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件;
确定所述目标样式文件对应的第二临时文件,并将所述第二临时文件确定为目标页面内容文件,以基于所述第二临时文件表征所述目标页面的页面内容文件,以及所述作用组件的页面内容文件。
在一实施例中,所述若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件的步骤包括:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的待筛选组件;
确定所述待筛选组件的隔离配置,并基于所述隔离配置,从所述待筛选组件中确定所述目标页面对应的作用组件。
在一实施例中,所述基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器的步骤包括:
基于所述第一语法结构,提取所述目标样式文件的第一选择器,并基于所述第二语法结构,提取所述目标页面内容文件的第二选择器;
基于所述第一选择器和所述第二选择器,确定所述目标样式文件的冗余选择器。
在一实施例中,所述确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构的步骤包括:
确定目标样式文件和目标页面内容文件,并获取所述目标样式文件的第一源代码和所述目标页面内容文件的第二源代码;
对所述第一源代码进行解析,并基于预设模板,生成所述目标样式文件对应的第一语法结构;
对所述第二源代码进行解析,并基于所述预设模板,生成所述目标页面内容文件对应的第二语法结构。
在一实施例中,所述确定所述目标样式文件的冗余选择器的过程中,所述冗余代码去除方法还包括:
确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表;
基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器;
若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
此外,为实现上述目的,本申请还提供一种冗余代码去除装置,所述冗余代码去除装置包括:
第一确定模块,用于确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
第二确定模块,用于基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;
删除模块,用于确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
在一实施例中,所述第一确定模块还用于:
确定所述目标样式文件的作用域;
若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
在一实施例中,所述第一确定模块还用于:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件;
确定所述目标样式文件对应的第二临时文件,并将所述第二临时文件确定为目标页面内容文件,以基于所述第二临时文件表征所述目标页面的页面内容文件,以及所述作用组件的页面内容文件。
在一实施例中,所述第一确定模块还用于:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的待筛选组件;
确定所述待筛选组件的隔离配置,并基于所述隔离配置,从所述待筛选组件中确定所述目标页面对应的作用组件。
在一实施例中,所述第二确定模块还用于:
基于所述第一语法结构,提取所述目标样式文件的第一选择器,并基于所述第二语法结构,提取所述目标页面内容文件的第二选择器;
基于所述第一选择器和所述第二选择器,确定所述目标样式文件的冗余选择器。
在一实施例中,所述第一确定模块还用于:
确定目标样式文件和目标页面内容文件,并获取所述目标样式文件的第一源代码和所述目标页面内容文件的第二源代码;
对所述第一源代码进行解析,并基于预设模板,生成所述目标样式文件对应的第一语法结构;
对所述第二源代码进行解析,并基于所述预设模板,生成所述目标页面内容文件对应的第二语法结构。
在一实施例中,所述第二确定模块还用于:
确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表;
基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器;
若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
此外,为实现上述目的,本申请还提供一种冗余代码去除设备,所述冗余代码去除设备包括:存储器、处理器及存储在所述存储器上并可在所述处理器上运行的冗余代码去除程序,所述冗余代码去除程序被所述处理器执行时实现如上所述的冗余代码去除方法的步骤。
此外,为实现上述目的,本申请还提供一种计算机可读存储介质,所述计算机可读存储介质上存储有冗余代码去除程序,所述冗余代码去除程序被处理器执行时实现如上所述的冗余代码去除方法的步骤。
有益效果
本申请提出的冗余代码去除方法,确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。本申请分别将样式文件和页面内容文件生成模块化的语法结构,通过两者的语法结构,确定冗余的选择器,从而确定冗余的选择器所定义的样式为无用样式,可以安全删除,实现冗余代码的高效去除,从而减少小程序代码体积。
附图说明
图1是本申请实施例方案涉及的硬件运行环境的设备结构示意图;
图2为本申请冗余代码去除方法第一实施例的流程示意图;
图3(a)为本申请冗余代码去除方法第一实施例中提取第一选择器的示意图;
图3(b)为本申请冗余代码去除方法第一实施例中提取第二选择器的示意图。
本申请目的的实现、功能特点及优点将结合实施例,参照附图做进一步说明。
本发明的实施方式
应当理解,此处所描述的具体实施例仅仅用以解释本申请,并不用于限定本申请。
如图1所示,图1是本申请实施例方案涉及的硬件运行环境的设备结构示意图。
本申请实施例设备可以是移动终端或服务器设备。
如图1所示,该设备可以包括:处理器1001,例如CPU,网络接口1004,用户接口1003,存储器1005,通信总线1002。其中,通信总线1002用于实现这些组件之间的连接通信。用户接口1003可以包括显示屏(Display)、输入单元比如键盘(Keyboard),可选用户接口1003还可以包括标准的有线接口、无线接口。网络接口1004可选的可以包括标准的有线接口、无线接口(如WI-FI接口)。存储器1005可以是高速RAM存储器,也可以是稳定的存储器(non-volatile memory),例如磁盘存储器。存储器1005可选的还可以是独立于前述处理器1001的存储装置。
本领域技术人员可以理解,图1中示出的设备结构并不构成对设备的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件布置。
如图1所示,作为一种计算机存储介质的存储器1005中可以包括操作系统、网络通信模块、用户接口模块以及冗余代码去除程序。
其中,操作系统是管理和控制冗余代码去除设备与软件资源的程序,支持网络通信模块、用户接口模块、冗余代码去除程序以及其他程序或软件的运行;网络通信模块用于管理和控制网络接口1002;用户接口模块用于管理和控制用户接口1003。
在图1所示的冗余代码去除设备中,所述冗余代码去除设备通过处理器1001调用存储器1005中存储的冗余代码去除程序,并执行下述冗余代码去除方法各个实施例中的操作。
基于上述硬件结构,提出本申请冗余代码去除方法实施例。
参照图2,图2为本申请冗余代码去除方法第一实施例的流程示意图,所述方法包括:
步骤S10,确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
步骤S20,基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;
步骤S30,确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
本实施例冗余代码去除方法运用于理财机构或者银行系统等金融机构的冗余代码去除设备中,冗余代码去除设备可以是终端、机器人或者PC设备等,为描述方便,冗余代码去除设备以去除设备简称。去除设备安装有各类应用,如微信应用等,并在应用中运行小程序,如微信小程序等。在下述描述中,以微信小程序为例进行描述。需要说明的是,小程序是通过框架约定的方式来规定小程序中的页面内容wxml和样式文件wxss的绑定关系的,其中,wxml是小程序框架设计的一套标签语言,结合基础组件和事件系统,可以构建出页面的内容,而wxss则是一套样式语言,用于描述wxml 的组件样式,也即wxss用来决定wxml的组件应该怎么显示。因此,在正常情况下,样式文件中的样式即为页面内容文件所使用的样式。
然而,在实际应用中,小程序作为应用(如微信)中直接接触用户的界面,经常需要适应业务的变更或者用户的使用习惯而不断迭代升级,代码变动十分频繁,在不断迭代升级的过程中,很可能造成代码的大量冗余,尤其是wxss代码,经过迭代后,小程序中很可能出现很多wxml不需要的wxss,也即经过迭代,样式文件中存在页面内容文件未用到的样式,如小程序经过迭代,当前界面显示的画面内容由静态升级为动态,则静态显示的样式就不再是当前画面内容所需要的样式,此时,静态显示的样式所对应的代码即为冗余代码。因wxss与wxml的关系并不像html与css那样通过超链接直接绑定,也即其无法很好模块化,因此很难识别出wxss中的无用代码。
本实施例先确定目标样式文件和目标页面内容文件,并将目标样式文件和目标页面内容文件分别生成模块化的语法结构,便于从中抽取出两者的选择器,再通过两者的选择器,筛选出目标页面内容文件没有用到的选择器,从而确定该选择器所定义的样式即为无用样式,最后,将无用样式所对应的代码删除,即实现冗余代码的高效去除。
以下将对各个步骤进行详细说明:
步骤S10,确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
在本实施例中,在确定小程序的冗余代码时,先确定小程序中的目标样式文件和目标页面内容文件,并将目标样式文件生成模块化的第一语法结构,将目标页面内容文件同样生成模块化的第二语法结构,便于后续从中确定目标样式文件和目标页面内容文件的选择器。
在一实施例中,步骤S10包括:
步骤a1,确定目标样式文件和目标页面内容文件,并获取所述目标样式文件的第一源代码和所述目标页面内容文件的第二源代码;
在一实施例中,去除设备先确定目标样式文件,以及目标样式文件对应的目标页面内容文件,再分别获取目标样式文件的第一源代码和目标页面内容文件的第二源代码。
步骤a2,对所述第一源代码进行解析,并基于预设模板,生成所述目标样式文件对应的第一语法结构;
然后,对第一源代码进行解析,具体的,对第一源代码进行词法分析,再对其进行语法分析,并根据预设模板,生成第一语法结构,其中,词法分析阶段是将字符串形式的代码转换为令牌(Tokens)流,语法分析阶段是将一个个令牌流转换成语法节点,从而根据预设模板,将各个语法节点拼凑成一个完整的语法结构,也即去除设备将一长串整行相连的字符串解析成简短字符串的排列,再将简短字符串作为节点填入预设模板中,从而得到第一语法结构,在具体实施时,预设模板可以为树状模板,也即以父节点作为根节点,子节点作为枝节点的可延伸模板,每一个节点对应一个语法节点。
步骤a3,对所述第二源代码进行解析,并基于所述预设模板,生成所述目标页面内容文件对应的第二语法结构。
接着,对第二源代码进行解析,并根据预设模板,生成目标页面内容文件对应的第二语法结构,具体的生成方式与生成第一语法结构的方式类似,在此不再赘述。
需要说明的是,在将目标样式文件生成第一语法结构,以及将目标页面内容文件生成第二语法结构的过程中,也可采用现有工具,如esprima或者babel等编译器,生成抽象语法树等。此外,生成第一语法结构和生成第二语法结构的顺序可互换,不影响本实施例的实施。
步骤S20,基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器。
在本实施例中,在得到第一语法结构和第二语法结构后,可将第一语法结构和第二语法结构进行比较,从而确定出目标样式的冗余选择器。
具体的,在一实施例中,步骤S20包括:
步骤b1,基于所述第一语法结构,提取所述目标样式文件的第一选择器,并基于所述第二语法结构,提取所述目标页面内容文件的第二选择器;
在一实施例中,通过第一语法结构,先提取目标样式文件的第一选择器,再通过第二语法结构,提取目标页面内容文件的第二选择器,可以理解的,提取顺序可互换,其中,选择器指的是样式的作用组件,在具体实施过程中,先获取小程序支持的选择器,如微信小程序支持的选择器有类选择器、id选择器和元素选择器等。如.class为类选择器,如.intro,表示选择所有拥有class=“intro”的组件;#id为id选择器,如#firstname,表示选择拥有di=“firstname”的组件;element为元素选择器,如view,表示选择所有view组件等。
因此,可根据选择器名称,在第一语法结构中确定目标样式文件所用到的第一选择器,以及在第二语法结构中确定目标页面内容文件所用到的第二选择器。
如图3(a)所示,左窗口为wxss代码,也即目标样式文件的源代码,右窗口为生成的第一语法结构,右窗口中的矩形框框选出来的即为第一选择器,分别是id选择器:#index,元素选择器:view,类选择器:.title、.content。如图3(b)所示,左窗口为wxml代码,也即目标页面内容文件的源代码,右窗口为生成的第二语法结构,右窗口中的矩形框框选出来的即为第二选择器,分别是元素选择器:view、text,id选择器:index,类选择器:title。
步骤b2,基于所述第一选择器和所述第二选择器,确定所述目标样式文件的冗余选择器
在一实施例中,基于第一选择器和第二选择器,确定目标样式文件的冗余选择器,具体的,根据公式:wxss的选择器- wxml的选择器=冗余选择器,用第一选择器减去第二选择器,即可得到目标页面内容文件未用到的选择器,也即冗余选择器是无用的。
如上述例子,将图3(a)中的选择器减去图3(b)中的选择器,会发现wxss中定义的.content样式在wxml中没有使用到,则确定这个选择器是冗余的。
为方便说明,将去除无用wxss的过程用函数unwxss标识,函数的输入有wxml代码和wxss代码,即函数为: unwxss(wxml,wxss) = wxss(有用)。
步骤S30,确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
在本实施例中,在确定出目标样式文件的冗余选择器后,即可确定冗余选择器所定义的目标样式是冗余的,因此,可安全删除,此时,删除目标样式对应的代码并不会影响小程序的正常使用。
在本实施例中,样式文件中的样式是通过选择器定义的,因此,可在确定出冗余选择器后,反向确定冗余选择器定义的目标样式,该目标样式即为冗余样式,可以安全删除,从而减少代码体积。
本实施例确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。本申请分别将样式文件和页面内容文件生成模块化的语法结构,通过两者的语法结构,确定冗余的选择器,从而确定冗余的选择器所定义的样式为无用样式,可以安全删除,实现冗余代码的高效去除,从而减少代码体积。
进一步地,基于本申请冗余代码去除方法第一实施例,提出本申请冗余代码去除方法第二实施例。
冗余代码去除方法的第二实施例与冗余代码去除方法的第一实施例的区别在于,在确定目标样式文件和目标页面内容文件的过程中,冗余代码去除方法还包括:
步骤c,确定所述目标样式文件的作用域;
步骤d,若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
本实施例小程序框架中定义了组件模块,组件模块中的组件又包含对应的配置文件,配置文件可以对绑定了当前组件的样式文件的作用范围有不同的配置,也即,样式文件有不同的作用域(scope,可用性代码范围),具体的,小程序含有wxss代码的模块有:app(全局)、page(页面)和component(组件),app的wxss(即app.wxss文件)会影响整个小程序,为全局样式;page的wxss会影响到该页面及其调用到的组件,为页面样式;组件的wxss只会影响本组件,为组件样式。也即小程序中wxss影响为 app -> page -> component,即全局样式影响整个小程序的页面和组件,页面样式影响本页面和本页面调用的组件,组件样式影响本组件。
因此,在确定目标样式文件和目标页面内容文件的过程中,先确定目标样式文件的作用域,再确定目标页面内容文件,从而进一步提高了确定目标页面内容文件的正确性。
以下将对各个步骤进行详细说明:
步骤c,确定所述目标样式文件的作用域。
在本实施例中,先确定目标样式文件的作用域,具体根据目标样式文件的定义,确定目标样式文件的作用域,也即,小程序框架中定义了多种作用域的样式文件,每一个样式文件都有其对应的作用域,其中,作用域包括全局、页面和组件。
步骤d,若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
在本实施例中,若目标样式文件的作用域为全局,也即目标样式文件为全局样式文件,则先确定目标样式文件对应的第一临时文件,并将第一临时文件确定为目标页面内容文件,其中,第一临时文件指的是全局内所有页面和组件的页面内容文件,也即,在确定目标样式文件的作用域为全局时,先确定小程序中所有页面和组件的页面内容文件,再将这些页面内容文件打包成一个临时文件,即为目标页面内容文件。
在具体实施时,小程序规定代码包根目录中名为app.wxss的文件作为小程序公共样式文件,其中定义的样式会作用于小程序内所有wxml文件,也即app.wxss文件为全局样式文件,所以识别app.wxss中的无用wxss,需要生成一个包含小程序所有页面内容的临时文件。
在后续去除冗余的过程中,用上述函数可以记为:unwxss(所有页面和组件的wxml,app的wxss),也即,unwxss(第一临时文件,目标样式文件)。
进一步地,在一实施例中,步骤c之后,冗余代码去除方法还包括:
步骤e,若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件;
在一实施例中,若确定目标样式文件的作用域为页面,也即目标样式文件为页面样式,则读取目标样式文件对应的目标页面的配置文件,也即目标样式文件作用的目标页面的配置文件,再通过配置文件,确定目标页面对应的作用组件,这是由于页面样式会影响到本页面及其调用到的组件,因此,需要通过配置文件,确定目标样式文件影响到的作用组件。
进一步地,在一实施例中,步骤e包括:
步骤e1,若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的待筛选组件;
在一实施例中,若确定目标样式文件的作用域为页面,也即目标样式文件为页面样式文件,则确定目标样式文件作用的目标页面,并读取目标样式文件对应的目标页面的配置文件,从而根据配置文件,确定目标页面对应的待筛选组件,也即确定与目标页面相关的所有组件。
步骤e2,确定所述待筛选组件的隔离配置,并基于所述隔离配置,从所述待筛选组件中确定所述目标页面对应的作用组件。
然后,依次确定待筛选组件的隔离配置,隔离配置包括绝对隔离、单向隔离和不隔离,其中绝对隔离指的是跟组件外部互不影响,而单向隔离指的是组件受页面影响,但页面不受组件影响,而不隔离指的是组件受页面影响,页面也受组件影响。
然后,根据隔离配置,从待筛选组件中筛选出目标页面对应的作用组件,也即受目标页面影响的组件,也即隔离配置为单向隔离和不隔离的组件。
在具体实施时,小程序规定每个页面只能有一个与其同名的且在同一目录下的样式文件。如有一个页面的页面内容文件为/pages/home.wxml,其对应的样式文件即为/pages/home.wxss。接着,读取当前页面的配置文件(如/pages/home.json)中usingComponents数组字段,获取当前页面用到的所有组件,也即待筛选组件,如字段内容为[“/components/modal”,“/components/msg”],则依次读取组件的配置文件/components/modal.json和/components/msg.json中styleIsolation字段来获取组件的隔离配置,若值为isolated,则确定目标样式文件不会影响该组件;若值为apply-shared,则确定目标样式文件会影响该组件。
步骤f,确定所述目标样式文件对应的第二临时文件,并将所述第二临时文件确定为目标页面内容文件,以基于所述第二临时文件表征所述目标页面的页面内容文件,以及所述作用组件的页面内容文件。
在一实施例中,若目标样式文件的作用域为页面,也即目标样式文件为页面样式文件,则先确定目标样式文件对应的第二临时文件,并将第二临时文件确定为目标页面内容文件,其中,第二临时文件指的是目标样式文件对应的目标页面的页面内容文件,以及目标页面对应的作用组件的页面内容文件,也即,在确定目标样式文件的作用域为页面时,先确定对应的目标页面的页面内容文件,以及目标页面影响到的作用组件的页面内容文件,再将这些页面内容文件打包成一个临时文件,即为目标页面内容文件。
因此,要识别页面作用域的wxss中的无用代码,需要生成包含目标页面wxml和页面调用到的所有styleIsolation配置值为apply-shared的组件的wxml的临时文件。然后对比目标页面的wxss和临时文件,用函数可以表示为:unwxss(某page的wxml+所有首页面样式影响的组件的component.wxml,某page的wxss),也即,unwxss(第二临时文件,目标样式文件)。
进一步地,若确定目标样式文件的作用域为组件,则只需要对比对应的wxml和wxss文件,用函数表示为:unwxss(某component的wxml, 某component的wxss)。
本实施例由于小程序框架中定于了多种作用域的wxss文件,因此,在去除冗余代码的过程中,需正确识别出目标样式文件和目标页面内容文件,也即,去除过程都是一样的,区别在于对比的wxml代码和wxss代码不一样,因此,在去除过程中,需确定目标样式的作用域,从而确定正确的目标页面内容文件,以便后续找到正确的冗余代码,实现冗余代码的正确去除。
进一步地,基于本申请冗余代码去除方法第一、第二实施例,提出本申请冗余代码去除方法第三实施例。
冗余代码去除方法的第三实施例与冗余代码去除方法的第一、第二实施例的区别在于,在确定目标样式文件的冗余选择器的过程中,冗余代码去除方法还包括:
步骤g,确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表;
步骤h,基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器;
步骤i,若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
本实施例由于小程序项目本身的限制,并不能支持很多选择器,也即小程序仅能支持定义好的选择器,对于不支持的选择器,若目标样式文件中使用了,则可认为其为冗余选择器。
以下将对各个步骤进行详细说明:
步骤g,确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表。
在本实施例中,先确定目标样式文件对应的小程序类型,并确定小程序类型对应的选择器列表,也即,通过小程序类型,确定小程序所能支持的选择器有哪些,如小程序类型为微信小程序,则确定其可支持的选择器包括类选择器、id选择器和元素选择器等,也即选择器列表包括类选择器、id选择器和元素选择器等。
步骤h,基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器。
在本实施例中,在确定当前小程序所能支持的选择器后,确定目标样式文件是否存在当前小程序不支持的选择器,也即,将目标样式文件中的选择器与选择器列表中的选择器进行比较,确定目标样式文件中是否存在选择器列表中所没有的选择器。
步骤i,若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
在本实施例中,若存在,则将该选择器确定为冗余选择器,这是由于该选择器并不在选择器列表中,即使目标样式文件使用了,在当前小程序中也无法执行,因此,认定其为冗余选择器。
此外,如微信小程序,对组件作用域wxss还有额外的规定:
1.组件和引用组件的页面不能使用id选择器(#a)、属性选择器([a])和标签名选择器。也即,目标样式文件的作用域为组件时,还需判断目标样式文件中组件和引用组件的页面是否使用了id 选择器、属性选择器和标签名选择器,若使用了,则认为这些选择器为冗余选择器。
2.组件和引用组件的页面中使用后代选择器(.a .b)在一些极端情况下会有非预期的表现。也即,目标样式文件的作用域为组件时,还需判断目标样式文件中组件和引用组件的页面是否使用了后代选择器,若使用了,则认为这些选择器为冗余选择器。
3.子元素选择器(.a>.b)只能用于 view 组件与其子节点之间,用于其他组件可能导致非预期的情况。也即,目标样式文件的作用域为组件时,判断是否有除view组件之外的组件用了子元素选择器,若有,则该组件使用的选择器为冗余选择器。
本实施例的小程序不支持除选择器列表以外的选择器,因此,在确定冗余选择器的过程中,可判断选择器是否在选择器列表中,若不在,则为冗余选择器,此外,对于命中额外规定的选择器也确定为冗余选择器,以便后续能精准地为冗余代码进行去除,提高冗余代码的去除精度。
本申请还提供一种冗余代码去除装置。本申请冗余代码去除装置包括:
第一确定模块,用于确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
第二确定模块,用于基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;
删除模块,用于确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
在一实施例中,所述第一确定模块还用于:
确定所述目标样式文件的作用域;
若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
在一实施例中,所述第一确定模块还用于:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件;
确定所述目标样式文件对应的第二临时文件,并将所述第二临时文件确定为目标页面内容文件,以基于所述第二临时文件表征所述目标页面的页面内容文件,以及所述作用组件的页面内容文件。
在一实施例中,所述第一确定模块还用于:
若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的待筛选组件;
确定所述待筛选组件的隔离配置,并基于所述隔离配置,从所述待筛选组件中确定所述目标页面对应的作用组件。
在一实施例中,所述第二确定模块还用于:
基于所述第一语法结构,提取所述目标样式文件的第一选择器,并基于所述第二语法结构,提取所述目标页面内容文件的第二选择器;
基于所述第一选择器和所述第二选择器,确定所述目标样式文件的冗余选择器。
在一实施例中,所述第一确定模块还用于:
确定目标样式文件和目标页面内容文件,并获取所述目标样式文件的第一源代码和所述目标页面内容文件的第二源代码;
对所述第一源代码进行解析,并基于预设模板,生成所述目标样式文件对应的第一语法结构;
对所述第二源代码进行解析,并基于所述预设模板,生成所述目标页面内容文件对应的第二语法结构。
在一实施例中,所述第二确定模块还用于:
确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表;
基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器;
若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
本申请还提供一种计算机可读存储介质。
本申请计算机可读存储介质上存储有冗余代码去除程序,所述冗余代码去除程序被处理器执行时实现如上所述的冗余代码去除方法的步骤。
其中,在所述处理器上运行的冗余代码去除程序被执行时所实现的方法可参照本申请冗余代码去除方法各个实施例,此处不再赘述。
需要说明的是,在本文中,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、物品或者系统不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、物品或者系统所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括该要素的过程、方法、物品或者系统中还存在另外的相同要素。
上述本申请实施例序号仅仅为了描述,不代表实施例的优劣。
通过以上的实施方式的描述,本领域的技术人员可以清楚地了解到上述实施例方法可借助软件加必需的通用硬件平台的方式来实现,当然也可以通过硬件,但很多情况下前者是更佳的实施方式。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品存储在如上所述的一个存储介质(如ROM/RAM、磁碟、光盘)中,包括若干指令用以使得一台终端设备(可以是手机,计算机,服务器,空调器,或者网络设备等)执行本申请各个实施例所述的方法。
以上仅为本申请的优选实施例,并非因此限制本申请的专利范围,凡是利用本申请说明书与附图内容所作的等效结构或等效流程变换,或直接或间接运用在其他相关的技术领域,均同理包括在本申请的专利保护范围内。

Claims (10)

  1. 一种冗余代码去除方法,其中,所述冗余代码去除方法包括如下步骤:
    确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
    基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;以及
    确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
  2. 如权利要求1所述的冗余代码去除方法,其中,所述确定目标样式文件和目标页面内容文件的步骤包括:
    确定所述目标样式文件的作用域;以及
    若所述目标样式文件的作用域为全局,则确定所述目标样式文件对应的第一临时文件,并将所述第一临时文件确定为目标页面内容文件,以基于所述第一临时文件表征全局内所有页面和组件的页面内容文件。
  3. 如权利要求2所述的冗余代码去除方法,其中,所述确定所述目标样式文件的作用域的步骤之后,所述冗余代码去除方法还包括:
    若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件;以及
    确定所述目标样式文件对应的第二临时文件,并将所述第二临时文件确定为目标页面内容文件,以基于所述第二临时文件表征所述目标页面的页面内容文件,以及所述作用组件的页面内容文件。
  4. 如权利要求3所述的冗余代码去除方法,其中,所述若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的作用组件的步骤包括:
    若所述目标样式文件的作用域为页面,则读取所述目标样式文件对应的目标页面的配置文件,并基于所述配置文件,确定所述目标页面对应的待筛选组件;以及
    确定所述待筛选组件的隔离配置,并基于所述隔离配置,从所述待筛选组件中确定所述目标页面对应的作用组件。
  5. 如权利要求1所述的冗余代码去除方法,其中,所述基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器的步骤包括:
    基于所述第一语法结构,提取所述目标样式文件的第一选择器,并基于所述第二语法结构,提取所述目标页面内容文件的第二选择器;以及
    基于所述第一选择器和所述第二选择器,确定所述目标样式文件的冗余选择器。
  6. 如权利要求1所述的冗余代码去除方法,其中,所述确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构的步骤包括:
    确定目标样式文件和目标页面内容文件,并获取所述目标样式文件的第一源代码和所述目标页面内容文件的第二源代码;
    对所述第一源代码进行解析,并基于预设模板,生成所述目标样式文件对应的第一语法结构;以及
    对所述第二源代码进行解析,并基于所述预设模板,生成所述目标页面内容文件对应的第二语法结构。
  7. 如权利要求1-6任一项所述的冗余代码去除方法,其中,所述确定所述目标样式文件的冗余选择器的过程中,所述冗余代码去除方法还包括:
    确定所述目标样式文件对应的小程序类型,并确定所述小程序类型对应的选择器列表;
    基于所述选择器列表,确定所述目标样式文件是否存在所述小程序类型不支持的选择器;以及
    若存在,则将所述小程序类型不支持的选择器确定为冗余选择器。
  8. 一种冗余代码去除装置,其中,所述冗余代码去除装置包括:
    第一确定模块,用于确定目标样式文件和目标页面内容文件,并生成所述目标样式文件对应的第一语法结构,以及所述目标页面内容文件对应的第二语法结构;
    第二确定模块,用于基于所述第一语法结构和所述第二语法结构,确定所述目标样式文件的冗余选择器;以及
    删除模块,用于确定所述冗余选择器对应的目标样式,并删除所述目标样式对应的代码。
  9. 一种冗余代码去除设备,其中,所述冗余代码去除设备包括:存储器、处理器及存储在所述存储器上并可在所述处理器上运行的冗余代码去除程序,所述冗余代码去除程序被所述处理器执行时实现如权利要求1至7中任一项所述的冗余代码去除方法的步骤。
  10. 一种计算机可读存储介质,其中,所述计算机可读存储介质上存储有冗余代码去除程序,所述冗余代码去除程序被处理器执行时实现如权利要求1至7中任一项所述的冗余代码去除方法的步骤。
PCT/CN2021/083624 2020-04-03 2021-03-29 冗余代码去除方法、装置、设备与计算机可读存储介质 WO2021197273A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010262738.0 2020-04-03
CN202010262738.0A CN111459537A (zh) 2020-04-03 2020-04-03 冗余代码去除方法、装置、设备与计算机可读存储介质

Publications (1)

Publication Number Publication Date
WO2021197273A1 true WO2021197273A1 (zh) 2021-10-07

Family

ID=71683629

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2021/083624 WO2021197273A1 (zh) 2020-04-03 2021-03-29 冗余代码去除方法、装置、设备与计算机可读存储介质

Country Status (2)

Country Link
CN (1) CN111459537A (zh)
WO (1) WO2021197273A1 (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115576603A (zh) * 2022-11-23 2023-01-06 云账户技术(天津)有限公司 一种获取代码片段中的变量值的方法及装置
CN117170734A (zh) * 2023-11-03 2023-12-05 成都数智创新精益科技有限公司 一种限定样式作用域的方法

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111459537A (zh) * 2020-04-03 2020-07-28 深圳前海微众银行股份有限公司 冗余代码去除方法、装置、设备与计算机可读存储介质

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20130159839A1 (en) * 2011-12-14 2013-06-20 Microsoft Corporation Semantic compression of cascading style sheets
CN105205080A (zh) * 2014-06-26 2015-12-30 阿里巴巴集团控股有限公司 冗余文件清理方法、装置和系统
CN109976991A (zh) * 2017-12-27 2019-07-05 北京奇虎科技有限公司 一种程序代码的处理方法和装置
US20190347190A1 (en) * 2015-02-27 2019-11-14 Walmart Apollo, Llc Code usage map
CN110688118A (zh) * 2019-10-28 2020-01-14 郑州阿帕斯科技有限公司 一种网页优化方法及装置
CN111459537A (zh) * 2020-04-03 2020-07-28 深圳前海微众银行股份有限公司 冗余代码去除方法、装置、设备与计算机可读存储介质

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20130159839A1 (en) * 2011-12-14 2013-06-20 Microsoft Corporation Semantic compression of cascading style sheets
CN105205080A (zh) * 2014-06-26 2015-12-30 阿里巴巴集团控股有限公司 冗余文件清理方法、装置和系统
US20190347190A1 (en) * 2015-02-27 2019-11-14 Walmart Apollo, Llc Code usage map
CN109976991A (zh) * 2017-12-27 2019-07-05 北京奇虎科技有限公司 一种程序代码的处理方法和装置
CN110688118A (zh) * 2019-10-28 2020-01-14 郑州阿帕斯科技有限公司 一种网页优化方法及装置
CN111459537A (zh) * 2020-04-03 2020-07-28 深圳前海微众银行股份有限公司 冗余代码去除方法、装置、设备与计算机可读存储介质

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115576603A (zh) * 2022-11-23 2023-01-06 云账户技术(天津)有限公司 一种获取代码片段中的变量值的方法及装置
CN115576603B (zh) * 2022-11-23 2023-03-10 云账户技术(天津)有限公司 一种获取代码片段中的变量值的方法及装置
CN117170734A (zh) * 2023-11-03 2023-12-05 成都数智创新精益科技有限公司 一种限定样式作用域的方法
CN117170734B (zh) * 2023-11-03 2023-12-29 成都数智创新精益科技有限公司 一种限定样式作用域的方法

Also Published As

Publication number Publication date
CN111459537A (zh) 2020-07-28

Similar Documents

Publication Publication Date Title
WO2021197273A1 (zh) 冗余代码去除方法、装置、设备与计算机可读存储介质
US7822710B1 (en) System and method for data collection
US10534830B2 (en) Dynamically updating a running page
US8983935B2 (en) Methods for utilizing a javascript emulator in a web content proxy server and devices thereof
WO2012034440A1 (zh) 一种数据库升级脚本的生成方法和装置
CN106293675B (zh) 系统静态资源加载方法及装置
US20150082298A1 (en) Packaging and deploying hybrid applications
CN108762743B (zh) 一种数据表操作代码生成方法及装置
US20080028302A1 (en) Method and apparatus for incrementally updating a web page
CN109947433A (zh) 一种小程序的生成方法、更新方法及装置
WO2004086222A2 (en) Development of software systems
CN102360290A (zh) 表单生成装置和表单生成方法
CN111897553B (zh) 一种项目系统搭建方法、装置、设备及可读存储介质
US20070143330A1 (en) Processing an XML schema
CN110109671B (zh) 一种webpack标签尺寸样式转换方法及装置
CN102521008B (zh) 程序编译装置和程序编译方法
JP2020067971A (ja) 情報処理システムおよび情報処理方法
CN110377371B (zh) 一种基于Web标签的样式表系统管理方法
WO2002046909A1 (en) Automatically deploy and upgrade an application based on markup language application definition
KR100762712B1 (ko) 규칙기반의 전자문서 변환방법 및 그 시스템
US7716653B2 (en) Configurable importers and resource writers for converting data into another format
CN113672233B (zh) 一种基于Redfish的服务器带外管理方法、装置及设备
CN114461454A (zh) 数据恢复方法、装置、存储介质及电子设备
CN114527979A (zh) 一种生成多端表单交互页面的方法及系统
CN113961238A (zh) 对象转换方法、装置及电子设备和存储介质

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 21782261

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 21782261

Country of ref document: EP

Kind code of ref document: A1