Skip to content

Commit

Permalink
chore: enable code formatting with prettier for composition library (w…
Browse files Browse the repository at this point in the history
…undergraph#368)

Co-authored-by: Aenimus <47415099+Aenimus@users.noreply.github.com>
  • Loading branch information
fiam and Aenimus authored Dec 15, 2023
1 parent 7ba1a80 commit 181aaa0
Show file tree
Hide file tree
Showing 36 changed files with 2,382 additions and 1,717 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/composition-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jobs:
- name: Test
run: pnpm run --filter composition test

- name: Lint
run: pnpm run --filter composition lint

- name: Run linter on composition-go
uses: ./.github/actions/go-linter
with:
Expand Down
6 changes: 4 additions & 2 deletions composition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"dev": "tsc --watch",
"build": "del dist && tsc",
"test:watch": "vitest test",
"test": "vitest run"
"test": "vitest run",
"lint": "prettier --check src tests",
"lint:fix": "prettier --write src tests"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -33,4 +35,4 @@
"del-cli": "^5.0.0",
"typescript": "^5.2.2"
}
}
}
51 changes: 34 additions & 17 deletions composition/src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { formatDescription } from './utils';

function deepCopyFieldsAndInterfaces(
node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode | ObjectTypeExtensionNode,
fields: MutableFieldDefinitionNode[], interfaces: NamedTypeNode[],
fields: MutableFieldDefinitionNode[],
interfaces: NamedTypeNode[],
) {
if (node.fields) {
for (const field of node.fields) {
Expand All @@ -42,15 +43,20 @@ function deepCopyFieldsAndInterfaces(
}
}

export type ConstValueNodeWithValue = IntValueNode | FloatValueNode | StringValueNode | BooleanValueNode | EnumValueNode;
export type ConstValueNodeWithValue =
| IntValueNode
| FloatValueNode
| StringValueNode
| BooleanValueNode
| EnumValueNode;

export function deepCopyTypeNode(node: TypeNode, parentName: string, fieldName: string): TypeNode {
const deepCopy: MutableTypeNode = { kind: node.kind };
let lastTypeNode = deepCopy;
for (let i = 0; i < maximumTypeNesting; i++) {
switch (node.kind) {
case Kind.NAMED_TYPE:
lastTypeNode.name = { ... node.name };
lastTypeNode.name = { ...node.name };
return deepCopy as TypeNode;
case Kind.LIST_TYPE:
lastTypeNode.kind = node.kind;
Expand Down Expand Up @@ -126,7 +132,7 @@ export function enumValueDefinitionNodeToMutable(node: EnumValueDefinitionNode):
description: formatDescription(node.description),
kind: node.kind,
name: { ...node.name },
}
};
}

export type MutableFieldDefinitionNode = {
Expand All @@ -138,7 +144,10 @@ export type MutableFieldDefinitionNode = {
type: TypeNode;
};

export function fieldDefinitionNodeToMutable(node: FieldDefinitionNode, parentName: string): MutableFieldDefinitionNode {
export function fieldDefinitionNodeToMutable(
node: FieldDefinitionNode,
parentName: string,
): MutableFieldDefinitionNode {
const args: MutableInputValueDefinitionNode[] = [];
if (node.arguments) {
for (const argument of node.arguments) {
Expand All @@ -162,7 +171,9 @@ export type MutableInputObjectTypeDefinitionNode = {
name: NameNode;
};

export function inputObjectTypeDefinitionNodeToMutable(node: InputObjectTypeDefinitionNode): MutableInputObjectTypeDefinitionNode {
export function inputObjectTypeDefinitionNodeToMutable(
node: InputObjectTypeDefinitionNode,
): MutableInputObjectTypeDefinitionNode {
const fields: MutableInputValueDefinitionNode[] = [];
if (node.fields) {
for (const field of node.fields) {
Expand All @@ -184,9 +195,12 @@ export type MutableInputValueDefinitionNode = {
kind: Kind.INPUT_VALUE_DEFINITION;
name: NameNode;
type: TypeNode;
}
};

export function inputValueDefinitionNodeToMutable(node: InputValueDefinitionNode, parentName: string): MutableInputValueDefinitionNode {
export function inputValueDefinitionNodeToMutable(
node: InputValueDefinitionNode,
parentName: string,
): MutableInputValueDefinitionNode {
return {
defaultValue: node.defaultValue ? { ...node.defaultValue } : undefined,
description: formatDescription(node.description),
Expand All @@ -204,9 +218,11 @@ export type MutableInterfaceTypeDefinitionNode = {
interfaces: NamedTypeNode[];
kind: Kind.INTERFACE_TYPE_DEFINITION;
name: NameNode;
}
};

export function interfaceTypeDefinitionNodeToMutable(node: InterfaceTypeDefinitionNode): MutableInterfaceTypeDefinitionNode {
export function interfaceTypeDefinitionNodeToMutable(
node: InterfaceTypeDefinitionNode,
): MutableInterfaceTypeDefinitionNode {
const fields: MutableFieldDefinitionNode[] = [];
const interfaces: NamedTypeNode[] = [];
deepCopyFieldsAndInterfaces(node, fields, interfaces);
Expand Down Expand Up @@ -262,7 +278,9 @@ export function objectTypeExtensionNodeToMutable(node: ObjectTypeExtensionNode):
};
}

export function objectTypeExtensionNodeToMutableDefinitionNode(node: ObjectTypeExtensionNode): MutableObjectTypeDefinitionNode {
export function objectTypeExtensionNodeToMutableDefinitionNode(
node: ObjectTypeExtensionNode,
): MutableObjectTypeDefinitionNode {
const fields: MutableFieldDefinitionNode[] = [];
const interfaces: NamedTypeNode[] = [];
deepCopyFieldsAndInterfaces(node, fields, interfaces);
Expand All @@ -271,7 +289,7 @@ export function objectTypeExtensionNodeToMutableDefinitionNode(node: ObjectTypeE
interfaces,
kind: Kind.OBJECT_TYPE_DEFINITION,
name: { ...node.name },
}
};
}

export type MutableScalarTypeDefinitionNode = {
Expand All @@ -293,7 +311,7 @@ export type MutableTypeNode = {
kind: Kind.NAMED_TYPE | Kind.LIST_TYPE | Kind.NON_NULL_TYPE;
name?: NameNode;
type?: MutableTypeNode;
}
};

export const maximumTypeNesting = 30;

Expand Down Expand Up @@ -321,17 +339,16 @@ export function unionTypeDefinitionNodeToMutable(node: UnionTypeDefinitionNode):
}

export type MutableTypeDefinitionNode =
MutableDirectiveDefinitionNode
| MutableDirectiveDefinitionNode
| MutableEnumTypeDefinitionNode
| MutableInputObjectTypeDefinitionNode
| MutableInterfaceTypeDefinitionNode
| MutableObjectTypeDefinitionNode
| MutableScalarTypeDefinitionNode
| MutableUnionTypeDefinitionNode;


export type ObjectLikeTypeNode =
InterfaceTypeDefinitionNode
| InterfaceTypeDefinitionNode
| InterfaceTypeExtensionNode
| ObjectTypeDefinitionNode
| ObjectTypeExtensionNode;
| ObjectTypeExtensionNode;
25 changes: 17 additions & 8 deletions composition/src/ast/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
FRAGMENT_SPREAD_UPPER,
INLINE_FRAGMENT_UPPER,
INPUT_FIELD_DEFINITION_UPPER,
INPUT_OBJECT_UPPER, INTERFACE_OBJECT,
INPUT_OBJECT_UPPER,
INTERFACE_OBJECT,
INTERFACE_UPPER,
KEY,
MUTATION,
Expand Down Expand Up @@ -266,7 +267,8 @@ export function isKindAbstract(kind: Kind) {
}

export function extractExecutableDirectiveLocations(
nodes: readonly NameNode[] | NameNode[], set: Set<string>,
nodes: readonly NameNode[] | NameNode[],
set: Set<string>,
): Set<string> {
for (const node of nodes) {
const name = node.value;
Expand All @@ -278,7 +280,8 @@ export function extractExecutableDirectiveLocations(
}

export function mergeExecutableDirectiveLocations(
nodes: readonly NameNode[] | NameNode[], directiveContainer: DirectiveContainer,
nodes: readonly NameNode[] | NameNode[],
directiveContainer: DirectiveContainer,
): Set<string> {
const mergedSet = new Set<string>();
for (const node of nodes) {
Expand Down Expand Up @@ -324,7 +327,8 @@ export function addConcreteTypesForImplementedInterfaces(
}

export function addConcreteTypesForUnion(
node: UnionTypeDefinitionNode | UnionTypeExtensionNode, abstractToConcreteTypeNames: Map<string, Set<string>>,
node: UnionTypeDefinitionNode | UnionTypeExtensionNode,
abstractToConcreteTypeNames: Map<string, Set<string>>,
) {
if (!node.types || node.types.length < 1) {
return;
Expand Down Expand Up @@ -356,7 +360,11 @@ export function formatDescription(description?: StringValueNode): StringValueNod
}

export function setLongestDescriptionForNode(
existingNode: MutableFieldDefinitionNode | MutableEnumValueDefinitionNode | MutableInputValueDefinitionNode | MutableTypeDefinitionNode,
existingNode:
| MutableFieldDefinitionNode
| MutableEnumValueDefinitionNode
| MutableInputValueDefinitionNode
| MutableTypeDefinitionNode,
newDescription?: StringValueNode,
) {
if (!newDescription) {
Expand Down Expand Up @@ -385,7 +393,8 @@ export function lexicographicallySortSelectionSetNode(selectionSetNode: Selectio
...selection,
arguments: lexicographicallySortArgumentNodes(selection),
selectionSet: selection.selectionSet
? lexicographicallySortSelectionSetNode(selection.selectionSet) : selection.selectionSet,
? lexicographicallySortSelectionSetNode(selection.selectionSet)
: selection.selectionSet,
})),
};
}
Expand All @@ -408,7 +417,7 @@ export function lexicographicallySortDocumentNode(documentNode: DocumentNode): D
type ParseResult = {
documentNode?: DocumentNode;
error?: Error;
}
};

export function safeParse(value: string): ParseResult {
try {
Expand All @@ -417,4 +426,4 @@ export function safeParse(value: string): ParseResult {
} catch (e) {
return { error: e as Error };
}
}
}
Loading

0 comments on commit 181aaa0

Please sign in to comment.