From 0987af04b0614476a6cfde3662bd716f8c7f6841 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 18 Jul 2021 16:39:08 +0200 Subject: [PATCH] Refactor code-style --- index.js | 39 ++++++++++++++++++--------------------- package.json | 7 +------ test/index.js | 38 ++++++++++++++++++++------------------ 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/index.js b/index.js index 9f9cf52..791e164 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,11 @@ import {pointStart, pointEnd} from 'unist-util-position' import {modifyChildren} from 'unist-util-modify-children' import {toString} from 'nlcst-to-string' -var word = convert('WordNode') -var punctuationOrSymbol = convert(['PunctuationNode', 'SymbolNode']) -var applicable = convert(['WordNode', 'PunctuationNode', 'SymbolNode']) +const word = convert('WordNode') +const punctuationOrSymbol = convert(['PunctuationNode', 'SymbolNode']) +const applicable = convert(['WordNode', 'PunctuationNode', 'SymbolNode']) -var slashes = /^\/{1,3}$/ +const slashes = /^\/{1,3}$/ export default function retextSyntaxUrls() { this.Parser.prototype.useFirst('tokenizeSentence', modifyChildren(mergeLinks)) @@ -15,21 +15,18 @@ export default function retextSyntaxUrls() { // eslint-disable-next-line complexity function mergeLinks(child, index, parent) { - var siblings = parent.children - var nodes = [child] - var start = index - var end = index - var currentIndex = index - var value - var initial - var final - var previous - var next + const siblings = parent.children + const nodes = [child] + let start = index + let end = index + const currentIndex = index if (!punctuationOrSymbol(child) || toString(child) !== '.') { return } + let previous + // Find preceding word/punctuation. // Stop before slashes, break after `www`. while ((previous = siblings[start - 1])) { @@ -50,7 +47,7 @@ function mergeLinks(child, index, parent) { } // Find following word/punctuation. - next = siblings[end + 1] + let next = siblings[end + 1] while (applicable(next)) { end++ nodes.push(next) @@ -81,7 +78,7 @@ function mergeLinks(child, index, parent) { start -= 2 } - value = null + let value = null // Remove the last node if it’s punctuation, unless it’s `/` or `)`. if (punctuationOrSymbol(siblings[end])) { @@ -93,16 +90,16 @@ function mergeLinks(child, index, parent) { } } - child = {type: 'SourceNode', value: toString(nodes)} - initial = pointStart(nodes[0]) - final = pointEnd(nodes[nodes.length - 1]) + const replacement = {type: 'SourceNode', value: toString(nodes)} + const initial = pointStart(nodes[0]) + const final = pointEnd(nodes[nodes.length - 1]) if (initial.line && final.line) { - child.position = {start: initial, end: final} + replacement.position = {start: initial, end: final} } // Remove the nodes and insert a SourceNode. - siblings.splice(start, end - start + 1, child) + siblings.splice(start, end - start + 1, replacement) // Ignore the following full-stop: it’s not part of a link. if (value === '.') { diff --git a/package.json b/package.json index e4d3cad..a7af5d2 100644 --- a/package.json +++ b/package.json @@ -63,12 +63,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "rules": { - "no-var": "off", - "prefer-arrow-callback": "off", - "unicorn/no-array-callback-reference": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ diff --git a/test/index.js b/test/index.js index 287a04b..f3a4277 100644 --- a/test/index.js +++ b/test/index.js @@ -9,21 +9,21 @@ import {visit} from 'unist-util-visit' import retextSyntaxUrls from '../index.js' import {correct, incorrect} from './lists.js' -var position = retext().use(retextSyntaxUrls) -var noPosition = retext().use(off).use(retextSyntaxUrls) - -function off() { - this.Parser.prototype.position = false -} +const position = retext().use(retextSyntaxUrls) +const noPosition = retext() + .use(function () { + Object.assign(this.Parser.prototype, {position: false}) + }) + .use(retextSyntaxUrls) -test('retext-syntax-urls', function (t) { - t.test('Correct URLs', function (st) { +test('retext-syntax-urls', (t) => { + t.test('Correct URLs', (st) => { let index = -1 while (++index < correct.length) { const url = correct[index] - st.doesNotThrow(function () { - var tree = position.parse('Check out ' + url + ' it’s awesome!') - var node = tree.children[0].children[0].children[4] + st.doesNotThrow(() => { + const tree = position.parse('Check out ' + url + ' it’s awesome!') + const node = tree.children[0].children[0].children[4] assert.strictEqual(node.type, 'SourceNode', 'is a source node') assert.strictEqual(node.value, url, 'should have the correct value') }, url) @@ -32,13 +32,13 @@ test('retext-syntax-urls', function (t) { st.end() }) - t.test('Incorrect URLs', function (st) { + t.test('Incorrect URLs', (st) => { let index = -1 while (++index < incorrect.length) { const url = incorrect[index] - st.doesNotThrow(function () { - var tree = position.parse('Check out ' + url + ' it’s bad!') + st.doesNotThrow(() => { + const tree = position.parse('Check out ' + url + ' it’s bad!') visit(tree, 'SourceNode', found) @@ -54,8 +54,8 @@ test('retext-syntax-urls', function (t) { t.end() }) -test('fixtures', function (t) { - var root = path.join('test', 'fixtures') +test('fixtures', (t) => { + const root = path.join('test', 'fixtures') const files = fs.readdirSync(root) let index = -1 @@ -64,8 +64,10 @@ test('fixtures', function (t) { if (isHidden(name)) continue - var input = fs.readFileSync(path.join(root, name, 'input.txt')) - var base = JSON.parse(fs.readFileSync(path.join(root, name, 'output.json'))) + const input = fs.readFileSync(path.join(root, name, 'input.txt')) + const base = JSON.parse( + fs.readFileSync(path.join(root, name, 'output.json')) + ) t.deepLooseEqual(position.parse(input), base, name + ' w/ position') t.deepLooseEqual(