Skip to content

Commit

Permalink
convert @babel/helper-skip-transparent-expression-wrappers to types…
Browse files Browse the repository at this point in the history
…cript
  • Loading branch information
lightmare committed Aug 17, 2021
1 parent fc66d4d commit b8be217
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
@@ -1,14 +1,21 @@
// @flow

import * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";

export type TransparentExprWrapper =
| t.TSAsExpression
| t.TSTypeAssertion
| t.TSNonNullExpression
| t.TypeCastExpression
| t.ParenthesizedExpression;

// 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) {
export function isTransparentExprWrapper(
node: t.Node,
): node is TransparentExprWrapper {
return (
t.isTSAsExpression(node) ||
t.isTSTypeAssertion(node) ||
Expand All @@ -18,10 +25,11 @@ export function isTransparentExprWrapper(node: Node) {
);
}

export function skipTransparentExprWrappers(path: NodePath): NodePath {
export function skipTransparentExprWrappers(
path: NodePath<t.Expression>,
): NodePath<t.Expression> {
while (isTransparentExprWrapper(path.node)) {
path = path.get("expression");
}

return path;
}
Expand Up @@ -22,7 +22,7 @@ function matchAffectedArguments(argumentNodes) {
export function shouldTransform(
path: NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>,
): boolean {
let optionalPath = path;
let optionalPath: NodePath<t.Expression> = path;
const chains = [];
while (
optionalPath.isOptionalMemberExpression() ||
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Expand Up @@ -20,6 +20,7 @@
"./packages/babel-helper-optimise-call-expression/src/**/*.ts",
"./packages/babel-helper-replace-supers/src/**/*.ts",
"./packages/babel-helper-simple-access/src/**/*.ts",
"./packages/babel-helper-skip-transparent-expression-wrappers/src/**/*.ts",
"./packages/babel-helper-split-export-declaration/src/**/*.ts",
"./packages/babel-helper-transform-fixture-test-runner/src/**/*.ts",
"./packages/babel-helper-validator-identifier/src/**/*.ts",
Expand Down Expand Up @@ -96,6 +97,9 @@
"@babel/helper-simple-access": [
"./packages/babel-helper-simple-access/src"
],
"@babel/helper-skip-transparent-expression-wrappers": [
"./packages/babel-helper-skip-transparent-expression-wrappers/src"
],
"@babel/helper-split-export-declaration": [
"./packages/babel-helper-split-export-declaration/src"
],
Expand Down

0 comments on commit b8be217

Please sign in to comment.