Skip to content

Commit

Permalink
Create @babel/helper-get-call-context package
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverdunk committed May 3, 2020
1 parent dc5316e commit e090927
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/babel-helper-get-call-context/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
17 changes: 17 additions & 0 deletions packages/babel-helper-get-call-context/README.md
@@ -0,0 +1,17 @@
# @babel/helper-get-call-context

> Helper function to get the context of a CallExpression
## Install

Using npm:

```sh
npm install --save-dev @babel/helper-get-call-context
```

or using yarn:

```sh
yarn add @babel/helper-get-call-context --dev
```
14 changes: 14 additions & 0 deletions packages/babel-helper-get-call-context/package.json
@@ -0,0 +1,14 @@
{
"name": "@babel/helper-get-call-context",
"version": "7.9.6",
"description": "Helper function to get call context",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-get-call-context",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"dependencies": {
"@babel/traverse": "^7.9.6"
}
}
18 changes: 18 additions & 0 deletions packages/babel-helper-get-call-context/src/index.js
@@ -0,0 +1,18 @@
import type { NodePath } from "@babel/traverse";

export default function(callPath: NodePath): NodePath {
callPath.assertCallExpression();

let calleePath = callPath.get("callee");

while (
calleePath.isTSAsExpression() ||
calleePath.isTypeCastExpression() ||
calleePath.isTSTypeAssertion() ||
calleePath.isParenthesizedExpression()
) {
calleePath = calleePath.get("expression");
}

return calleePath;
}
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-spread/package.json
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@babel/core": "^7.8.3",
"@babel/helper-plugin-test-runner": "^7.8.3"
"@babel/helper-plugin-test-runner": "^7.8.3",
"@babel/helper-get-call-context": "7.9.6"
}
}
9 changes: 2 additions & 7 deletions packages/babel-plugin-transform-spread/src/index.js
@@ -1,4 +1,5 @@
import { declare } from "@babel/helper-plugin-utils";
import getCallContext from "@babel/helper-get-call-context";
import { types as t } from "@babel/core";

export default declare((api, options) => {
Expand Down Expand Up @@ -94,13 +95,7 @@ export default declare((api, options) => {
const args = node.arguments;
if (!hasSpread(args)) return;

let calleePath = path.get("callee");

if (calleePath.isTSAsExpression()) {
do {
calleePath = calleePath.get("expression");
} while (calleePath.isTSAsExpression());
}
const calleePath = getCallContext(path);

if (calleePath.isSuper()) return;

Expand Down
@@ -0,0 +1 @@
(a.b: any)(...args)
@@ -0,0 +1,7 @@
{
"presets": [
[
"flow"
]
]
}
@@ -0,0 +1,3 @@
var _a;

(_a = a).b.apply(_a, babelHelpers.toConsumableArray(args));
@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-spread", "transform-parameters"]
}
@@ -0,0 +1 @@
(a.b)(...args)
@@ -0,0 +1,5 @@
{
"parserOpts": {
"createParenthesizedExpressions": true
}
}
@@ -0,0 +1,3 @@
var _a;

((_a = a).b.apply)(_a, babelHelpers.toConsumableArray(args));
@@ -0,0 +1,7 @@
class A {
#x;

fn() {
(this.#x as any)(...args);
}
}
@@ -0,0 +1,12 @@
{
"presets": [
[
"typescript"
]
],
"plugins": [
[
"proposal-class-properties"
]
]
}
@@ -0,0 +1,17 @@
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = privateMap.get(receiver); if (!descriptor) { throw new TypeError("attempted to get private field on non-instance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }

class A {
constructor() {
_x.set(this, {
writable: true,
value: void 0
});
}

fn() {
_classPrivateFieldGet(this, _x)(...args);
}

}

var _x = new WeakMap();
@@ -0,0 +1 @@
(<any> a.b)(...args)
@@ -0,0 +1,7 @@
{
"presets": [
[
"typescript"
]
]
}
@@ -0,0 +1,3 @@
var _a;

(_a = a).b.apply(_a, babelHelpers.toConsumableArray(args));
@@ -1 +1 @@
(dog.bark as any)(...args)
(dog.bark as any)(...args)
@@ -1,7 +1,7 @@
{
"presets": [
[
"typescript"
]
"presets": [
[
"typescript"
]
}
]
}

0 comments on commit e090927

Please sign in to comment.