Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix typescript for babel-types #10098

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be export declare function ${type}? They always start with an uppercase letter (thus are valid identifiers), and it would match https://github.com/babel/babel/blob/6467cd1e27ffe2a3a1cfe97bc8c67b407cb1e7c4/packages/babel-types/src/builders/generated/index.js

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. this is for the edge case of super() and import() where you can't export declare function super(): SuperNode;

but instead I made it:

declare function _super(): SuperNode;
declare export { _super as super };

to workaround of not being able to export super and import

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo what do you think?

", "
)}): ${NODE_PREFIX}${type};`,
`declare export { _${functionName} as ${functionName} }`
);
}
}

Expand Down Expand Up @@ -131,7 +139,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
24 changes: 23 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 Down Expand Up @@ -142,7 +148,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 @@ -146,8 +146,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 @@ -157,6 +157,13 @@ defineType("ClassPrivateProperty", {
validate: assertNodeType("Expression"),
optional: true,
},
decorators: {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("Decorator")),
),
optional: true,
},
},
});

Expand Down