Skip to content

语法降级

🌐 Syntax Lowering

Oxc 转换器支持将 ESNext 降级为 ES2015 语法。

🌐 Oxc transformer supports lowering ESNext to ES2015 syntax.

目标

🌐 Target

Oxc 转换器接收一个 target 选项来指定目标运行时。这将决定哪些语法会被降级以及哪些警告会被发出。

🌐 Oxc transformer receives a target option to specify the target runtime. This will determine which syntaxes are lowered and which warnings are emitted.

每个目标环境都是环境名称后跟版本号。当前支持以下环境名称:

🌐 Each target environment is an environment name followed by a version number. The following environment names are currently supported:

  • chrome
  • deno
  • edge
  • firefox
  • hermes
  • ie
  • ios
  • node
  • opera
  • rhino
  • safari
  • samsung
  • es

受支持的值与 esbuild 的 target 选项 所支持的值相同。

🌐 The values that are supported by esbuild's target option are supported.

js
import { transform } from "oxc-transform";

const result = await transform("lib.js", "const foo = a ?? b;", {
  target: ["chrome87", "es2022"],
});

变革

🌐 Transformations

Oxc 支持降低以下语法。请注意,正则表达式相关的转换仅将正则表达式字面量(/foo/v)转换为使用正则表达式构造函数(new RegExp('foo', 'v'))。你需要同时使用 polyfill 才能支持旧浏览器。

🌐 Oxc supports lowering the syntaxes below. Note that RegExp related transformations only transforms the RegExp literal (/foo/v) to use a RegExp constructor (new RegExp('foo', 'v')). You will need to use a polyfill together to support older browsers.

ES2026

  • 显式资源管理 (using a = foo())

ES2024

  • RegExp v 标志与集合表示法 + 字符串属性(/\p{Emoji}--\p{ASCII}/v

ES2022

  • 类静态块 (class A { static { foo() } })
  • 类字段 (class A { foo = 1; #bar = 2; static baz = 3; static qux = 4; foobar(a) { return #bar in a } })
  • 正则表达式匹配索引(/foo/d

ES2021

  • 逻辑赋值运算符(foo ||= bar
  • 数字分隔符(注意:这不是作为转换实现的,但代码生成总是会移除分隔符)

ES2020

  • 空值合并运算符(foo ?? bar
  • 可选链(foo?.bar
  • 从 (export * as foo from "bar") 导出命名空间

ES2019

  • 可选的 catch 绑定 (try {} catch {})

ES2018

  • 剩余/展开 属性(const foo = { a, b, ...c }const { x, y, ...z } = foo;
  • 异步迭代(for await (const x of y) {}async function* foo() {}
  • 正则表达式 Unicode 属性转义(/\p{Script=Greek}/u
  • 正则表达式回顾断言(/(?<=foo)bar/
  • 正则表达式命名捕获组(/(?<foo>bar)/
  • sdotAll)正则表达式标志(/foo./s

ES2017

  • 异步函数 (async function foo() {})

ES2016

  • 指数运算符 (foo ** bar)

ES2015

  • 箭头函数 (const foo = () => {})
  • RegExp 粘性标志(/foo/y
  • RegExp Unicode 标志 (/foo/u)

警告

🌐 Warnings

如果目标运行时不支持以下语法,Oxc 转换器会发出警告。

🌐 Oxc transformer emits warnings for the syntaxes below if the target runtime does not support them.

ES2022

  • 顶层 await (await foo())
  • 任意模块命名空间标识符(import * as "f o o" from "bar"

ES2020

  • 大整数 (1n)

编译器假设

🌐 Compiler assumptions

你可以为编译器指定假设,以使输出更小。

🌐 You can specify assumptions for the compiler to make the output more smaller.

js
import { transform } from "oxc-transform";

const result = await transform("lib.js", "const foo = a ?? b;", {
  target: ["chrome87", "es2022"],
  assumptions: {
    noDocumentAll: true,
  },
});

以下假设得到了支持。

🌐 The following assumptions are supported.

noDocumentAll

假设不使用带有特殊行为的已弃用 document.all

🌐 Assume that the deprecated document.all with its special behavior is not used.

pureGetters

假设 getter 没有副作用。

🌐 Assume that getters does not have side effects.

setPublicClassFields

在使用公共类字段时,假设它们不会覆盖当前类、其子类或其父类中的任何 getter。因此,直接赋值是安全的,而不必使用 Object.defineProperty

🌐 When using public class fields, assume that they don't shadow any getter in the current class, in its subclasses or in its superclass. Thus, it's safe to assign them rather than using Object.defineProperty.

注意

对于 TypeScript,如果你希望的行为与 useDefineForClassFields: false 相同,你应该将 setPublicClassFieldsremoveClassFieldsWithoutInitializer 都设置为 true。更多信息请参见 TypeScript 页面

不支持的语法

🌐 Not supported syntaxes

以下语法不会被 Oxc 转换器降级。

🌐 The following syntaxes are not lowered by Oxc transformer.