Skip to content

Commit

Permalink
Enable eslint rule: prefer-const, no-var (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Oct 12, 2023
1 parent f520f6d commit f6242c2
Show file tree
Hide file tree
Showing 121 changed files with 412 additions and 404 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,7 @@ module.exports = {
},
],
'import/no-duplicates': 'error',
'prefer-const': 'error',
'no-var': 'error',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const tableProcessor: ElementProcessor<HTMLTableElement> = (
};

function calcSizes(positions: number[]): number[] {
let result: number[] = [];
const result: number[] = [];
let lastPos = positions[positions.length - 1];

for (let i = positions.length - 2; i >= 0; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const textProcessor: ElementProcessor<Text> = (
context: DomToModelContext
) => {
let txt = textNode.nodeValue || '';
let [txtStartOffset, txtEndOffset] = getRegularSelectionOffsets(context, textNode);
const offsets = getRegularSelectionOffsets(context, textNode);
const txtStartOffset = offsets[0];
let txtEndOffset = offsets[1];
const segments: (ContentModelText | undefined)[] = [];
const paragraph = ensureParagraph(group, context.blockFormat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getDefaultStyle(
element: HTMLElement,
context: DomToModelContext
): Partial<CSSStyleDeclaration> {
let tag = element.tagName.toLowerCase() as keyof DefaultStyleMap;
const tag = element.tagName.toLowerCase() as keyof DefaultStyleMap;

return defaultHTMLStyleMap[tag] || {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function getRegularSelectionOffsets(
): [number, number] {
const range = context.selection?.type == 'range' ? context.selection.range : null;

let startOffset = range?.startContainer == currentContainer ? range.startOffset : -1;
let endOffset = range?.endContainer == currentContainer ? range.endOffset! : -1;
const startOffset = range?.startContainer == currentContainer ? range.startOffset : -1;
const endOffset = range?.endContainer == currentContainer ? range.endOffset! : -1;

return [startOffset, endOffset];
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ function convertDecimalsToAlpha(decimal: number, isLowerCase?: boolean): string

function convertDecimalsToRoman(decimal: number, isLowerCase?: boolean) {
let romanValue = '';
for (let i of getObjectKeys(RomanValues)) {
let timesRomanCharAppear = Math.floor(decimal / RomanValues[i]);
for (const i of getObjectKeys(RomanValues)) {
const timesRomanCharAppear = Math.floor(decimal / RomanValues[i]);
decimal = decimal - timesRomanCharAppear * RomanValues[i];
romanValue = romanValue + i.repeat(timesRomanCharAppear);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const handleEntityBlock: ContentModelBlockHandler<ContentModelEntity> = (
context,
refNode
) => {
let { entityFormat, wrapper } = entityModel;
const { entityFormat, wrapper } = entityModel;

applyFormat(wrapper, context.formatAppliers.entity, entityFormat, context);

Expand All @@ -38,7 +38,7 @@ export const handleEntitySegment: ContentModelSegmentHandler<ContentModelEntity>
context,
newSegments
) => {
let { entityFormat, wrapper, format } = entityModel;
const { entityFormat, wrapper, format } = entityModel;

parent.appendChild(wrapper);
newSegments?.push(wrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const handleListItem: ContentModelBlockHandler<ContentModelListItem> = (

const { nodeStack } = context.listFormat;

let listParent = nodeStack?.[nodeStack?.length - 1]?.node || parent;
const listParent = nodeStack?.[nodeStack?.length - 1]?.node || parent;
const li = doc.createElement('li');
const level = listItem.levels[listItem.levels.length - 1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}

if (!cell.spanAbove && !cell.spanLeft) {
let td =
const td =
(context.allowCacheElement && cell.cachedElement) ||
doc.createElement(cell.isHeader ? 'th' : 'td');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export function excelHandler(html: string, htmlBefore: string): string {
html = tr + html + '</TR>';
}
if (html.match(LAST_TR_END_REGEX)) {
let tableMatch = htmlBefore.match(LAST_TABLE_REGEX);
let table = tableMatch ? tableMatch[0] : '<TABLE>';
const tableMatch = htmlBefore.match(LAST_TABLE_REGEX);
const table = tableMatch ? tableMatch[0] : '<TABLE>';
html = table + html + '</TABLE>';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const wacLiElementProcessor: ElementProcessor<HTMLLIElement> = (
const currentLevel = lastblock.levels[lastblock.levels.length - 1];

// Get item level from 'data-aria-level' attribute
let level = parseInt(element.getAttribute('data-aria-level') ?? '');
const level = parseInt(element.getAttribute('data-aria-level') ?? '');
if (level > 0) {
if (level > lastblock.levels.length) {
while (level != lastblock.levels.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function getFakeBulletText(node: Node, levels?: number): string {
*/
function isIgnoreNode(node: Node): boolean {
if (isNodeOfType(node, 'ELEMENT_NODE')) {
let listAttribute = getStyles(node as HTMLElement)[MSO_LIST];
const listAttribute = getStyles(node as HTMLElement)[MSO_LIST];
if (
listAttribute &&
listAttribute.length > 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getLeafSiblingBlock(
const newPath = [...path];

while (newPath.length > 0) {
let group = newPath[0];
const group = newPath[0];
const index = group.blocks.indexOf(block);

if (index < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getDeleteWordSelection(direction: 'forward' | 'backward'): DeleteSelect
const startIndex = paragraph.segments.indexOf(marker);
const deleteNext = direction == 'forward';

let iterator = iterateSegments(paragraph, startIndex, deleteNext, context);
const iterator = iterateSegments(paragraph, startIndex, deleteNext, context);
let curr = iterator.next();

for (let state = DeleteWordState.Start; state != DeleteWordState.End && !curr.done; ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function insertLink(
) {
editor.focus();

let url = (checkXss(link) || '').trim();
const url = (checkXss(link) || '').trim();
if (url) {
const linkData = matchLink(url);
const linkUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function changeFontSizeInternal(
paragraph: ContentModelParagraph | null
) {
if (format.fontSize) {
let sizeInPt = parseValueWithUnit(format.fontSize, undefined /*element*/, 'pt');
const sizeInPt = parseValueWithUnit(format.fontSize, undefined /*element*/, 'pt');

if (sizeInPt > 0) {
const newSize = getNewFontSize(sizeInPt, change == 'increase' ? 1 : -1, FONT_SIZES);
Expand All @@ -54,7 +54,7 @@ function changeFontSizeInternal(

function getNewFontSize(pt: number, changeBase: 1 | -1, fontSizes: number[]): number {
pt = changeBase == 1 ? Math.floor(pt) : Math.ceil(pt);
let last = fontSizes[fontSizes.length - 1];
const last = fontSizes[fontSizes.length - 1];
if (pt <= fontSizes[0]) {
pt = Math.max(pt + changeBase, MIN_FONT_SIZE);
} else if (pt > last || (pt == last && changeBase == 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function triggerPluginEventAndCreatePasteFragment(
const { rawHtml, text, imageDataUri } = clipboardData;
const trustedHTMLHandler = editor.getTrustedHTMLHandler();

let doc: Document | undefined = rawHtml
const doc: Document | undefined = rawHtml
? new DOMParser().parseFromString(trustedHTMLHandler(rawHtml), 'text/html')
: undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createContentModelEditor(
plugins = plugins.concat(additionalPlugins);
}

let options: ContentModelEditorOptions = {
const options: ContentModelEditorOptions = {
plugins: plugins,
initialContent: initialContent,
getDarkColor: getDarkColor,
Expand Down
6 changes: 3 additions & 3 deletions packages-ui/roosterjs-react/lib/emoji/utils/emojiList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ export function forEachEmoji(callback: (emoji: Emoji) => boolean): void {

// get emoji code point from an emoji key
function getEmojiCodePoint(key: string): string | null {
let unicode = parseInt(key, 16);
const unicode = parseInt(key, 16);
if (isNaN(unicode)) {
return null;
}
Expand All @@ -771,8 +771,8 @@ function getEmojiCodePoint(key: string): string | null {
// 0x00023 - 0x04000 -> does not have surrogate pairs
let surrogatePairs: number[];
if (unicode >= 0x1f000 && unicode <= 0x1f700) {
let hi = Math.floor((unicode - 0x10000) / 0x400) + 0xd800;
let lo = ((unicode - 0x10000) % 0x400) + 0xdc00;
const hi = Math.floor((unicode - 0x10000) / 0x400) + 0xd800;
const lo = ((unicode - 0x10000) % 0x400) + 0xdc00;
surrogatePairs = [hi, lo];
} else if (unicode >= 0x00023 && unicode <= 0x04000) {
surrogatePairs = [unicode];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function InsertTablePane(props: {
const ariaLabels = React.useMemo<string[][]>(() => {
const result: string[][] = [];
for (let i = 1; i <= MaxCols; i++) {
let col: string[] = [];
const col: string[] = [];
for (let j = 1; j <= MaxRows; j++) {
col[j] = formatText(item.text ?? '', i, j);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/roosterjs-editor-api/lib/format/changeFontSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export default function changeFontSize(
change: FontSizeChange | CompatibleFontSizeChange,
fontSizes: number[] = FONT_SIZES
) {
let changeBase: 1 | -1 = change == FontSizeChange.Increase ? 1 : -1;
const changeBase: 1 | -1 = change == FontSizeChange.Increase ? 1 : -1;
applyInlineStyle(
editor,
element => {
let pt = parseFloat(getComputedStyle(element, 'font-size') || element.style.fontSize);
const pt = parseFloat(getComputedStyle(element, 'font-size') || element.style.fontSize);
element.style.fontSize = getNewFontSize(pt, changeBase, fontSizes) + 'pt';
let lineHeight = getComputedStyle(element, 'line-height');
const lineHeight = getComputedStyle(element, 'line-height');
if (lineHeight && lineHeight != 'normal') {
element.style.lineHeight = 'normal';
}
Expand All @@ -47,7 +47,7 @@ export default function changeFontSize(
*/
export function getNewFontSize(pt: number, changeBase: 1 | -1, fontSizes: number[]): number {
pt = changeBase == 1 ? Math.floor(pt) : Math.ceil(pt);
let last = fontSizes[fontSizes.length - 1];
const last = fontSizes[fontSizes.length - 1];
if (pt <= fontSizes[0]) {
pt = Math.max(pt + changeBase, MIN_FONT_SIZE);
} else if (pt > last || (pt == last && changeBase == 1)) {
Expand Down
14 changes: 7 additions & 7 deletions packages/roosterjs-editor-api/lib/format/clearFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ const TAGS_TO_STOP_UNWRAP = ['TD', 'TH', 'TR', 'TABLE', 'TBODY', 'THEAD'];
* @returns if the current selection is composed of two or more block elements
*/
function isMultiBlockSelection(editor: IEditor): boolean {
let transverser = editor.getSelectionTraverser();
let blockElement = transverser?.currentBlockElement;
const transverser = editor.getSelectionTraverser();
const blockElement = transverser?.currentBlockElement;
if (!blockElement) {
return false;
}

let nextBlockElement = transverser?.getNextBlockElement();
const nextBlockElement = transverser?.getNextBlockElement();

//At least two blocks are selected
return !!nextBlockElement;
Expand All @@ -58,8 +58,8 @@ function isMultiBlockSelection(editor: IEditor): boolean {
function clearNodeFormat(node: Node): boolean {
// 1. Recursively clear format of all its child nodes
const areBlockElements = toArray(node.childNodes).map(clearNodeFormat);
let areAllChildrenBlock = areBlockElements.every(b => b);
let returnBlockElement = isBlockElement(node);
const areAllChildrenBlock = areBlockElements.every(b => b);
const returnBlockElement = isBlockElement(node);

// 2. Unwrap the tag if necessary
const tag = getTagOfNode(node);
Expand Down Expand Up @@ -87,7 +87,7 @@ function clearAttribute(element: HTMLElement) {
const isTableCell = safeInstanceOf(element, 'HTMLTableCellElement');
const isTable = safeInstanceOf(element, 'HTMLTableElement');

for (let attr of toArray(element.attributes)) {
for (const attr of toArray(element.attributes)) {
if (isTableCell && attr.name == 'style') {
removeNonBorderStyles(element);
} else if (isTable && attr.name == 'style') {
Expand Down Expand Up @@ -280,7 +280,7 @@ function setDefaultFormat(editor: IEditor) {
QueryScope.OnSelection
);

let shouldApplyInlineStyle =
const shouldApplyInlineStyle =
setColorIgnoredElements.length > 0
? (element: HTMLElement) => setColorIgnoredElements.indexOf(element) == -1
: undefined;
Expand Down
10 changes: 5 additions & 5 deletions packages/roosterjs-editor-api/lib/format/createLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ export default function createLink(
target?: string
) {
editor.focus();
let url = (checkXss(link) || '').trim();
const url = (checkXss(link) || '').trim();
if (url) {
let linkData = matchLink(url);
const linkData = matchLink(url);
// matchLink can match most links, but not all, i.e. if you pass link a link as "abc", it won't match
// we know in that case, users will want to insert a link like http://abc
// so we have separate logic in applyLinkPrefix to add link prefix depending on the format of the link
// i.e. if the link starts with something like abc@xxx, we will add mailto: prefix
// if the link starts with ftp.xxx, we will add ftp:// link. For more, see applyLinkPrefix
let normalizedUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);
let originalUrl = linkData ? linkData.originalUrl : url;
const normalizedUrl = linkData ? linkData.normalizedUrl : applyLinkPrefix(url);
const originalUrl = linkData ? linkData.originalUrl : url;

editor.addUndoSnapshot(() => {
const selection = editor.getSelectionRangeEx();
Expand Down Expand Up @@ -100,7 +100,7 @@ export default function createLink(
let currentInline = traverser?.getNextInlineElement();

// list for removing unwanted lines
let deletionInlineList: Node[] = [];
const deletionInlineList: Node[] = [];

while (currentInline) {
deletionInlineList.push(currentInline.getContainerNode());
Expand Down
4 changes: 2 additions & 2 deletions packages/roosterjs-editor-api/lib/format/getFormatState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function getElementBasedFormatState(
let multiline = false;

if (range && !range.collapsed) {
let startingBlock = editor.getBlockElementAtNode(range.startContainer);
let endingBlock = editor.getBlockElementAtNode(range.endContainer);
const startingBlock = editor.getBlockElementAtNode(range.startContainer);
const endingBlock = editor.getBlockElementAtNode(range.endContainer);
multiline = endingBlock && startingBlock ? !endingBlock.equals(startingBlock) : false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/roosterjs-editor-api/lib/format/setFontSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function setFontSize(editor: IEditor, fontSize: string) {
'font-size',
(element, isInnerNode) => {
element.style.fontSize = isInnerNode ? '' : fontSize;
let lineHeight = getComputedStyle(element, 'line-height');
const lineHeight = getComputedStyle(element, 'line-height');
if (lineHeight && lineHeight != 'normal') {
element.style.lineHeight = 'normal';
}
Expand Down
6 changes: 3 additions & 3 deletions packages/roosterjs-editor-api/lib/format/setHeadingLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export default function setHeadingLevel(editor: IEditor, level: number) {
});

if (level > 0) {
let traverser = editor.getSelectionTraverser();
const traverser = editor.getSelectionTraverser();
let blockElement = traverser?.currentBlockElement;
let sanitizer = new HtmlSanitizer({
const sanitizer = new HtmlSanitizer({
cssStyleCallbacks: {
'font-size': () => false,
},
});
while (blockElement) {
let element = blockElement.collapseToSingleElement();
const element = blockElement.collapseToSingleElement();
sanitizer.sanitize(element);
blockElement = traverser?.getNextBlockElement();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/roosterjs-editor-api/lib/table/editTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default function editTable(
editor: IEditor,
operation: TableOperation | CompatibleTableOperation
) {
let td = editor.getElementAtCursor('TD,TH') as HTMLTableCellElement;
const td = editor.getElementAtCursor('TD,TH') as HTMLTableCellElement;
if (td) {
formatUndoSnapshot(
editor,
() => {
let vtable = new VTable(td);
const vtable = new VTable(td);

saveTableSelection(editor, vtable);
vtable.edit(operation);
Expand All @@ -29,7 +29,7 @@ export default function editTable(
if (isUndefined(vtable.row) || isUndefined(vtable.col)) {
return;
}
let { newCol, newRow } = calculateCellToSelect(operation, vtable.row, vtable.col);
const { newCol, newRow } = calculateCellToSelect(operation, vtable.row, vtable.col);
const newTd = vtable.getCell(newRow, newCol).td;
if (newTd) {
editor.select(newTd, PositionType.Begin);
Expand Down
2 changes: 1 addition & 1 deletion packages/roosterjs-editor-api/lib/table/formatTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function formatTable(
return;
}

let vtable = new VTable(table);
const vtable = new VTable(table);
vtable.applyFormat(format);
vtable.writeBack(false /** skipApplyFormat */, editor.getDarkColorHandler());
editor.transformToDarkColor(vtable.table);
Expand Down
Loading

0 comments on commit f6242c2

Please sign in to comment.