Skip to content

Commit

Permalink
Remove support for passing file directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 5, 2023
1 parent ca404c7 commit 81cde21
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 165 deletions.
30 changes: 3 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,17 @@ const proto = Object.prototype
*
* @param {P5Node} tree
* `parse5` tree to transform.
* @param {Options | VFile | null | undefined} [options]
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Nodes}
* hast tree.
*/
export function fromParse5(tree, options) {
const options_ = options || {}
/** @type {Options} */
let settings
/** @type {VFile | undefined} */
let file

if (isFile(options_)) {
file = options_
settings = {}
} else {
file = options_.file || undefined
settings = options_
}
const settings = options || {}

return one(
{
file,
file: settings.file || undefined,
location: false,
schema: settings.space === 'svg' ? svg : html,
verbose: settings.verbose
Expand Down Expand Up @@ -381,15 +369,3 @@ function position(loc) {
function point(point) {
return point.line && point.column ? point : undefined
}

/**
* Check if something is a file.
*
* @param {VFile | Options} value
* File or options.
* @returns {value is VFile}
* Whether `value` is a file.
*/
function isFile(value) {
return 'messages' in value
}
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`fromParse5(tree[, file|options])`](#fromparse5tree-fileoptions)
* [`fromParse5(tree[, options])`](#fromparse5tree-options)
* [`Options`](#options)
* [`Space`](#space-1)
* [Types](#types)
Expand Down Expand Up @@ -84,7 +84,7 @@ import {fromParse5} from 'hast-util-from-parse5'

const file = await read('example.html')
const p5ast = parse(String(file), {sourceCodeLocationInfo: true})
const hast = fromParse5(p5ast, file)
const hast = fromParse5(p5ast, {file})

console.log(inspect(hast))
```
Expand Down Expand Up @@ -118,16 +118,14 @@ root[2] (1:1-2:1, 0-70)
This package exports the identifier [`fromParse5`][fromparse5].
There is no default export.

### `fromParse5(tree[, file|options])`
### `fromParse5(tree[, options])`

Transform a `parse5` AST to hast.

###### Parameters

* `tree` ([`Parse5Node`][parse5-node])
`parse5` tree to transform
* `file` ([`VFile`][vfile], optional)
— corresponding file (treated as `{file: file}`)
* `options` ([`Options`][options], optional)
— configuration

Expand Down Expand Up @@ -326,7 +324,7 @@ abide by its terms.
[hast-node]: https://github.com/syntax-tree/hast#nodes
[fromparse5]: #fromparse5tree-fileoptions
[fromparse5]: #fromparse5tree-options
[options]: #options
Expand Down
134 changes: 2 additions & 132 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,136 +93,6 @@ test('fromParse5', async function (t) {
})
})

await t.test('should accept a file as options', async function () {
assert.deepEqual(
fromParse5(parse(String(file), {sourceCodeLocationInfo: true}), file),
{
type: 'root',
children: [
{
type: 'element',
tagName: 'html',
properties: {},
children: [
{
type: 'element',
tagName: 'head',
properties: {},
children: [
{
type: 'element',
tagName: 'title',
properties: {},
children: [
{
type: 'text',
value: 'Hello!',
position: {
start: {line: 1, column: 8, offset: 7},
end: {line: 1, column: 14, offset: 13}
}
}
],
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 22, offset: 21}
}
}
]
},
{
type: 'element',
tagName: 'body',
properties: {},
children: [
{
type: 'element',
tagName: 'h1',
properties: {},
children: [
{
type: 'text',
value: 'World!',
position: {
start: {line: 1, column: 26, offset: 25},
end: {line: 1, column: 32, offset: 31}
}
}
],
position: {
start: {line: 1, column: 22, offset: 21},
end: {line: 1, column: 32, offset: 31}
}
}
]
}
]
}
],
data: {quirksMode: true},
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 32, offset: 31}
}
}
)
})

await t.test(
'should accept a file as options (without location info)',
async function () {
assert.deepEqual(fromParse5(parse(String(file)), file), {
type: 'root',
children: [
{
type: 'element',
tagName: 'html',
properties: {},
children: [
{
type: 'element',
tagName: 'head',
properties: {},
children: [
{
type: 'element',
tagName: 'title',
properties: {},
children: [
{
type: 'text',
value: 'Hello!'
}
]
}
]
},
{
type: 'element',
tagName: 'body',
properties: {},
children: [
{
type: 'element',
tagName: 'h1',
properties: {},
children: [
{
type: 'text',
value: 'World!'
}
]
}
]
}
]
}
],
data: {quirksMode: true}
})
}
)

await t.test('should support synthetic locations', async function () {
assert.deepEqual(
fromParse5(
Expand All @@ -246,7 +116,7 @@ test('fromParse5', async function (t) {
startOffset: 0
}
},
file
{file}
),
{
type: 'element',
Expand Down Expand Up @@ -295,7 +165,7 @@ test('fromParse5', async function (t) {
startOffset: 0
}
},
file
{file}
),
{
type: 'element',
Expand Down

0 comments on commit 81cde21

Please sign in to comment.