Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform class static block (#12143)
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com> Co-authored-by: Brian Ng <bng412@gmail.com>
- Loading branch information
1 parent
c76b519
commit 413892f
Showing
85 changed files
with
857 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 13 additions & 17 deletions
30
packages/babel-helper-module-transforms/src/rewrite-this.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
import { skipAllButComputedKey } from "@babel/helper-replace-supers"; | ||
import { environmentVisitor } from "@babel/helper-replace-supers"; | ||
import traverse from "@babel/traverse"; | ||
import * as t from "@babel/types"; | ||
|
||
export default function rewriteThis(programPath: NodePath) { | ||
// Rewrite "this" to be "undefined". | ||
programPath.traverse(rewriteThisVisitor); | ||
traverse(programPath.node, { ...rewriteThisVisitor, noScope: true }); | ||
} | ||
|
||
/** | ||
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be | ||
* `undefined`. | ||
* `void 0` (undefined). | ||
*/ | ||
const rewriteThisVisitor = { | ||
ThisExpression(path) { | ||
path.replaceWith(path.scope.buildUndefinedNode()); | ||
}, | ||
Function(path) { | ||
if (path.isMethod()) skipAllButComputedKey(path); | ||
else if (!path.isArrowFunctionExpression()) path.skip(); | ||
}, | ||
ClassProperty(path) { | ||
skipAllButComputedKey(path); | ||
}, | ||
ClassPrivateProperty(path) { | ||
path.skip(); | ||
const rewriteThisVisitor = traverse.visitors.merge([ | ||
environmentVisitor, | ||
{ | ||
ThisExpression(path) { | ||
path.replaceWith(t.unaryExpression("void", t.numericLiteral(0), true)); | ||
}, | ||
}, | ||
}; | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
19 changes: 19 additions & 0 deletions
19
packages/babel-plugin-proposal-class-static-block/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @babel/plugin-proposal-class-static-block | ||
|
||
> Allow transforming of class static blocks | ||
See our website [@babel/plugin-proposal-class-static-block](https://babeljs.io/docs/en/next/babel-plugin-proposal-class-static-block.html) for more information. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-proposal-class-static-block | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/plugin-proposal-class-static-block --dev | ||
``` |
28 changes: 28 additions & 0 deletions
28
packages/babel-plugin-proposal-class-static-block/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@babel/plugin-proposal-class-static-block", | ||
"version": "7.11.0", | ||
"description": "Allow parsing of class static blocks", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-plugin-proposal-class-static-block" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js" | ||
}, | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"@babel/helper-plugin-utils": "workspace:^7.10.1", | ||
"@babel/plugin-syntax-class-static-block": "workspace:^7.11.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.12.0" | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/babel-plugin-proposal-class-static-block/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block"; | ||
|
||
/** | ||
* Generate a uid that is not in `denyList` | ||
* | ||
* @param {*} scope | ||
* @param {Set<string>} a deny list that the generated uid should avoid | ||
* @returns | ||
*/ | ||
function generateUid(scope, denyList: Set<string>) { | ||
const name = ""; | ||
let uid; | ||
let i = 1; | ||
do { | ||
uid = scope._generateUid(name, i); | ||
i++; | ||
} while (denyList.has(uid)); | ||
return uid; | ||
} | ||
|
||
export default declare(({ types: t, template, assertVersion }) => { | ||
// todo: change to ^7.12.0 when it is published | ||
assertVersion("^7.11.6"); | ||
|
||
return { | ||
name: "proposal-class-static-block", | ||
inherits: syntaxClassStaticBlock, | ||
visitor: { | ||
Class(path: NodePath<Class>) { | ||
const { scope } = path; | ||
const classBody = path.get("body"); | ||
const privateNames = new Set(); | ||
let staticBlockPath; | ||
for (const path of classBody.get("body")) { | ||
if (path.isPrivate()) { | ||
privateNames.add(path.get("key.id").node.name); | ||
} else if (path.isStaticBlock()) { | ||
staticBlockPath = path; | ||
} | ||
} | ||
if (!staticBlockPath) { | ||
return; | ||
} | ||
const staticBlockRef = t.privateName( | ||
t.identifier(generateUid(scope, privateNames)), | ||
); | ||
classBody.pushContainer( | ||
"body", | ||
t.classPrivateProperty( | ||
staticBlockRef, | ||
template.expression.ast`(() => { ${staticBlockPath.node.body} })()`, | ||
[], | ||
/* static */ true, | ||
), | ||
); | ||
staticBlockPath.remove(); | ||
}, | ||
}, | ||
}; | ||
}); |
7 changes: 7 additions & 0 deletions
7
...plugin-proposal-class-static-block/test/fixtures/class-static-block/class-binding/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
static { | ||
this.foo = Foo.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
7 changes: 7 additions & 0 deletions
7
...in-proposal-class-static-block/test/fixtures/class-static-block/class-declaration/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
7 changes: 7 additions & 0 deletions
7
...n-proposal-class-static-block/test/fixtures/class-static-block/class-declaration/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
static { | ||
this.foo = this.bar; | ||
} | ||
static bar = 42; | ||
} | ||
expect(Foo.foo).toBe(42); |
8 changes: 8 additions & 0 deletions
8
...-proposal-class-static-block/test/fixtures/class-static-block/class-declaration/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
static bar = 42; | ||
static #_ = (() => { | ||
this.foo = this.bar; | ||
})(); | ||
} | ||
|
||
expect(Foo.foo).toBe(42); |
6 changes: 6 additions & 0 deletions
6
...gin-proposal-class-static-block/test/fixtures/class-static-block/class-expression/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Foo { | ||
static { | ||
this.foo = 42; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
14 changes: 14 additions & 0 deletions
14
...in-proposal-class-static-block/test/fixtures/class-static-block/in-class-heritage/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Foo extends class extends class Base { | ||
static { | ||
this.qux = 21; | ||
} | ||
} { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = this.bar + this.qux; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
14 changes: 14 additions & 0 deletions
14
...n-proposal-class-static-block/test/fixtures/class-static-block/in-class-heritage/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Foo extends class extends class Base { | ||
static { | ||
this.qux = 21; | ||
} | ||
} { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = this.bar + this.qux; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
15 changes: 15 additions & 0 deletions
15
...-proposal-class-static-block/test/fixtures/class-static-block/in-class-heritage/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Foo extends class extends class Base { | ||
static #_ = (() => { | ||
this.qux = 21; | ||
})(); | ||
} { | ||
static #_ = (() => { | ||
this.bar = 21; | ||
})(); | ||
} { | ||
static #_ = (() => { | ||
this.foo = this.bar + this.qux; | ||
})(); | ||
} | ||
|
||
expect(Foo.foo).toBe(42); |
8 changes: 8 additions & 0 deletions
8
...-class-static-block/test/fixtures/class-static-block/multiple-static-initializers/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
static #bar = 21; | ||
static { | ||
this.foo = this.#bar + this.qux; | ||
} | ||
static qux = 21; | ||
} | ||
expect(Foo.foo).toBe(42); |
8 changes: 8 additions & 0 deletions
8
...plugin-proposal-class-static-block/test/fixtures/class-static-block/name-conflict/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
static #_ = 42; | ||
// static block can not be tranformed as `#_` here | ||
static { | ||
this.foo = this.#_; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
8 changes: 8 additions & 0 deletions
8
...lugin-proposal-class-static-block/test/fixtures/class-static-block/name-conflict/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
static #_ = 42; | ||
// static block can not be tranformed as `#_` here | ||
static { | ||
this.foo = this.#_; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
9 changes: 9 additions & 0 deletions
9
...ugin-proposal-class-static-block/test/fixtures/class-static-block/name-conflict/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Foo { | ||
static #_ = 42; // static block can not be tranformed as `#_` here | ||
|
||
static #_2 = (() => { | ||
this.foo = this.#_; | ||
})(); | ||
} | ||
|
||
expect(Foo.foo).toBe(42); |
7 changes: 7 additions & 0 deletions
7
...el-plugin-proposal-class-static-block/test/fixtures/class-static-block/non-static/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
bar = 21; | ||
static { | ||
this.foo = this.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(undefined); |
4 changes: 4 additions & 0 deletions
4
...es/babel-plugin-proposal-class-static-block/test/fixtures/class-static-block/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": ["proposal-class-static-block", "syntax-class-properties"], | ||
"minNodeVersion": "12.0.0" | ||
} |
8 changes: 8 additions & 0 deletions
8
...posal-class-static-block/test/fixtures/class-static-block/private-elements-access/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
let getFoo; | ||
class Foo { | ||
static #foo = 42; | ||
static { | ||
getFoo = () => this.#foo; | ||
} | ||
} | ||
expect(getFoo()).toBe(42); |
Oops, something went wrong.