Skip to Content
开发文档ContributingECMAScript Commons变量管理

变量管理

¥Variable Management

SWC 使用一种非常独特的方式来管理变量。

¥SWC uses a very unique way to manage variables.

语法上下文

¥Syntax context

SWC 使用语法上下文来管理变量。内部文档位于 resolver 过程的 rustdoc

¥SWC uses a syntax context to manage variables. Internals are documented at the rustdoc of the resolver pass.

处理顺序基本如下。此处理过程应由 GLOBALS.set(&Default::default(), || { ... }) 包装。

¥Basically the order of processing is like this. This processing should be wrapped by GLOBALS.set(&Default::default(), || { ... }).

  • resolver

  • your_pass(可以多次执行)

    ¥your_pass (can be multiple passes)

  • hygiene

  • fixer(可选,但如果您的转换可能产生无效语法,则需要添加必要的括号)

    ¥fixer (Optional, but if your transform may produce invalid syntax, this would add necessary parentheses)

术语

¥Terms

private identifier 是一个保证唯一的标识符,如果发生冲突,hygiene 过程会重命名它。

¥A private identifier is a identifier that is guaranteed to be unique, and it’s renamed by the hygiene pass if there’s a conflict.

创建私有标识符

¥Creating a private identifier

您可以使用 Ident::new_private 创建私有标识符。请注意,这需要访问 GLOBALS,它是一个作用域线程本地存储。

¥You can use Ident::new_private to create a private identifier. Note that this requires an access to GLOBALS, which is a scoped thread-local storage.

let id = Ident::new_private("my_var");

完整示例

¥Full example

完整示例:

¥Full example:

GLOBALS.set(&Default::default(), || { let unresolved_mark = Mark::new(); let top_level_mark = Mark::new(); // https://rustdoc.swc.rs/swc_core/ecma/transforms/base/fn.resolver.html program.mutate(resolver(unresolved_mark, top_level_mark, is_your_input_file_typescript)); // Apply your transforms and use Ident::new_private from there program.mutate(your_pass); // or program.visit_mut_with(your_pass); // or program = program.fold(your_pass); // https://rustdoc.swc.rs/swc_core/ecma/transforms/base/hygiene/fn.hygiene.html program.mutate(hygiene()); })
Last updated on
SWC v1.11 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站