Skip to content

Commit

Permalink
Fix bug where nodes where added multiple times
Browse files Browse the repository at this point in the history
Related to get-alex/alex#218.
  • Loading branch information
wooorm committed Oct 5, 2018
1 parent 816c4a1 commit 135f30c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 18 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function toNLCST(tree, file, Parser) {
}
} else if (child && start === -1) {
find(child)
start = index + 1
} else {
;(viable ? add : findAll)(children.slice(start, index))

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/eof-eol-missing/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>🤔</p>
67 changes: 67 additions & 0 deletions test/fixtures/eof-eol-missing/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "RootNode",
"children": [
{
"type": "ParagraphNode",
"children": [
{
"type": "SentenceNode",
"children": [
{
"type": "SymbolNode",
"value": "🤔",
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 10,
"offset": 9
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/eof-eol/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>🤔</p>
67 changes: 67 additions & 0 deletions test/fixtures/eof-eol/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "RootNode",
"children": [
{
"type": "ParagraphNode",
"children": [
{
"type": "SentenceNode",
"children": [
{
"type": "SymbolNode",
"value": "🤔",
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 4,
"offset": 3
},
"end": {
"line": 1,
"column": 6,
"offset": 5
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 2,
"column": 1,
"offset": 10
}
}
}
38 changes: 20 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ var negate = require('negate')
var hidden = require('is-hidden')
var toNLCST = require('..')

var read = fs.readFileSync
var join = path.join

var ROOT = join(__dirname, 'fixtures')

var fixtures = fs.readdirSync(ROOT)

test('hast-util-to-nlcst', function(t) {
t.throws(
function() {
Expand Down Expand Up @@ -146,17 +139,26 @@ test('hast-util-to-nlcst', function(t) {
})

test('Fixtures', function(t) {
fixtures.filter(negate(hidden)).forEach(function(fixture) {
var filepath = join(ROOT, fixture)
var input = vfile(read(join(filepath, 'input.html')))
var output = read(join(filepath, 'output.json'))

t.deepEqual(
toNLCST(rehype().parse(input), input, Latin),
JSON.parse(output),
'should work on `' + fixture + '`'
)
})
var root = path.join(__dirname, 'fixtures')

fs.readdirSync(root)
.filter(negate(hidden))
.forEach(function(fixture) {
var input = path.join(root, fixture, 'input.html')
var output = path.join(root, fixture, 'output.json')
var file = vfile(fs.readFileSync(input))
var actual = toNLCST(rehype().parse(file), file, Latin)
var expected

try {
expected = JSON.parse(fs.readFileSync(output))
} catch (error) {
fs.writeFileSync(output, JSON.stringify(actual, null, 2) + '\n')
return
}

t.deepEqual(actual, expected, 'should work on `' + fixture + '`')
})

t.end()
})

0 comments on commit 135f30c

Please sign in to comment.