Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 18, 2021
1 parent 3ed16ae commit 0987af0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
39 changes: 18 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ 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))
}

// 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])) {
Expand All @@ -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)
Expand Down Expand Up @@ -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])) {
Expand All @@ -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 === '.') {
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
38 changes: 20 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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(
Expand Down

0 comments on commit 0987af0

Please sign in to comment.