Skip to content

Commit

Permalink
improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed Apr 22, 2024
1 parent da06d8f commit 7685e95
Show file tree
Hide file tree
Showing 64 changed files with 281 additions and 288 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module.exports = {
'dot-notation': [ERROR, {allowPattern: '^(error|warn)$'}],

'eol-last': ERROR,
eqeqeq: [ERROR, 'allow-null'],
eqeqeq: [ERROR, 'always'],
// Prettier forces semicolons in a few places
'flowtype/object-type-delimiter': OFF,

Expand Down
12 changes: 5 additions & 7 deletions packages/lexical-clipboard/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const getDOMSelection = (targetWindow: Window | null): Selection | null =>
export function $getHtmlContent(editor: LexicalEditor): string {
const selection = $getSelection();

if (selection == null) {
if (selection === null) {
invariant(false, 'Expected valid LexicalSelection');
}

Expand All @@ -76,7 +76,7 @@ export function $getHtmlContent(editor: LexicalEditor): string {
export function $getLexicalContent(editor: LexicalEditor): null | string {
const selection = $getSelection();

if (selection == null) {
if (selection === null) {
invariant(false, 'Expected valid LexicalSelection');
}

Expand Down Expand Up @@ -106,9 +106,7 @@ export function $insertDataTransferForPlainText(
const text =
dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');

if (text != null) {
selection.insertRawText(text);
}
selection.insertRawText(text);
}

/**
Expand Down Expand Up @@ -159,7 +157,7 @@ export function $insertDataTransferForRichText(
// Webkit-specific: Supports read 'text/uri-list' in clipboard.
const text =
dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
if (text != null) {
if (text !== '') {
if ($isRangeSelection(selection)) {
const parts = text.split(/(\r?\n|\t)/);
if (parts[parts.length - 1] === '') {
Expand Down Expand Up @@ -399,7 +397,7 @@ export async function copyToClipboard(

const rootElement = editor.getRootElement();
const windowDocument =
editor._window == null ? window.document : editor._window.document;
editor._window === null ? window.document : editor._window.document;
const domSelection = getDOMSelection(editor._window);
if (rootElement === null || domSelection === null) {
return false;
Expand Down
8 changes: 4 additions & 4 deletions packages/lexical-code/src/CodeHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ function handleShiftLines(
start = getFirstCodeNodeOfLine(focusNode);
end = getLastCodeNodeOfLine(anchorNode);
}
if (start == null || end == null) {
if (start === null || end === null) {
return false;
}

Expand Down Expand Up @@ -726,7 +726,7 @@ function handleShiftLines(
const sibling = arrowIsUp
? linebreak.getPreviousSibling()
: linebreak.getNextSibling();
if (sibling == null) {
if (sibling === null) {
return true;
}

Expand All @@ -739,7 +739,7 @@ function handleShiftLines(
: getLastCodeNodeOfLine(sibling)
: null;
let insertionPoint =
maybeInsertionPoint != null ? maybeInsertionPoint : sibling;
maybeInsertionPoint !== null ? maybeInsertionPoint : sibling;
linebreak.remove();
range.forEach((node) => node.remove());
if (type === KEY_ARROW_UP_COMMAND) {
Expand Down Expand Up @@ -813,7 +813,7 @@ export function registerCodeHighlighting(
);
}

if (tokenizer == null) {
if (tokenizer === undefined) {
tokenizer = PrismTokenizer;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/lexical-code/src/CodeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export type SerializedCodeNode = Spread<
const mapToPrismLanguage = (
language: string | null | undefined,
): string | null | undefined => {
// eslint-disable-next-line no-prototype-builtins
return language != null && window.Prism.languages.hasOwnProperty(language)
return language !== null &&
language !== undefined &&
Object.hasOwn(window.Prism.languages, language)
? language
: undefined;
};
Expand Down Expand Up @@ -134,7 +135,7 @@ export class CodeNode extends ElementNode {
// inline format handled by TextNode otherwise.
code: (node: Node) => {
const isMultiLine =
node.textContent != null &&
node.textContent !== null &&
(/\r?\n/.test(node.textContent) || hasChildDOMNodeTag(node, 'BR'));

return isMultiLine
Expand Down Expand Up @@ -350,7 +351,7 @@ function convertDivElement(domNode: Node): DOMConversionOutput {
return {
after: (childLexicalNodes) => {
const domParent = domNode.parentNode;
if (domParent != null && domNode !== domParent.lastChild) {
if (domParent !== null && domNode !== domParent.lastChild) {
childLexicalNodes.push($createLineBreakNode());
}
return childLexicalNodes;
Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-devtools-core/src/generateContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function printFormatProperties(nodeOrSelection: TextNode | RangeSelection) {
function printTargetProperties(node: LinkNode) {
let str = node.getTarget();
// TODO Fix nullish on LinkNode
if (str != null) {
if (str !== null) {
str = 'target: ' + str;
}
return str;
Expand All @@ -349,7 +349,7 @@ function printTargetProperties(node: LinkNode) {
function printRelProperties(node: LinkNode) {
let str = node.getRel();
// TODO Fix nullish on LinkNode
if (str != null) {
if (str !== null) {
str = 'rel: ' + str;
}
return str;
Expand All @@ -358,7 +358,7 @@ function printRelProperties(node: LinkNode) {
function printTitleProperties(node: LinkNode) {
let str = node.getTitle();
// TODO Fix nullish on LinkNode
if (str != null) {
if (str !== null) {
str = 'title: ' + str;
}
return str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class InjectedPegasusService
isReadonly: boolean,
): void {
const editorNode = queryLexicalNodeByKey(editorKey);
if (editorNode == null) {
if (editorNode === undefined) {
throw new Error(`Can't find editor with key: ${editorKey}`);
}

Expand All @@ -57,7 +57,7 @@ export class InjectedPegasusService
exportDOM: boolean,
): string {
const editor = queryLexicalEditorByKey(editorKey);
if (editor == null) {
if (editor === undefined) {
throw new Error(`Can't find editor with key: ${editorKey}`);
}

Expand All @@ -72,7 +72,7 @@ export class InjectedPegasusService
editorState: SerializedRawEditorState,
): void {
const editor = queryLexicalEditorByKey(editorKey);
if (editor == null) {
if (editor === undefined) {
throw new Error(`Can't find editor with key: ${editorKey}`);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-devtools/src/serializeEditorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function deserializeEditorState(
typeof editorState.deserealizationID === 'number'
) {
const state = deserealizationMap.get(editorState.deserealizationID);
if (state == null) {
if (state === undefined) {
throw new Error(
`Can't find deserealization ref for state with id ${editorState.deserealizationID}`,
);
Expand All @@ -64,7 +64,7 @@ export function serializeEditorState(
selection &&
'anchor' in selection &&
typeof selection.anchor === 'object' &&
selection.anchor != null
selection.anchor !== null
) {
// remove _selection.anchor._selection property if present in RangeSelection or GridSelection
// otherwise, the recursive structure makes the selection object unserializable
Expand All @@ -74,7 +74,7 @@ export function serializeEditorState(
selection &&
'focus' in selection &&
typeof selection.focus === 'object' &&
selection.focus != null
selection.focus !== null
) {
// remove _selection.anchor._selection property if present in RangeSelection or GridSelection
// otherwise, the recursive structure makes the selection object unserializable
Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function $createNodesFromDOM(
}
}

if (transformOutput.forChild != null) {
if (transformOutput.forChild !== undefined) {
forChildMap.set(node.nodeName, transformOutput.forChild);
}
}
Expand All @@ -245,11 +245,11 @@ function $createNodesFromDOM(
);
}

if (postTransform != null) {
if (postTransform !== null && postTransform !== undefined) {
childLexicalNodes = postTransform(childLexicalNodes);
}

if (currentLexicalNode == null) {
if (currentLexicalNode === null || currentLexicalNode === undefined) {
// If it hasn't been converted to a LexicalNode, we hoist its children
// up to the same level as it.
lexicalNodes = lexicalNodes.concat(childLexicalNodes);
Expand Down
9 changes: 6 additions & 3 deletions packages/lexical-list/src/LexicalListItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ export class ListItemNode extends ElementNode {
static transform(): (node: LexicalNode) => void {
return (node: LexicalNode) => {
invariant($isListItemNode(node), 'node is not a ListItemNode');
if (node.__checked == null) {
if (node.__checked === undefined) {
return;
}
const parent = node.getParent();
if ($isListNode(parent)) {
if (parent.getListType() !== 'check' && node.getChecked() != null) {
if (
parent.getListType() !== 'check' &&
node.getChecked() !== undefined
) {
node.setChecked(undefined);
}
}
Expand Down Expand Up @@ -261,7 +264,7 @@ export class ListItemNode extends ElementNode {
restoreSelection = true,
): ListItemNode | ParagraphNode {
const newElement = $createListItemNode(
this.__checked == null ? undefined : false,
this.__checked === undefined ? undefined : false,
);
this.insertAfter(newElement, restoreSelection);

Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-list/src/formatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function insertList(editor: LexicalEditor, listType: ListType): void {

if ($isLeafNode(node)) {
let parent = node.getParent();
while (parent != null) {
while (parent !== null) {
const parentKey = parent.getKey();

if ($isListNode(parent)) {
Expand Down Expand Up @@ -242,7 +242,7 @@ export function removeList(editor: LexicalEditor): void {
if ($isLeafNode(node)) {
const listItemNode = $getNearestNodeOfType(node, ListItemNode);

if (listItemNode != null) {
if (listItemNode !== null) {
listNodes.add($getTopListNode(listItemNode));
}
}
Expand Down Expand Up @@ -297,7 +297,7 @@ export function updateChildrenListItemValue(list: ListNode): void {
if (child.getValue() !== value) {
child.setValue(value);
}
if (isNotChecklist && child.getChecked() != null) {
if (isNotChecklist && child.getChecked() !== undefined) {
child.setChecked(undefined);
}
if (!$isListNode(child.getFirstChild())) {
Expand Down
8 changes: 4 additions & 4 deletions packages/lexical-list/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function $getListDepth(listNode: ListNode): number {
let depth = 1;
let parent = listNode.getParent();

while (parent != null) {
while (parent !== null) {
if ($isListItemNode(parent)) {
const parentList = parent.getParent();

Expand Down Expand Up @@ -176,13 +176,13 @@ export function $removeHighestEmptyListParent(
let emptyListPtr = sublist;

while (
emptyListPtr.getNextSibling() == null &&
emptyListPtr.getPreviousSibling() == null
emptyListPtr.getNextSibling() === null &&
emptyListPtr.getPreviousSibling() === null
) {
const parent = emptyListPtr.getParent<ListItemNode | ListNode>();

if (
parent == null ||
parent === null ||
!($isListItemNode(emptyListPtr) || $isListNode(emptyListPtr))
) {
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical-mark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function $wrapSelectionInMarkNode(
continue;
}
const parentNode = targetNode.getParent();
if (parentNode == null || !parentNode.is(currentNodeParent)) {
if (parentNode === null || !parentNode.is(currentNodeParent)) {
// If the parent node is not the current node's parent node, we can
// clear the last created mark node.
lastCreatedMarkNode = undefined;
Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-markdown/src/MarkdownExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function createMarkdownExport(
byType.textMatch,
);

if (result != null) {
if (result !== null) {
output.push(result);
}
}
Expand All @@ -67,7 +67,7 @@ function exportTopLevelElements(
exportChildren(_node, textTransformersIndex, textMatchTransformers),
);

if (result != null) {
if (result !== null) {
return result;
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ function exportChildren(
exportTextFormat(textNode, textContent, textTransformersIndex),
);

if (result != null) {
if (result !== null) {
output.push(result);
continue mainLoop;
}
Expand Down
Loading

0 comments on commit 7685e95

Please sign in to comment.