Skip to content

Commit

Permalink
Fix support for functions in data
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 5, 2023
1 parent 0d78ebb commit 8dee2ab
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 4 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@
* be stored on the `result` field of {@link VFile `VFile`}.
*/

import structuredClone from '@ungap/structured-clone'
import {bail} from 'bail'
import extend from 'extend'
import {ok as assert} from 'devlop'
import isPlainObj from 'is-plain-obj'
import {trough} from 'trough'
Expand Down Expand Up @@ -507,7 +507,7 @@ export class Processor extends CallableInstance {
destination.use(...attacher)
}

destination.data(structuredClone(this.namespace))
destination.data(extend(true, {}, this.namespace))

return destination
}
Expand Down Expand Up @@ -1115,10 +1115,7 @@ export class Processor extends CallableInstance {
addList(result.plugins)

if (result.settings) {
namespace.settings = {
...namespace.settings,
...structuredClone(result.settings)
}
namespace.settings = extend(true, namespace.settings, result.settings)
}
}

Expand Down Expand Up @@ -1166,7 +1163,7 @@ export class Processor extends CallableInstance {
let [primary, ...rest] = parameters
const currentPrimary = attachers[entryIndex][1]
if (isPlainObj(currentPrimary) && isPlainObj(primary)) {
primary = structuredClone({...currentPrimary, ...primary})
primary = extend(true, currentPrimary, primary)
}

attachers[entryIndex] = [plugin, primary, ...rest]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@
],
"dependencies": {
"@types/unist": "^3.0.0",
"@ungap/structured-clone": "^1.0.0",
"bail": "^2.0.0",
"devlop": "^1.0.0",
"extend": "^3.0.0",
"is-plain-obj": "^4.0.0",
"trough": "^2.0.0",
"vfile": "^6.0.0"
},
"devDependencies": {
"@types/extend": "^3.0.0",
"@types/hast": "^3.0.0",
"@types/mdast": "^4.0.0",
"@types/node": "^20.0.0",
"@types/ungap__structured-clone": "^0.3.0",
"c8": "^8.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
Expand Down
11 changes: 11 additions & 0 deletions test/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ test('`data`', async function (t) {
baz: 'qux'
})
})

await t.test('should support functions in data', async function () {
const fn = unified().data('fn', func)().data('fn')

assert(typeof fn === 'function')
assert.equal(fn, func)

function func() {
return 'alpha'
}
})
})
1 change: 1 addition & 0 deletions test/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare module 'unified' {
baz?: 'qux' | undefined
foo?: 'bar' | undefined
x?: boolean | undefined
fn?: Function | undefined
}

interface Settings {
Expand Down

0 comments on commit 8dee2ab

Please sign in to comment.