Skip TSAsExpression when transforming spread in CallExpression (#11404)

* Skip TSAsExpression when transforming spread in CallExpression

* Create @babel/helper-get-call-context package

* Support OptionalCallExpressions

* Use helper in optional chaining plugin, and move tests

* Update package.json files

* Use dot notation to access property

* Remove private method tests until future MR

* Update packages/babel-plugin-transform-spread/package.json

* Rename @babel/helper-get-call-context to @babel/helper-skip-transparent-expr-wrappers

* Handle typed OptionalMemberExpressions

* Make @babel/helper-skip-transparent-expr-wrappers a dependency

* Support TSNonNullExpressions

* Use named import instead of default

* Add test for call context when parenthesized call expression has type

* Improve handling of member expressions inside transparent expression wrappers

* Add comment explaining what a transparent expression wrapper is

* Add newlines to test fixtures

* Pass correct parameter type to skipTransparentExprWrappers

* Rename to babel-helper-skip-transparent-expression-wrappers

* Remove getCallContext helper

* Fixed exports key

* Preserve types in babel-plugin-transform-spread tests

* Use external-helpers to avoid inlining helper functions in tests

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Oliver Dunk
2020-07-30 19:17:37 +01:00
committed by GitHub
parent 32e7bb4027
commit db56261414
36 changed files with 224 additions and 33 deletions

View File

@@ -16,7 +16,8 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.10.4"
"@babel/helper-plugin-utils": "^7.10.4",
"@babel/helper-skip-transparent-expression-wrappers": "7.9.6"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"

View File

@@ -1,4 +1,5 @@
import { declare } from "@babel/helper-plugin-utils";
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers";
import { types as t } from "@babel/core";
export default declare((api, options) => {
@@ -94,7 +95,8 @@ export default declare((api, options) => {
const args = node.arguments;
if (!hasSpread(args)) return;
const calleePath = path.get("callee");
const calleePath = skipTransparentExprWrappers(path.get("callee"));
if (calleePath.isSuper()) return;
let contextLiteral = scope.buildUndefinedNode();
@@ -120,7 +122,7 @@ export default declare((api, options) => {
node.arguments.push(first);
}
const callee = node.callee;
const callee = calleePath.node;
if (calleePath.isMemberExpression()) {
const temp = scope.maybeGenerateMemoised(callee.object);
@@ -130,11 +132,11 @@ export default declare((api, options) => {
} else {
contextLiteral = t.cloneNode(callee.object);
}
t.appendToMemberExpression(callee, t.identifier("apply"));
} else {
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
}
// We use the original callee here, to preserve any types/parentheses
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
if (t.isSuper(contextLiteral)) {
contextLiteral = t.thisExpression();
}

View File

@@ -0,0 +1 @@
(a.b: any)(...args)

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-spread", "syntax-flow"]
}

View File

@@ -0,0 +1,3 @@
var _a;
((_a = a).b: any).apply(_a, babelHelpers.toConsumableArray(args));

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-spread", "transform-parameters"]
}

View File

@@ -0,0 +1,5 @@
{
"parserOpts": {
"createParenthesizedExpressions": true
}
}

View File

@@ -0,0 +1,3 @@
var _a;
((_a = a).b).apply(_a, babelHelpers.toConsumableArray(args));

View File

@@ -0,0 +1 @@
(<any> a.b)(...args)

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-spread", "syntax-typescript"]
}

View File

@@ -0,0 +1,3 @@
var _a;
(<any> (_a = a).b).apply(_a, babelHelpers.toConsumableArray(args));

View File

@@ -0,0 +1 @@
(dog.bark as any)(...args)

View File

@@ -0,0 +1,7 @@
{
"presets": [
[
"typescript"
]
]
}

View File

@@ -0,0 +1,3 @@
var _dog;
(_dog = dog).bark.apply(_dog, babelHelpers.toConsumableArray(args));