Remove "class-constructor-call" syntax and transform plugins (#5119)
* removed class-constructor-call syntax and transform plugins and all references to them * #5112, removed another reference to constructor call
This commit is contained in:
parent
d0b42d4313
commit
1aa7a2a1a3
@ -81,10 +81,5 @@ export function ClassMethod(node: Object) {
|
||||
this.space();
|
||||
}
|
||||
|
||||
if (node.kind === "constructorCall") {
|
||||
this.word("call");
|
||||
this.space();
|
||||
}
|
||||
|
||||
this._method(node);
|
||||
}
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
@ -1,41 +0,0 @@
|
||||
# babel-plugin-syntax-class-constructor-call (deprecated)
|
||||
|
||||
> Proposal Withdrawn: can be solved with decorators.
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev babel-plugin-syntax-class-constructor-call
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["syntax-class-constructor-call"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins syntax-class-constructor-call script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["syntax-class-constructor-call"]
|
||||
});
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
* [Inactive Proposals](https://github.com/tc39/proposals/blob/master/inactive-proposals.md)
|
||||
* [Proposal: Call Constructor](https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md)
|
||||
* [Blog post: ECMAScript proposal: function-callable classes](http://www.2ality.com/2015/10/call-constructor-esprop.html)
|
||||
@ -1,13 +0,0 @@
|
||||
{
|
||||
"name": "babel-plugin-syntax-class-constructor-call",
|
||||
"version": "6.18.0",
|
||||
"description": "Allow parsing of class constructor calls (deprecated)",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-class-constructor-call",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {},
|
||||
"devDependencies": {}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
export default function () {
|
||||
return {
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
parserOpts.plugins.push("classConstructorCall");
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
test
|
||||
@ -1,101 +0,0 @@
|
||||
# babel-plugin-transform-class-constructor-call (deprecated)
|
||||
|
||||
> Proposal Withdrawn: can be solved with decorators.
|
||||
|
||||
This plugin allows Babel to transform class constructors.
|
||||
|
||||
It basically allows to use the [new.target](http://mdn.io/new.target) feature on ES2015 classes:
|
||||
|
||||
```js
|
||||
class Point {
|
||||
|
||||
constructor(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
call constructor(x, y) {
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let p1 = new Point(1, 2); // OK
|
||||
let p2 = Point(3, 4); // OK
|
||||
```
|
||||
[Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=class%20Point%20%7B%0A%0A%20%20constructor(x%2C%20y)%20%7B%0A%20%20%20%20this.x%20%3D%20x%3B%0A%20%20%20%20this.y%20%3D%20y%3B%0A%20%20%7D%0A%0A%20%20call%20constructor(x%2C%20y)%20%7B%0A%20%20%20%20return%20new%20Point(x%2C%20y)%3B%0A%20%20%7D%0A%0A%7D%0A%0Alet%20p1%20%3D%20new%20Point(1%2C%202)%3B%20%2F%2F%20OK%0Alet%20p2%20%3D%20Point(3%2C%204)%3B%20%2F%2F%20OK)
|
||||
|
||||
## Example
|
||||
|
||||
### Date example
|
||||
The javascript [Date](http://mdn.io/date) works this way:
|
||||
|
||||
```js
|
||||
// You can get a Date instance using the new keyword
|
||||
let now = new Date();
|
||||
console.log(now.getMonth()); // Prints '3'
|
||||
console.log(now.toString()); // Prints 'Mon Apr 11 2016 13:26:07 GMT+0100 (BST)'
|
||||
|
||||
// You can get a string of the current date using Date as a function:
|
||||
let nowStr = Date();
|
||||
console.log(nowStr); // Prints 'Mon Apr 11 2016 13:26:07 GMT+0100 (BST)'
|
||||
```
|
||||
|
||||
It is currently possible to implement something like that using [new.target](http://mdn.io/new.target) (see [example in proposal](https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md#motivating-example)) and this new feature makes it available for ES2015 classes.
|
||||
|
||||
A date implementation could be:
|
||||
|
||||
```js
|
||||
class Date {
|
||||
constructor() {
|
||||
// ...
|
||||
}
|
||||
|
||||
call constructor() {
|
||||
let date = new Date();
|
||||
return date.toString();
|
||||
}
|
||||
}
|
||||
|
||||
let now = new Date(); // Get a Date instance
|
||||
let nowStr = Date(); // Use the 'call constructor()' part to get a string value of the current date
|
||||
```
|
||||
[Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=class%20Date%20%7B%0A%20%20constructor()%20%7B%0A%20%20%20%20%2F%2F%20...%0A%20%20%7D%0A%0A%20%20call%20constructor()%20%7B%0A%20%20%20%20let%20date%20%3D%20new%20Date()%3B%0A%20%20%20%20return%20date.toString()%3B%0A%20%20%7D%0A%7D%0A%0Alet%20now%20%3D%20new%20Date()%3B%20%2F%2F%20Get%20a%20Date%20instance%0Alet%20nowStr%20%3D%20Date()%3B%20%2F%2F%20Use%20the%20'call%20constructor()'%20part%20to%20get%20a%20string%20value%20of%20the%20current%20date)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev babel-plugin-transform-class-constructor-call
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["transform-class-constructor-call"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins transform-class-constructor-call script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["transform-class-constructor-call"]
|
||||
});
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
* [Inactive Proposals](https://github.com/tc39/proposals/blob/master/inactive-proposals.md)
|
||||
* [Proposal: Call Constructor](https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md)
|
||||
* [Blog post: ECMAScript proposal: function-callable classes](http://www.2ality.com/2015/10/call-constructor-esprop.html)
|
||||
@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-class-constructor-call",
|
||||
"version": "6.22.0",
|
||||
"description": "This plugin allows Babel to transform class constructors (deprecated)",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-class-constructor-call",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"babel-template": "^6.22.0",
|
||||
"babel-plugin-syntax-class-constructor-call": "^6.18.0",
|
||||
"babel-runtime": "^6.22.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-helper-plugin-test-runner": "^6.22.0"
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
import template from "babel-template";
|
||||
|
||||
const buildWrapper = template(`
|
||||
let CLASS_REF = CLASS;
|
||||
var CALL_REF = CALL;
|
||||
var WRAPPER_REF = function (...args) {
|
||||
if (this instanceof WRAPPER_REF) {
|
||||
return Reflect.construct(CLASS_REF, args);
|
||||
} else {
|
||||
return CALL_REF.apply(this, args);
|
||||
}
|
||||
};
|
||||
WRAPPER_REF.__proto__ = CLASS_REF;
|
||||
WRAPPER_REF;
|
||||
`);
|
||||
|
||||
export default function ({ types: t }) {
|
||||
const ALREADY_VISITED = Symbol();
|
||||
|
||||
function findConstructorCall(path): ?Object {
|
||||
const methods: Array<Object> = path.get("body.body");
|
||||
|
||||
for (const method of methods) {
|
||||
if (method.node.kind === "constructorCall") {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function handleClassWithCall(constructorCall, classPath) {
|
||||
const { node } = classPath;
|
||||
const ref = node.id || classPath.scope.generateUidIdentifier("class");
|
||||
|
||||
if (classPath.parentPath.isExportDefaultDeclaration()) {
|
||||
classPath = classPath.parentPath;
|
||||
classPath.insertAfter(t.exportDefaultDeclaration(ref));
|
||||
}
|
||||
|
||||
classPath.replaceWithMultiple(buildWrapper({
|
||||
CLASS_REF: classPath.scope.generateUidIdentifier(ref.name),
|
||||
CALL_REF: classPath.scope.generateUidIdentifier(`${ref.name}Call`),
|
||||
CALL: t.functionExpression(null, constructorCall.node.params, constructorCall.node.body),
|
||||
CLASS: t.toExpression(node),
|
||||
WRAPPER_REF: ref
|
||||
}));
|
||||
|
||||
constructorCall.remove();
|
||||
}
|
||||
|
||||
return {
|
||||
inherits: require("babel-plugin-syntax-class-constructor-call"),
|
||||
|
||||
visitor: {
|
||||
Class(path) {
|
||||
if (path.node[ALREADY_VISITED]) return;
|
||||
path.node[ALREADY_VISITED] = true;
|
||||
|
||||
const constructorCall = findConstructorCall(path);
|
||||
|
||||
if (constructorCall) {
|
||||
handleClassWithCall(constructorCall, path);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
class Foo {
|
||||
constructor() {
|
||||
this.num = 1;
|
||||
}
|
||||
|
||||
call constructor() {
|
||||
return { num: 2 };
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(new Foo().num, 1);
|
||||
assert.equal(Foo().num, 2);
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["transform-class-constructor-call", "transform-es2015-classes", "transform-es2015-block-scoping", "transform-es2015-parameters"]
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
class Foo {
|
||||
call constructor() {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
let _Foo = class Foo {};
|
||||
|
||||
var _FooCall = function () {
|
||||
foo();
|
||||
};
|
||||
|
||||
var Foo = function (...args) {
|
||||
if (this instanceof Foo) {
|
||||
return Reflect.construct(_Foo, args);
|
||||
} else {
|
||||
return _FooCall.apply(this, args);
|
||||
}
|
||||
};
|
||||
|
||||
Foo.__proto__ = _Foo;
|
||||
Foo;
|
||||
@ -1,5 +0,0 @@
|
||||
export default class Foo {
|
||||
call constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
let _Foo = class Foo {};
|
||||
|
||||
var _FooCall = function () {};
|
||||
|
||||
var Foo = function (...args) {
|
||||
if (this instanceof Foo) {
|
||||
return Reflect.construct(_Foo, args);
|
||||
} else {
|
||||
return _FooCall.apply(this, args);
|
||||
}
|
||||
};
|
||||
|
||||
Foo.__proto__ = _Foo;
|
||||
export default Foo;
|
||||
@ -1,5 +0,0 @@
|
||||
(class {
|
||||
call constructor() {
|
||||
foo();
|
||||
}
|
||||
});
|
||||
@ -1,16 +0,0 @@
|
||||
let _class2 = class {};
|
||||
|
||||
var _classCall = function () {
|
||||
foo();
|
||||
};
|
||||
|
||||
var _class = function (...args) {
|
||||
if (this instanceof _class) {
|
||||
return Reflect.construct(_class2, args);
|
||||
} else {
|
||||
return _classCall.apply(this, args);
|
||||
}
|
||||
};
|
||||
|
||||
_class.__proto__ = _class2;
|
||||
_class;
|
||||
@ -1,12 +0,0 @@
|
||||
let Foo = class {
|
||||
constructor() {
|
||||
this.num = 1;
|
||||
}
|
||||
|
||||
call constructor() {
|
||||
return { num: 2 };
|
||||
}
|
||||
};
|
||||
|
||||
assert.equal(new Foo().num, 1);
|
||||
assert.equal(Foo().num, 2);
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["transform-class-constructor-call", "transform-es2015-classes", "transform-es2015-block-scoping", "transform-es2015-parameters"]
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
let Foo = class {
|
||||
call constructor() {
|
||||
foo();
|
||||
}
|
||||
};
|
||||
@ -1,18 +0,0 @@
|
||||
let Foo = function () {
|
||||
let _class2 = class {};
|
||||
|
||||
var _classCall = function () {
|
||||
foo();
|
||||
};
|
||||
|
||||
var _class = function (...args) {
|
||||
if (this instanceof _class) {
|
||||
return Reflect.construct(_class2, args);
|
||||
} else {
|
||||
return _classCall.apply(this, args);
|
||||
}
|
||||
};
|
||||
|
||||
_class.__proto__ = _class2;
|
||||
return _class;
|
||||
}();
|
||||
@ -1 +0,0 @@
|
||||
class Foo {}
|
||||
@ -1 +0,0 @@
|
||||
class Foo {}
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["transform-class-constructor-call"]
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
import runner from "babel-helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
@ -8,7 +8,6 @@
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-preset-stage-1",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"babel-plugin-transform-class-constructor-call": "^6.22.0",
|
||||
"babel-plugin-transform-export-extensions": "^6.22.0",
|
||||
"babel-preset-stage-2": "^6.22.0"
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import presetStage2 from "babel-preset-stage-2";
|
||||
|
||||
import transformClassConstructorCall from "babel-plugin-transform-class-constructor-call";
|
||||
import transformExportExtensions from "babel-plugin-transform-export-extensions";
|
||||
|
||||
export default {
|
||||
@ -8,7 +7,6 @@ export default {
|
||||
presetStage2
|
||||
],
|
||||
plugins: [
|
||||
transformClassConstructorCall,
|
||||
transformExportExtensions
|
||||
]
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user