Skip to content

Commit

Permalink
Update vfile
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 23, 2021
1 parent 343fae5 commit c855be8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
11 changes: 5 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @typedef {Parent['children'][number]} Child
* @typedef {Element['children'][number]} ElementChild
* @typedef {Child|Root} Node
* @typedef {import('vfile-location').Location} Location
* @typedef {import('vfile').VFile} VFile
*
* @typedef {{
Expand All @@ -27,7 +26,7 @@ import toString from 'hast-util-to-string'
import {whitespace} from 'hast-util-whitespace'
import {toString as nlcstToString} from 'nlcst-to-string'
import {pointStart} from 'unist-util-position'
import vfileLocation from 'vfile-location'
import {location} from 'vfile-location'

const source = convertElement(['code', dataNlcstSourced])
const ignore = convertElement([
Expand Down Expand Up @@ -96,7 +95,7 @@ export function toNlcst(tree, file, Parser) {
}

const doc = String(file)
const location = vfileLocation(doc)
const loc = location(doc)
const parser = 'parse' in Parser ? Parser : new Parser()
/** @type {Array.<UnistNode>} */
const results = []
Expand All @@ -106,7 +105,7 @@ export function toNlcst(tree, file, Parser) {
return {
type: 'RootNode',
children: results,
position: {start: location.toPoint(0), end: location.toPoint(doc.length)}
position: {start: loc.toPoint(0), end: loc.toPoint(doc.length)}
}

/**
Expand Down Expand Up @@ -239,7 +238,7 @@ export function toNlcst(tree, file, Parser) {
}

return change && replacement
? patch(replacement, location, location.toOffset(pointStart(node)))
? patch(replacement, loc, loc.toOffset(pointStart(node)))
: replacement
}

Expand Down Expand Up @@ -270,7 +269,7 @@ export function toNlcst(tree, file, Parser) {
*
* @template {Array.<UnistNode>} T
* @param {T} nodes
* @param {Location} location
* @param {ReturnType<location>} location
* @param {number} offset
* @returns {T}
*/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"hast-util-whitespace": "^2.0.0",
"nlcst-to-string": "^3.0.0",
"unist-util-position": "^4.0.0",
"vfile": "^4.0.0",
"vfile-location": "^3.1.0"
"vfile": "^5.0.0",
"vfile-location": "^4.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
Expand Down
18 changes: 9 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import test from 'tape'
import rehype from 'rehype'
import vfile from 'vfile'
import {VFile} from 'vfile'
// @ts-expect-error: to do type.
import {ParseLatin} from 'parse-latin'
// @ts-expect-error: to do type.
Expand Down Expand Up @@ -61,15 +61,15 @@ test('hast-util-to-nlcst', (t) => {
t.throws(
() => {
// @ts-ignore runtime.
toNlcst({type: 'text', value: 'foo'}, vfile('foo'))
toNlcst({type: 'text', value: 'foo'}, new VFile('foo'))
},
/hast-util-to-nlcst expected parser/,
'should fail without parser'
)

t.throws(
() => {
toNlcst({type: 'text', value: 'foo'}, vfile(), ParseLatin)
toNlcst({type: 'text', value: 'foo'}, new VFile(), ParseLatin)
},
/hast-util-to-nlcst expected position on nodes/,
'should fail when not given positional information'
Expand All @@ -85,7 +85,7 @@ test('hast-util-to-nlcst', (t) => {
end: {line: 1, column: 4}
}
},
vfile(),
new VFile(),
ParseEnglish
)
}, 'should accept a parser constructor')
Expand All @@ -100,7 +100,7 @@ test('hast-util-to-nlcst', (t) => {
end: {line: 1, column: 4}
}
},
vfile(),
new VFile(),
new ParseDutch()
)
}, 'should accept a parser instance')
Expand All @@ -114,7 +114,7 @@ test('hast-util-to-nlcst', (t) => {
// @ts-ignore runtime.
position: {start: {}, end: {}}
},
vfile(),
new VFile(),
ParseLatin
)
},
Expand All @@ -132,7 +132,7 @@ test('hast-util-to-nlcst', (t) => {
end: {line: 1, column: 4}
}
},
vfile('foo'),
new VFile('foo'),
ParseLatin
)

Expand All @@ -149,7 +149,7 @@ test('hast-util-to-nlcst', (t) => {
value: 'a',
position: {start: {line: 1, column: 1}, end: {line: 1, column: 9}}
},
vfile('<!--a-->'),
new VFile('<!--a-->'),
ParseLatin
)

Expand Down Expand Up @@ -192,7 +192,7 @@ test('Fixtures', (t) => {

input = path.join(root, files[index], 'input.html')
output = path.join(root, files[index], 'output.json')
file = vfile(fs.readFileSync(input))
file = new VFile(fs.readFileSync(input))
// @ts-ignore Assume hast.
actual = toNlcst(rehype().parse(file), file, ParseLatin)

Expand Down

0 comments on commit c855be8

Please sign in to comment.