Skip to content

Commit

Permalink
support babel-ts (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab authored Dec 4, 2023
1 parent d89e5de commit 3b1401e
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 209 deletions.
1 change: 1 addition & 0 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ await Bun.build({
outdir: "./dist",
external: [
"prettier",
"@babel/types",
"@typescript-eslint/visitor-keys",
"@typescript-eslint/types",
],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
63 changes: 4 additions & 59 deletions lib/ast/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
import { type Node as BabelNode } from "@babel/types";

export const functionExpressions = [
export type BabelNodeTypes = Exclude<BabelNode["type"], never>;

export const functionExpressions: NodeTypes[] = [
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ArrowFunctionExpression,
];
Expand All @@ -12,61 +15,3 @@ export type Node<T extends NodeTypes = NodeTypes> = (
) & {
type: T;
};

// --- babel specific nodes
export enum BabelNodeTypes {
ClassProperty = "ClassProperty",
ClassPrivateProperty = "ClassPrivateProperty",
ClassMethod = "ClassMethod",
ClassPrivateMethod = "ClassPrivateMethod",
TSDeclareMethod = "TSDeclareMethod",
PrivateName = "PrivateName",
File = "File",
}
// type BabelNodeTypes = (typeof BabelNodeTypes)[keyof typeof BabelNodeTypes];

type BabelNode =
| ClassProperty
| ClassPrivateProperty
| ClassMethod
| ClassPrivateMethod
| TSDeclareMethod
| PrivateName
| File;

type ClassProperty = Override<
TSESTree.PropertyDefinition,
{ type: BabelNodeTypes.ClassProperty; abstract: boolean; key: Node }
>;

type ClassPrivateProperty = Override<
ClassProperty,
{ type: BabelNodeTypes.ClassPrivateProperty }
>;

type ClassMethod = Override<
TSESTree.MethodDefinition,
{ type: BabelNodeTypes.ClassMethod; key: Node }
>;

type ClassPrivateMethod = Override<
ClassMethod,
{ type: BabelNodeTypes.ClassPrivateMethod }
>;

type TSDeclareMethod = Override<
TSESTree.TSAbstractMethodDefinition,
{ type: BabelNodeTypes.TSDeclareMethod; abstract: boolean; key: Node }
>;

type PrivateName = {
type: BabelNodeTypes.PrivateName;
id: Node;
};

type File = {
type: BabelNodeTypes.File;
program: Node;
};

type Override<T, U> = Omit<T, keyof U> & U;
48 changes: 48 additions & 0 deletions lib/ast/member-like.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types";
import * as BabelTypes from "@babel/types";

export const MemberLikeNodeTypesArray = [
// ts-es-tree
AST_NODE_TYPES.PropertyDefinition,
AST_NODE_TYPES.MethodDefinition,
AST_NODE_TYPES.TSAbstractMethodDefinition,
AST_NODE_TYPES.TSAbstractPropertyDefinition,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSIndexSignature,
AST_NODE_TYPES.TSMethodSignature,
AST_NODE_TYPES.TSPropertySignature,

// babel-ast
"ClassMethod",
"ClassPrivateMethod",
"ClassPrivateProperty",
"ClassProperty",
"TSDeclareMethod",
] as const;

export const MemberTypes = Object.fromEntries(
MemberLikeNodeTypesArray.map((type) => [type, type]),
) as { [K in MemberType]: K };

export type MemberType = (typeof MemberLikeNodeTypesArray)[number];

export type MemberNode<K extends MemberType = MemberType> = (
| never // avoid prettier bug

// babel-ast
| TSESTree.PropertyDefinition
| TSESTree.MethodDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSIndexSignature
| TSESTree.TSMethodSignature
| TSESTree.TSPropertySignature

// ts-es-tree
| BabelTypes.ClassMethod
| BabelTypes.ClassPrivateMethod
| BabelTypes.ClassPrivateProperty
| BabelTypes.ClassProperty
| BabelTypes.TSDeclareMethod
) & { type: K };
19 changes: 19 additions & 0 deletions lib/ast/member-like.typeckeck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NodeTypes } from ".";
import {
MemberNode,
MemberType,
MemberLikeNodeTypesArray,
} from "./member-like";

(function _() {
check<MemberType, NodeTypes>;
check<MemberNode["type"], MemberType>;
check<MemberType, MemberNode["type"]>;
check<typeof MemberLikeNodeTypesArray, readonly MemberType[]>;
check<MemberType, (typeof MemberLikeNodeTypesArray)[number]>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function check<S extends T, T>(_a: T, _b: S): never {
throw new Error("Just for type check");
}
})();
12 changes: 6 additions & 6 deletions lib/comparator/abstracted.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { BabelNodeTypes, Node } from "../ast";
import { Node } from "../ast";
import { MemberTypes } from "../ast/member-like";

export function abstracted(node: Node): boolean {
switch (node.type) {
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case MemberTypes.TSAbstractPropertyDefinition:
case MemberTypes.TSAbstractMethodDefinition:
return true;
case BabelNodeTypes.ClassProperty:
case BabelNodeTypes.TSDeclareMethod:
case MemberTypes.ClassProperty:
case MemberTypes.TSDeclareMethod:
return node.abstract === true;
}
return false;
Expand Down
12 changes: 6 additions & 6 deletions lib/comparator/accessibility.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { C, Comparator } from "./comparator";
import { BabelNodeTypes, Node } from "../ast";
import { MemberNode, MemberTypes } from "../ast/member-like";

export function accessibility<T extends Node>(): Comparator<T> {
export function accessibility<T extends MemberNode>(): Comparator<T> {
return C.by(($) => {
if ("accessibility" in $) {
switch ($.accessibility) {
Expand All @@ -15,12 +15,12 @@ export function accessibility<T extends Node>(): Comparator<T> {
}
}
switch ($.type) {
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.MethodDefinition:
case MemberTypes.PropertyDefinition:
case MemberTypes.MethodDefinition:
if ($.key.type === AST_NODE_TYPES.PrivateIdentifier) return 3;
break;
case BabelNodeTypes.ClassPrivateMethod:
case BabelNodeTypes.ClassPrivateProperty:
case MemberTypes.ClassPrivateMethod:
case MemberTypes.ClassPrivateProperty:
return 3;
}
return 0;
Expand Down
9 changes: 9 additions & 0 deletions lib/comparator/class-member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Node } from "../ast";
import { C, Comparator } from "./comparator";

export function classMember(): Comparator<Node> {
return C.by(($) => {
if ("static" in $) return $.static === true;
return false;
}, C.prefer);
}
12 changes: 8 additions & 4 deletions lib/comparator/decorated.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { AST_NODE_TYPES } from "@typescript-eslint/types";
import { Node } from "../ast";
import { MemberTypes } from "../ast/member-like";

export function decorated(node: Node): boolean {
switch (node.type) {
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.MethodDefinition:
return node.decorators.length > 0;
case MemberTypes.PropertyDefinition:
case MemberTypes.MethodDefinition:
case MemberTypes.ClassProperty:
case MemberTypes.ClassPrivateProperty:
case MemberTypes.ClassMethod:
case MemberTypes.ClassPrivateMethod:
return (node.decorators?.length ?? 0) > 0;
}
return false;
}
Loading

0 comments on commit 3b1401e

Please sign in to comment.