Rename all proposal plugins to -proposal- from -transform- (#6570)

This commit is contained in:
Henry Zhu
2017-10-27 15:26:38 -04:00
committed by GitHub
parent a94aa54230
commit c41abd79a1
599 changed files with 372 additions and 372 deletions

View File

@@ -0,0 +1,3 @@
src
test
*.log

View File

@@ -0,0 +1,121 @@
# @babel/plugin-proposal-function-bind
> Compile the new function bind operator `::` to ES5.
## Detail
```js
obj::func
// is equivalent to:
func.bind(obj)
::obj.func
// is equivalent to:
obj.func.bind(obj)
obj::func(val)
// is equivalent to:
func.call(obj, val)
::obj.func(val)
// is equivalent to:
obj.func.call(obj, val)
```
## Example
### Basic
```js
const box = {
weight: 2,
getWeight() { return this.weight; },
};
const { getWeight } = box;
console.log(box.getWeight()); // prints '2'
const bigBox = { weight: 10 };
console.log(bigBox::getWeight()); // prints '10'
// Can be chained:
function add(val) { return this + val; }
console.log(bigBox::getWeight()::add(5)); // prints '15'
```
### Using with `document.querySelectorAll`
It can be very handy when used with `document.querySelectorAll`:
```js
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a')
::map(node => node.href)
::filter(href => href.substring(0, 5) === 'https');
console.log(sslUrls);
```
`document.querySelectorAll` returns a `NodeList` element which is not a plain array, so you normally can't use the `map` function on it, and have to use it this way: `Array.prototype.map.call(document.querySelectorAll(...), node => { ... })`. The above code using the `::` will work because it is equivalent to:
```js
const { map, filter } = Array.prototype;
let sslUrls = document.querySelectorAll('a');
sslUrls = map.call(sslUrls, node => node.href);
sslUrls = filter.call(sslUrls, href => href.substring(0, 5) === 'https');
console.log(sslUrls);
```
### Auto self binding
When nothing is specified before the `::` operator, the function is bound to its object:
```js
$('.some-link').on('click', ::view.reset);
// is equivalent to:
$('.some-link').on('click', view.reset.bind(view));
```
## Installation
```sh
npm install --save-dev @babel/plugin-proposal-function-bind
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["@babel/proposal-function-bind"]
}
```
### Via CLI
```sh
babel --plugins @babel/proposal-function-bind script.js
```
### Via Node API
```javascript
require("@babel/core").transform("code", {
plugins: ["@babel/proposal-function-bind"]
});
```
## References
* [Proposal](https://github.com/zenparsing/es-function-bind)
* [Babel Blog: Function Bind Syntax](/blog/2015/05/14/function-bind)

View File

@@ -0,0 +1,20 @@
{
"name": "@babel/plugin-proposal-function-bind",
"version": "7.0.0-beta.3",
"description": "Compile function bind operator to ES5",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-function-bind",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/plugin-syntax-function-bind": "7.0.0-beta.3"
},
"peerDependencies": {
"@babel/core": "7.0.0-beta.3"
},
"devDependencies": {
"@babel/helper-plugin-test-runner": "7.0.0-beta.3"
}
}

View File

@@ -0,0 +1,62 @@
import syntaxFunctionBind from "@babel/plugin-syntax-function-bind";
export default function({ types: t }) {
function getTempId(scope) {
let id = scope.path.getData("functionBind");
if (id) return id;
id = scope.generateDeclaredUidIdentifier("context");
return scope.path.setData("functionBind", id);
}
function getStaticContext(bind, scope) {
const object = bind.object || bind.callee.object;
return scope.isStatic(object) && object;
}
function inferBindContext(bind, scope) {
const staticContext = getStaticContext(bind, scope);
if (staticContext) return t.cloneDeep(staticContext);
const tempId = getTempId(scope);
if (bind.object) {
bind.callee = t.sequenceExpression([
t.assignmentExpression("=", tempId, bind.object),
bind.callee,
]);
} else {
bind.callee.object = t.assignmentExpression(
"=",
tempId,
bind.callee.object,
);
}
return tempId;
}
return {
inherits: syntaxFunctionBind,
visitor: {
CallExpression({ node, scope }) {
const bind = node.callee;
if (!t.isBindExpression(bind)) return;
const context = inferBindContext(bind, scope);
node.callee = t.memberExpression(bind.callee, t.identifier("call"));
node.arguments.unshift(context);
},
BindExpression(path) {
const { node, scope } = path;
const context = inferBindContext(node, scope);
path.replaceWith(
t.callExpression(
t.memberExpression(node.callee, t.identifier("bind")),
[context],
),
);
},
},
};
}

View File

@@ -0,0 +1,3 @@
var f = ctx::ns.obj.func;
var g = ::ns.obj.func;
var h = new X::y;

View File

@@ -0,0 +1,7 @@
var _context;
var f = (_context = ctx, ns.obj.func).bind(_context);
var g = (_context = ns.obj).func.bind(_context);
var h = (_context = new X(), y).bind(_context);

View File

@@ -0,0 +1,4 @@
ctx::ns.obj.func();
::ns.obj.func();
ns.obj2::ns.obj1.func();

View File

@@ -0,0 +1,7 @@
var _context;
(_context = ctx, ns.obj.func).call(_context);
(_context = ns.obj).func.call(_context);
(_context = ns.obj2, ns.obj1.func).call(_context);

View File

@@ -0,0 +1,6 @@
import { map, takeWhile, forEach } from "iterlib";
getPlayers()
::map(x => x.character())
::takeWhile(x => x.strength > 100)
::forEach(x => console.log(x));

View File

@@ -0,0 +1,29 @@
var operations = [];
var lib = {};
['f', 'g', 'h'].forEach(function (key) {
var func = function () {
return operations.push("lib." + key + "()");
};
Object.defineProperty(lib, key, {
get: function () {
operations.push("get lib." + key);
return func;
}
});
});
({prop:'value'})
::lib.f()
::lib.g()
::lib.h();
assert.deepEqual(operations, [
'get lib.f',
'lib.f()',
'get lib.g',
'lib.g()',
'get lib.h',
'lib.h()'
]);

View File

@@ -0,0 +1,10 @@
var _context;
import { map, takeWhile, forEach } from "iterlib";
(_context = (_context = (_context = getPlayers(), map).call(_context, function (x) {
return x.character();
}), takeWhile).call(_context, function (x) {
return x.strength > 100;
}), forEach).call(_context, function (x) {
return console.log(x);
});

View File

@@ -0,0 +1,3 @@
{
"plugins": ["proposal-function-bind", "transform-es2015-arrow-functions"]
}

View File

@@ -0,0 +1,5 @@
var bar = function () {};
foo::bar;
var foo = {};
::foo.bar;

View File

@@ -0,0 +1,5 @@
var bar = function () {};
bar.bind(foo);
var foo = {};
foo.bar.bind(foo);

View File

@@ -0,0 +1,3 @@
import Obj from "./file";
console.log(::Obj.method);

View File

@@ -0,0 +1,7 @@
"use strict";
var _file = _interopRequireDefault(require("./file"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_file.default.method.bind(_file.default));

View File

@@ -0,0 +1,3 @@
{
"plugins": ["proposal-function-bind", "transform-es2015-modules-commonjs"]
}

View File

@@ -0,0 +1,12 @@
function one(){}
function two(){}
class Test1 {
one(){
one::two(1, 2)
}
two(){
one::two(1, 2)
}
}

View File

@@ -0,0 +1,31 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _one() {}
function _two() {}
let Test1 =
/*#__PURE__*/
function () {
function Test1() {
_classCallCheck(this, Test1);
}
_createClass(Test1, [{
key: "one",
value: function one() {
_two.call(_one, 1, 2);
}
}, {
key: "two",
value: function two() {
_two.call(_one, 1, 2);
}
}]);
return Test1;
}();

View File

@@ -0,0 +1,3 @@
{
"plugins": ["proposal-function-bind", "transform-es2015-classes"]
}

View File

@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);