diff --git a/.gitignore b/.gitignore index 0c2bcdb..f14fd9b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Thumbs.db /dist /node_modules +/test/__fixtures__/.temp-* diff --git a/package-lock.json b/package-lock.json index 4eb7671..868a820 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "find-up-json": "^2.0.5", "function-once": "^3.0.1", "import-meta-resolve": "^4.1.0", - "is-binary-path": "^3.0.0", + "is-binary-path": "^2.0.0", "js-yaml": "^4.1.0", "json-sorted-stringify": "^1.0.1", "json5": "^2.2.3", @@ -28,7 +28,7 @@ "tiny-readdir": "^2.7.4", "tiny-readdir-glob": "^1.23.2", "tiny-spinner": "^2.0.5", - "worktank": "^2.7.3", + "worktank": "^3.0.2", "zeptomatch": "^2.0.1", "zeptomatch-escape": "^1.0.1", "zeptomatch-is-static": "^1.0.1" @@ -1988,11 +1988,11 @@ "dev": true }, "node_modules/binary-extensions": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-3.0.0.tgz", - "integrity": "sha512-X0RfwMgXPEesg6PCXzytQZt9Unh9gtc4SfeTNJvKifUL//Oegcc/Yf31z6hThNZ8dnD3Ir3wkHVN0eWrTvP5ww==", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", "engines": { - "node": ">=18.20" + "node": ">=8" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" @@ -2699,6 +2699,11 @@ "node": ">=10.17.0" } }, + "node_modules/immediato": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/immediato/-/immediato-1.1.0.tgz", + "integrity": "sha512-6DTWQWiM3SyxAbNRDmMvFgZVwVP6wT8ciQv7GivxXejtXZFIcemC0Wlzfd/jEouJ2JroCIp4qZVloKW4BviUpQ==" + }, "node_modules/import-local": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz", @@ -2770,17 +2775,14 @@ "dev": true }, "node_modules/is-binary-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-3.0.0.tgz", - "integrity": "sha512-eSkpSYbqKip82Uw4z0iBK/5KmVzL2pf36kNKRtu6+mKvrow9sqF4w5hocQ9yV5v+9+wzHt620x3B7Wws/8lsGg==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", "dependencies": { - "binary-extensions": "^3.0.0" + "binary-extensions": "^2.0.0" }, "engines": { - "node": ">=18.20" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" + "node": ">=8" } }, "node_modules/is-core-module": { @@ -2843,6 +2845,19 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/isoconcurrency": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isoconcurrency/-/isoconcurrency-1.0.0.tgz", + "integrity": "sha512-YhuPf5V6uOtQQHt9gIkOTbq75ceXqraDvxtZZeS/XbNsre6fmM+WpJgNTSkGX5jB3+gnbwoTVqW1c3qdfyVpOA==" + }, + "node_modules/isotimer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isotimer/-/isotimer-1.0.0.tgz", + "integrity": "sha512-1p1wborMl9fFbulXx9YBpIqFnfUn/2tN8Ne9g3GLMaiQAPmN/wLlpNOKCNT822div3Sq7LKkApZJ+6JipDUusQ==", + "dependencies": { + "immediato": "^1.1.0" + } + }, "node_modules/istanbul-lib-coverage": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", @@ -5076,12 +5091,6 @@ "@types/node": "*" } }, - "node_modules/nanoexec/node_modules/promise-make-naked": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-3.0.0.tgz", - "integrity": "sha512-h71wwAMB2udFnlPmcxQMqKl6CckNLVKdk/ROtFivE6/VmW+rQKV0DWlGJ6VphRIoq22Tkonvdi3F+jlm5XDlow==", - "dev": true - }, "node_modules/natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -5350,16 +5359,11 @@ "promise-make-naked": "^3.0.2" } }, - "node_modules/promise-make-counter/node_modules/promise-make-naked": { + "node_modules/promise-make-naked": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-3.0.2.tgz", "integrity": "sha512-B+b+kQ1YrYS7zO7P7bQcoqqMUizP06BOyNSBEnB5VJKDSWo8fsVuDkfSmwdjF0JsRtaNh83so5MMFJ95soH5jg==" }, - "node_modules/promise-make-naked": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-2.1.1.tgz", - "integrity": "sha512-BLvgZSNRkQNM5RGL4Cz8wK76WSb+t3VeMJL+/kxRBHI5+nliqZezranGGtiu/ePeFo5+CaLRvvGMzXrBuu2tAA==" - }, "node_modules/promise-resolve-timeout": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/promise-resolve-timeout/-/promise-resolve-timeout-2.0.1.tgz", @@ -5984,9 +5988,9 @@ } }, "node_modules/webworker-shim": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/webworker-shim/-/webworker-shim-1.1.0.tgz", - "integrity": "sha512-LhPJDED3cM0+K9w4JjIio+RYPqvr712b3lyM+JjMR/rSD9elrSYHrrwE9qu3inPSSf60xqePgFgEwuAg17AuhA==" + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/webworker-shim/-/webworker-shim-1.1.4.tgz", + "integrity": "sha512-W/40L5W6ZQyGhYr3hJ7N/2SjdK5OdFtnYm94j6xlRyjckegXnIGwz0EdxdkQx6VGTglJjK8mqBhMz3fd3AY4bg==" }, "node_modules/when-exit": { "version": "2.1.4", @@ -6009,12 +6013,14 @@ } }, "node_modules/worktank": { - "version": "2.7.3", - "resolved": "https://registry.npmjs.org/worktank/-/worktank-2.7.3.tgz", - "integrity": "sha512-M0fesnpttBPdvNYBdzRvLDsacN0na9RYWFxwmM/x1+/6mufjduv9/9vBObK8EXDqxRMX/SOYJabpo0UCYYBUdQ==", + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/worktank/-/worktank-3.0.2.tgz", + "integrity": "sha512-ry5gPtWnakOnUBAAa2aiyWZwAFJuBtd/MwZH6o9DXnQHD4AZvidtl2uTLrb2d3Zjy9D04n84lHJNnIETQl7tuA==", "dependencies": { - "promise-make-naked": "^2.0.0", - "webworker-shim": "^1.1.0" + "isoconcurrency": "^1.0.0", + "isotimer": "^1.0.0", + "promise-make-naked": "^3.0.2", + "webworker-shim": "^1.1.4" } }, "node_modules/wrappy": { @@ -7678,9 +7684,9 @@ "dev": true }, "binary-extensions": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-3.0.0.tgz", - "integrity": "sha512-X0RfwMgXPEesg6PCXzytQZt9Unh9gtc4SfeTNJvKifUL//Oegcc/Yf31z6hThNZ8dnD3Ir3wkHVN0eWrTvP5ww==" + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==" }, "braces": { "version": "3.0.3", @@ -8185,6 +8191,11 @@ "integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==", "dev": true }, + "immediato": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/immediato/-/immediato-1.1.0.tgz", + "integrity": "sha512-6DTWQWiM3SyxAbNRDmMvFgZVwVP6wT8ciQv7GivxXejtXZFIcemC0Wlzfd/jEouJ2JroCIp4qZVloKW4BviUpQ==" + }, "import-local": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz", @@ -8239,11 +8250,11 @@ "dev": true }, "is-binary-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-3.0.0.tgz", - "integrity": "sha512-eSkpSYbqKip82Uw4z0iBK/5KmVzL2pf36kNKRtu6+mKvrow9sqF4w5hocQ9yV5v+9+wzHt620x3B7Wws/8lsGg==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", "requires": { - "binary-extensions": "^3.0.0" + "binary-extensions": "^2.0.0" } }, "is-core-module": { @@ -8285,6 +8296,19 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "isoconcurrency": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isoconcurrency/-/isoconcurrency-1.0.0.tgz", + "integrity": "sha512-YhuPf5V6uOtQQHt9gIkOTbq75ceXqraDvxtZZeS/XbNsre6fmM+WpJgNTSkGX5jB3+gnbwoTVqW1c3qdfyVpOA==" + }, + "isotimer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isotimer/-/isotimer-1.0.0.tgz", + "integrity": "sha512-1p1wborMl9fFbulXx9YBpIqFnfUn/2tN8Ne9g3GLMaiQAPmN/wLlpNOKCNT822div3Sq7LKkApZJ+6JipDUusQ==", + "requires": { + "immediato": "^1.1.0" + } + }, "istanbul-lib-coverage": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", @@ -10039,14 +10063,6 @@ "dev": true, "requires": { "promise-make-naked": "^3.0.0" - }, - "dependencies": { - "promise-make-naked": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-3.0.0.tgz", - "integrity": "sha512-h71wwAMB2udFnlPmcxQMqKl6CckNLVKdk/ROtFivE6/VmW+rQKV0DWlGJ6VphRIoq22Tkonvdi3F+jlm5XDlow==", - "dev": true - } } }, "natural-compare": { @@ -10244,19 +10260,12 @@ "integrity": "sha512-FJAxTBWQuQoAs4ZOYuKX1FHXxEgKLEzBxUvwr4RoOglkTpOjWuM+RXsK3M9q5lMa8kjqctUrhwYeZFT4ygsnag==", "requires": { "promise-make-naked": "^3.0.2" - }, - "dependencies": { - "promise-make-naked": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-3.0.2.tgz", - "integrity": "sha512-B+b+kQ1YrYS7zO7P7bQcoqqMUizP06BOyNSBEnB5VJKDSWo8fsVuDkfSmwdjF0JsRtaNh83so5MMFJ95soH5jg==" - } } }, "promise-make-naked": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-2.1.1.tgz", - "integrity": "sha512-BLvgZSNRkQNM5RGL4Cz8wK76WSb+t3VeMJL+/kxRBHI5+nliqZezranGGtiu/ePeFo5+CaLRvvGMzXrBuu2tAA==" + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/promise-make-naked/-/promise-make-naked-3.0.2.tgz", + "integrity": "sha512-B+b+kQ1YrYS7zO7P7bQcoqqMUizP06BOyNSBEnB5VJKDSWo8fsVuDkfSmwdjF0JsRtaNh83so5MMFJ95soH5jg==" }, "promise-resolve-timeout": { "version": "2.0.1", @@ -10745,9 +10754,9 @@ } }, "webworker-shim": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/webworker-shim/-/webworker-shim-1.1.0.tgz", - "integrity": "sha512-LhPJDED3cM0+K9w4JjIio+RYPqvr712b3lyM+JjMR/rSD9elrSYHrrwE9qu3inPSSf60xqePgFgEwuAg17AuhA==" + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/webworker-shim/-/webworker-shim-1.1.4.tgz", + "integrity": "sha512-W/40L5W6ZQyGhYr3hJ7N/2SjdK5OdFtnYm94j6xlRyjckegXnIGwz0EdxdkQx6VGTglJjK8mqBhMz3fd3AY4bg==" }, "when-exit": { "version": "2.1.4", @@ -10764,12 +10773,14 @@ } }, "worktank": { - "version": "2.7.3", - "resolved": "https://registry.npmjs.org/worktank/-/worktank-2.7.3.tgz", - "integrity": "sha512-M0fesnpttBPdvNYBdzRvLDsacN0na9RYWFxwmM/x1+/6mufjduv9/9vBObK8EXDqxRMX/SOYJabpo0UCYYBUdQ==", + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/worktank/-/worktank-3.0.2.tgz", + "integrity": "sha512-ry5gPtWnakOnUBAAa2aiyWZwAFJuBtd/MwZH6o9DXnQHD4AZvidtl2uTLrb2d3Zjy9D04n84lHJNnIETQl7tuA==", "requires": { - "promise-make-naked": "^2.0.0", - "webworker-shim": "^1.1.0" + "isoconcurrency": "^1.0.0", + "isotimer": "^1.0.0", + "promise-make-naked": "^3.0.2", + "webworker-shim": "^1.1.4" } }, "wrappy": { diff --git a/package.json b/package.json index 2c332ce..c79b434 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "find-up-json": "^2.0.5", "function-once": "^3.0.1", "import-meta-resolve": "^4.1.0", - "is-binary-path": "^3.0.0", + "is-binary-path": "^2.0.0", "js-yaml": "^4.1.0", "json-sorted-stringify": "^1.0.1", "json5": "^2.2.3", @@ -54,7 +54,7 @@ "tiny-readdir": "^2.7.4", "tiny-readdir-glob": "^1.23.2", "tiny-spinner": "^2.0.5", - "worktank": "^2.7.3", + "worktank": "^3.0.2", "zeptomatch": "^2.0.1", "zeptomatch-escape": "^1.0.1", "zeptomatch-is-static": "^1.0.1" diff --git a/src/prettier_parallel.ts b/src/prettier_parallel.ts index 3ba7594..25c47d0 100644 --- a/src/prettier_parallel.ts +++ b/src/prettier_parallel.ts @@ -1,14 +1,20 @@ import os from "node:os"; +import process from "node:process"; import WorkTank from "worktank"; import { resolve } from "./utils.js"; import type { Options, Prettier } from "./types.js"; function makeParallel(options: Options): Prettier { const pool = new WorkTank({ - name: "prettier", - size: options.parallelWorkers || Math.max(1, os.cpus().length - 1), - methods: new URL("./prettier_serial.js", import.meta.url), - warmup: true, + pool: { + name: "prettier", + size: options.parallelWorkers || Math.max(1, os.cpus().length - 1), + }, + worker: { + autoInstantiate: true, + env: process.env, + methods: new URL("./prettier_serial.js", import.meta.url), + }, }); return { diff --git a/src/utils.ts b/src/utils.ts index 25c6422..aa6ae35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -277,8 +277,15 @@ async function getTargetsPaths( } const filesExplicitPaths = targetFiles; - const filesFoundPaths = globResult.filesFound; - const foldersFoundPaths = [rootPath, ...globResult.directoriesFound]; + + const globFilesFoundPaths = globResult.filesFound; + const directoryFilesFoundPaths = directoriesResultsFiles.flat(); + const filesFoundPaths = [...globFilesFoundPaths, ...directoryFilesFoundPaths]; + + const globFoldersFoundPaths = globResult.directoriesFound; + const directoryFoldersFoundPaths = directoriesResults.flatMap((result) => result.directories); + const foldersFoundPaths = [rootPath, ...globFoldersFoundPaths, ...directoryFoldersFoundPaths]; + return [filesPaths, filesNames, filesNamesToPaths, filesExplicitPaths, filesFoundPaths, foldersFoundPaths]; } diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/.prettierrc.mjs b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/.prettierrc.mjs new file mode 100644 index 0000000..17730e3 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/.prettierrc.mjs @@ -0,0 +1,10 @@ +export default { + overrides: [ + { + files: ["dir/*.foo"], + options: { + plugins: ["../plugin-extensions/plugin.cjs"] + } + }, + ] +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/1.foo b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/1.foo new file mode 100644 index 0000000..6d1f6b8 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/1.foo @@ -0,0 +1 @@ +1.foo diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/dir/2.foo b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/dir/2.foo new file mode 100644 index 0000000..e2b9e47 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-complex-overrides/dir/2.foo @@ -0,0 +1 @@ +2.foo diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-config/foo.mjs b/test/__fixtures__/infer-plugins-ext-dir-with-config/foo.mjs new file mode 100644 index 0000000..5cd903e --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-config/foo.mjs @@ -0,0 +1,3 @@ +export default { + plugins: ["../plugin-extensions/plugin.cjs"], +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-config/src/file.foo b/test/__fixtures__/infer-plugins-ext-dir-with-config/src/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-config/src/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-default-options/.prettierrc.mjs b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/.prettierrc.mjs new file mode 100644 index 0000000..2fcf129 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/.prettierrc.mjs @@ -0,0 +1,3 @@ +export default { + plugins: ["../plugin-default-options/plugin.cjs"] +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/file.foo b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/index.js b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/index.js new file mode 100644 index 0000000..9e449a2 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-default-options/src/index.js @@ -0,0 +1,3 @@ +function main() { + console.log("Hello, World!"); +} diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/.prettierrc.mjs b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/.prettierrc.mjs new file mode 100644 index 0000000..6e5919c --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/.prettierrc.mjs @@ -0,0 +1,12 @@ +export default { + overrides: [ + { + // TODO (43081j): in prettier, this was `*.foo` and would successfully + // match `src/*.foo`. In prettier CLI, this is not the case + files: ["**/*.foo"], + options: { + plugins: ["../plugin-default-options/plugin.cjs"] + } + } + ] +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/file.foo b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/index.js b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/index.js new file mode 100644 index 0000000..9e449a2 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides-and-default-options/src/index.js @@ -0,0 +1,3 @@ +function main() { + console.log("Hello, World!"); +} diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides/.prettierrc.mjs b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/.prettierrc.mjs new file mode 100644 index 0000000..bdb3a28 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/.prettierrc.mjs @@ -0,0 +1,18 @@ +export default { + overrides: [ + { + // TODO (43081j): this was originally `*.foo` in prettier. Our globs + // behave differently. `*.foo` was matching `src/*.foo` + files: ["**/*.foo"], + options: { + plugins: ["../plugin-extensions/plugin.cjs"] + } + }, + { + files: ["**/*.bar"], + options: { + plugins: ["../plugin-extensions2/plugin.cjs"] + } + } + ] +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.bar b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.bar new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.bar @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.foo b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir-with-overrides/src/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-ext-dir/.prettierrc.mjs b/test/__fixtures__/infer-plugins-ext-dir/.prettierrc.mjs new file mode 100644 index 0000000..5cd903e --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir/.prettierrc.mjs @@ -0,0 +1,3 @@ +export default { + plugins: ["../plugin-extensions/plugin.cjs"], +}; diff --git a/test/__fixtures__/infer-plugins-ext-dir/src/file.foo b/test/__fixtures__/infer-plugins-ext-dir/src/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/infer-plugins-ext-dir/src/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/infer-plugins-with-multiple-config/dir/.prettierrc.mjs b/test/__fixtures__/infer-plugins-with-multiple-config/dir/.prettierrc.mjs new file mode 100644 index 0000000..da14dc4 --- /dev/null +++ b/test/__fixtures__/infer-plugins-with-multiple-config/dir/.prettierrc.mjs @@ -0,0 +1 @@ +export default { }; diff --git a/test/__fixtures__/infer-plugins-with-multiple-config/dir/1.foo b/test/__fixtures__/infer-plugins-with-multiple-config/dir/1.foo new file mode 100644 index 0000000..6d1f6b8 --- /dev/null +++ b/test/__fixtures__/infer-plugins-with-multiple-config/dir/1.foo @@ -0,0 +1 @@ +1.foo diff --git a/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/.prettierrc.mjs b/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/.prettierrc.mjs new file mode 100644 index 0000000..57150d3 --- /dev/null +++ b/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/.prettierrc.mjs @@ -0,0 +1,3 @@ +export default { + plugins: ["../plugin-default-options/plugin.cjs"] +} diff --git a/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/2.foo b/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/2.foo new file mode 100644 index 0000000..e2b9e47 --- /dev/null +++ b/test/__fixtures__/infer-plugins-with-multiple-config/dir/subdir/2.foo @@ -0,0 +1 @@ +2.foo diff --git a/test/__fixtures__/plugin-extensions2/plugin.cjs b/test/__fixtures__/plugin-extensions2/plugin.cjs new file mode 100644 index 0000000..50dfd3a --- /dev/null +++ b/test/__fixtures__/plugin-extensions2/plugin.cjs @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "bar", + parsers: ["bar-parser"], + extensions: [".bar"], + }, + ], + parsers: { + "bar-parser": { + parse: (text) => ({ text }), + astFormat: "bar-ast", + }, + }, + printers: { + "bar-ast": { + print(path) { + return ["!", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__tests__/__snapshots__/infer-plugins-ext-dir.js.snap b/test/__tests__/__snapshots__/infer-plugins-ext-dir.js.snap new file mode 100644 index 0000000..d3a28a6 --- /dev/null +++ b/test/__tests__/__snapshots__/infer-plugins-ext-dir.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`infer file ext that supported by only plugins basic (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with config option (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with defaultOptions (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with multiple config for nested dir (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with multiple config for nested dir 2 (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with multiple config for nested dir 2 (stderr) 2`] = `""`; + +exports[`infer file ext that supported by only plugins with overrides and defaultOptions (stderr) 1`] = `""`; + +exports[`infer file ext that supported by only plugins with overrides options (stderr) 1`] = `""`; diff --git a/test/__tests__/ignore-path.js b/test/__tests__/ignore-path.js index 1752343..75639bc 100644 --- a/test/__tests__/ignore-path.js +++ b/test/__tests__/ignore-path.js @@ -11,7 +11,7 @@ import { getFixturesPath, runCli } from "../utils"; }); const getUnformattedFiles = async (args) => { - const { stdout } = await runCli("ignore-path/ignore-path-test/", [ + const { stdout } = await runCli("ignore-path/ignore-path-test", [ "**/*.js", "-l", ...args, diff --git a/test/__tests__/infer-parser.js b/test/__tests__/infer-parser.js index a12260d..5752003 100644 --- a/test/__tests__/infer-parser.js +++ b/test/__tests__/infer-parser.js @@ -3,7 +3,7 @@ import { color } from "specialist"; describe("stdin no path and no parser", () => { describe("logs error and exits with 2", () => { - runCli("infer-parser/", [], { + runCli("infer-parser", [], { input: "foo" }).test({ status: 1, @@ -15,7 +15,7 @@ describe("stdin no path and no parser", () => { // TODO (43081j): in prettier, this tests that it exits with 0 for // whatever reason. should we do the same? describe("--check logs error but exits with 1", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--check" ], { input: "foo", @@ -29,7 +29,7 @@ describe("stdin no path and no parser", () => { // TODO (43081j): in prettier, this tests that it exits with 0 for // whatever reason. should we do the same? describe("--list-different logs error but exits with 1", () => { - runCli("infer-parser/", ["--list-different"], { + runCli("infer-parser", ["--list-different"], { input: "foo", }).test({ status: 1, @@ -41,7 +41,7 @@ describe("stdin no path and no parser", () => { describe("stdin with unknown path and no parser", () => { describe("logs error and exits with 2", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--stdin-filepath", "foo" ], { @@ -56,7 +56,7 @@ describe("stdin with unknown path and no parser", () => { // TODO (43081j): in prettier, this tests that it exits with 0 for // whatever reason. should we do the same? describe("--check logs error but exits with 1", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--check", "--stdin-filepath", "foo" @@ -72,7 +72,7 @@ describe("stdin with unknown path and no parser", () => { // TODO (43081j): in prettier, this tests that it exits with 0 for // whatever reason. should we do the same? describe("--list-different logs error but exits with 1", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--list-different", "--stdin-filepath", "foo" @@ -88,7 +88,7 @@ describe("stdin with unknown path and no parser", () => { describe("unknown path and no parser", () => { describe("specific file is ignored", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--end-of-line", "lf", "FOO" @@ -100,7 +100,7 @@ describe("unknown path and no parser", () => { }); describe("multiple files are ignored", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--end-of-line", "lf", "*" @@ -113,7 +113,7 @@ describe("unknown path and no parser", () => { describe("--check with unknown path and no parser", () => { describe("specific file is ignored", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--check", "FOO" ]).test({ @@ -123,7 +123,7 @@ describe("--check with unknown path and no parser", () => { }); describe("multiple files", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--check", "*" ]).test({ @@ -138,7 +138,7 @@ describe("--check with unknown path and no parser", () => { describe("--list-different with unknown path and no parser", () => { describe("specific file should be ignored", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--list-different", "FOO" ]).test({ @@ -149,7 +149,7 @@ describe("--list-different with unknown path and no parser", () => { }); describe("multiple files", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--list-different", "*" ]).test({ @@ -162,7 +162,7 @@ describe("--list-different with unknown path and no parser", () => { describe("--write with unknown path and no parser", () => { describe("specific file should be ignored", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--write", "FOO" ]).test({ @@ -173,7 +173,7 @@ describe("--write with unknown path and no parser", () => { }); describe("multiple files", () => { - runCli("infer-parser/", [ + runCli("infer-parser", [ "--write", "*" ]).test({ diff --git a/test/__tests__/infer-plugins-ext-dir.js b/test/__tests__/infer-plugins-ext-dir.js new file mode 100644 index 0000000..a310985 --- /dev/null +++ b/test/__tests__/infer-plugins-ext-dir.js @@ -0,0 +1,215 @@ +import { runCli } from "../utils"; +import dedent from "dedent"; + +describe("infer file ext that supported by only plugins", () => { + describe("basic", () => { + runCli("infer-plugins-ext-dir", [ + "--write", + "src" + ]).test({ + status: 0, + stdout: "src/file.foo", + write: [ + { + content: "!contents\n", + filename: "src/file.foo", + }, + ], + }); + }); + + describe("with config option", () => { + runCli("infer-plugins-ext-dir-with-config", [ + "--config-path", + "foo.mjs", + "--write", + "src", + ]).test({ + status: 0, + stdout: "src/file.foo", + write: [ + { + content: "!contents\n", + filename: "src/file.foo", + }, + ], + }); + }); + + describe("with overrides options", () => { + runCli("infer-plugins-ext-dir-with-overrides", [ + "--write", + "src", + ]).test({ + status: 0, + stdout: "src/file.bar\nsrc/file.foo", + write: [ + { + content: "!contents\n", + filename: "src/file.bar", + }, + { + content: "!contents\n", + filename: "src/file.foo", + }, + ], + }); + }); + + describe("with defaultOptions", () => { + runCli("infer-plugins-ext-dir-with-default-options", [ + "--write", + "--no-editorconfig", + "src", + ]).test({ + status: 0, + stdout: "src/file.foo\nsrc/index.js", + write: [ + { + content: '{"tabWidth":8,"bracketSpacing":false}', + filename: "src/file.foo", + }, + { + // formatted with `tabWidth: 2` + content: dedent` + function main() { + console.log("Hello, World!"); + }\n + `, + filename: "src/index.js", + }, + ], + }); + }); + + describe("with overrides and defaultOptions", () => { + runCli("infer-plugins-ext-dir-with-overrides-and-default-options", [ + "--write", + "--no-editorconfig", + "src", + ]).test({ + status: 0, + stdout: "src/file.foo\nsrc/index.js", + write: [ + { + content: '{"tabWidth":8,"bracketSpacing":false}', + filename: "src/file.foo", + }, + { + // formatted with `tabWidth: 2` + content: dedent` + function main() { + console.log("Hello, World!"); + }\n + `, + filename: "src/index.js", + }, + ], + }); + }); + + //FIXME: relative plugins in nested configs are resolved from the CWD, incorrectly + describe.skip("with multiple config for nested dir", () => { + runCli("infer-plugins-with-multiple-config", [ + "--write", + "--no-editorconfig", + ".", + ]).test({ + status: 0, + stdout: dedent` + dir/.prettierrc.mjs + dir/subdir/.prettierrc.mjs + dir/subdir/2.foo + `, + write: [ + { + content: "export default {};\n", + filename: "dir/.prettierrc.mjs", + }, + { + content: dedent` + export default { + plugins: ["../plugin-default-options/plugin.cjs"], + };\n + `, + filename: "dir/subdir/.prettierrc.mjs", + }, + { + content: '{"tabWidth":8,"bracketSpacing":false}', + filename: "dir/subdir/2.foo", + }, + ], + }); + }); + + //FIXME: relative plugins in nested configs are resolved from the CWD, incorrectly + describe.skip("with multiple config for nested dir 2", () => { + runCli("infer-plugins-with-multiple-config", [ + "--write", + "--no-editorconfig", + "dir", + "dir/subdir", + ]).test({ + status: 0, + stdout: dedent` + dir/.prettierrc.mjs + dir/subdir/.prettierrc.mjs + dir/subdir/2.foo + `, + write: [ + { + content: "export default {};\n", + filename: "dir/.prettierrc.mjs", + }, + { + content: dedent` + export default { + plugins: ["../plugin-default-options/plugin.cjs"], + };\n + `, + filename: "dir/subdir/.prettierrc.mjs", + }, + { + content: '{"tabWidth":8,"bracketSpacing":false}', + filename: "dir/subdir/2.foo", + }, + ], + }); + }); + + //FIXME: relative plugins in nested configs are resolved from the CWD, incorrectly + describe.skip("with multiple config for nested dir 2", () => { + runCli("infer-plugins-ext-dir-with-complex-overrides", [ + "--write", + "--no-editorconfig", + ".", + ]).test({ + status: 0, + stdout: dedent` + .prettierrc.mjs + dir/2.foo + `, + write: [ + { + content: dedent` + export default { + overrides: [ + { + files: ["dir/*.foo"], + options: { + plugins: ["../plugin-extensions/plugin.cjs"], + }, + }, + ], + };\n + `, + filename: ".prettierrc.mjs", + }, + { + content: "!2.foo\n", + filename: "dir/2.foo", + }, + ], + }); + }); +}); diff --git a/test/utils.js b/test/utils.js index 1afa63c..7d310ec 100644 --- a/test/utils.js +++ b/test/utils.js @@ -2,7 +2,6 @@ import once from "function-once"; import * as Archive from "json-archive"; import exec from "nanoexec"; import fs from "node:fs/promises"; -import os from "node:os"; import path from "node:path"; import process from "node:process"; import Base64 from "radix64-encoding"; @@ -50,7 +49,7 @@ async function getArchive(folderPath) { for (const fileName of changed) { const fileNext = packNext[fileName]; diff.push({ - filename: fileName, + filename: getNormalizedPath(fileName), content: Base64.decodeStr(fileNext.contents), }); } @@ -79,7 +78,7 @@ function getFixturesPath(dir) { async function getIsolatedFixtures(dir) { const [rootPart, ...subParts] = dir.split("/"); const fixturesPath = getFixturesPath(rootPart); - const tempPath = await getTempPath(`prettier-${rootPart}`); + const tempPath = getFixturesPath(`.temp-${rootPart}-${zeptoid()}`); const tempGitPath = path.join(tempPath, ".git"); const isolatedPath = path.join(tempPath, ...subParts); @@ -115,10 +114,8 @@ function getNormalizedOutput(output, options) { return output; } -async function getTempPath(prefix) { - const rootPath = await fs.realpath(os.tmpdir()); - const tempPath = path.join(rootPath, `${prefix}-${zeptoid()}`); - return tempPath; +function getNormalizedPath(filePath) { + return filePath.replaceAll("\\", "/"); } async function runCommand(dir, args, options) {