Skip to content

Commit

Permalink
Add ESLint rule for consistent type imports (#10852)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas authored May 15, 2023
1 parent e96812f commit 27fbdb3
Show file tree
Hide file tree
Showing 107 changed files with 447 additions and 350 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-ravens-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Chore: Add ESLint rule for consistent type imports, apply autofix
14 changes: 13 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
"ecmaVersion": "latest"
},
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"excludedFiles": ["**/__tests__/**/*.*"],
"rules": {
"@typescript-eslint/consistent-type-imports": ["error", {
"prefer": "type-imports",
"disallowTypeAnnotations": false,
"fixStyle": "separate-type-imports"
}],
"@typescript-eslint/no-import-type-side-effects": "error"
}
},
{
"files": ["**/__tests__/**/*.[jt]sx", "**/?(*.)+(test).[jt]sx"],
"extends": ["plugin:testing-library/react"],
Expand All @@ -18,5 +30,5 @@
"testing-library/no-wait-for-multiple-assertions": "off"
}
}
]
],
}
11 changes: 6 additions & 5 deletions src/cache/core/cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DocumentNode } from 'graphql';
import type { DocumentNode } from 'graphql';
import { wrap } from 'optimism';

import {
import type {
StoreObject,
Reference,
Reference} from '../../utilities';
import {
getFragmentQueryDocument,
} from '../../utilities';
import { DataProxy } from './types/DataProxy';
import { Cache } from './types/Cache';
import type { DataProxy } from './types/DataProxy';
import type { Cache } from './types/Cache';

export type Transaction<T> = (c: ApolloCache<T>) => void;

Expand Down
4 changes: 2 additions & 2 deletions src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataProxy } from './DataProxy';
import { Modifier, Modifiers } from './common';
import { ApolloCache } from '../cache';
import type { Modifier, Modifiers } from './common';
import type { ApolloCache } from '../cache';

export namespace Cache {
export type WatchCallback<TData = any> = (
Expand Down
8 changes: 4 additions & 4 deletions src/cache/core/types/DataProxy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DocumentNode } from 'graphql'; // eslint-disable-line import/no-extraneous-dependencies, import/no-unresolved
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import type { DocumentNode } from 'graphql'; // ignore-comment eslint-disable-line import/no-extraneous-dependencies, import/no-unresolved
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';

import { MissingFieldError } from './common';
import { Reference } from '../../../utilities';
import type { MissingFieldError } from './common';
import type { Reference } from '../../../utilities';

export namespace DataProxy {
export interface Query<TVariables, TData> {
Expand Down
6 changes: 3 additions & 3 deletions src/cache/core/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DocumentNode, FieldNode } from 'graphql';
import type { DocumentNode, FieldNode } from 'graphql';

import {
import type {
Reference,
StoreObject,
StoreValue,
isReference,
} from '../../../utilities';

import { StorageType } from '../../inmemory/policies';
import type { StorageType } from '../../inmemory/policies';

// The Readonly<T> type only really works for object types, since it marks
// all of the object's properties as readonly, but there are many cases when
Expand Down
18 changes: 10 additions & 8 deletions src/cache/inmemory/entityStore.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { invariant } from '../../utilities/globals';
import { dep, OptimisticDependencyFunction } from 'optimism';
import type { OptimisticDependencyFunction } from 'optimism';
import { dep } from 'optimism';
import { equal } from '@wry/equality';
import { Trie } from '@wry/trie';

import {
isReference,
import type {
StoreValue,
StoreObject,
Reference,
Reference} from '../../utilities';
import {
isReference,
makeReference,
DeepMerger,
maybeDeepFreeze,
canUseWeakMap,
isNonNullObject,
} from '../../utilities';
import { NormalizedCache, NormalizedCacheObject } from './types';
import type { NormalizedCache, NormalizedCacheObject } from './types';
import { hasOwn, fieldNameFromStoreName } from './helpers';
import { Policies, StorageType } from './policies';
import { Cache } from '../core/types/Cache';
import {
import type { Policies, StorageType } from './policies';
import type { Cache } from '../core/types/Cache';
import type {
SafeReadonly,
Modifier,
Modifiers,
Expand Down
8 changes: 5 additions & 3 deletions src/cache/inmemory/fragmentRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
import type {
DocumentNode,
ASTNode,
FragmentDefinitionNode,
FragmentSpreadNode,
FragmentSpreadNode} from "graphql";
import {
visit,
} from "graphql";

import { wrap } from "optimism";

import { FragmentMap, getFragmentDefinitions } from "../../utilities";
import type { FragmentMap} from "../../utilities";
import { getFragmentDefinitions } from "../../utilities";

export interface FragmentRegistryAPI {
register(...fragments: DocumentNode[]): this;
Expand Down
17 changes: 9 additions & 8 deletions src/cache/inmemory/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { DocumentNode, FragmentDefinitionNode, SelectionSetNode } from 'graphql';
import type { DocumentNode, FragmentDefinitionNode, SelectionSetNode } from 'graphql';

import {
import type {
NormalizedCache,
InMemoryCacheConfig,
} from './types';

import { KeyFieldsContext } from './policies';
import { FragmentRegistryAPI } from './fragmentRegistry';
import type { KeyFieldsContext } from './policies';
import type { FragmentRegistryAPI } from './fragmentRegistry';

import {
import type {
Reference,
isReference,
StoreValue,
StoreObject,
FragmentMap,
FragmentMapFunction} from '../../utilities';
import {
isReference,
isField,
DeepMerger,
resultKeyNameFromField,
shouldInclude,
isNonNullObject,
compact,
FragmentMap,
FragmentMapFunction,
createFragmentMap,
getFragmentDefinitions,
isArray,
Expand Down
16 changes: 9 additions & 7 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { invariant } from '../../utilities/globals';
// Make builtins like Map and Set safe to use with non-extensible objects.
import './fixPolyfills';

import { DocumentNode } from 'graphql';
import { OptimisticWrapperFunction, wrap } from 'optimism';
import type { DocumentNode } from 'graphql';
import type { OptimisticWrapperFunction} from 'optimism';
import { wrap } from 'optimism';
import { equal } from '@wry/equality';

import { ApolloCache } from '../core/cache';
import { Cache } from '../core/types/Cache';
import type { Cache } from '../core/types/Cache';
import { MissingFieldError } from '../core/types/common';
import type {
StoreObject,
Reference} from '../../utilities';
import {
addTypenameToDocument,
StoreObject,
Reference,
isReference,
} from '../../utilities';
import { InMemoryCacheConfig, NormalizedCacheObject } from './types';
import type { InMemoryCacheConfig, NormalizedCacheObject } from './types';
import { StoreReader } from './readFromStore';
import { StoreWriter } from './writeToStore';
import { EntityStore, supportsResultCaching } from './entityStore';
import { makeVar, forgetCache, recallCache } from './reactiveVars';
import { Policies } from './policies';
import { hasOwn, normalizeConfig, shouldCanonizeResults } from './helpers';
import { canonicalStringify } from './object-canon';
import { OperationVariables } from '../../core';
import type { OperationVariables } from '../../core';

type BroadcastOptions = Pick<
Cache.BatchOptions<InMemoryCache>,
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/key-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../../utilities";

import { hasOwn, isArray } from "./helpers";
import {
import type {
KeySpecifier,
KeyFieldsFunction,
KeyArgsFunction,
Expand Down
17 changes: 9 additions & 8 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { invariant, InvariantError, __DEV__ } from '../../utilities/globals';

import {
import type {
InlineFragmentNode,
FragmentDefinitionNode,
SelectionSetNode,
FieldNode,
} from 'graphql';

import {
import type {
FragmentMap,
storeKeyNameFromField,
StoreValue,
StoreObject,
Reference} from '../../utilities';
import {
storeKeyNameFromField,
argumentsObjectFromField,
Reference,
isReference,
getStoreKeyName,
isNonNullObject,
stringifyForDisplay,
} from '../../utilities';
import {
import type {
IdGetter,
MergeInfo,
NormalizedCache,
Expand All @@ -35,16 +36,16 @@ import {
isArray,
} from './helpers';
import { cacheSlot } from './reactiveVars';
import { InMemoryCache } from './inMemoryCache';
import {
import type { InMemoryCache } from './inMemoryCache';
import type {
SafeReadonly,
FieldSpecifier,
ToReferenceFunction,
ReadFieldFunction,
ReadFieldOptions,
CanReadFunction,
} from '../core/types/common';
import { WriteContext } from './writeToStore';
import type { WriteContext } from './writeToStore';

// Upgrade to a faster version of the default stable JSON.stringify function
// used by getStoreKeyName. This function is used when computing storeFieldName
Expand Down
7 changes: 4 additions & 3 deletions src/cache/inmemory/reactiveVars.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dep, OptimisticDependencyFunction } from "optimism";
import type { OptimisticDependencyFunction } from "optimism";
import { dep } from "optimism";
import { Slot } from "@wry/context";
import { InMemoryCache } from "./inMemoryCache";
import { ApolloCache } from '../../core';
import type { InMemoryCache } from "./inMemoryCache";
import type { ApolloCache } from '../../core';

export interface ReactiveVar<T> {
(newValue?: T): T;
Expand Down
32 changes: 18 additions & 14 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { invariant, InvariantError, __DEV__ } from '../../utilities/globals';

import {
import type {
DocumentNode,
FieldNode,
Kind,
SelectionSetNode,
SelectionSetNode} from 'graphql';
import {
Kind
} from 'graphql';
import { wrap, OptimisticWrapperFunction } from 'optimism';
import type { OptimisticWrapperFunction } from 'optimism';
import { wrap } from 'optimism';

import type {
Reference,
StoreObject,
FragmentMap,
FragmentMapFunction} from '../../utilities';
import {
isField,
resultKeyNameFromField,
Reference,
isReference,
makeReference,
StoreObject,
FragmentMap,
shouldInclude,
addTypenameToDocument,
getDefaultValues,
Expand All @@ -27,21 +31,21 @@ import {
DeepMerger,
isNonNullObject,
canUseWeakMap,
compact,
FragmentMapFunction,
compact
} from '../../utilities';
import { Cache } from '../core/types/Cache';
import {
import type { Cache } from '../core/types/Cache';
import type {
DiffQueryAgainstStoreOptions,
InMemoryCacheConfig,
NormalizedCache,
ReadMergeModifyContext,
} from './types';
import { maybeDependOnExistenceOfEntity, supportsResultCaching } from './entityStore';
import { isArray, extractFragmentContext, getTypenameFromStoreObject, shouldCanonizeResults } from './helpers';
import { Policies } from './policies';
import { InMemoryCache } from './inMemoryCache';
import { MissingFieldError, MissingTree } from '../core/types/common';
import type { Policies } from './policies';
import type { InMemoryCache } from './inMemoryCache';
import type { MissingTree } from '../core/types/common';
import { MissingFieldError } from '../core/types/common';
import { canonicalStringify, ObjectCanon } from './object-canon';

export type VariableMap = { [name: string]: any };
Expand Down
12 changes: 6 additions & 6 deletions src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { DocumentNode, FieldNode } from 'graphql';
import type { DocumentNode, FieldNode } from 'graphql';

import { Transaction } from '../core/cache';
import type { Transaction } from '../core/cache';
import {
StoreObject,
StoreValue,
Reference,
} from '../../utilities';
import { FieldValueGetter } from './entityStore';
import {
import type { FieldValueGetter } from './entityStore';
import type {
TypePolicies,
PossibleTypesMap,
KeyFieldsFunction,
StorageType,
FieldMergeFunction,
} from './policies';
import {
import type {
Modifier,
Modifiers,
ToReferenceFunction,
CanReadFunction,
} from '../core/types/common';

import { FragmentRegistryAPI } from './fragmentRegistry';
import type { FragmentRegistryAPI } from './fragmentRegistry';

export { StoreObject, StoreValue, Reference }

Expand Down
Loading

0 comments on commit 27fbdb3

Please sign in to comment.