Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement proposal-class-static-block
- Loading branch information
Showing
22 changed files
with
252 additions
and
1 deletion.
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
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.0.0-0" | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import syntaxClassStaticBlock from "@babel/plugin-syntax-class-static-block"; | ||
|
||
export default declare(({ types: t, template, assertVersion }) => { | ||
assertVersion(7); | ||
|
||
return { | ||
name: "proposal-class-static-block", | ||
inherits: syntaxClassStaticBlock, | ||
visitor: { | ||
StaticBlock(path) { | ||
const staticBlockRef = path.scope.generateUidIdentifier("init"); | ||
const classPath = path.parentPath.parentPath; | ||
path.replaceWith( | ||
t.classMethod( | ||
"method", | ||
staticBlockRef, | ||
[], | ||
t.BlockStatement( | ||
// Add completion record. A static block can not contain directly | ||
// `return`, `break`, `continue`. So `return this` must be executed. | ||
path.node.body.concat([ | ||
template.ast`delete this.${t.cloneNode(staticBlockRef)};`, | ||
template.ast`return this;`, | ||
]), | ||
), | ||
/* computed */ false, | ||
/* static */ true, | ||
), | ||
); | ||
const classId = classPath.node.id; | ||
if (!classId) { | ||
// If `classId` is not defined, we don't have to preserve the class binding | ||
// `class { }` transformed as `(class { })._init()` | ||
classPath.replaceWith( | ||
template.ast`(${classPath.node}).${t.cloneNode(staticBlockRef)}()`, | ||
); | ||
} else { | ||
// If `classId` is defined, preserve the class binding. | ||
// `class Foo { }` transformed as `(Foo = (class Foo { })._init())` | ||
classPath.node.type = "ClassExpression"; | ||
classPath.replaceWith( | ||
template.ast`${t.cloneNode(classId)} = (${ | ||
classPath.node | ||
}).${t.cloneNode(staticBlockRef)}()`, | ||
); | ||
classPath.insertBefore(template.ast`let ${t.cloneNode(classId)};`); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
6 changes: 6 additions & 0 deletions
6
...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,6 @@ | ||
class Foo { | ||
static { | ||
this.foo = 42; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
6 changes: 6 additions & 0 deletions
6
...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,6 @@ | ||
class Foo { | ||
static { | ||
this.foo = 42; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
10 changes: 10 additions & 0 deletions
10
...-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,10 @@ | ||
let Foo; | ||
Foo = class Foo { | ||
static _init() { | ||
this.foo = 42; | ||
delete this._init; | ||
return this; | ||
} | ||
|
||
}._init(); | ||
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); |
3 changes: 3 additions & 0 deletions
3
...roposal-class-static-block/test/fixtures/class-static-block/class-expression/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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-static-block", "syntax-class-properties"] | ||
} |
10 changes: 10 additions & 0 deletions
10
...posal-class-static-block/test/fixtures/class-static-block/in-class-heritage-named/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,10 @@ | ||
class Foo extends class { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = 2 * this.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
10 changes: 10 additions & 0 deletions
10
...osal-class-static-block/test/fixtures/class-static-block/in-class-heritage-named/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,10 @@ | ||
class Foo extends class Bar { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = 2 * this.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
18 changes: 18 additions & 0 deletions
18
...sal-class-static-block/test/fixtures/class-static-block/in-class-heritage-named/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,18 @@ | ||
let Foo; | ||
let Bar; | ||
Foo = class Foo extends (Bar = class Bar { | ||
static _init2() { | ||
this.bar = 21; | ||
delete this._init2; | ||
return this; | ||
} | ||
|
||
}._init2()) { | ||
static _init() { | ||
this.foo = 2 * this.bar; | ||
delete this._init; | ||
return this; | ||
} | ||
|
||
}._init(); | ||
expect(Foo.foo).toBe(42); |
10 changes: 10 additions & 0 deletions
10
...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,10 @@ | ||
class Foo extends class { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = 2 * this.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
10 changes: 10 additions & 0 deletions
10
...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,10 @@ | ||
class Foo extends class { | ||
static { | ||
this.bar = 21; | ||
} | ||
} { | ||
static { | ||
this.foo = 2 * this.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
17 changes: 17 additions & 0 deletions
17
...-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,17 @@ | ||
let Foo; | ||
Foo = class Foo extends class { | ||
static _init2() { | ||
this.bar = 21; | ||
delete this._init2; | ||
return this; | ||
} | ||
|
||
}._init2() { | ||
static _init() { | ||
this.foo = 2 * this.bar; | ||
delete this._init; | ||
return this; | ||
} | ||
|
||
}._init(); | ||
expect(Foo.foo).toBe(42); |
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); |
4 changes: 4 additions & 0 deletions
4
...-class-static-block/test/fixtures/class-static-block/private-elements-access/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" | ||
} |
11 changes: 11 additions & 0 deletions
11
...lugin-proposal-class-static-block/test/fixtures/class-static-block/super-property/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,11 @@ | ||
class Foo extends class { | ||
static { | ||
this.bar = 42; | ||
} | ||
} { | ||
static bar = 21; | ||
static { | ||
this.foo = super.bar; | ||
} | ||
} | ||
expect(Foo.foo).toBe(42); |
3 changes: 3 additions & 0 deletions
3
...-proposal-class-static-block/test/fixtures/class-static-block/super-property/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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-static-block", "syntax-class-properties"] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-class-static-block/test/fixtures/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,3 @@ | ||
{ | ||
"plugins": ["proposal-class-static-block"] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-class-static-block/test/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,3 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
|
||
runner(__dirname); |
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