1"use strict";(self.rspackChunkswc_site=self.rspackChunkswc_site||[]).push([[2523],{6822(e,s,r){r.r(s);var n=r(1987),i=r(7008);function a(e){let s={a:"a",code:"code",h1:"h1",h2:"h2",li:"li",p:"p",pre:"pre",span:"span",ul:"ul",...(0,i.R)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)(s.h1,{id:"variable-management",children:[(0,n.jsx)(s.a,{className:"rp-header-anchor","aria-hidden":"true",href:"#variable-management",children:"#"}),"Variable Management"]}),"\n",(0,n.jsx)(s.p,{children:"SWC uses a very unique way to manage variables."}),"\n",(0,n.jsxs)(s.h2,{id:"syntax-context",children:[(0,n.jsx)(s.a,{className:"rp-header-anchor","aria-hidden":"true",href:"#syntax-context",children:"#"}),"Syntax context"]}),"\n",(0,n.jsxs)(s.p,{children:["SWC uses a syntax context to manage variables. Internals are documented at ",(0,n.jsxs)(s.a,{href:"https://rustdoc.swc.rs/swc_core/ecma/transforms/base/fn.resolver.html",rel:"noopener noreferrer",target:"_blank",children:["the rustdoc of the ",(0,n.jsx)(s.code,{children:"resolver"})," pass"]}),"."]}),"\n",(0,n.jsxs)(s.p,{children:["Basically the order of processing is like this. This processing should be wrapped by ",(0,n.jsx)(s.code,{children:"GLOBALS.set(&Default::default(), || { ... })"}),"."]}),"\n",(0,n.jsxs)(s.ul,{children:["\n",(0,n.jsx)(s.li,{children:(0,n.jsx)(s.code,{children:"resolver"})}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsx)(s.code,{children:"your_pass"})," (can be multiple passes)"]}),"\n",(0,n.jsx)(s.li,{children:(0,n.jsx)(s.code,{children:"hygiene"})}),"\n",(0,n.jsxs)(s.li,{children:[(0,n.jsx)(s.code,{children:"fixer"})," (Optional, but if your transform may produce invalid syntax, this would add necessary parentheses)"]}),"\n"]}),"\n",(0,n.jsxs)(s.h2,{id:"terms",children:[(0,n.jsx)(s.a,{className:"rp-header-anchor","aria-hidden":"true",href:"#terms",children:"#"}),"Terms"]}),"\n",(0,n.jsxs)(s.p,{children:["A ",(0,n.jsx)(s.code,{children:"private identifier"})," is a identifier that is guaranteed to be unique, and it's renamed by the ",(0,n.jsx)(s.code,{children:"hygiene"})," pass if there's a conflict."]}),"\n",(0,n.jsxs)(s.h2,{id:"creating-a-private-identifier",children:[(0,n.jsx)(s.a,{className:"rp-header-anchor","aria-hidden":"true",href:"#creating-a-private-identifier",children:"#"}),"Creating a private identifier"]}),"\n",(0,n.jsxs)(s.p,{children:["You can use ",(0,n.jsx)(s.code,{children:"Ident::new_private"})," to create a private identifier. Note that this requires an access to ",(0,n.jsx)(s.code,{children:"GLOBALS"}),", which is a scoped thread-local storage."]}),"\n",(0,n.jsx)(n.Fragment,{children:(0,n.jsx)(s.pre,{className:"shiki css-variables",style:{backgroundColor:"var(--shiki-background)",color:"var(--shiki-foreground)"},tabIndex:"0",lang:"rust",children:(0,n.jsx)(s.code,{children:(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"let"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" id "}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"="}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:" Ident"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"::"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"new_private"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"("}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-string-expression)"},children:'"my_var"'}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:");"})]})})})}),"\n",(0,n.jsxs)(s.h2,{id:"full-example",children:[(0,n.jsx)(s.a,{className:"rp-header-anchor","aria-hidden":"true",href:"#full-example",children:"#"}),"Full example"]}),"\n",(0,n.jsx)(s.p,{children:"Full example:"}),"\n",(0,n.jsx)(n.Fragment,{children:(0,n.jsx)(s.pre,{className:"shiki css-variables",style:{backgroundColor:"var(--shiki-background)",color:"var(--shiki-foreground)"},tabIndex:"0",lang:"rust",children:(0,n.jsxs)(s.code,{children:[(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"GLOBALS"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"set"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"("}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"&"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"Default"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"::"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"default"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"}
1,children:"(), "}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"||"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" {"})]}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:" let"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" unresolved_mark "}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"="}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" Mark"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"::"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"new"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"();"})]}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:" let"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" top_level_mark "}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"="}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" Mark"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"::"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"new"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"();"})]}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-comment)"},children:" // https://rustdoc.swc.rs/swc_core/ecma/transforms/base/fn.resolver.html"})}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"mutate"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"("}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"resolver"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"(unresolved_mark, top_level_mark, is_your_input_file_typescript));"})]}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-comment)"},children:" // Apply your transforms and use Ident::new_private from there"})}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"mutate"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"(your_pass);"})]}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-comment)"},children:" // or"})}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"visit_mut_with"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"(your_pass);"})]}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-comment)"},children:" // or"})}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program "}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"="}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"fold"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"(your_pass);"})]}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-comment)"},children:" // https://rustdoc.swc.rs/swc_core/ecma/transforms/base/hygiene/fn.hygiene.html"})}),"\n",(0,n.jsxs)(s.span,{className:"line",children:[(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:" program"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-keyword)"},children:"."}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"mutate"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"("}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-token-function)"},children:"hygiene"}),(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"}
1,children:"());"})]}),"\n",(0,n.jsx)(s.span,{className:"line"}),"\n",(0,n.jsx)(s.span,{className:"line",children:(0,n.jsx)(s.span,{style:{color:"var(--shiki-foreground)"},children:"})"})})]})})})]})}function l(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{wrapper:s}={...(0,i.R)(),...e.components};return s?(0,n.jsx)(s,{...e,children:(0,n.jsx)(a,{...e})}):a(e)}l.__RSPRESS_PAGE_META={},l.__RSPRESS_PAGE_META["docs%2Fcontributing%2Fes-commons%2Fvariable-management.mdx"]={toc:[{id:"syntax-context",text:"Syntax context",depth:2},{id:"terms",text:"Terms",depth:2},{id:"creating-a-private-identifier",text:"Creating a private identifier",depth:2},{id:"full-example",text:"Full example",depth:2}],title:"Variable Management",headingTitle:"Variable Management",frontmatter:{}},r.d(s,{default:()=>l})}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.