Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
32e7bb4
commit db56261
Showing
36 changed files
with
224 additions
and
33 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/babel-helper-skip-transparent-expression-wrappers/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
17 changes: 17 additions & 0 deletions
17
packages/babel-helper-skip-transparent-expression-wrappers/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# @babel/helper-skip-transparent-expression-wrappers | ||
|
||
> Helper which skips types and parentheses | ||
## Install | ||
|
||
Using npm: | ||
|
||
```sh | ||
npm install --save-dev @babel/helper-skip-transparent-expression-wrappers | ||
``` | ||
|
||
or using yarn: | ||
|
||
```sh | ||
yarn add @babel/helper-skip-transparent-expression-wrappers --dev | ||
``` |
25 changes: 25 additions & 0 deletions
25
packages/babel-helper-skip-transparent-expression-wrappers/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@babel/helper-skip-transparent-expression-wrappers", | ||
"version": "7.9.6", | ||
"description": "Helper which skips types and parentheses", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/babel/babel.git", | ||
"directory": "packages/babel-helper-skip-transparent-expression-wrappers" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"dependencies": { | ||
"@babel/types": "^7.9.6" | ||
}, | ||
"devDependencies": { | ||
"@babel/traverse": "^7.9.6" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/babel-helper-skip-transparent-expression-wrappers/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @flow | ||
|
||
import * as t from "@babel/types"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
// A transparent expression wrapper is an AST node that most plugins will wish | ||
// to skip, as its presence does not affect the behaviour of the code. This | ||
// includes expressions used for types, and extra parenthesis. For example, in | ||
// (a as any)(), this helper can be used to skip the TSAsExpression when | ||
// determining the callee. | ||
export function isTransparentExprWrapper(node: Node) { | ||
return ( | ||
t.isTSAsExpression(node) || | ||
t.isTSTypeAssertion(node) || | ||
t.isTSNonNullExpression(node) || | ||
t.isTypeCastExpression(node) || | ||
t.isParenthesizedExpression(node) | ||
); | ||
} | ||
|
||
export function skipTransparentExprWrappers(path: NodePath): NodePath { | ||
while (isTransparentExprWrapper(path.node)) { | ||
path = path.get("expression"); | ||
} | ||
|
||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...al-optional-chaining/test/fixtures/regression/10959-transform-optional-chaining/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
var _a, _a2, _a3, _b, _a4, _ref, _a5, _c, _a6, _a7; | ||
var _a, _a2, _a3, _b, _a4, _a4$b, _a5, _c, _a6, _a7; | ||
|
||
(_a = a) === null || _a === void 0 ? void 0 : _a.b!.c; | ||
(_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b!.c.d; | ||
(_a3 = a) === null || _a3 === void 0 ? void 0 : _a3.b.c!.d; | ||
(_b = a!.b) === null || _b === void 0 ? void 0 : _b.c; | ||
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_ref = _a4.b!) === null || _ref === void 0 ? void 0 : _ref.c; | ||
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_a4$b = _a4.b!) === null || _a4$b === void 0 ? void 0 : _a4$b.c; | ||
(_a5 = a) === null || _a5 === void 0 ? void 0 : (_c = _a5.b!.c) === null || _c === void 0 ? void 0 : _c.c; | ||
((_a6 = a) === null || _a6 === void 0 ? void 0 : _a6.b)!.c; | ||
((_a7 = a) === null || _a7 === void 0 ? void 0 : _a7.b)!.c; |
4 changes: 2 additions & 2 deletions
4
...onal-chaining/test/fixtures/regression/10959-transform-ts-and-optional-chaining/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
var _a, _a2, _a3, _b, _a4, _ref, _a5, _c, _a6, _a7; | ||
var _a, _a2, _a3, _b, _a4, _a4$b, _a5, _c, _a6, _a7; | ||
|
||
(_a = a) === null || _a === void 0 ? void 0 : _a.b.c; | ||
(_a2 = a) === null || _a2 === void 0 ? void 0 : _a2.b.c.d; | ||
(_a3 = a) === null || _a3 === void 0 ? void 0 : _a3.b.c.d; | ||
(_b = a.b) === null || _b === void 0 ? void 0 : _b.c; | ||
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_ref = _a4.b) === null || _ref === void 0 ? void 0 : _ref.c; | ||
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_a4$b = _a4.b) === null || _a4$b === void 0 ? void 0 : _a4$b.c; | ||
(_a5 = a) === null || _a5 === void 0 ? void 0 : (_c = _a5.b.c) === null || _c === void 0 ? void 0 : _c.c; | ||
((_a6 = a) === null || _a6 === void 0 ? void 0 : _a6.b).c; | ||
((_a7 = a) === null || _a7 === void 0 ? void 0 : _a7.b).c; |
3 changes: 3 additions & 0 deletions
3
...el-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["proposal-optional-chaining"] | ||
} |
1 change: 1 addition & 0 deletions
1
...sal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-call-context/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a.b as any)?.() |
10 changes: 10 additions & 0 deletions
10
...optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-call-context/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"syntax-typescript" | ||
], | ||
[ | ||
"proposal-optional-chaining" | ||
] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...al-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-call-context/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var _a$b, _a; | ||
|
||
(_a$b = ((_a = a).b as any)) === null || _a$b === void 0 ? void 0 : _a$b.call(_a); |
1 change: 1 addition & 0 deletions
1
...ional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-function-call-loose/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(((foo as A).bar) as B)?.(foo.bar, false) |
13 changes: 13 additions & 0 deletions
13
...l-chaining/test/fixtures/transparent-expr-wrappers/ts-as-function-call-loose/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"syntax-typescript" | ||
], | ||
[ | ||
"proposal-optional-chaining", | ||
{ | ||
"loose": true | ||
} | ||
] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...onal-chaining/test/fixtures/transparent-expr-wrappers/ts-as-function-call-loose/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var _bar, _ref; | ||
|
||
(_bar = ((_ref = (foo as A)).bar as B)) == null ? void 0 : _bar.call(_ref, foo.bar, false); |
1 change: 1 addition & 0 deletions
1
...ptional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-member-expression/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a?.b as ExampleType)?.c as ExampleType2 |
10 changes: 10 additions & 0 deletions
10
...nal-chaining/test/fixtures/transparent-expr-wrappers/ts-as-member-expression/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"syntax-typescript" | ||
], | ||
[ | ||
"proposal-optional-chaining" | ||
] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...tional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-member-expression/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var _a, _a$b; | ||
|
||
(((_a = a) === null || _a === void 0 ? void 0 : (_a$b = (_a.b as ExampleType)) === null || _a$b === void 0 ? void 0 : _a$b.c) as ExampleType2); |
1 change: 1 addition & 0 deletions
1
.../test/fixtures/transparent-expr-wrappers/ts-parenthesized-expression-member-call/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
((o?.Foo.m) as ExampleType)() |
10 changes: 10 additions & 0 deletions
10
...t/fixtures/transparent-expr-wrappers/ts-parenthesized-expression-member-call/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"syntax-typescript" | ||
], | ||
[ | ||
"proposal-optional-chaining" | ||
] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...test/fixtures/transparent-expr-wrappers/ts-parenthesized-expression-member-call/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var _o, _o$Foo; | ||
|
||
(((_o = o) === null || _o === void 0 ? void 0 : (_o$Foo = _o.Foo).m.bind(_o$Foo)) as ExampleType)(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a.b: any)(...args) |
3 changes: 3 additions & 0 deletions
3
...ages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["external-helpers", "transform-spread", "syntax-flow"] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/flow-type-cast/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var _a; | ||
|
||
((_a = a).b: any).apply(_a, babelHelpers.toConsumableArray(args)); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-spread/test/fixtures/call-context/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["external-helpers", "transform-spread", "transform-parameters"] | ||
} |
1 change: 1 addition & 0 deletions
1
...bel-plugin-transform-spread/test/fixtures/call-context/parenthesized-expressions/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(a.b)(...args) |
Oops, something went wrong.