diff --git a/.eslintrc.js b/.eslintrc.js index 56915edad1..7647facc37 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -35,7 +35,7 @@ module.exports = { // TODO maybe "camelcase": "off", // TODO: turn on later - // "init-declarations": ["error","always"] + "init-declarations": ["error","always"] }, "overrides": [ { diff --git a/src/highlight.js b/src/highlight.js index a0171494e8..d91970e559 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -56,13 +56,12 @@ const HLJS = function(hljs) { } function blockLanguage(block) { - var match; var classes = block.className + ' '; classes += block.parentNode ? block.parentNode.className : ''; // language-* takes precedence over non-prefixed class names. - match = options.languageDetectRe.exec(classes); + const match = options.languageDetectRe.exec(classes); if (match) { var language = getLanguage(match[1]); if (!language) { @@ -137,22 +136,20 @@ const HLJS = function(hljs) { } function processKeywords() { - var keyword_match, last_index, match, buf; - if (!top.keywords) { emitter.addText(mode_buffer); return; } - last_index = 0; + let last_index = 0; top.lexemesRe.lastIndex = 0; - match = top.lexemesRe.exec(mode_buffer); - buf = ""; + let match = top.lexemesRe.exec(mode_buffer); + let buf = ""; while (match) { buf += mode_buffer.substring(last_index, match.index); - keyword_match = keywordMatch(top, match); - var kind = null; + const keyword_match = keywordMatch(top, match); + let kind = null; if (keyword_match) { emitter.addText(buf); buf = ""; @@ -305,7 +302,6 @@ const HLJS = function(hljs) { var lastMatch = {}; function processLexeme(text_before_match, match) { - var err; var lexeme = match && match[0]; // add non-matched text to the current mode buffer @@ -324,7 +320,7 @@ const HLJS = function(hljs) { // spit the "skipped" character that our regex choked on back into the output sequence mode_buffer += codeToHighlight.slice(match.index, match.index + 1); if (!SAFE_MODE) { - err = new Error('0 width match regex'); + const err = new Error('0 width match regex'); err.languageName = languageName; err.badRule = lastMatch.rule; throw err; @@ -337,7 +333,7 @@ const HLJS = function(hljs) { return doBeginMatch(match); } else if (match.type === "illegal" && !ignore_illegals) { // illegal match, we do not continue processing - err = new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '') + '"'); + const err = new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '') + '"'); err.mode = top; throw err; } else if (match.type === "end") { @@ -370,9 +366,9 @@ const HLJS = function(hljs) { } compileLanguage(language); + var result = ''; var top = continuation || language; var continuations = {}; // keep continuations for sub-languages - var result; var emitter = new options.__emitter(options); processContinuations(); var mode_buffer = ''; @@ -520,8 +516,8 @@ const HLJS = function(hljs) { two optional parameters for fixMarkup. */ function highlightBlock(block) { - var node, originalStream, result, resultNode, text; - var language = blockLanguage(block); + let node = null; + const language = blockLanguage(block); if (shouldNotHighlight(language)) return; @@ -534,12 +530,12 @@ const HLJS = function(hljs) { } else { node = block; } - text = node.textContent; - result = language ? highlight(language, text, true) : highlightAuto(text); + const text = node.textContent; + const result = language ? highlight(language, text, true) : highlightAuto(text); - originalStream = nodeStream(node); + const originalStream = nodeStream(node); if (originalStream.length) { - resultNode = document.createElement('div'); + const resultNode = document.createElement('div'); resultNode.innerHTML = result.value; result.value = mergeStreams(originalStream, nodeStream(resultNode), text); } @@ -589,7 +585,7 @@ const HLJS = function(hljs) { var PLAINTEXT_LANGUAGE = { disableAutodetect: true }; function registerLanguage(name, language) { - var lang; + var lang = null; try { lang = language(hljs); } catch (error) { diff --git a/src/lib/utils.js b/src/lib/utils.js index 6d8b8e2950..c07128de10 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -9,15 +9,14 @@ export function escapeHTML(value) { * @returns a single new object */ export function inherit(parent) { // inherit(parent, override_obj, override_obj, ...) - var key; var result = {}; var objects = Array.prototype.slice.call(arguments, 1); - for (key in parent) { + for (const key in parent) { result[key] = parent[key]; } objects.forEach(function(obj) { - for (key in obj) { + for (const key in obj) { result[key] = obj[key]; } });