Skip to content

Commit

Permalink
Remove utility-types (facebook#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegreatercurve authored Jun 15, 2022
1 parent 239129f commit ddbe465
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 32 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
"tmp": "^0.2.1",
"ts-jest": "^28.0.2",
"typescript": "^4.6.4",
"utility-types": "^3.10.0",
"uuid": "^8.3.2"
},
"overrides": {
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-headless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import type {
LexicalEditor,
LexicalNode,
} from 'lexical';
import type {Klass} from 'shared/types';

import {createEditor} from 'lexical';
import {Class} from 'utility-types';

export function createHeadlessEditor(editorConfig?: {
disableEvents?: boolean;
editorState?: EditorState;
namespace: string;
nodes?: ReadonlyArray<Class<LexicalNode>>;
nodes?: ReadonlyArray<Klass<LexicalNode>>;
onError: (error: Error) => void;
parentEditor?: LexicalEditor;
readOnly?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-playground/src/nodes/PlaygroundNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import type {LexicalNode} from 'lexical';
import type {Klass} from 'shared/types';

import {CodeHighlightNode, CodeNode} from '@lexical/code';
import {HashtagNode} from '@lexical/hashtag';
Expand All @@ -17,7 +18,6 @@ import {OverflowNode} from '@lexical/overflow';
import {HorizontalRuleNode} from '@lexical/react/LexicalHorizontalRuleNode';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {TableCellNode, TableNode, TableRowNode} from '@lexical/table';
import {Class} from 'utility-types';

import {AutocompleteNode} from './AutocompleteNode';
import {EmojiNode} from './EmojiNode';
Expand All @@ -32,7 +32,7 @@ import {TweetNode} from './TweetNode';
import {TypeaheadNode} from './TypeaheadNode';
import {YouTubeNode} from './YouTubeNode';

const PlaygroundNodes: Array<Class<LexicalNode>> = [
const PlaygroundNodes: Array<Klass<LexicalNode>> = [
HeadingNode,
ListNode,
ListItemNode,
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-react/src/DEPRECATED_useLexical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import type {
LexicalEditor,
LexicalNode,
} from 'lexical';
import type {Klass} from 'shared/types';

import {createEditor} from 'lexical';
import {useMemo} from 'react';
import {Class} from 'utility-types';

import {useLexicalEditor} from './DEPRECATED_useLexicalEditor';

export function useLexical(editorConfig: {
disableEvents?: boolean;
editorState?: EditorState;
namespace: string;
nodes?: ReadonlyArray<Class<LexicalNode>>;
nodes?: ReadonlyArray<Klass<LexicalNode>>;
onError: (error: Error) => void;
parentEditor?: LexicalEditor;
readOnly?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-react/src/useLexicalTextEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import type {EntityMatch} from '@lexical/text';
import type {TextNode} from 'lexical';
import type {Klass} from 'shared/types';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {registerLexicalTextEntity} from '@lexical/text';
import {mergeRegister} from '@lexical/utils';
import {useEffect} from 'react';
import {Class} from 'utility-types';

export function useLexicalTextEntity<N extends TextNode>(
getMatch: (text: string) => null | EntityMatch,
targetNode: Class<N>,
targetNode: Klass<N>,
createNode: (textNode: TextNode) => N,
): void {
const [editor] = useLexicalComposerContext();
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import type {ElementNode, LexicalEditor, LexicalNode, RootNode} from 'lexical';
import type {Klass} from 'shared/types';

import {
$createTextNode,
Expand All @@ -16,7 +17,6 @@ import {
TextNode,
} from 'lexical';
import invariant from 'shared/invariant';
import {Class} from 'utility-types';

export type TextNodeWithOffset = {
node: TextNode;
Expand Down Expand Up @@ -255,7 +255,7 @@ export type EntityMatch = {end: number; start: number};
export function registerLexicalTextEntity<N extends TextNode>(
editor: LexicalEditor,
getMatch: (text: string) => null | EntityMatch,
targetNode: Class<N>,
targetNode: Klass<N>,
createNode: (textNode: TextNode) => N,
): Array<() => void> {
const isTargetNode = (node: LexicalNode | null | undefined): node is N => {
Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
LexicalNode,
NodeKey,
} from 'lexical';
import type {Klass} from 'shared/types';

import {
$getRoot,
Expand All @@ -22,7 +23,6 @@ import {
createEditor,
} from 'lexical';
import invariant from 'shared/invariant';
import {Class} from 'utility-types';

export type DFSNode = Readonly<{
depth: number;
Expand Down Expand Up @@ -105,7 +105,7 @@ function $getDepth(node: LexicalNode): number {

export function $getNearestNodeOfType<T extends ElementNode>(
node: LexicalNode,
klass: Class<T>,
klass: Klass<T>,
): T | LexicalNode {
let parent: T | LexicalNode = node;

Expand Down Expand Up @@ -173,7 +173,7 @@ export function mergeRegister(...func: Array<Func>): () => void {

export function registerNestedElementResolver<N extends ElementNode>(
editor: LexicalEditor,
targetNode: {new (...args: unknown[]): N},
targetNode: Klass<N>,
cloneNode: (from: N) => N,
handleOverlap: (from: N, to: N) => void,
): () => void {
Expand Down
18 changes: 8 additions & 10 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import type {EditorState, SerializedEditorState} from './LexicalEditorState';
import type {DOMConversion, LexicalNode, NodeKey} from './LexicalNode';
import type {Klass} from 'shared/types';

import getDOMSelection from 'shared/getDOMSelection';
import invariant from 'shared/invariant';
import {Class} from 'utility-types';

import {$getRoot, $getSelection, TextNode} from '.';
import {FULL_RECONCILE, NO_DIRTY_NODES} from './LexicalConstants';
Expand Down Expand Up @@ -120,17 +120,17 @@ export type EditorConfig = {
export type RegisteredNodes = Map<string, RegisteredNode>;

export type RegisteredNode = {
klass: Class<LexicalNode>;
klass: Klass<LexicalNode>;
transforms: Set<Transform<LexicalNode>>;
};

export type Transform<T> = (node: T) => void;

export type ErrorHandler = (error: Error) => void;

export type MutationListeners = Map<MutationListener, Class<LexicalNode>>;
export type MutationListeners = Map<MutationListener, Klass<LexicalNode>>;

export type MutatedNodes = Map<Class<LexicalNode>, Map<NodeKey, NodeMutation>>;
export type MutatedNodes = Map<Klass<LexicalNode>, Map<NodeKey, NodeMutation>>;

export type NodeMutation = 'created' | 'updated' | 'destroyed';

Expand Down Expand Up @@ -286,7 +286,7 @@ export function createEditor(editorConfig?: {
disableEvents?: boolean;
editorState?: EditorState;
namespace?: string;
nodes?: ReadonlyArray<Class<LexicalNode>>;
nodes?: ReadonlyArray<Klass<LexicalNode>>;
onError?: ErrorHandler;
parentEditor?: LexicalEditor;
readOnly?: boolean;
Expand Down Expand Up @@ -578,7 +578,7 @@ export class LexicalEditor {
}

registerMutationListener(
klass: Class<LexicalNode>,
klass: Klass<LexicalNode>,
listener: MutationListener,
): () => void {
// @ts-expect-error TODO Replace Class utility type with InstanceType
Expand All @@ -600,7 +600,7 @@ export class LexicalEditor {
}

registerNodeTransform<T extends LexicalNode>(
klass: Class<T>,
klass: Klass<T>,
listener: Transform<T>,
): () => void {
// @ts-expect-error TODO Replace Class utility type with InstanceType
Expand All @@ -624,9 +624,7 @@ export class LexicalEditor {
};
}

hasNodes<T extends {new (...args: unknown[]): LexicalNode}>(
nodes: Array<T>,
): boolean {
hasNodes<T extends Klass<LexicalNode>>(nodes: Array<T>): boolean {
for (let i = 0; i < nodes.length; i++) {
const klass = nodes[i];
// @ts-expect-error
Expand Down
4 changes: 2 additions & 2 deletions packages/lexical/src/LexicalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import type {EditorConfig, LexicalEditor} from './LexicalEditor';
import type {RangeSelection} from './LexicalSelection';
import type {Klass} from 'shared/types';

import invariant from 'shared/invariant';
import {Class} from 'utility-types';

import {$isElementNode, $isRootNode, $isTextNode, ElementNode} from '.';
import {
Expand Down Expand Up @@ -778,7 +778,7 @@ export class LexicalNode {

function errorOnTypeKlassMismatch(
type: string,
klass: Class<LexicalNode>,
klass: Klass<LexicalNode>,
): void {
const registeredNode = getActiveEditor()._nodes.get(type);
// Common error - split in its own invariant
Expand Down
6 changes: 2 additions & 4 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import type {
} from './LexicalSelection';
import type {RootNode} from './nodes/LexicalRootNode';
import type {TextFormatType, TextNode} from './nodes/LexicalTextNode';
import type {Klass} from 'shared/types';

import {IS_APPLE, IS_IOS, IS_SAFARI} from 'shared/environment';
import getDOMSelection from 'shared/getDOMSelection';
import invariant from 'shared/invariant';
import {Class} from 'utility-types';

import {
$createTextNode,
Expand Down Expand Up @@ -992,9 +992,7 @@ export function setMutatedNode(
}
}

export function $nodesOfType<T extends LexicalNode>(
klass: Class<LexicalNode>,
): Array<T> {
export function $nodesOfType<T extends LexicalNode>(klass: Klass<T>): Array<T> {
const editorState = getActiveEditorState();
const readOnly = editorState._readOnly;
// @ts-expect-error TODO Replace Class utility type with InstanceType
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {LexicalNode} from 'lexical';

export type Klass<T extends LexicalNode> = new (...args: unknown[]) => T;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
],
"shared/invariant": ["./packages/shared/src/invariant.ts"],
"shared/environment": ["./packages/shared/src/environment.ts"],
"shared/useLayoutEffect": ["./packages/shared/src/useLayoutEffect.ts"]
"shared/useLayoutEffect": ["./packages/shared/src/useLayoutEffect.ts"],
"shared/types": ["./packages/shared/src/types.d.ts"]
}
},
"include": ["./libdefs", "./packages"],
Expand Down

0 comments on commit ddbe465

Please sign in to comment.