Skip to content

Commit

Permalink
fix typescript for babel-types (#10098)
Browse files Browse the repository at this point in the history
* fix typescript for babel-types

* fix missing typescript/flow types for babel-types

* Add cloneNode to flow.js

* Add cloneNode to typescript.js
  • Loading branch information
tanhauhau authored and nicolo-ribaudo committed Sep 4, 2019
1 parent c0e3fa0 commit a08e856
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
27 changes: 26 additions & 1 deletion packages/babel-types/scripts/generators/flow.js
Expand Up @@ -99,6 +99,14 @@ for (const type in t.NODE_FIELDS) {
", "
)}): ${NODE_PREFIX}${type};`
);
} else {
const functionName = toFunctionName(type);
lines.push(
`declare function _${functionName}(${args.join(
", "
)}): ${NODE_PREFIX}${type};`,
`declare export { _${functionName} as ${functionName} }`
);
}
}

Expand All @@ -116,6 +124,7 @@ lines.push(
`declare function validate(n: BabelNode, key: string, value: mixed): void;`,
`declare function clone<T>(n: T): T;`,
`declare function cloneDeep<T>(n: T): T;`,
`declare function cloneNode<T>(n: T, deep?: boolean): T;`,
`declare function removeProperties<T>(n: T, opts: ?{}): void;`,
`declare function removePropertiesDeep<T>(n: T, opts: ?{}): T;`,
`declare type TraversalAncestors = Array<{
Expand All @@ -129,7 +138,23 @@ lines.push(
exit?: TraversalHandler<T>,
};`.replace(/(^|\n) {2}/g, "$1"),
// eslint-disable-next-line
`declare function traverse<T>(n: BabelNode, TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`
`declare function traverse<T>(n: BabelNode, TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`,
`declare function is(type: string, n: BabelNode, opts: Object): boolean;`,
`declare function isBinding(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
`declare function isBlockScoped(node: BabelNode): boolean`,
`declare function isImmutable(node: BabelNode): boolean`,
`declare function isLet(node: BabelNode): boolean`,
`declare function isNode(node: ?Object): boolean`,
`declare function isNodesEquivalent(a: any, b: any): boolean`,
`declare function isPlaceholderType(placeholderType: ?string, targetType: string): boolean`,
`declare function isReferenced(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
`declare function isScope(node: BabelNode, parent: BabelNode): boolean`,
`declare function isSpecifierDefault(specifier: BabelNodeModuleSpecifier): boolean`,
`declare function isType(nodetype: ?string, targetType: string): boolean`,
`declare function isValidES3Identifier(name: string): boolean`,
`declare function isValidES3Identifier(name: string): boolean`,
`declare function isValidIdentifier(name: string): boolean`,
`declare function isVar(node: BabelNode): boolean`
);

for (const type in t.FLIPPED_ALIAS_KEYS) {
Expand Down
25 changes: 24 additions & 1 deletion packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -100,6 +100,12 @@ for (const type in t.NODE_FIELDS) {
lines.push(
`export function ${toFunctionName(type)}(${args.join(", ")}): ${type};`
);
} else {
const functionName = toFunctionName(type);
lines.push(
`declare function _${functionName}(${args.join(", ")}): ${type};`,
`export { _${functionName} as ${functionName}}`
);
}
}

Expand All @@ -121,6 +127,7 @@ lines.push(
`export function validate(n: Node, key: string, value: any): void;`,
`export function clone<T extends Node>(n: T): T;`,
`export function cloneDeep<T extends Node>(n: T): T;`,
`export function cloneNode<T extends Node>(n: T, deep?: boolean): T;`,
`export function removeProperties(
n: Node,
opts?: { preserveComments: boolean } | null
Expand All @@ -140,7 +147,23 @@ lines.push(
exit?: TraversalHandler<T>,
};`.replace(/(^|\n) {2}/g, "$1"),
// eslint-disable-next-line
`export function traverse<T>(n: Node, h: TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`
`export function traverse<T>(n: Node, h: TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`,
`export function is(type: string, n: Node, opts: object): boolean;`,
`export function isBinding(node: Node, parent: Node, grandparent?: Node): boolean`,
`export function isBlockScoped(node: Node): boolean`,
`export function isImmutable(node: Node): boolean`,
`export function isLet(node: Node): boolean`,
`export function isNode(node: ?object): boolean`,
`export function isNodesEquivalent(a: any, b: any): boolean`,
`export function isPlaceholderType(placeholderType: ?string, targetType: string): boolean`,
`export function isReferenced(node: Node, parent: Node, grandparent?: Node): boolean`,
`export function isScope(node: Node, parent: Node): boolean`,
`export function isSpecifierDefault(specifier: ModuleSpecifier): boolean`,
`export function isType(nodetype: ?string, targetType: string): boolean`,
`export function isValidES3Identifier(name: string): boolean`,
`export function isValidES3Identifier(name: string): boolean`,
`export function isValidIdentifier(name: string): boolean`,
`export function isVar(node: Node): boolean`
);

for (const type in t.DEPRECATED_KEYS) {
Expand Down
11 changes: 9 additions & 2 deletions packages/babel-types/src/definitions/experimental.js
Expand Up @@ -153,8 +153,8 @@ defineType("OptionalCallExpression", {
});

defineType("ClassPrivateProperty", {
visitor: ["key", "value"],
builder: ["key", "value"],
visitor: ["key", "value", "decorators"],
builder: ["key", "value", "decorators"],
aliases: ["Property", "Private"],
fields: {
key: {
Expand All @@ -164,6 +164,13 @@ defineType("ClassPrivateProperty", {
validate: assertNodeType("Expression"),
optional: true,
},
decorators: {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("Decorator")),
),
optional: true,
},
},
});

Expand Down

0 comments on commit a08e856

Please sign in to comment.