From 5c3c16d3767efbeff683a440fb0767be4c79d132 Mon Sep 17 00:00:00 2001 From: Oliver Blanthorn Date: Thu, 18 Jun 2020 18:28:10 +0100 Subject: [PATCH] Re-enable `prefer-const` rule --- .eslintrc.js | 2 +- src/content/toys.ts | 2 +- src/excmds.ts | 3 +-- src/lib/html-tagged-template.js | 29 ++++++++++++++--------------- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 665fdfbd..4490fefc 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -224,7 +224,7 @@ module.exports = { ], "prefer-arrow/prefer-arrow-functions": "off", "prefer-const": [ - "off", //"error", + "error", { "destructuring": "all" } diff --git a/src/content/toys.ts b/src/content/toys.ts index 0be96b1a..3bd4af91 100644 --- a/src/content/toys.ts +++ b/src/content/toys.ts @@ -20,7 +20,7 @@ export function no_mouse() { rain([" "], "#FFF", 0) // No characters, unused colour code, no darkening } -export let snow = () => rain(["❄"], "#FFF", 0.15) +export const snow = () => rain(["❄"], "#FFF", 0.15) export function rain(characters: string[], colour, darkening = 0.05) { const d = document.createElement("div") diff --git a/src/excmds.ts b/src/excmds.ts index ae57a884..c516b1db 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -2322,8 +2322,7 @@ export async function fullscreen() { export async function tabclose(...indexes: string[]) { let done if (indexes.length > 0) { - let ids: number[] - ids = await Promise.all(indexes.map(index => idFromIndex(index))) + const ids = await Promise.all(indexes.map(index => idFromIndex(index))) done = browser.tabs.remove(ids) } else { // Close current tab diff --git a/src/lib/html-tagged-template.js b/src/lib/html-tagged-template.js index 191f77fc..cc003f75 100644 --- a/src/lib/html-tagged-template.js +++ b/src/lib/html-tagged-template.js @@ -272,19 +272,19 @@ // template tags allow any HTML (even elements out of context) // @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template - let template = document.createElement("template") + const template = document.createElement("template") template.innerHTML = str // find all substitution values and safely encode them using DOM APIs and // contextual auto-escaping - let walker = document.createNodeIterator( + const walker = document.createNodeIterator( template.content, NodeFilter.SHOW_ALL, ) let node while ((node = walker.nextNode())) { let tag = null - let attributesToRemove = [] + const attributesToRemove = [] // -------------------------------------------------- // node name substitution @@ -319,7 +319,7 @@ // tag and append its contents which will make it execute correctly. // @see http://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml else if (node.nodeName === "SCRIPT") { - let script = document.createElement("script") + const script = document.createElement("script") tag = script node._replacedWith = script @@ -338,20 +338,20 @@ // object so the loop will still work as expected. if (!(node.attributes instanceof NamedNodeMap)) { // first clone the node so we can isolate it from any children - let temp = node.cloneNode() + const temp = node.cloneNode() // parse the node string for all attributes - let attributeMatches = temp.outerHTML.match( + const attributeMatches = temp.outerHTML.match( ATTRIBUTE_PARSER_REGEX, ) // get all attribute names and their value attributes = [] - for (let i = 0; i < attributeMatches.length; i++) { - let attributeName = attributeMatches[i] + for (const attribute of attributeMatches.length) { + const attributeName = attribute .trim() .split("=")[0] - let attributeValue = node.getAttribute( + const attributeValue = node.getAttribute( attributeName, ) @@ -367,8 +367,7 @@ attributes = Array.from(node.attributes) } - for (let i = 0; i < attributes.length; i++) { - let attribute = attributes[i] + for (const attribute of attributes) { let name = attribute.name let value = attribute.value let hasSubstitution = false @@ -431,7 +430,7 @@ CUSTOM_URI_ATTRIBUTES_REGEX.test(name) ) { // percent encode if the value is inside of a query parameter - let queryParamIndex = value.indexOf("=") + const queryParamIndex = value.indexOf("=") if ( queryParamIndex !== -1 && offset > queryParamIndex @@ -506,7 +505,7 @@ // all of that for us // @see https://www.mediawiki.org/wiki/DOM-based_XSS if (tag || hasSubstitution) { - let el = tag || node + const el = tag || node // optional attribute if (name.substr(-1) === "?") { @@ -553,13 +552,13 @@ node.nodeType === 3 && node.nodeValue.indexOf(SUBSTITUTION_INDEX) !== -1 ) { - let nodeValue = node.nodeValue.replace( + const nodeValue = node.nodeValue.replace( SUBSTITUTION_REGEX, replaceSubstitution, ) // createTextNode() should not need to be escaped to prevent XSS? - let text = document.createTextNode(nodeValue) + const text = document.createTextNode(nodeValue) // since the parent node has already gone through the iterator, we can use // replaceChild() here