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:
@@ -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"
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
1
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/input.ts
vendored
Normal file
1
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/input.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
(a.b: any)(...args)
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-spread", "syntax-flow"]
|
||||
}
|
||||
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/output.js
vendored
Normal file
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/output.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var _a;
|
||||
|
||||
((_a = a).b: any).apply(_a, babelHelpers.toConsumableArray(args));
|
||||
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/options.json
vendored
Normal file
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/options.json
vendored
Normal file
@@ -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));
|
||||
1
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/input.ts
vendored
Normal file
1
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/input.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
(<any> a.b)(...args)
|
||||
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/options.json
vendored
Normal file
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-spread", "syntax-typescript"]
|
||||
}
|
||||
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/output.js
vendored
Normal file
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/ts-type-cast/output.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var _a;
|
||||
|
||||
(<any> (_a = a).b).apply(_a, babelHelpers.toConsumableArray(args));
|
||||
1
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/input.ts
vendored
Normal file
1
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/input.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
(dog.bark as any)(...args)
|
||||
7
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/options.json
vendored
Normal file
7
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/options.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"presets": [
|
||||
[
|
||||
"typescript"
|
||||
]
|
||||
]
|
||||
}
|
||||
3
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/output.js
vendored
Normal file
3
packages/babel-plugin-transform-spread/test/fixtures/regression/11400/output.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var _dog;
|
||||
|
||||
(_dog = dog).bark.apply(_dog, babelHelpers.toConsumableArray(args));
|
||||
Reference in New Issue
Block a user