Skip to content

Commit

Permalink
Fix to revert to ducktyping
Browse files Browse the repository at this point in the history
* Before this major, files were ducktyped.
* With this major, VFiles across realms (e.g., from multiple install locations)
  were wrapped, causing state to be lost
* This fixes that
  • Loading branch information
wooorm committed Jul 9, 2021
1 parent f67d195 commit 9c5ac78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function toVFile(options) {
options = {path: fileURLToPath(options)}
}

return options instanceof VFile ? options : new VFile(options)
return looksLikeAVFile(options) ? options : new VFile(options)
}

toVFile.readSync = readSync
Expand Down Expand Up @@ -208,3 +208,16 @@ toVFile.write =
}
}
)

/**
* @param {Compatible} value
* @returns {value is VFile}
*/
function looksLikeAVFile(value) {
return (
value &&
typeof value === 'object' &&
'message' in value &&
'messages' in value
)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand Down

0 comments on commit 9c5ac78

Please sign in to comment.