From 9c5ac781891c32e98e77d996abe447de26835760 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 9 Jul 2021 17:00:55 +0200 Subject: [PATCH] Fix to revert to ducktyping * 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 --- lib/index.js | 15 ++++++++++++++- package.json | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index dae3a12..18b7a5a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -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 + ) +} diff --git a/package.json b/package.json index 76ea7d1..40a9672 100644 --- a/package.json +++ b/package.json @@ -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,