Skip to content

Commit

Permalink
Cleanup converters types
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 10, 2020
1 parent 6edacce commit 1669fe6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 89 deletions.
10 changes: 8 additions & 2 deletions packages/babel-types/src/converters/toComputedKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { stringLiteral } from "../builders/generated";
import type * as t from "../types";

export default function toComputedKey(
// todo(flow->ts) should be "t.Method | t.Property" or maybe also MemberExpression or t.OptionalMemberExpression
node: any,
node:
| t.ObjectMember
| t.ObjectProperty
| t.ClassMethod
| t.ClassProperty
| t.MemberExpression
| t.OptionalMemberExpression,
// @ts-expect-error todo(flow->ts): maybe check the type of node before accessing .key and .property
key: t.Expression = node.key || node.property,
) {
if (!node.computed && isIdentifier(key)) key = stringLiteral(key.name);
Expand Down
12 changes: 7 additions & 5 deletions packages/babel-types/src/converters/toExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import {
} from "../validators/generated";
import type * as t from "../types";

function toExpression(node: t.Function): t.FunctionExpression;
function toExpression(node: t.Class): t.ClassExpression;
function toExpression(node: t.ExpressionStatement | t.Expression): t.Expression;
export default toExpression as {
(node: t.Function): t.FunctionExpression;
(node: t.Class): t.ClassExpression;
(
node: t.ExpressionStatement | t.Expression | t.Class | t.Function,
): t.Expression;
};

function toExpression(
node: t.ExpressionStatement | t.Expression | t.Class | t.Function,
Expand Down Expand Up @@ -45,5 +49,3 @@ function toExpression(

return node;
}

export default toExpression;
51 changes: 12 additions & 39 deletions packages/babel-types/src/converters/toStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,23 @@ import {
import { expressionStatement } from "../builders/generated";
import type * as t from "../types";

export default function toStatement(
node: t.AssignmentExpression,
ignore?: boolean,
): t.ExpressionStatement;
export default toStatement as {
(node: t.AssignmentExpression, ignore?: boolean): t.ExpressionStatement;

export default function toStatement(
node: t.Statement | t.AssignmentExpression,
ignore?: boolean,
): t.Statement;
<T extends t.Statement>(node: T, ignore: false): T;
<T extends t.Statement>(node: T, ignore?: boolean): T | false;

export default function toStatement(
node: t.Class,
ignore: true,
): t.ClassDeclaration | undefined;
(node: t.Class, ignore: false): t.ClassDeclaration;
(node: t.Class, ignore?: boolean): t.ClassDeclaration | false;

export default function toStatement(
node: t.Class,
ignore?: boolean,
): t.ClassDeclaration;
(node: t.Function, ignore: false): t.FunctionDeclaration;
(node: t.Function, ignore?: boolean): t.FunctionDeclaration | false;

export default function toStatement(
node: t.Function,
ignore: true,
): t.FunctionDeclaration | undefined;
(node: t.Node, ignore: false): t.Statement;
(node: t.Node, ignore?: boolean): t.Statement | false;
};

export default function toStatement(
node: t.Function,
ignore?: boolean,
): t.FunctionDeclaration;

export default function toStatement(
node: t.Statement | t.Class | t.Function | t.AssignmentExpression,
ignore: true,
): t.Statement | undefined;

export default function toStatement(
node: t.Statement | t.Class | t.Function | t.AssignmentExpression,
ignore?: boolean,
): t.Statement | false;

export default function toStatement(
node: t.Statement | t.Class | t.Function | t.AssignmentExpression,
ignore?: boolean,
): t.Statement | false {
function toStatement(node: t.Node, ignore?: boolean): t.Statement | false {
if (isStatement(node)) {
return node;
}
Expand Down
39 changes: 16 additions & 23 deletions packages/babel-types/src/converters/valueToNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,24 @@ import {
} from "../builders/generated";
import type * as t from "../types";

export default function valueToNode(value: undefined): t.Identifier; // todo(flow->ts): (should this not be a types.UnaryExpression to avoid shadowing?)
export default function valueToNode(value: boolean): t.BooleanLiteral;
export default function valueToNode(value: null): t.NullLiteral;
export default function valueToNode(value: string): t.StringLiteral;
// Infinities and NaN need to use a types.BinaryExpression; negative values must be wrapped in types.UnaryExpression
export default function valueToNode(
value: number,
): t.NumericLiteral | t.BinaryExpression | t.UnaryExpression;
export default function valueToNode(value: RegExp): t.RegExpLiteral;
export default function valueToNode(
value: ReadonlyArray<
undefined | boolean | null | string | number | RegExp | object
>,
): t.ArrayExpression;
// this throws with objects that are not PlainObject according to lodash,
// or if there are non-valueToNode-able values
export default function valueToNode(value: object): t.ObjectExpression;
export default valueToNode as {
(value: undefined): t.Identifier; // TODO: This should return "void 0"
(value: boolean): t.BooleanLiteral;
(value: null): t.NullLiteral;
(value: string): t.StringLiteral;
// Infinities and NaN need to use a BinaryExpression; negative values must be wrapped in UnaryExpression
(value: number): t.NumericLiteral | t.BinaryExpression | t.UnaryExpression;
(value: RegExp): t.RegExpLiteral;
(value: ReadonlyArray<unknown>): t.ArrayExpression;

export default function valueToNode(
value: undefined | boolean | null | string | number | RegExp | object,
): t.Expression;
// this throws with objects that are not PlainObject according to lodash,
// or if there are non-valueToNode-able values
(value: object): t.ObjectExpression;

export default function valueToNode(
value: undefined | boolean | null | string | number | RegExp | object,
): t.Expression {
(value: unknown): t.Expression;
};

function valueToNode(value: unknown): t.Expression {
// undefined
if (value === undefined) {
return identifier("undefined");
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-types/src/retrievers/getBindingIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "../validators/generated";
import type * as t from "../types";

export { getBindingIdentifiers as default };

function getBindingIdentifiers(
node: t.Node,
duplicates: true,
Expand All @@ -24,7 +26,7 @@ function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
): Record<string, t.Identifier | Array<t.Identifier>>;
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>;

/**
* Return a list of binding identifiers associated with the input `node`.
Expand All @@ -33,7 +35,7 @@ function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
): Record<string, t.Identifier | Array<t.Identifier>> {
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {
let search = [].concat(node);
const ids = Object.create(null);

Expand Down Expand Up @@ -138,5 +140,3 @@ getBindingIdentifiers.keys = {
VariableDeclaration: ["declarations"],
VariableDeclarator: ["id"],
};

export default getBindingIdentifiers;
25 changes: 9 additions & 16 deletions packages/babel-types/src/retrievers/getOuterBindingIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import getBindingIdentifiers from "./getBindingIdentifiers";
import type * as t from "../types";

export default function getOuterBindingIdentifiers(
node: t.Node,
duplicates: true,
): Record<string, Array<t.Identifier>>;

export default function getOuterBindingIdentifiers(
node: t.Node,
duplicates?: false,
): Record<string, t.Identifier>;

export default function getOuterBindingIdentifiers(
node: t.Node,
duplicates: boolean,
): Record<string, t.Identifier | Array<t.Identifier>>;
export default getOuterBindingIdentifiers as {
(node: t.Node, duplicates: true): Record<string, Array<t.Identifier>>;
(node: t.Node, duplicates?: false): Record<string, t.Identifier>;
(node: t.Node, duplicates?: boolean):
| Record<string, t.Identifier>
| Record<string, Array<t.Identifier>>;
};

export default function getOuterBindingIdentifiers(
function getOuterBindingIdentifiers(
node: t.Node,
duplicates: boolean,
): Record<string, t.Identifier | Array<t.Identifier>> {
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {
return getBindingIdentifiers(node, duplicates, true);
}

0 comments on commit 1669fe6

Please sign in to comment.