升级到 Babel 8 (API)
本文档列出了 Babel 8.0.0 中引入的重大更改,以及从 Babel 7.0.0 升级时如何调整 Babel 的使用以适应这些更改。本文档面向直接使用 Babel API 的开发者,例如 Babel 插件作者或使用 Babel 作为库的项目作者。
¥This document lists the breaking changes introduced in Babel 8.0.0, and how to adapt your usage of Babel to them when upgrading from Babel 7.0.0. This document is intended for developers using Babel's API directly, such as Babel plugin authors, or authors of projects that use Babel as a library.
请先查看 Babel 8 迁移指南,了解面向用户的重大变更。
¥Check out the Babel 8 migration guide first to learn about user-facing breaking changes.
AST 变更
¥AST Changes
JavaScript 节点
¥JavaScript nodes
-
将动态
import()
表示为ImportExpression
节点(#15682、#16114)。¥Represent dynamic
import()
with anImportExpression
node (#15682, #16114).// Example input
import("foo", options);
// AST in Babel 7
{
type: "CallExpression",
callee: { type: "Import" },
arguments: [
StringLiteral("foo"),
Identifier("options")
]
}
// AST in Babel 8
{
type: "ImportExpression",
source: StringLiteral("foo"),
options: Identifier("options")
}迁移:我们鼓励你使用新的 AST(从 v7.23.0 开始)测试你的 Babel 插件,并在 Babel 配置中指定
{ parserOpts: { createImportExpressions: true } }
。对于使用依赖于旧版import()
AST 的 Babel 插件的终端用户,可以将createImportExpressions
设置为false
。请注意,Babel 7import()
AST 现已弃用,它不支持新的 ES 功能,例如 源短语导入。我们将在 Babel 9 中移除createImportExpressions
解析器选项。¥Migration: You are encouraged to test your Babel plugins with the new AST, starting from v7.23.0, specifying
{ parserOpts: { createImportExpressions: true } }
in the Babel config. For end users utilizing Babel plugins that rely on the legacyimport()
AST, it is possible to setcreateImportExpressions
tofalse
. Note that the Babel 7import()
AST is now considered deprecated, it does not support new ES features such as Source Phrase Imports. We will remove thecreateImportExpressions
parser option in Babel 9.
-
使用
bigint
代替BigIntLiteral.value
,而不是字符串(#17378)¥ Use
bigint
forBigIntLiteral.value
, rather than a string(#17378)// Example input
0xff01_0000_0000_0000_0000_0000_0000_0001n
// AST in Babel 7
{
type: "BigIntLiteral",
value: "0xff010000000000000000000000000001",
extra: {
rawValue: "0xff010000000000000000000000000001",
raw: "0xff01_0000_0000_0000_0000_0000_0000_0001n",
}
}
// AST in Babel 8
{
type: "BigIntLiteral",
value: 338958331222012082418099330867817086977n,
extra: {
rawValue: 338958331222012082418099330867817086977n,
raw: "0xff01_0000_0000_0000_0000_0000_0000_0001n"
}
}迁移:此项变更与我们处理
NumericLiteral
的方式一致。如果你想在 Babel 7 中访问字符串表示,请调用toString()
的 bigint 实例方法。请注意,内置 JavaScript 方法JSON.stringify
无法序列化bigint
。如果你想将 Babel 8 AST 存储为 JSON,可以使用 提供你自己的bigint
序列化器。¥Migration: This change aligns with how we handle
NumericLiteral
. If you want to access string representation in Babel 7, call thetoString()
bigint instance method. Note that the builtin JavaScript methodJSON.stringify
can not serializebigint
. If you want to store the Babel 8 AST as JSON, you can provide your ownbigint
serializer.
TypeScript 节点
¥TypeScript nodes
我们对 TypeScript 特定的 AST 节点所做的大部分更改都是为了减少与 @typescript-eslint
项目 AST 形状的差异。这将使编写 ESLint 规则变得更容易,当不依赖类型信息时,这些规则可以同时与 @typescript-eslint/parser
和 @babel/eslint-parser
兼容。
¥Most of the changes to our TypeScript-specific AST nodes are to reduce the differences with the AST shape of the @typescript-eslint
project. This will make it easier to write ESLint rules that, when not depending on type information, can work both with @typescript-eslint/parser
and @babel/eslint-parser
.
-
使用标识符代替
TSTypeParameter.name
,而不是纯字符串(#12829)¥ Use an identifier for
TSTypeParameter.name
, rather than a plain string (#12829)input.ts// T is a TSTypeParameter
function process<T>(input: T): T {}
// AST in Babel 7
{
type: "TSTypeParameter",
name: "T",
}
// AST in Babel 8
{
type: "TSTypeParameter",
name: { type: "Identifier", name: "T" },
} -
允许将
ThisExpression
用作TSTypeQuery.exprName
,而不是this
标识符 (#17059)¥Allow
ThisExpression
asTSTypeQuery.exprName
, rather than athis
identifier (#17059)input.tsfunction fn() {
// `typeof this` is a TSTypeQuery
var self: typeof this
}
// AST in Babel 7
{
type: "TSTypeQuery",
exprName: { type: "Identifier", name: "this" }
}
// AST in Babel 8
{
type: "TSTypeQuery",
exprName: { type: "ThisExpression" }
} -
将
TSImportEqualsDeclaration
导出为ExportNamedDeclaration
进行解析(#17073)¥Parse exporting
TSImportEqualsDeclaration
as anExportNamedDeclaration
(#17073)请注意,
isExport
字段也已被移除。对于任何TSImportEqualsDeclaration
NodePathp
,使用p.parentPath.isExportNamedDeclaration()
检测它是否位于export
关键字之后。¥Note that the
isExport
field is also removed. For anyTSImportEqualsDeclaration
NodePathp
, usep.parentPath.isExportNamedDeclaration()
to detect whether it is following anexport
keyword.input.tsexport import foo = require("foo")
// AST in Babel 7
{
type: "TSImportEqualsDeclaration",
importKind: "value"
isExport: true,
id: Identifier("foo"),
moduleReference: {
type: "TSExternalModuleReference",
expression: StringLiteral("foo")
}
}
// AST in Babel 8
{
type: "ExportNamedDeclaration",
declaration: {
type: "TSImportEqualsDeclaration",
importKind: "value"
id: Identifier("foo"),
moduleReference: {
type: "TSExternalModuleReference",
expression: StringLiteral("foo")
}
},
specifiers: []
} -
将
TSCallSignatureDeclaration
、TSConstructSignatureDeclaration
、TSFunctionType
、TSConstructorType
和TSMethodSignature
(#9231、#13709)中的parameters
重命名为params
,将typeAnnotation
重命名为returnType
¥Rename
parameters
toparams
andtypeAnnotation
toreturnType
inTSCallSignatureDeclaration
,TSConstructSignatureDeclaration
,TSFunctionType
,TSConstructorType
andTSMethodSignature
(#9231, #13709)TSCallSignatureDeclaration
input.tsinterface Foo {
(x: number): string;
}
// AST in Babel 7
{
type: "TSCallSignatureDeclaration",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSCallSignatureDeclaration",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSConstructSignatureDeclaration
input.tsinterface Foo {
new (x: number): string;
}
// AST in Babel 7
{
type: "TSConstructSignatureDeclaration",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSConstructSignatureDeclaration",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSMethodSignature
input.tsinterface Foo {
foo(x: number): string;
}
// AST in Babel 7
{
type: "TSMethodSignature",
key: Identifier("foo"),
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSMethodSignature",
key: Identifier("foo"),
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSFunctionType
input.tstype Bar = (x: number) => string;
// AST in Babel 7
{
type: "TSFunctionType",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSFunctionType",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSConstructorType
input.tstype Bar = (x: number) => string;
// AST in Babel 7
{
type: "TSConstructorType",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSConstructorType",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
} -
将
CallExpression
、JSXOpeningElement
、NewExpression
、OptionalCallExpression
、TSImportType
、TSInstantiationExpression
、TSTypeQuery
和TSTypeReference
(#16679、#17008、#17012、#17020、#17042)中的typeParameters
重命名为typeArguments
¥ Rename
typeParameters
totypeArguments
inCallExpression
,JSXOpeningElement
,NewExpression
,OptionalCallExpression
,TSImportType
,TSInstantiationExpression
,TSTypeQuery
andTSTypeReference
(#16679, #17008, #17012, #17020, #17042)CallExpression
input.tsfn<string>()
// AST in Babel 7
{
type: "CallExpression",
callee: Identifier("fn"),
arguments: [],
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "CallExpression",
callee: Identifier("fn"),
arguments: [],
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}JSXOpeningElement
input.ts<Component<string>/>
// AST in Babel 7
{
type: "JSXOpeningElement",
name: JSXIdentifier("Component"),
attributes: [],
selfClosing: true,
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "JSXOpeningElement",
name: JSXIdentifier("Component"),
attributes: [],
selfClosing: true,
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}NewExpression
input.tsnew Component<string>()
// AST in Babel 7
{
type: "NewExpression",
callee: Identifier("Component"),
arguments: [],
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "NewExpression",
callee: Identifier("Component"),
arguments: [],
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}OptionalCallExpression
input.tsfn?.<string>()
// AST in Babel 7
{
type: "OptionalCallExpression",
callee: Identifier("fn"),
arguments: [],
optional: true,
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "OptionalCallExpression",
callee: Identifier("fn"),
arguments: [],
optional: true,
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSImportType
input.tsvar arr: import("./Array")<string>
// AST in Babel 7
{
type: "TSImportType",
argument: StringLiteral("./Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSImportType",
argument: {
type: "TSLiteralType",
literal: StringLiteral("./Array")
},
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSInstantiationExpression
input.tsfn<string>
// AST in Babel 7
{
type: "TSInstantiationExpression",
expression: Identifier("fn"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSInstantiationExpression",
expression: Identifier("fn"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSTypeQuery
input.tsvar arr: typeof Array<string>;
// AST in Babel 7
{
type: "TSTypeQuery",
exprName: Identifier("Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSTypeQuery",
exprName: Identifier("Array"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSTypeReference
input.tsvar arr: Array<string>;
// AST in Babel 7
{
type: "TSTypeReference",
typeName: Identifier("Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSTypeReference",
typeName: Identifier("Array"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
} -
将
ClassDeclaration
和ClassExpression
(#16679、#16997)中的superTypeParameters
重命名为superTypeArguments
¥Rename
superTypeParameters
tosuperTypeArguments
inClassDeclaration
andClassExpression
(#16679, #16997)input.tsclass X extends Y<string> {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("X"),
superClass: Identifier("Y"),
superTypeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("X"),
superClass: Identifier("Y"),
superTypeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
} -
将
typeParameter
从TSMappedType
(#16733)中拆分出来。¥ Split
typeParameter
ofTSMappedType
(#16733).在
TSMappedType
节点中,typeParameter
属性被展平为TSMappedType
本身的key
和constraint
属性。¥In
TSMappedType
nodes, thetypeParameter
property is flattened askey
andconstraint
properties ofTSMappedType
itself.input.tslet map1: { [P in string]: number; };
// AST in Babel 7
{
type: "TSMappedType",
typeParameter: {
type: "TypeParameter",
name: Identifier("P"),
constraint: TSStringKeyword()
},
typeAnnotation: TSNumberKeyword(),
}
// AST in Babel 8
{
type: "TSMappedType",
key: Identifier("P"),
constraint: TSStringKeyword()
typeAnnotation: TSNumberKeyword(),
} -
将
TSExpressionWithTypeArguments
拆分为TSClassImplements
和TSInterfaceHeritage
(#16731)。¥Split
TSExpressionWithTypeArguments
intoTSClassImplements
andTSInterfaceHeritage
(#16731).新节点也使用
typeArguments
而不是typeParameters
(#17017)。如果expression
是 TS 限定名称(例如a.b
),它将被解析为MemberExpression
(#17139)。¥The new nodes also use
typeArguments
instead oftypeParameters
(#17017). If theexpression
is a TS qualified name (e.g.a.b
), it will be parsed as aMemberExpression
(#17139).TSClassImplements
input.tsclass C implements X<T> {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSExpressionWithTypeArguments",
expression: Identifier("X"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: ClassBody([]),
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSClassImplements",
expression: Identifier("X"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: ClassBody([]),
}TSInterfaceHeritage
input.tsinterface I extends X<T> {}
// AST in Babel 7
{
type: "TSInterfaceDeclaration",
id: Identifier("I"),
extends: [
{
type: "TSExpressionWithTypeArguments",
expression: Identifier("X"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: TSInterfaceBody([]),
}
// AST in Babel 8
{
type: "TSInterfaceDeclaration",
id: Identifier("I"),
extends: [
{
type: "TSInterfaceHeritage",
expression: Identifier("X"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: TSInterfaceBody([]),
}Qualified name
input.tsclass C implements X.Y {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSExpressionWithTypeArguments",
expression: {
type: "TSQualifiedName",
left: Identifier("X"),
right: Identifier("Y")
}
}
],
body: ClassBody([]),
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSClassImplements",
expression: {
type: "MemberExpression",
object: Identifier("X"),
property: Identifier("Y"),
computed: false
}
}
],
body: ClassBody([]),
} -
将
TSImportType
的argument
封装在TSLiteralType
节点中(#17046)¥ Wrap the
argument
ofTSImportType
within aTSLiteralType
(#17046)TSImportType
还使用typeArguments
而不是typeParameters
(#17042)。有关示例,请参阅 此处。¥The
TSImportType
also usestypeArguments
instead oftypeParameters
(#17042). See here for an example.input.tsvar arr: import("./Array")
// AST in Babel 7
{
type: "TSImportType",
argument: StringLiteral("./Array")
}
// AST in Babel 8
{
type: "TSImportType",
argument: {
type: "TSLiteralType",
literal: StringLiteral("./Array")
}
} -
将
TSEnumDeclaration
的members
封装在TSEnumBody
节点中(#16979)¥ Wrap the
members
ofTSEnumDeclaration
within aTSEnumBody
node (#16979)input.ts// Example input
enum ColorType {
Red,
Green,
Blue,
}
// AST in Babel 7
{
type: "TSEnumDeclaration",
id: Identifier("ColorType")
members: [
EnumMember("Red"),
EnumMember("Green"),
EnumMember("Blue")
]
}
// AST in Babel 8
{
type: "TSEnumDeclaration",
id: Identifier("ColorType")
body: {
type: "TSEnumBody",
members: [
EnumMember("Red"),
EnumMember("Green"),
EnumMember("Blue")
]
}
} -
当至少有一个插值位置时,创建
TSTemplateLiteralType
。¥Create
TSTemplateLiteralType
when there is at least one interpolated position请注意,当没有插值位置时,AST 不会更改,例如,作为模板字面量类型的
foo
仍然会被解析为 TSLiteralType 中的 TemplateLiteral 节点。¥Note that the AST is not changed when there is no interpolated position, e.g.
`foo`
as a template literal type is still parsed as a TemplateLiteral node within a TSLiteralType.input.tstype World = "world";
// `hello ${World}` is a template literal type
type Greeting = `hello ${World}`;
// AST in Babel 7
{
type: "TSLiteralType",
literal: {
type: "TemplateLiteral",
expressions: [{
type: "TSTypeReference",
typeName: Identifier("World")
}],
quasis: [
TemplateElement("hello "),
TemplateElement("")
]
}
}
// AST in Babel 8
{
type: "TSTemplateLiteralType",
types: [{
type: "TSTypeReference",
typeName: Identifier("World")
}],
quasis: [
TemplateElement("hello "),
TemplateElement("")
]
} -
当同时启用
estree
和typescript
解析器插件时,创建TSAbstractMethodDefinition
和TSPropertyDefinition
(#16679、#17014)。¥Create
TSAbstractMethodDefinition
andTSPropertyDefinition
when bothestree
andtypescript
parser plugins are enabled (#16679, #17014)迁移:此项重大变更是为了使库和 ESLint 插件能够同时与
typescript-eslint
和@babel/eslint-parser
兼容。对于大多数 Babel 插件开发者,你可以放心地忽略此更改,因为它不会影响 TypeScript 转换和代码修改。也就是说,如果你尝试使用@babel/eslint-parser
开发自定义 ESLint 规则,此更改将使 Babel AST 与typescript-eslint
AST 保持一致。¥Migration: This breaking change is part of the efforts to libraries and ESLint plugins that can work both with
typescript-eslint
and@babel/eslint-parser
. For most Babel plugin developers you can safely ignore this change as it does not affect the typescript transform and codemod. That said, if you are trying to develop a custom ESLint rule with@babel/eslint-parser
, this change aligns the Babel AST to thetypescript-eslint
AST. -
使用
TSQualifiedName
作为namespace X.Y {}
的名称 (#16982)¥Use
TSQualifiedName
fornamespace X.Y {}
's name (#16982)namespace X.Y {}
不再表示为两个嵌套的TSModuleDeclaration
节点,名称分别为X
和Y
,而是表示为单个TSModuleDeclaration
,名称为TSQualifiedName(X, Y)
。此项变更使 AST 与@typescript-eslint
解析器保持一致。¥Rather than representing
namespace X.Y {}
as two nestedTSModuleDeclaration
nodes with namesX
andY
, it is now represented as a singleTSModuleDeclaration
whose name is aTSQualifiedName(X, Y)
. This change aligns the AST with the@typescript-eslint
parser.// Example input
namespace X.Y {}
// AST in Babel 7
{
type: "TSModuleDeclaration",
id: Identifier("X"),
body: {
type: "TSModuleDeclaration",
id: Identifier("Y")
body: TSModuleBlock([]),
},
}
// AST in Babel 8
{
type: "TSModuleDeclaration",
id: {
type: "TSQualifiedName",
left: Identifier("X"),
right: Identifier("Y"),
},
body: TSModuleBlock([]),
}
-
除非启用
createParenthesizedExpression
,否则不要生成TSParenthesizedType
(#9546、#12608)。¥Don't generate
TSParenthesizedType
unlesscreateParenthesizedExpression
is enabled (#9546, #12608)input.tstype T = ({});
// Babel 8 with createParenthesizedExpression: true, and Babel 7
TSParenthesizedType { typeAnnotation: TSTypeLiteral { members: [] } }
// Babel 8 with createParenthesizedExpression: false
TSTypeLiteral { members: [] }迁移:如果你需要有关括号的信息,请指定
createParenthesizedExpression
解析器选项。¥Migration: If you need information about parentheses, specify the
createParenthesizedExpression
parser option.babel.config.json{ "parserOpts": { "createParenthesizedExpression": true } }
当
createParenthesizedExpression
为false
时,你还可以使用node.extra.parenthesized
来检测node
是否包含在括号中。¥When
createParenthesizedExpression
isfalse
, you can also usenode.extra.parenthesized
to detect whethernode
is wrapped in parentheses.
API 变更
¥API Changes
所有软件包
¥All packages
-
¥Disallow importing internal files (#14013, #14179).
迁移:仅使用导出的 API。如果你依赖 Babel 内部组件,请提交问题并告知我们。
¥Migration: Use the exported API only. If you are relying on Babel internals, please open an issue and let us know.
@babel/core
-
禁止同步使用
babel.transform
、babel.transformFile
、babel.transformFromAst
、babel.parse
、babel.loadOptions
、babel.loadPartialConfig
和babel.createConfigItem
(#11110、#12695、#15869)。¥Disallow using
babel.transform
,babel.transformFile
,babel.transformFromAst
,babel.parse
,babel.loadOptions
,babel.loadPartialConfig
andbabel.createConfigItem
synchronously (#11110, #12695, #15869).迁移:上述 API 需要回调参数。如果你不提供回调,请使用其同步版本:
babel.transformSync
、babel.transformFileSync
、babel.transformFromAstSync
、babel.parseSync
、babel.loadOptionsSync
、babel.loadPartialConfigSync
和babel.createConfigItemSync
。¥Migration: The APIs above require a callback argument. If you are not providing a callback, please use their sync versions:
babel.transformSync
,babel.transformFileSync
,babel.transformFromAstSync
,babel.parseSync
,babel.loadOptionsSync
,babel.loadPartialConfigSync
andbabel.createConfigItemSync
.
@babel/generator
-
移除
CodeGenerator
类 (#16126)¥Remove
CodeGenerator
class (#16126)迁移:在 Babel 8 中,未记录的
CodeGenerator
类已被移除,请使用默认导出的generate
函数。¥Migration: In Babel 8 the undocumented
CodeGenerator
class has been removed, please use the default exportedgenerate
function instead.- new CodeGenerator(ast).generate()
+ generate(ast)
@babel/types
-
拒绝
t.identifier
构建器 (#10917) 中的无效标识符名称。¥Reject invalid identifier names in
t.identifier
builder (#10917).babel-plugin.js// Empty string is an invalid identifier name
t.identifier("");迁移:使用有效名称调用
t.identifier
。¥Migration: Call
t.identifier
with a valid name. -
拒绝
t.variableDeclaration
构建器中的无效变量声明符(#10917、#17217)¥Reject invalid variable declarator in
t.variableDeclaration
builder (#10917, #17217)babel-plugin.js// var [x] is invalid without an initializer
t.variableDeclaration("var", [
t.variableDeclarator(t.arrayPattern([t.identifier("x")]))
]); -
从
Expression
别名中移除Super
(#14750)。¥Remove
Super
from theExpression
alias (#14750).Super
节点在父类调用super()
和父类属性super.foo
中代表super
。由于super
不能是独立表达式,因此在 Babel 8 中,t.isExpression(t.super())
将返回false
。¥A
Super
node representssuper
in super callsuper()
and super propertysuper.foo
. Assuper
can not be a standalone expression,t.isExpression(t.super())
will returnfalse
in Babel 8.迁移:
t.isExpression
和t.assertsExpression
函数以及t.Expression
类型别名的搜索用法:如果他们还需要接受Super
节点,请相应地更新它们。¥Migration: Search usage of the
t.isExpression
andt.assertsExpression
functions, and of thet.Expression
type alias: if they need to also acceptSuper
nodes, update them accordingly.my-babel-plugin.js// Add `.foo` to an expression
- if (t.isExpression(path.node)) {
+ if (t.isExpression(path.node) || t.isSuper(path.node)) {
path.replaceWith(
t.memberExpression(
path.node,
t.identifier("foo")
))
}如果不涉及
super()
和super.foo
,则无需更新用法:¥You don't have to update the usage if
super()
andsuper.foo
is not involved:my-babel-plugin.js// define an expression as a computed key of `foo`
if (t.isExpression(path.node)) {
path.replaceWith(
t.memberExpression(
t.identifier("foo"),
// `super` can not be a computed key, so we don't update `isExpression`
path.node,
/* computed */ true
))
} -
从
LVal
别名中移除AssignmentPattern
和RestElement
(#17387)¥Remove
AssignmentPattern
andRestElement
from theLVal
alias (#17387)LVal
节点表示赋值表达式左侧 (LHS) 的有效节点。由于AssignmentPattern
和RestElement
不能是 LHS,因此在 Babel 8 中,t.isLVal(t.assignmentPattern(...))
和t.isLVal(t.restElement(...))
将返回false
。¥An
LVal
node represents valid nodes as the left hand side (LHS) of an assignment expression. AsAssignmentPattern
andRestElement
can not be the LHS,t.isLVal(t.assignmentPattern(...))
andt.isLVal(t.restElement(...))
will returnfalse
in Babel 8.迁移:
t.isLVal
和t.assertsLVal
函数以及t.LVal
类型别名的搜索用法:如果他们还需要接受AssignmentPattern
或RestElement
节点,你可以使用t.PatternLike
扩展t.LVal
:¥Migration: Search usage of the
t.isLVal
andt.assertsLVal
functions, and of thet.LVal
type alias: if they need to also acceptAssignmentPattern
orRestElement
nodes, you can broadent.LVal
witht.PatternLike
:my-babel-plugin.js// Iterate rest element within an object pattern
for (const propertyPath of path.get("properties")) {
- if (t.isLVal(propertyPath.node)) {
+ if (t.isLVal(propertyPath.node) || t.isPatternLike(propertyPath.node)) {
doSomethingWith(propertyPath);
}
} -
从
LVal
别名中移除TSParameterProperty
(#17391)¥Remove
TSParameterProperty
from theLVal
alias (#17391)LVal
节点表示赋值表达式左侧 (LHS) 的有效节点。TSParameterProperty
是一种特殊的函数参数类型,仅允许在类构造函数中使用。由于TSParameterProperty
不能作为 LHS,因此在 Babel 8 中t.isLVal(t.tsParameterProperty(...))
将返回false
。¥An
LVal
node represents valid nodes as the left hand side (LHS) of an assignment expression. ATSParameterProperty
is a special function parameter type allowed only in a class constructor. SinceTSParameterProperty
can not be the LHS,t.isLVal(t.tsParameterProperty(...))
will returnfalse
in Babel 8. -
要求
bigint
作为t.bigIntLiteral
(#17378)的唯一参数¥Require a
bigint
as the only argument oft.bigIntLiteral
(#17378)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
bigint
原语传递给bigIntLiteral
构建器。bigIntLiteral
构建器从 Babel 7.28 开始支持bigint
,你可以在升级到 Babel 8 之前开始迁移。¥Migration: Pass the
bigint
primitive to thebigIntLiteral
builder. Support ofbigint
in thebigIntLiteral
builder starts from Babel 7.28, you can begin the migration before you upgrade to Babel 8my-babel-codemod.jst.bigIntLiteral(
-
"0xff020000000000000000000000000002"
- 0xff02000000000000000000000000000002n )
- Require an `Identifier` node as the third argument of `t.tsTypeParameter` ([#12829](https://github.com/babel/babel/pull/12829))
This is due to the corresponding [AST shape change](#ast-TSTypeParameter).
__Migration__: Wrap the `name` string within the `identifier` builder
```diff title="my-babel-codemod.js"
t.tsTypeParameter(
/* constraint */ undefined,
/* default */ undefined,
+ t.identifier(
name
+ )
)
-
要求
TSLiteralType
节点作为t.tsImportType
(#17046)的第一个参数¥Require a
TSLiteralType
node as the first argument oft.tsImportType
(#17046)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
argument
字符串字面量封装在tsLiteralType
构建器中¥Migration: Wrap the
argument
string literal within thetsLiteralType
buildermy-babel-codemod.jst.tsImportType(
+ t.tsLiteralType(
t.stringLiteral("foo")
+ )
) -
要求
TSEnumBody
节点作为t.tsEnumDeclaration
(#16979)的第二个参数¥Require a
TSEnumBody
node as the second argument oft.tsEnumDeclaration
(#16979)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
members
数组封装在tsEnumBody
构建器中¥Migration: Wrap the
members
array within thetsEnumBody
buildermy-babel-codemod.js// Create `enum ColorType { Red, Green, Blue }`
t.tsEnumDeclaration(
t.identifier("ColorType"),
- [
+ t.tsEnumBody([
t.tsEnumMember(t.identifier("Red")),
t.tsEnumMember(t.identifier("Green")),
t.tsEnumMember(t.identifier("Blue"))
- ],
+ ]),
) -
更新
t.tsMappedType
签名 (#16733)¥Update the
t.tsMappedType
signature (#16733)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
// Babel 7
declare function tsMappedType(
typeParameter: TSTypeParameter,
typeAnnotation?: TSType,
nameType?: TSType
): TSMappedType
// Babel 8
declare function tsMappedType(
key: Identifier,
constraint: TSType,
nameType?: TSType,
typeAnnotation?: TSType
): TSMappedType迁移:参见以下示例。
¥Migration: See the example below.
my-babel-codemod.ts// To create { [P in string as Q]: number }
// Babel 7
t.tsMappedType(
t.tsTypeParameter(t.tsStringKeyword(), undefined, "P"),
t.tsNumberKeyword(),
t.tsTypeReference(t.identifier("Q"))
)
// Babel 8
t.tsMappedType(
t.identifier("P"),
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("Q")),
t.tsNumberKeyword()
) -
TSModuleDeclaration
的第二个参数 (body
) 不能再是TSModuleDeclaration
(#16982)¥The second argument (
body
) ofTSModuleDeclaration
cannot be aTSModuleDeclaration
anymore (#16982) -
t.tsModuleDeclaration
的第二个参数 (body
) 不能再是TSModuleDeclaration
节点 (#16982)¥The second argument (
body
) oft.tsModuleDeclaration
cannot be aTSModuleDeclaration
node anymore (#16982)迁移:创建一个导出
TSModuleDeclaration
的TSModuleBlock
主体,或者创建一个以TSQualifiedName
为名称的TSModuleDeclaration
。¥Migration: Either create a
TSModuleBlock
body that exports theTSModuleDeclaration
, or create a singleTSModuleDeclaration
that has aTSQualifiedName
as its name.my-babel-codemod.js// Option 1
// Create `namespace X.Y {}`
t.tsModuleDeclaration(
t.identifier("X"),
+ t.exportNamedDeclaration(
t.tsModuleDeclaration(
t.identifier("Y"),
t.tsModuleBlock([])
),
+ ),
)
```diff title="my-babel-codemod.js"
// Option 2
// Create `namespace X.Y {}`
t.tsModuleDeclaration(
+ t.tsQualifiedName(
t.identifier("X"),
- t.tsModuleDeclaration(
t.identifier("Y"),
+ ),
t.tsModuleBlock([])
- )
)
-
移除
t.jSX*
和t.tS*
构建器别名(#6989、#15527)¥Remove
t.jSX*
andt.tS*
builder aliases (#6989, #15527)迁移:改用
t.jsx*
和t.ts*
。例如,将t.jSXIdentifier("foo")
替换为t.jsxIdentifier("foo")
。¥Migration: Use
t.jsx*
andt.ts*
instead. For example, replacet.jSXIdentifier("foo")
witht.jsxIdentifier("foo")
. -
从
t.jsxElement
中移除selfClosing
参数(#14464)¥Remove
selfClosing
argument fromt.jsxElement
(#14464)- t.jsxElement(openingElement, closingElement, children, selfClosing?: boolean)
+ t.jsxElement(openingElement, closingElement, children)迁移:构建器中已不再使用
selfClosing
参数。你可以安全地将其移除。¥Migration: The
selfClosing
argument was already not used in the builder. You can safely remove it. -
从
t.memberExpression
中移除optional
参数(#13407)¥Remove
optional
argument fromt.memberExpression
(#13407)- t.memberExpression(object, property, computed, optional?: boolean)
+ t.memberExpression(object, property, computed)迁移:构建器中已不再使用
optional
参数。你可以安全地将其移除。¥Migration: The
optional
argument was already not used in the builder. You can safely remove it. -
删除
Noop
节点类型 (#12361)¥Remove the
Noop
node type (#12361)迁移:没有通用的迁移路径:你应该将其替换为
Noop
用作占位符的实际节点。如果你依赖Noop
并且没有其他选择,请提交问题并解释你的用例。¥Migration: There is no generic migration path: you should replace it with the actual node that
Noop
is being used as a placeholder for. If you are depending onNoop
and have no alternative, please open an issue and explain your use case. -
在
t.objectTypeAnnotation
(#14465)中,将节点ObjectTypeAnnotation
中的indexers
、callProperties
和internalSlots
初始化为空数组。¥Initialize
indexers
,callProperties
andinternalSlots
in the nodeObjectTypeAnnotation
as an empty array int.objectTypeAnnotation
(#14465)在 Babel 7 中,构建器
t.objectTypeAnnotation
将它们初始化为null
,这与@babel/parser
解析 Flow 对象类型注释的方式不一致。在 Babel 8 中,新的默认值[]
与解析器行为匹配。¥In Babel 7 the builder
t.objectTypeAnnotation
initializes them asnull
, this is inconsistent with how@babel/parser
will parse the Flow object type annotations. In Babel 8 the new default value[]
matches the parser behaviour. -
拒绝
t.numericLiteral
中的负数和 NaN/无穷大数(#15802)¥Reject negative and NaN/infinite numbers from
t.numericLiteral
(#15802)babel-plugin.js// NumericLiterals must be non-negative finite numbers.
t.numericLiteral(-1);迁移:要表示负数,请使用
t.unaryExpression("-", t.numericLiteral(1))
。要表示 NaN 或无穷大,请使用t.identifier("NaN")
。要将通用数值(也可以是负数、NaN 或无穷大)转换为正确的 AST 节点,请使用t.valueToNode(num)
。¥Migration: To represent a negative number, use
t.unaryExpression("-", t.numericLiteral(1))
. To represent NaN or Infinity, uset.identifier("NaN")
. To convert a generic numeric value (which could also be negative, NaN, or an inifinity) to the proper AST node, uset.valueToNode(num)
.
@babel/parser
除以下列出的更改外,@babel/parser
受所有 AST 变更 的影响。
¥Other than the changes listed below, @babel/parser
is affected by all the AST changes.
-
在 Flow 和 TypeScript 之间对齐 Babel 解析器错误代码 (#13294)
¥Align Babel parser error codes between Flow and TypeScript (#13294)
OptionalBindingPattern
的error.code
已重命名为PatternIsOptional
。¥The
error.code
forOptionalBindingPattern
is renamed asPatternIsOptional
. -
从选项
tokens: true
返回的tokens[].type
中移除updateContext
字段(#13768)¥Remove
updateContext
field fromtokens[].type
returned from optiontokens: true
(#13768)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("a = 42", { tokens: true });
tokens[0].type;
// Babel 7
// { label: "name", updateContext: null, ...other properties }
// Babel 8
// { label: "name", ... other properties }请注意,
tokens[].type
是一个不透明对象,用作 token 类型的标识,就像一个符号一样。其确切结构应作为内部实现细节。¥Note that
tokens[].type
is meant to be an opaque object used as an identity for token types, as if it was a symol. Its exact shape is meant to be an internal implementation detail. -
将私有名称 (
#priv
) 标记为单个privateName
标记 (#13256)¥Tokenize private names (
#priv
) as a singleprivateName
token (#13256)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("class C { #priv }", { tokens: true });
tokens.filter(t => t.start >= 10 && t.end <= 15) // get tokens for `#priv`
// Babel 7
// [
// Token (#) { value: "#", start: 10, end: 11 },
// Token (name) { value: "priv", start: 11, end: 15 }
// ]
// Babel 8
// [
// Token (privateName) { value: "priv", start: 10, end: 15 }
// ] -
将模板字面量标记为
templateNonTail
和templateTail
(#13919)¥Tokenize template literals as
templateNonTail
andtemplateTail
(#13919)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("`head${x}middle${y}tail`", { tokens: true });
console.log(tokens); // print tokens
// Babel 7
// [
// Token (`),
// Token (template) { value: "head" }, Token (${),
// Token (name) { value: "x" }, Token (}),
// Token (template) { value: "middle" }, Token (${),
// Token (name) { value: "y" }, Token (}),
// Token (template) { value: "tail" }
// Token (`)
// ]
// Babel 8
// [
// Token (templateNonTail) { value: "head" },
// Token (name) { value: "x" },
// Token (templateNonTail) { value: "middle" },
// Token (name) { value: "y" },
// Token (templateTail) { value: "tail" }
// ] -
从
ObjectProperty
节点中移除extra.shorthand
(#16521)¥Remove
extra.shorthand
fromObjectProperty
nodes (#16521)迁移:使用
node.shorthand
而不是node.extra.shorthand
。¥Migration: Use
node.shorthand
rather thannode.extra.shorthand
.
@babel/traverse
-
从
NodePath
(#16655) 中移除is
、isnt
、has
、equals
方法¥Remove
is
,isnt
,has
,equals
methods fromNodePath
(#16655)迁移:改为直接比较
path.node
的属性。¥Migration: Directly compare properties of
path.node
instead.- functionExpressionPath.equals("id", idNode)
+ functionExpressionPath.node.id === idNode
- functionExpressionPath.is("id")
- functionExpressionPath.has("id")
+ functionExpressionPath.node.id
- functionExpressionPath.has("arguments")
+ !!functionExpressionPath.node.arguments.length
- functionExpressionPath.isnt("async")
+ !functionExpressionPath.node.async -
移除
Scope.prototype.traverse
、Scope#parentBlock
和Scope#hub
(#16705)¥Remove
Scope.prototype.traverse
,Scope#parentBlock
andScope#hub
(#16705)迁移:改用
scope.path
的方法和属性:¥Migration: Use
scope.path
methods and properties instead:- scope.traverse(scopeRootNode, visitor, state)
+ scope.path.traverse(visitor, state)
- scope.parentBlock
+ scope.path.parent
- scope.hub
+ scope.path.hub -
移除
Scope.prototype.getAllBindingsOfKind
和Scope.prototype.toArray
(#16705)¥Remove
Scope.prototype.getAllBindingsOfKind
andScope.prototype.toArray
(#16705)这些方法已被删除,因为它们在我们的代码库中不再使用。
¥These methods have been removed as they are not used anymore in our code base.
迁移:你可以将它们从 Babel 7 的源代码复制粘贴到你的插件中。
¥Migration: You can copy&paste them from Babel 7's source to your plugin.
-
从
NodePath
(#16655) 中移除hoist
、updateSiblingKeys
、call
、setScope
、resync
、popContext
、pushContext
、setup
、setKey
方法¥Remove
hoist
,updateSiblingKeys
,call
,setScope
,resync
,popContext
,pushContext
,setup
,setKey
methods fromNodePath
(#16655)这些方法应为私有方法,因此没有真正的迁移方法。如果你的插件/构建因此更改而无法正常工作,请随时打开一个问题并告诉我们你如何使用这些方法,我们将在 Babel 8 发布后看看可以做些什么。
¥These methods are meant to be private so there is no real migration approach. If your plugin / build is broken by this change, feel free to open an issue and tell us how you use these methods and we can see what we can do after Babel 8 is released.
-
从
Scope#rename
中移除block
参数(#15288)¥Remove
block
argument fromScope#rename
(#15288)- rename(oldName: string, newName?: string, block?: t.Pattern | t.Scopable)
+ rename(oldName: string, newName?: string)迁移:不要将其他块传递给
scope.rename()
,而是直接在该块对应的作用域中调用.rename()
。¥Migration: Instead of passing a different block to
scope.rename()
, directly call.rename()
on the scope corresponding to that block. -
将
Scope#uids
和Scope#references
替换为Scope#uidsSet
和Scope#referencesSet
(#16624)¥Replace
Scope#uids
andScope#references
withScope#uidsSet
andScope#referencesSet
(#16624)它们已从布尔值对象更改为
Set
对象。此外,它们现在仅在程序作用域 (scope.getProgramParent()
) 上定义,而不是在所有作用域对象上定义。¥They have been changed from objects with boolean values to
Set
objects. In addition to that, they are now only defined on the program scope (scope.getProgramParent()
) rather than on all scope objects.- if (programScope.references["foo"])
+ if (programScope.referencesSet.has("foo")) -
允许将跳过的
NodePath
重新排队 (#13291)¥Allow skipped
NodePath
s to be requeued (#13291)NodePath#requeue()
可以重新排队跳过的 NodePath。这实际上是一个错误修复,但它会导致 Babel 7 中@babel/plugin-transform-block-scoping
的 暂时死区 实现出现无限循环:它也可能会破坏其他插件。¥
NodePath#requeue()
can requeue a skipped NodePath. This is actually a bugfix, but it causes an infinite loop in the temporal dead zone implementation of@babel/plugin-transform-block-scoping
in Babel 7: it may break other plugins as well.迁移:如果你想保留旧的行为,可以在调用
NodePath#requeue()
之前使用NodePath#shouldSkip
检查 NodePath 是否被跳过。¥Migration: If you want to preserve the old behavior, you can use
NodePath#shouldSkip
to check whether a NodePath has been skipped before callingNodePath#requeue()
. -
从
Scope
和NodePath
(#16504、#16705) 中移除以_
开头的方法¥Remove methods starting with
_
fromScope
andNodePath
(#16504, #16705)// NodePath.prototype
_assertUnremoved
_call
_callRemovalHooks
_containerInsert
_containerInsertAfter
_containerInsertBefore
_getKey
_getPattern
_getQueueContexts
_getTypeAnnotation
_markRemoved
_remove
_removeFromScope
_replaceWith
_resolve
_resyncKey
_resyncList
_resyncParent
_resyncRemoved
_verifyNodeList
// Scope.prototype
_renameFromMap
_generateUid这些方法应为私有方法,因此没有真正的迁移方法。如果你的插件/构建因此更改而无法正常工作,请随时打开一个问题并告诉我们你如何使用这些方法,我们将在 Babel 8 发布后看看可以做些什么。
¥These methods are meant to be private so there is no real migration approach. If your plugin / build is broken by this change, feel free to open an issue and tell us how you use these methods and we can see what we can do after Babel 8 is released.
@babel/eslint-plugin
-
删除 exports 对象的
default
属性 (#14180)¥Remove the
default
property of the exports object (#14180)迁移:如果你在 ESLint 配置中使用该插件,则此项变更无效。但是,如果你要扩展
@babel/eslint-plugin
,请确保从require("@babel/eslint-plugin")
而不是require("@babel/eslint-plugin").default
获取导出。¥Migration: This change has no effect if you are using the plugin in your ESLint config. However, if you are extending
@babel/eslint-plugin
, ensure that you obtain exports fromrequire("@babel/eslint-plugin")
rather thanrequire("@babel/eslint-plugin").default
.my-eslint-plugin.cjs// Don't add `.default` after `require()`
const { rules, rulesConfig } = require("@babel/eslint-plugin")
@babel/compat-data
-
将
plugins.json
(#14976)中的第 4 阶段插件条目从proposal-*
重命名为transform-*
¥Rename stage 4 plugin entries from
proposal-*
totransform-*
inplugins.json
(#14976)此项变更还会影响
@babel/helper-compilation-targets
的isRequired
函数,该函数接收插件名称作为其第一个参数。¥This change also affects the
isRequired
function of@babel/helper-compilation-targets
, which receives plugin names as its first parameter.迁移:例如,使用
transform-class-properties
而不是proposal-class-properties
。有关重命名插件的完整列表,请参阅 Babel 8 迁移的软件包重命名部分。¥Migration: For example, use
transform-class-properties
rather thanproposal-class-properties
. For a complete list of renamed plugin, see Packages Renames section of Babel 8 migration.my-babel-plugin.jsmodule.exports = api => {
const targets = api.targets();
// The targets have native optional chaining support
// if `transform-optional-chaining` is _not_ required
const optionalChainingSupported = !isRequired(
- "proposal-optional-chaining",
+ "transform-optional-chaining",
targets
);
}; -
直接将数据导出为 JSON 文件(#17267)。
¥Directly export data as JSON files (#17267)
软件包的入口点现在是 JSON 文件,而不是 JS 文件。
¥The entrypoints of the package are now JSON files, rather than JS files.
迁移:如果你从 ESM 导入此包,则需要更改导入,并在其中添加
with { type: "json" }
。¥Migration: If you are importing this package from ESM, you will need to change your imports adding
with { type: "json" }
to them.
-
从
data/native-modules.json
中移除ios_saf
(#15068)¥Remove
ios_saf
fromdata/native-modules.json
(#15068)迁移:请改用
ios
。¥Migration: Use
ios
instead.
@babel/helper-replace-supers
-
移除命名导出
environmentVisitor
(#15550)¥Remove named export
environmentVisitor
(#15550)迁移:从
@babel/helper-environment-visitor
导入。¥Migration: Import it from
@babel/helper-environment-visitor
.- import { environmentVisitor } from "@babel/helper-replace-supers";
+ import environmentVisitor from `@babel/helper-environment-visitor`; -
移除命名导出
skipAllButComputedKey
(#15550)¥Remove named export
skipAllButComputedKey
(#15550)迁移:请改用
requeueComputedKeyAndDecorators
。查找并替换以下导入和用法。¥Migration: Use
requeueComputedKeyAndDecorators
instead. Find and replace the following import and usagemy-babel7-plugin.jsimport { skipAllButComputedKey } from "@babel/helper-replace-supers";
skipAllButComputedKey(path);to
my-babel8-plugin.jsimport { requeueComputedKeyAndDecorators } from `@babel/helper-environment-visitor`;
path.skip();
requeueComputedKeyAndDecorators(path);
@babel/helper-simple-access
-
从默认导出 (#15550) 中删除第三个参数
includeUpdateExpression
¥Remove the the third parameter
includeUpdateExpression
from the default export (#15550)请注意,Babel 8 的行为与默认的 Babel 7 行为(即
includeUpdateExpression: true
)相同。¥Note that the Babel 8 behavior is the same as the default Babel 7 behavior (i.e.
includeUpdateExpression: true
).
@babel/highlight
-
删除
getChalk
函数 (https://github.com/babel/babel/pull/15812)¥Remove the
getChalk
function (https://github.com/babel/babel/pull/15812)如果你需要使用
chalk
,请将其添加到你的依赖中。¥If you need to use
chalk
, add it to your dependencies.
插件 API 变更
¥Plugin API changes
-
从插件传递中移除
getModuleName
(#12724)¥Remove
getModuleName
from plugin pass (#12724)迁移:请改用
.file.getModuleName
。¥Migration: Use
.file.getModuleName
instead.my-babel-plugin.jsmodule.exports = {
name: "my-babel-plugin",
visitor: {
Identifier(path, pass) {
- const moduleName = pass.getModuleName();
+ const moduleName = pass.file.getModuleName();
}
}
}
-
从插件传递中移除
addImport
(#15576)¥Remove
addImport
from plugin pass (#15576)此更改可能不会影响你的插件,因为此方法在 Babel 7 中已经引发错误。
¥This change probably will not affect your plugin as this method is already throwing an error in Babel 7.
迁移:改用
@babel/helper-module-imports
中的addNamed
或addDefault
。¥Migration: Use
addNamed
oraddDefault
from@babel/helper-module-imports
instead. -
停止支持将插件字段作为命名导出 (#15576)
¥Stop supporting plugin fields as named exports (#15576)
例如,以下文件不再是有效的插件:
¥For example, the following file is not a valid plugin anymore:
legacy-babel-plugin.jsexport const name = "legacy-babel-plugin";
export const visitor = {
Identifier() {}
}迁移:找到此类模式并使用默认导出,例如导出插件对象或工厂函数。
¥Migration: Find such patterns and use a default export instead, either exporting a plugin object or a factory function.
my-babel-plugin.cjsexport default {
name: "babel-plugin",
visitor: {
Identifier() {}
}
}