Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plugin-handbook.md #263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
135 changes: 73 additions & 62 deletions translations/zh-Hans/plugin-handbook.md
Expand Up @@ -1311,7 +1311,7 @@ path.getStatementParent();
* 使用 `path.container`获取路径的容器(包含所有同级节点的数组)
* 使用 `path.listKey`获取容器的key

> 这些API用于 babel-minify </>中使用的 transform-merge-sibling-variables </>插件.</p> </blockquote>
> 这些API用于 babel-minify `中使用的 transform-merge-sibling-variables `插件.</p> </blockquote>
>
> ```js
var a = 1; // pathA, path.key = 0
Expand Down Expand Up @@ -1403,11 +1403,11 @@ ReturnStatement(path) {
}
```

> **注意:</>当用多个节点替换一个表达式时,它们必须是   声明。 这是因为Babel在更换节点时广泛使用启发式算法,这意味着您可以做一些非常疯狂的转换,否则将会非常冗长。</p> </blockquote>
>
> ### <a id="toc-replacing-a-node-with-a-source-string"></a>用字符串源码替换节点
>
> ```js
> **注意:`当用多个节点替换一个表达式时,它们必须是   声明。 这是因为Babel在更换节点时广泛使用启发式算法,这意味着您可以做一些非常疯狂的转换,否则将会非常冗长。

### <a id="toc-replacing-a-node-with-a-source-string"></a>用字符串源码替换节点

```js
FunctionDeclaration(path) {
path.replaceWithSourceString(`function add(a, b) {
return a + b;
Expand All @@ -1423,11 +1423,11 @@ FunctionDeclaration(path) {
}
```

> **注意:</>不建议使用这个API,除非您正在处理动态的源码字符串,否则在访问者外部解析代码更有效率。</p> </blockquote>
>
> ### <a id="toc-inserting-a-sibling-node"></a>插入兄弟节点
>
> ```js
> **注意:`不建议使用这个API,除非您正在处理动态的源码字符串,否则在访问者外部解析代码更有效率。

### <a id="toc-inserting-a-sibling-node"></a>插入兄弟节点

```js
FunctionDeclaration(path) {
path.insertBefore(t.expressionStatement(t.stringLiteral("Because I'm easy come, easy go.")));
path.insertAfter(t.expressionStatement(t.stringLiteral("A little high, little low.")));
Expand All @@ -1442,14 +1442,14 @@ FunctionDeclaration(path) {
+ "A little high, little low.";
```

> 注意:</>这里同样应该使用声明或者一个声明数组。 这个使用了在用多个节点替换一个节点</>中提到的相同的启发式算法。.</p> </blockquote>
>
> ### <a id="toc-inserting-into-a-container"></a>插入到容器(container)中
>
> 如果您想要在AST节点属性中插入一个像` body </ 0>那样的数组。
它与 <code> insertBefore `/` insertAfter ` 类似, 但您必须指定 ` listKey ` (通常是 ` 正文 `).
>
> ```js
> 注意:`这里同样应该使用声明或者一个声明数组。 这个使用了在用多个节点替换一个节点`中提到的相同的启发式算法。

### <a id="toc-inserting-into-a-container"></a>插入到容器(container)中

> 如果您想要在AST节点属性中插入一个像` body `那样的数组。
它与 ` insertBefore `/` insertAfter ` 类似, 但您必须指定 ` listKey ` (通常是 ` 正文 `).

```js
ClassMethod(path) {
path.get('body').unshiftContainer('body', t.expressionStatement(t.stringLiteral('before')));
path.get('body').pushContainer('body', t.expressionStatement(t.stringLiteral('after')));
Expand Down Expand Up @@ -1480,16 +1480,17 @@ FunctionDeclaration(path) {
- }
```

### <a id="toc-replacing-a-parent"></a>替换父节点
### <a id="toc-replacing-a-parent">替换父节点</a>

只需使用parentPath:` path.parentPath </>调用<code> replaceWith </>即可</p>
只需使用parentPath:` path.parentPath `调用` replaceWith `即可

<pre><code class="js">BinaryExpression(path) {
```js
BinaryExpression(path) {
path.parentPath.replaceWith(
t.expressionStatement(t.stringLiteral("Anyway the wind blows, doesn't really matter to me, to me."))
);
}
`</pre>
```

```diff
function square(n) {
Expand Down Expand Up @@ -1526,7 +1527,7 @@ FunctionDeclaration(path) {

这将遍历范围树并检查特定的绑定。

您也可以检查一个作用域是否有**自己的</>绑定:</p>
您也可以检查一个作用域是否有**自己的`绑定:</p>

```js
FunctionDeclaration(path) {
Expand All @@ -1551,14 +1552,15 @@ FunctionDeclaration(path) {

### <a id="toc-pushing-a-variable-declaration-to-a-parent-scope"></a>提升变量声明至父级作用域

有时你可能想要推送一个` VariableDeclaration </>,这样你就可以分配给它。</p>
有时你可能想要推送一个` VariableDeclaration `,这样你就可以分配给它。</p>

<pre><code class="js">FunctionDeclaration(path) {
```js
FunctionDeclaration(path) {
const id = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.remove();
path.scope.parent.push({ id, init: path.node });
}
`</pre>
```

```diff
- function square(n) {
Expand Down Expand Up @@ -1617,9 +1619,10 @@ FunctionDeclaration(path) {
}
```

这些选项会通过`状态</>对象传递给插件访问者:</p>
这些选项会通过`状态`对象传递给插件访问者:</p>

<pre><code class="js">export default function({ types: t }) {
```js
export default function({ types: t }) {
return {
visitor: {
FunctionDeclaration(path, state) {
Expand All @@ -1629,7 +1632,7 @@ FunctionDeclaration(path) {
}
}
}
`</pre>
```

这些选项是特定于插件的,您不能访问其他插件中的选项。

Expand Down Expand Up @@ -1657,7 +1660,7 @@ export default function({ types: t }) {

## <a id="toc-enabling-syntax-in-plugins"></a> 在插件中启用其他语法

插件可以启用babylon plugins</>,以便用户不需要安装/启用它们。 这可以防止解析错误,而不会继承语法插件。</p>
插件可以启用babylon plugins`,以便用户不需要安装/启用它们。 这可以防止解析错误,而不会继承语法插件。</p>

```js
export default function({ types: t }) {
Expand Down Expand Up @@ -1699,15 +1702,16 @@ export default function({ types: t }) {

# <a id="toc-building-nodes"></a>构建节点

编写转换时,通常需要构建一些要插入的节点进入AST。 如前所述,您可以使用` babel-types </>包中的<a href="#builders">builder </>方法。</p>
编写转换时,通常需要构建一些要插入的节点进入AST。 如前所述,您可以使用 ` babel-types` 包中的<a href="#builders">builder </a>方法。

<p>构建器的方法名称就是您想要的节点类型的名称,除了第一个字母小写。 例如,如果您想建立一个<code> MemberExpression </>您可以使用<code> t.memberExpression(...)</>.</p>
<p>构建器的方法名称就是您想要的节点类型的名称,除了第一个字母小写。 例如,如果您想建立一个` MemberExpression `您可以使用` t.memberExpression(...)`.</p>

<p>这些构建器的参数由节点定义决定。 有一些正在做的工作,以生成易于阅读的文件定义,但现在他们都可以在<a href="https://github.com/babel/babel/tree/master/packages/babel-types/src/definitions">此处</a>找到。.</p>

<p>节点定义如下所示:</p>

<pre><code class="js">defineType("MemberExpression", {
```js
defineType("MemberExpression", {
builder: ["object", "property", "computed"],
visitor: ["object", "property"],
aliases: ["Expression", "LVal"],
Expand All @@ -1726,7 +1730,7 @@ export default function({ types: t }) {
}
}
});
`</pre>
```

在这里你可以看到关于这个特定节点类型的所有信息,包括如何构建它,遍历它,并验证它。

Expand All @@ -1736,10 +1740,11 @@ export default function({ types: t }) {
生成器: ["object", "property", "computed"],
```

> 请注意,有时在节点上可以定制的属性比``构建器</>数组包含的属性更多。 这是为了防止生成器有太多的参数。 在这些情况下,您需要手动设置属性。 一个例子是<class> ClassMethod </>.</p>
> 请注意,有时在节点上可以定制的属性比``构建器`数组包含的属性更多。 这是为了防止生成器有太多的参数。 在这些情况下,您需要手动设置属性。 一个例子是<class> ClassMethod `.</p>
</blockquote>

<pre><code class="js">// Example
```js
// Example
// because the builder doesn't contain `async` as a property
var node = t.classMethod(
"constructor",
Expand All @@ -1749,7 +1754,7 @@ var node = t.classMethod(
)
// set it manually after creation
node.async = true;
``</pre>
````
>
> You can see the validation for the builder arguments with the `fields` object.
>
Expand Down Expand Up @@ -1919,14 +1924,15 @@ const MyVisitor = {
};
```

但是,每当调用`FunctionDeclaration()</>时都会创建一个新的访问者对象。 That can be costly, because Babel does some processing each time a new
但是,每当调用`FunctionDeclaration()`时都会创建一个新的访问者对象。 That can be costly, because Babel does some processing each time a new
visitor object is passed in (such as exploding keys containing multiple types,
performing validation, and adjusting the object structure). Because Babel stores
flags on visitor objects indicating that it's already performed that processing,
it's better to store the visitor in a variable and pass the same object each
time.</p>

<pre><code class="js">const nestedVisitor = {
```js
const nestedVisitor = {
Identifier(path) {
// ...
}
Expand All @@ -1937,7 +1943,7 @@ const MyVisitor = {
path.traverse(nestedVisitor);
}
};
`</pre>
```

如果您在嵌套的访问者中需要一些状态,像这样:

Expand All @@ -1957,7 +1963,7 @@ const MyVisitor = {
};
```

您可以将它作为状态传递给`traverse()</ 0>方法,并有权访问<code>this`在访问者中。
您可以将它作为状态传递给`traverse()</ 0>方法,并有权访问`this`在访问者中。

```js
const nestedVisitor = {
Expand Down Expand Up @@ -2008,9 +2014,10 @@ const MyVisitor = {
}
```

我们忽略了类可以嵌套的事实,使用遍历的话,上面我们也会得到一个嵌套的`构造函数</>:</p>
我们忽略了类可以嵌套的事实,使用遍历的话,上面我们也会得到一个嵌套的`构造函数`:</p>

<pre><code class="js">class Foo {
```js
class Foo {
constructor() {
class Bar {
constructor() {
Expand All @@ -2019,11 +2026,11 @@ const MyVisitor = {
}
}
}
`</pre>
```

## <a id="toc-unit-testing"></a>单元测试

有几种主要的方法来测试babel插件:快照测试,AST测试和执行测试。 对于这个例子,我们将使用 jest </>,因为它支持盒外快照测试。 我们在这里创建的示例是托管在这个 repo</>.</p>
有几种主要的方法来测试babel插件:快照测试,AST测试和执行测试。 对于这个例子,我们将使用 jest `,因为它支持盒外快照测试。 我们在这里创建的示例是托管在这个 repo`.</p>

首先我们需要一个babel插件,我们将把它放在src / index.js中。

Expand All @@ -2043,10 +2050,11 @@ const MyVisitor = {

### 快照测试

接下来,用`` npm install --save-dev babel-core jest </>安装我们的依赖关系,
那么我们可以开始写我们的第一个测试:快照。 快照测试允许我们直观地检查我们的babel插件的输出。 我们给它一个输入,告诉它一个快照,并将其保存到一个文件。 我们检查快照到git中。 这允许我们来看看我们什么时候影响了我们任何试用例子测试的输出。 它也给出了使用差异在拉请求的时候。 当然,您可以用任何测试框架来做到这一点,但是要更新一下快照就像<code>jest -u </>一样简单.</p>
接下来,用`npm install --save-dev babel-core jest `安装我们的依赖关系,
那么我们可以开始写我们的第一个测试:快照。 快照测试允许我们直观地检查我们的babel插件的输出。 我们给它一个输入,告诉它一个快照,并将其保存到一个文件。 我们检查快照到git中。 这允许我们来看看我们什么时候影响了我们任何试用例子测试的输出。 它也给出了使用差异在拉请求的时候。 当然,您可以用任何测试框架来做到这一点,但是要更新一下快照就像`jest -u `一样简单.</p>

<pre><code class="js">// src/__tests__/index-test.js
```js
// src/__tests__/index-test.js
const babel = require('babel-core');
const plugin = require('../');

Expand All @@ -2059,16 +2067,17 @@ it('works', () => {
const {code} = babel.transform(example, {plugins: [plugin]});
expect(code).toMatchSnapshot();
});
``</pre>
````

这给了我们一个快照文件在`` src / __ tests __ / __ snapshots __ / index-test.js.snap </>.</p>
这给了我们一个快照文件在`src / __ tests __ / __ snapshots __ / index-test.js.snap `.</p>

<pre><code class="js">exports[`test works 1`] = `
```js
exports[`test works 1`] = `
"
var bar = 1;
if (bar) console.log(bar);"
`;
``</pre>
````

如果我们在插件中将“bar”更改为“baz”并再次运行,则可以得到以下结果:

Expand All @@ -2086,26 +2095,28 @@ if (bar) console.log(bar);"
+if (baz) console.log(baz);"
```

我们看到我们对插件代码的改变如何影响了我们插件的输出 如果输出看起来不错,我们可以运行`jest -u </>来更新快照。</p>
我们看到我们对插件代码的改变如何影响了我们插件的输出 如果输出看起来不错,我们可以运行`jest -u `来更新快照。</p>

<h3>AST 测试</h3>

<p>除了快照测试外,我们还可以手动检查AST。 这是一个简单但是脆弱的例子。 对于更多涉及的情况,您可能希望利用Babel-遍历。 它允许您用<code>访问者</>键指定一个对象,就像您使用插件本身。</p>
<p>除了快照测试外,我们还可以手动检查AST。 这是一个简单但是脆弱的例子。 对于更多涉及的情况,您可能希望利用Babel-遍历。 它允许您用`访问者`键指定一个对象,就像您使用插件本身。</p>

<pre><code class="js">it('contains baz', () => {
```js
it('contains baz', () => {
const {ast} = babel.transform(example, {plugins: [plugin]});
const program = ast.program;
const declaration = program.body[0].declarations[0];
assert.equal(declaration.id.name, 'baz');
// or babelTraverse(program, {visitor: ...})
});
`</pre>
```

### Exec Tests

在这里,我们将转换代码,然后评估它的行为是否正确。 请注意,我们在测试中没有使用``assert</>。 这确保如果我们的插件做了奇怪的操作,如意外删除断言线,但测试仍然失败。</p>
在这里,我们将转换代码,然后评估它的行为是否正确。 请注意,我们在测试中没有使用``assert`。 这确保如果我们的插件做了奇怪的操作,如意外删除断言线,但测试仍然失败。</p>

<pre><code class="js">it('foo is an alias to baz', () => {
```js
it('foo is an alias to baz', () => {
var input = `
var foo = 1;
// test that foo was renamed to baz
Expand All @@ -2119,9 +2130,9 @@ if (bar) console.log(bar);"
var res = f();
assert(res === 1, 'res is 1');
});
``</pre>
````

Babel核心使用类似的方法</>去获取快照和执行测试。</p>
Babel核心使用类似的方法`去获取快照和执行测试。</p>

### [`babel-plugin-tester`](https://github.com/kentcdodds/babel-plugin-tester)

Expand Down Expand Up @@ -2158,4 +2169,4 @@ pluginTester({

* * *

> ***对于将来的更新,请跟随 @thejameskyle </>和 @babeljs </> 的Twitter。</em></strong></p> </blockquote>
> **对于将来的更新,请跟随 @thejameskyle `和 @babeljs ` 的Twitter。**