-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially support babel parser (#10)
- Loading branch information
Showing
15 changed files
with
305 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { printers as estreePrinters } from "prettier/plugins/estree"; | ||
import { preprocess as p } from "./lib/preprocess"; | ||
export { parsers } from "prettier/plugins/typescript"; | ||
import { AST } from "prettier"; | ||
|
||
export const printers = { | ||
estree: { | ||
...estreePrinters.estree, | ||
preprocess: p, | ||
}, | ||
}; | ||
const originalPreprocess = estreePrinters.estree.preprocess ?? ((x: AST) => x); | ||
|
||
estreePrinters.estree.preprocess = (x, options) => | ||
p(originalPreprocess(x, options)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,72 @@ | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types"; | ||
|
||
export const functionExpressions = [ | ||
AST_NODE_TYPES.FunctionExpression, | ||
AST_NODE_TYPES.ArrowFunctionExpression, | ||
]; | ||
|
||
export type NodeTypes = AST_NODE_TYPES | BabelNodeTypes; | ||
export type Node<T extends NodeTypes = NodeTypes> = ( | ||
| TSESTree.Node | ||
| BabelNode | ||
) & { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types"; | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import { BabelNodeTypes, Node } from "../ast"; | ||
|
||
export function abstracted(node: TSESTree.Node): boolean { | ||
export function abstracted(node: Node): boolean { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.TSAbstractPropertyDefinition: | ||
case AST_NODE_TYPES.TSAbstractMethodDefinition: | ||
return true; | ||
case BabelNodeTypes.ClassProperty: | ||
case BabelNodeTypes.TSDeclareMethod: | ||
return node.abstract === true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/types"; | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import { C, Comparator } from "./comparator"; | ||
import { select } from "./select"; | ||
import { BabelNodeTypes, Node } from "../ast"; | ||
|
||
export const keyIdentifierName = < | ||
T extends { | ||
key: TSESTree.Node; | ||
key: Node; | ||
}, | ||
>(): Comparator<T> => | ||
C.property( | ||
"key", | ||
C.capture<TSESTree.Node, TSESTree.Identifier>( | ||
select.node(AST_NODE_TYPES.Identifier), | ||
C.property("name", C.string), | ||
), | ||
); | ||
C.by(($) => { | ||
switch ($.key.type) { | ||
case AST_NODE_TYPES.Identifier: | ||
case AST_NODE_TYPES.PrivateIdentifier: | ||
return $.key.name; | ||
case BabelNodeTypes.PrivateName: | ||
if ($.key.id.type === AST_NODE_TYPES.Identifier) return $.key.id.name; | ||
} | ||
return null; | ||
}, C.maybe(C.string)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.