From 6325878f9fa7dba17c8af1c5f190a455191495aa Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Sat, 21 May 2022 17:39:07 +0300 Subject: [PATCH] Multiple refactors removed unused and duplicated variables --- lib/common/utils.js | 4 +- lib/helpers/parse_link_destination.js | 6 +-- lib/helpers/parse_link_title.js | 4 +- lib/renderer.js | 4 +- lib/rules_block/list.js | 54 +++++++++++++-------------- lib/rules_block/paragraph.js | 5 +-- lib/rules_inline/backticks.js | 2 +- lib/rules_inline/balance_pairs.js | 6 +-- lib/rules_inline/html_inline.js | 2 +- 9 files changed, 41 insertions(+), 46 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index 712cd29..f97bbc9 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -75,12 +75,12 @@ var UNESCAPE_MD_RE = /\\([!"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~])/g; var ENTITY_RE = /&([a-z#][a-z0-9]{1,31});/gi; var UNESCAPE_ALL_RE = new RegExp(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source, 'gi'); -var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))/i; +var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i; var entities = require('./entities'); function replaceEntityPattern(match, name) { - var code = 0; + var code; if (has(entities, name)) { return entities[name]; diff --git a/lib/helpers/parse_link_destination.js b/lib/helpers/parse_link_destination.js index 637f1f4..4840ff8 100644 --- a/lib/helpers/parse_link_destination.js +++ b/lib/helpers/parse_link_destination.js @@ -6,10 +6,9 @@ var unescapeAll = require('../common/utils').unescapeAll; -module.exports = function parseLinkDestination(str, pos, max) { +module.exports = function parseLinkDestination(str, start, max) { var code, level, - lines = 0, - start = pos, + pos = start, result = { ok: false, pos: 0, @@ -75,7 +74,6 @@ module.exports = function parseLinkDestination(str, pos, max) { if (level !== 0) { return result; } result.str = unescapeAll(str.slice(start, pos)); - result.lines = lines; result.pos = pos; result.ok = true; return result; diff --git a/lib/helpers/parse_link_title.js b/lib/helpers/parse_link_title.js index 051d6f4..f64f87c 100644 --- a/lib/helpers/parse_link_title.js +++ b/lib/helpers/parse_link_title.js @@ -6,11 +6,11 @@ var unescapeAll = require('../common/utils').unescapeAll; -module.exports = function parseLinkTitle(str, pos, max) { +module.exports = function parseLinkTitle(str, start, max) { var code, marker, lines = 0, - start = pos, + pos = start, result = { ok: false, pos: 0, diff --git a/lib/renderer.js b/lib/renderer.js index 08eacf3..268cf78 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -22,7 +22,7 @@ default_rules.code_inline = function (tokens, idx, options, env, slf) { var token = tokens[idx]; return '' + - escapeHtml(tokens[idx].content) + + escapeHtml(token.content) + ''; }; @@ -329,7 +329,7 @@ Renderer.prototype.render = function (tokens, options, env) { if (type === 'inline') { result += this.renderInline(tokens[i].children, options, env); } else if (typeof rules[type] !== 'undefined') { - result += rules[tokens[i].type](tokens, i, options, env, this); + result += rules[type](tokens, i, options, env, this); } else { result += this.renderToken(tokens, i, options, env); } diff --git a/lib/rules_block/list.js b/lib/rules_block/list.js index 1e5e87b..f45aebc 100644 --- a/lib/rules_block/list.js +++ b/lib/rules_block/list.js @@ -112,7 +112,6 @@ module.exports = function list(state, startLine, endLine, silent) { markerCharCode, markerValue, max, - nextLine, offset, oldListIndent, oldParentType, @@ -126,11 +125,12 @@ module.exports = function list(state, startLine, endLine, silent) { terminate, terminatorRules, token, + nextLine = startLine, isTerminatingParagraph = false, tight = true; // if it's indented more than 3 spaces, it should be a code block - if (state.sCount[startLine] - state.blkIndent >= 4) { return false; } + if (state.sCount[nextLine] - state.blkIndent >= 4) { return false; } // Special case: // - item 1 @@ -139,8 +139,8 @@ module.exports = function list(state, startLine, endLine, silent) { // - item 4 // - this one is a paragraph continuation if (state.listIndent >= 0 && - state.sCount[startLine] - state.listIndent >= 4 && - state.sCount[startLine] < state.blkIndent) { + state.sCount[nextLine] - state.listIndent >= 4 && + state.sCount[nextLine] < state.blkIndent) { return false; } @@ -152,22 +152,22 @@ module.exports = function list(state, startLine, endLine, silent) { // This code can fail if plugins use blkIndent as well as lists, // but I hope the spec gets fixed long before that happens. // - if (state.sCount[startLine] >= state.blkIndent) { + if (state.sCount[nextLine] >= state.blkIndent) { isTerminatingParagraph = true; } } // Detect list type and position after marker - if ((posAfterMarker = skipOrderedListMarker(state, startLine)) >= 0) { + if ((posAfterMarker = skipOrderedListMarker(state, nextLine)) >= 0) { isOrdered = true; - start = state.bMarks[startLine] + state.tShift[startLine]; + start = state.bMarks[nextLine] + state.tShift[nextLine]; markerValue = Number(state.src.slice(start, posAfterMarker - 1)); // If we're starting a new ordered list right after // a paragraph, it should start with 1. if (isTerminatingParagraph && markerValue !== 1) return false; - } else if ((posAfterMarker = skipBulletListMarker(state, startLine)) >= 0) { + } else if ((posAfterMarker = skipBulletListMarker(state, nextLine)) >= 0) { isOrdered = false; } else { @@ -177,15 +177,15 @@ module.exports = function list(state, startLine, endLine, silent) { // If we're starting a new unordered list right after // a paragraph, first line should not be empty. if (isTerminatingParagraph) { - if (state.skipSpaces(posAfterMarker) >= state.eMarks[startLine]) return false; + if (state.skipSpaces(posAfterMarker) >= state.eMarks[nextLine]) return false; } - // We should terminate list on style change. Remember first one to compare. - markerCharCode = state.src.charCodeAt(posAfterMarker - 1); - // For validation mode we can terminate immediately if (silent) { return true; } + // We should terminate list on style change. Remember first one to compare. + markerCharCode = state.src.charCodeAt(posAfterMarker - 1); + // Start list listTokIdx = state.tokens.length; @@ -199,14 +199,13 @@ module.exports = function list(state, startLine, endLine, silent) { token = state.push('bullet_list_open', 'ul', 1); } - token.map = listLines = [ startLine, 0 ]; + token.map = listLines = [ nextLine, 0 ]; token.markup = String.fromCharCode(markerCharCode); // // Iterate list items // - nextLine = startLine; prevEmptyEnd = false; terminatorRules = state.md.block.ruler.getRules('list'); @@ -217,7 +216,7 @@ module.exports = function list(state, startLine, endLine, silent) { pos = posAfterMarker; max = state.eMarks[nextLine]; - initial = offset = state.sCount[nextLine] + posAfterMarker - (state.bMarks[startLine] + state.tShift[startLine]); + initial = offset = state.sCount[nextLine] + posAfterMarker - (state.bMarks[nextLine] + state.tShift[nextLine]); while (pos < max) { ch = state.src.charCodeAt(pos); @@ -253,15 +252,15 @@ module.exports = function list(state, startLine, endLine, silent) { // Run subparser & write tokens token = state.push('list_item_open', 'li', 1); token.markup = String.fromCharCode(markerCharCode); - token.map = itemLines = [ startLine, 0 ]; + token.map = itemLines = [ nextLine, 0 ]; if (isOrdered) { token.info = state.src.slice(start, posAfterMarker - 1); } // change current state, then restore it after parser subcall oldTight = state.tight; - oldTShift = state.tShift[startLine]; - oldSCount = state.sCount[startLine]; + oldTShift = state.tShift[nextLine]; + oldSCount = state.sCount[nextLine]; // - example list // ^ listIndent position will be here @@ -272,10 +271,10 @@ module.exports = function list(state, startLine, endLine, silent) { state.blkIndent = indent; state.tight = true; - state.tShift[startLine] = contentStart - state.bMarks[startLine]; - state.sCount[startLine] = offset; + state.tShift[nextLine] = contentStart - state.bMarks[nextLine]; + state.sCount[nextLine] = offset; - if (contentStart >= max && state.isEmpty(startLine + 1)) { + if (contentStart >= max && state.isEmpty(nextLine + 1)) { // workaround for this case // (list item is empty, list terminates before "foo"): // ~~~~~~~~ @@ -285,7 +284,7 @@ module.exports = function list(state, startLine, endLine, silent) { // ~~~~~~~~ state.line = Math.min(state.line + 2, endLine); } else { - state.md.block.tokenize(state, startLine, endLine, true); + state.md.block.tokenize(state, nextLine, endLine, true); } // If any of list item is tight, mark list as tight @@ -294,20 +293,19 @@ module.exports = function list(state, startLine, endLine, silent) { } // Item become loose if finish with empty line, // but we should filter last element, because it means list finish - prevEmptyEnd = (state.line - startLine) > 1 && state.isEmpty(state.line - 1); + prevEmptyEnd = (state.line - nextLine) > 1 && state.isEmpty(state.line - 1); state.blkIndent = state.listIndent; state.listIndent = oldListIndent; - state.tShift[startLine] = oldTShift; - state.sCount[startLine] = oldSCount; + state.tShift[nextLine] = oldTShift; + state.sCount[nextLine] = oldSCount; state.tight = oldTight; token = state.push('list_item_close', 'li', -1); token.markup = String.fromCharCode(markerCharCode); - nextLine = startLine = state.line; + nextLine = state.line; itemLines[1] = nextLine; - contentStart = state.bMarks[startLine]; if (nextLine >= endLine) { break; } @@ -317,7 +315,7 @@ module.exports = function list(state, startLine, endLine, silent) { if (state.sCount[nextLine] < state.blkIndent) { break; } // if it's indented more than 3 spaces, it should be a code block - if (state.sCount[startLine] - state.blkIndent >= 4) { break; } + if (state.sCount[nextLine] - state.blkIndent >= 4) { break; } // fail if terminating block found terminate = false; diff --git a/lib/rules_block/paragraph.js b/lib/rules_block/paragraph.js index f0c6872..31d25b2 100644 --- a/lib/rules_block/paragraph.js +++ b/lib/rules_block/paragraph.js @@ -3,11 +3,10 @@ 'use strict'; -module.exports = function paragraph(state, startLine/*, endLine*/) { +module.exports = function paragraph(state, startLine, endLine) { var content, terminate, i, l, token, oldParentType, nextLine = startLine + 1, - terminatorRules = state.md.block.ruler.getRules('paragraph'), - endLine = state.lineMax; + terminatorRules = state.md.block.ruler.getRules('paragraph'); oldParentType = state.parentType; state.parentType = 'paragraph'; diff --git a/lib/rules_inline/backticks.js b/lib/rules_inline/backticks.js index b9c9ddb..21282de 100644 --- a/lib/rules_inline/backticks.js +++ b/lib/rules_inline/backticks.js @@ -26,7 +26,7 @@ module.exports = function backtick(state, silent) { return true; } - matchStart = matchEnd = pos; + matchEnd = pos; // Nothing found in the cache, scan until the end of the line (or until marker is found) while ((matchStart = state.src.indexOf('`', matchEnd)) !== -1) { diff --git a/lib/rules_inline/balance_pairs.js b/lib/rules_inline/balance_pairs.js index 4faad90..d4fdfdb 100644 --- a/lib/rules_inline/balance_pairs.js +++ b/lib/rules_inline/balance_pairs.js @@ -3,7 +3,7 @@ 'use strict'; -function processDelimiters(state, delimiters) { +function processDelimiters(delimiters) { var closerIdx, openerIdx, closer, opener, minOpenerIdx, newMinOpenerIdx, isOddMatch, lastJump, openersBottom = {}, @@ -120,11 +120,11 @@ module.exports = function link_pairs(state) { tokens_meta = state.tokens_meta, max = state.tokens_meta.length; - processDelimiters(state, state.delimiters); + processDelimiters(state.delimiters); for (curr = 0; curr < max; curr++) { if (tokens_meta[curr] && tokens_meta[curr].delimiters) { - processDelimiters(state, tokens_meta[curr].delimiters); + processDelimiters(tokens_meta[curr].delimiters); } } }; diff --git a/lib/rules_inline/html_inline.js b/lib/rules_inline/html_inline.js index da319b7..d307819 100644 --- a/lib/rules_inline/html_inline.js +++ b/lib/rules_inline/html_inline.js @@ -48,7 +48,7 @@ module.exports = function html_inline(state, silent) { if (!silent) { token = state.push('html_inline', '', 0); - token.content = state.src.slice(pos, pos + match[0].length); + token.content = match[0]; if (isLinkOpen(token.content)) state.linkLevel++; if (isLinkClose(token.content)) state.linkLevel--;