From bfc59df972570de19c74bf55ec35f25cfad1270a Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Sat, 5 May 2018 08:56:11 +1000 Subject: [PATCH] fix(prosemirror-*): replace void with undefined in return union types (#25520) * fix(prosemirror-*): replace void with undefined in return union types * fix(prosemirror-state): PluginSpec#appendTransaction return type * fix(prosemirror-model): remove unnecessary assertion * fix(prosemirror-state): remove unnecessary assertion --- types/prosemirror-collab/index.d.ts | 2 +- .../prosemirror-collab-tests.ts | 1 + types/prosemirror-inputrules/index.d.ts | 6 +-- .../prosemirror-inputrules-tests.ts | 5 +- types/prosemirror-model/index.d.ts | 40 ++++++++-------- .../prosemirror-model-tests.ts | 48 +++++++++++++++++-- types/prosemirror-state/index.d.ts | 8 ++-- .../prosemirror-state-tests.ts | 12 +++++ types/prosemirror-transform/index.d.ts | 14 +++--- .../prosemirror-transform-tests.ts | 14 ++++++ types/prosemirror-view/index.d.ts | 8 ++-- .../prosemirror-view-tests.ts | 12 +++++ 12 files changed, 127 insertions(+), 43 deletions(-) diff --git a/types/prosemirror-collab/index.d.ts b/types/prosemirror-collab/index.d.ts index cb4949bcba..6412b4b2e2 100644 --- a/types/prosemirror-collab/index.d.ts +++ b/types/prosemirror-collab/index.d.ts @@ -47,7 +47,7 @@ export function sendableSteps( steps: Array>; clientID: number | string; origins: Array>; -} | null | void; +} | null | undefined; /** * Get the version up to which the collab plugin has synced with the * central authority. diff --git a/types/prosemirror-collab/prosemirror-collab-tests.ts b/types/prosemirror-collab/prosemirror-collab-tests.ts index 9d892ca707..60dd42d7d9 100644 --- a/types/prosemirror-collab/prosemirror-collab-tests.ts +++ b/types/prosemirror-collab/prosemirror-collab-tests.ts @@ -10,3 +10,4 @@ plugin = collab.collab({ version: 1 }); plugin = collab.collab({ clientID: 1 }); const sendableSteps = collab.sendableSteps(state); +sendableSteps!.clientID; diff --git a/types/prosemirror-inputrules/index.d.ts b/types/prosemirror-inputrules/index.d.ts index 7c405530e0..7d14305ea6 100644 --- a/types/prosemirror-inputrules/index.d.ts +++ b/types/prosemirror-inputrules/index.d.ts @@ -42,7 +42,7 @@ export class InputRule { match: string[], start: number, end: number - ) => Transaction | null | void) + ) => Transaction | null) ); } /** @@ -81,7 +81,7 @@ export function undoInputRule( export function wrappingInputRule( regexp: RegExp, nodeType: NodeType, - getAttrs?: { [key: string]: any } | ((p: string[]) => { [key: string]: any } | null | void), + getAttrs?: { [key: string]: any } | ((p: string[]) => { [key: string]: any } | null | undefined), joinPredicate?: (p1: string[], p2: ProsemirrorNode) => boolean ): InputRule; /** @@ -95,7 +95,7 @@ export function wrappingInputRule( export function textblockTypeInputRule( regexp: RegExp, nodeType: NodeType, - getAttrs?: { [key: string]: any } | ((p: string[]) => { [key: string]: any } | null | void) + getAttrs?: { [key: string]: any } | ((p: string[]) => { [key: string]: any } | null | undefined) ): InputRule; /** * Converts double dashes to an emdash. diff --git a/types/prosemirror-inputrules/prosemirror-inputrules-tests.ts b/types/prosemirror-inputrules/prosemirror-inputrules-tests.ts index 0d59a02be7..25fa52e6af 100644 --- a/types/prosemirror-inputrules/prosemirror-inputrules-tests.ts +++ b/types/prosemirror-inputrules/prosemirror-inputrules-tests.ts @@ -1,5 +1,8 @@ import * as inputrules from 'prosemirror-inputrules'; import { NodeType } from 'prosemirror-model'; +import { Transaction } from 'prosemirror-state'; const nodeType = new NodeType(); -const rule: inputrules.InputRule = inputrules.wrappingInputRule(/^\$/, nodeType); +const rule1: inputrules.InputRule = inputrules.wrappingInputRule(/^\$/, nodeType); +const rule2 = new inputrules.InputRule(/^$/, 'str'); +const rule3 = new inputrules.InputRule(/^$/, () => null); diff --git a/types/prosemirror-model/index.d.ts b/types/prosemirror-model/index.d.ts index adb8a6141c..095b88f3ba 100644 --- a/types/prosemirror-model/index.d.ts +++ b/types/prosemirror-model/index.d.ts @@ -35,12 +35,12 @@ export class ContentMatch { * Match a node type and marks, returning a match after that node * if successful. */ - matchType(type: NodeType): ContentMatch | null | void; + matchType(type: NodeType): ContentMatch | null | undefined; /** * Try to match a fragment. Returns the resulting match when * successful. */ - matchFragment(frag: Fragment, start?: number, end?: number): ContentMatch | null | void; + matchFragment(frag: Fragment, start?: number, end?: number): ContentMatch | null | undefined; /** * Try to match the given fragment, and if that fails, see if it can * be made to match by inserting nodes in front of it. When @@ -49,14 +49,14 @@ export class ContentMatch { * return a fragment if the resulting match goes to the end of the * content expression. */ - fillBefore(after: Fragment, toEnd?: boolean, startIndex?: number): Fragment | null | void; + fillBefore(after: Fragment, toEnd?: boolean, startIndex?: number): Fragment | null | undefined; /** * Find a set of wrapping node types that would allow a node of the * given type to appear at this position. The result may be empty * (when it fits directly) and will be null when no such wrapping * exists. */ - findWrapping(target: NodeType): Array> | null | void; + findWrapping(target: NodeType): Array> | null | undefined; /** * Get the _n_th outgoing edge from this node in the finite automaton * that describes the content expression. @@ -89,7 +89,7 @@ export class Fragment { start: number, parent: ProsemirrorNode, index: number - ) => boolean | null | void, + ) => boolean | null | undefined | void, startPos?: number ): void; /** @@ -101,7 +101,7 @@ export class Fragment { node: ProsemirrorNode, pos: number, parent: ProsemirrorNode - ) => boolean | null | void + ) => boolean | null | undefined | void ): void; /** * Create a new fragment containing the combined content of this @@ -141,7 +141,7 @@ export class Fragment { /** * Get the child node at the given index, if it exists. */ - maybeChild(index: number): ProsemirrorNode | null | void; + maybeChild(index: number): ProsemirrorNode | null | undefined; /** * Call `f` for every child node, passing the node, its offset * into this parent node, and its index. @@ -151,14 +151,14 @@ export class Fragment { * Find the first position at which this fragment and another * fragment differ, or `null` if they are the same. */ - findDiffStart(other: Fragment): number | null | void; + findDiffStart(other: Fragment): number | null | undefined; /** * Find the first position, searching from the end, at which this * fragment and the given fragment differ, or `null` if they are the * same. Since this position will not be the same in both nodes, an * object with two separate positions is returned. */ - findDiffEnd(other: ProsemirrorNode): { a: number; b: number } | null | void; + findDiffEnd(other: ProsemirrorNode): { a: number; b: number } | null | undefined; /** * Return a debugging string that describes this fragment. */ @@ -166,7 +166,7 @@ export class Fragment { /** * Create a JSON-serializeable representation of this fragment. */ - toJSON(): { [key: string]: any } | null | void; + toJSON(): { [key: string]: any } | null | undefined; /** * Deserialize a fragment from its JSON representation. */ @@ -326,7 +326,7 @@ export interface ParseRule { * Called with a DOM Element for `tag` rules, and with a string (the * style's value) for `style` rules. */ - getAttrs?: ((p: Node | string) => { [key: string]: any } | false | null | void) | null; + getAttrs?: ((p: Node | string) => { [key: string]: any } | false | null | undefined) | null; /** * For `tag` rules that produce non-leaf nodes or marks, by default * the content of the DOM element is parsed as content of the mark @@ -507,7 +507,7 @@ declare class ProsemirrorNode { /** * Get the child node at the given index, if it exists. */ - maybeChild(index: number): ProsemirrorNode | null | void; + maybeChild(index: number): ProsemirrorNode | null | undefined; /** * Call `f` for every child node, passing the node, its offset * into this parent node, and its index. @@ -529,7 +529,7 @@ declare class ProsemirrorNode { pos: number, parent: ProsemirrorNode, index: number - ) => boolean | null | void, + ) => boolean | null | undefined | void, startPos?: number ): void; /** @@ -541,7 +541,7 @@ declare class ProsemirrorNode { node: ProsemirrorNode, pos: number, parent: ProsemirrorNode - ) => boolean | null | void + ) => boolean | null | undefined | void ): void; /** * Concatenates all the text nodes found in this fragment and its @@ -612,7 +612,7 @@ declare class ProsemirrorNode { /** * Find the node starting at the given position. */ - nodeAt(pos: number): ProsemirrorNode | null | void; + nodeAt(pos: number): ProsemirrorNode | null | undefined; /** * Find the (direct) child node after the given offset, if any, * and return it along with its index and offset relative to this @@ -769,7 +769,7 @@ export class Slice { /** * Convert a slice to a JSON-serializable representation. */ - toJSON(): { [key: string]: any } | null | void; + toJSON(): { [key: string]: any } | null | undefined; /** * Deserialize a slice from its JSON representation. */ @@ -892,7 +892,7 @@ export class ResolvedPos { * its parent node or its parent node isn't a textblock (in which * case no marks should be preserved). */ - marksAcross($end: ResolvedPos): Array> | null | void; + marksAcross($end: ResolvedPos): Array> | null | undefined; /** * The depth up to which this position and the given (non-resolved) * position share the same parent nodes. @@ -910,7 +910,7 @@ export class ResolvedPos { blockRange( other?: ResolvedPos, pred?: (p: ProsemirrorNode) => boolean - ): NodeRange | null | void; + ): NodeRange | null | undefined; /** * Query whether the given position shares the same parent node. */ @@ -1061,7 +1061,7 @@ export class NodeType { attrs?: { [key: string]: any }, content?: Fragment | ProsemirrorNode | Array>, marks?: Array> - ): ProsemirrorNode | null | void; + ): ProsemirrorNode | null | undefined; /** * Returns true if the given fragment is valid content for this node * type with the given attributes. @@ -1113,7 +1113,7 @@ export class MarkType { /** * Tests whether there is a mark of this type in the given set. */ - isInSet(set: Array>): Mark | null | void; + isInSet(set: Array>): Mark | null | undefined; /** * Queries whether a given mark type is * [excluded](#model.MarkSpec.excludes) by this one. diff --git a/types/prosemirror-model/prosemirror-model-tests.ts b/types/prosemirror-model/prosemirror-model-tests.ts index 98460d4313..b9041597e7 100644 --- a/types/prosemirror-model/prosemirror-model-tests.ts +++ b/types/prosemirror-model/prosemirror-model-tests.ts @@ -37,6 +37,48 @@ export const nodeSpec: model.NodeSpec = { } }; -const node = new model.Node(); -node.nodesBetween(0, 1, () => {}); -node.descendants(() => {}); +// Verify that non-null assertion operator can be used. + +const res1_1 = new model.Node(); +res1_1.nodesBetween(0, 1, () => {}); +res1_1.nodesBetween(0, 1, () => null); +res1_1.nodesBetween(0, 1, () => undefined); +res1_1.nodesBetween(0, 1, () => true); +res1_1.descendants(() => {}); +res1_1.descendants(() => null); +res1_1.descendants(() => undefined); +res1_1.descendants(() => true); +const res1_2: model.Node = res1_1.maybeChild(0)!; +const res1_3: model.Node = res1_1.nodeAt(0)!; + +const cm1 = new model.ContentMatch(); +const cm2: model.ContentMatch = cm1.matchType({} as any)!; +const cm3: model.ContentMatch = cm1.matchFragment({} as any)!; +const cm4: model.Fragment = cm1.fillBefore({} as any)!; +const cm5: model.NodeType[] = cm1.findWrapping({} as any)!; + +const f1 = new model.Fragment(); +f1.nodesBetween(0, 0, () => {}); +f1.nodesBetween(0, 0, () => null); +f1.nodesBetween(0, 0, () => undefined); +f1.nodesBetween(0, 0, () => true); + +f1.descendants(() => {}); +f1.descendants(() => null); +f1.descendants(() => undefined); +f1.descendants(() => true); + +const res2_1: model.Node = f1.maybeChild(0)!; +const res2_2: number = f1.findDiffStart(f1)!; +const res2_3: { a: number, b: number } = f1.findDiffEnd({} as any)!; +const res2_4: object = f1.toJSON()!; + +const res3_1 = new model.ResolvedPos(); +const res3_2: model.Mark[] = res3_1.marksAcross(res3_1)!; +const res3_3: model.NodeRange = res3_1.blockRange(res3_1)!; + +const res4_1 = new model.NodeType(); +const res4_2: model.Node = res4_1.createAndFill()!; + +const res5_1 = new model.MarkType(); +const res5_2: model.Mark = res5_1.isInSet([])!; diff --git a/types/prosemirror-state/index.d.ts b/types/prosemirror-state/index.d.ts index 6b210a73e1..db5a16e9d3 100644 --- a/types/prosemirror-state/index.d.ts +++ b/types/prosemirror-state/index.d.ts @@ -74,7 +74,7 @@ export interface PluginSpec { transactions: Transaction[], oldState: EditorState, newState: EditorState - ) => Transaction | null | void) + ) => Transaction | null | undefined | void) | null; } /** @@ -147,11 +147,11 @@ export class PluginKey { * Get the active plugin with this key, if any, from an editor * state. */ - get(state: EditorState): Plugin | null | void; + get(state: EditorState): Plugin | null | undefined; /** * Get the plugin's state from an editor state. */ - getState(state: EditorState): any | null | void; + getState(state: EditorState): any | null | undefined; } /** * Superclass for editor selections. Every selection type should @@ -263,7 +263,7 @@ export class Selection { $pos: ResolvedPos, dir: number, textOnly?: boolean - ): Selection | null | void; + ): Selection | null | undefined; /** * Find a valid cursor or leaf node selection near the given * position. Searches forward first by default, but if `bias` is diff --git a/types/prosemirror-state/prosemirror-state-tests.ts b/types/prosemirror-state/prosemirror-state-tests.ts index 0c1cddc24e..1668b844af 100644 --- a/types/prosemirror-state/prosemirror-state-tests.ts +++ b/types/prosemirror-state/prosemirror-state-tests.ts @@ -40,3 +40,15 @@ transaction = transaction.setNodeMarkup(0); transaction = transaction.split(0); transaction = transaction.join(0); transaction = transaction.step(step); + +const res1_1: state.PluginSpec["appendTransaction"] = null; +const res1_2: state.PluginSpec["appendTransaction"] = () => {}; +const res1_3: state.PluginSpec["appendTransaction"] = () => null; +const res1_4: state.PluginSpec["appendTransaction"] = () => undefined; +const res1_5: state.PluginSpec["appendTransaction"] = () => ({} as state.Transaction); + +const res2_1 = new state.PluginKey(); +const res2_2: state.Plugin = res2_1.get({} as state.EditorState)!; + +const res3_1 = new state.Selection({} as any, {} as any); +const res3_2: state.Selection = state.Selection.findFrom({} as model.ResolvedPos, 0)!; diff --git a/types/prosemirror-transform/index.d.ts b/types/prosemirror-transform/index.d.ts index c0b63136f4..e74fd9c094 100644 --- a/types/prosemirror-transform/index.d.ts +++ b/types/prosemirror-transform/index.d.ts @@ -403,7 +403,7 @@ export function replaceStep( from: number, to?: number, slice?: Slice -): Step | null | void; +): Step | null | undefined; /** * A step object represents an atomic change. It generally applies * only to the document it was created for, since the positions @@ -439,13 +439,13 @@ export class Step { * version of that step with its positions adjusted, or `null` if * the step was entirely deleted by the mapping. */ - map(mapping: Mappable): Step | null | void; + map(mapping: Mappable): Step | null | undefined; /** * Try to merge this step with another one, to be applied directly * after it. Returns the merged step when possible, null if the * steps can't be merged. */ - merge(other: Step): Step | null | void; + merge(other: Step): Step | null | undefined; /** * Create a JSON-serializeable representation of this step. When * defining this for a custom subclass, make sure the result object @@ -504,7 +504,7 @@ export class StepResult { * can be lifted. Will not go across * [isolating](#model.NodeSpec.isolating) parent nodes. */ -export function liftTarget(range: NodeRange): number | null | void; +export function liftTarget(range: NodeRange): number | null | undefined; /** * Try to find a valid way to wrap the content in the given range in a * node of the given type. May introduce extra nodes around and inside @@ -515,7 +515,7 @@ export function findWrapping( range: NodeRange, nodeType: NodeType, attrs?: { [key: string]: any } -): Array<{ type: NodeType; attrs?: { [key: string]: any } | null }> | null | void; +): Array<{ type: NodeType; attrs?: { [key: string]: any } | null }> | null | undefined; /** * Check whether splitting at the given position is allowed. */ @@ -535,7 +535,7 @@ export function canJoin(doc: ProsemirrorNode, pos: number): boolean; * block before (or after if `dir` is positive). Returns the joinable * point, if any. */ -export function joinPoint(doc: ProsemirrorNode, pos: number, dir?: number): number | null | void; +export function joinPoint(doc: ProsemirrorNode, pos: number, dir?: number): number | null | undefined; /** * Try to find a point where a node of the given type can be inserted * near `pos`, by searching up the node hierarchy when `pos` itself @@ -546,4 +546,4 @@ export function insertPoint( doc: ProsemirrorNode, pos: number, nodeType: NodeType -): number | null | void; +): number | null | undefined; diff --git a/types/prosemirror-transform/prosemirror-transform-tests.ts b/types/prosemirror-transform/prosemirror-transform-tests.ts index c3a18ef916..a61108a2a9 100644 --- a/types/prosemirror-transform/prosemirror-transform-tests.ts +++ b/types/prosemirror-transform/prosemirror-transform-tests.ts @@ -1,3 +1,17 @@ import * as transform from 'prosemirror-transform'; const stepmap = new transform.StepMap([]); + +// Verify non-null assertion operator can be used. + +const res1_1: transform.Step = transform.replaceStep({} as any, 0)!; +const res1_2: transform.Step = res1_1.map({} as any)!; +const res1_3: transform.Step = res1_1.merge({} as any)!; + +const res2_1: number = transform.liftTarget({} as any)!; + +const res3_1: any[] = transform.findWrapping({} as any, {} as any)!; + +const res4_1: number = transform.joinPoint({} as any, 0)!; + +const res5_1: number = transform.insertPoint({} as any, 0, {} as any)!; diff --git a/types/prosemirror-view/index.d.ts b/types/prosemirror-view/index.d.ts index f24a5270d1..dba4fb8aec 100644 --- a/types/prosemirror-view/index.d.ts +++ b/types/prosemirror-view/index.d.ts @@ -237,7 +237,7 @@ export class EditorView { posAtCoords(coords: { left: number; top: number; - }): { pos: number; inside: number } | null | void; + }): { pos: number; inside: number } | null | undefined; /** * Returns the viewport rectangle at a given document position. `left` * and `right` will be the same number, as this returns a flat @@ -404,7 +404,7 @@ export interface EditorProps { view: EditorView, anchor: ResolvedPos, head: ResolvedPos - ) => Selection | null | void) + ) => Selection | null | undefined) | null; /** * The [parser](#model.DOMParser) to use when reading editor changes @@ -482,7 +482,7 @@ export interface EditorProps { * A set of [document decorations](#view.Decoration) to show in the * view. */ - decorations?: ((state: EditorState) => DecorationSet | null | void) | null; + decorations?: ((state: EditorState) => DecorationSet | null | undefined) | null; /** * When this returns false, the content of the view is not directly * editable. @@ -500,7 +500,7 @@ export interface EditorProps { */ attributes?: | { [name: string]: string } - | ((p: EditorState) => { [name: string]: string } | null | void) + | ((p: EditorState) => { [name: string]: string } | null | undefined | void) | null; /** * Determines the distance (in pixels) between the cursor and the diff --git a/types/prosemirror-view/prosemirror-view-tests.ts b/types/prosemirror-view/prosemirror-view-tests.ts index fd6c0538eb..a3e76946b8 100644 --- a/types/prosemirror-view/prosemirror-view-tests.ts +++ b/types/prosemirror-view/prosemirror-view-tests.ts @@ -1,3 +1,15 @@ import * as view from 'prosemirror-view'; +import * as state from 'prosemirror-state'; const decoration = new view.Decoration(); + +const res1_1 = new view.EditorView({} as any, {} as any); +const res1_2: { pos: number, inside: number } = res1_1.posAtCoords({ left: 0, top: 0})!; + +const res2_1: view.EditorProps = {} as any; +const res2_2: state.Selection = res2_1.createSelectionBetween!({} as any, {} as any, {} as any)!; +const res2_3: view.DecorationSet = res2_1.decorations!({} as any)!; + +const res3_1: view.EditorProps["attributes"] = () => {}; +const res3_2: view.EditorProps["attributes"] = () => null; +const res3_3: view.EditorProps["attributes"] = () => undefined;