Remove {resolveConfig,resolveConfigFile,getFileInfo}.sync (#12788)
This commit is contained in:
1 parent
c1f6d021e5
commit
34fadbe67d
14 files changed
+128
-454
No files matched your search
@@ -1,7 +1,8 @@
|
||||
#### [BREAKING] Change public apis to asynchronous (#12574 by @fisker)
|
||||
#### [BREAKING] Change public apis to asynchronous (#12574, #12788 by @fisker)
|
||||
|
||||
- `prettier.format()` returns `Promise<string>`
|
||||
- `prettier.formatWithCursor()` returns `Promise<{formatted: string, cursorOffset: number}>`
|
||||
- `prettier.check()` returns `Promise<boolean>`
|
||||
|
||||
More changes are coming...
|
||||
- `prettier.resolveConfig.sync` is removed
|
||||
- `prettier.resolveConfigFile.sync` is removed
|
||||
- `prettier.getFileInfo.sync` is removed
|
||||
@@ -58,8 +58,6 @@ If `options.editorconfig` is `true` and an [`.editorconfig` file](https://editor
|
||||
- `indent_size`/`tab_width`
|
||||
- `max_line_length`
|
||||
|
||||
Use `prettier.resolveConfig.sync(filePath [, options])` if you’d like to use sync version.
|
||||
|
||||
## `prettier.resolveConfigFile([filePath])`
|
||||
|
||||
`resolveConfigFile` can be used to find the path of the Prettier configuration file that will be used when resolving the config (i.e. when calling `resolveConfig`). A promise is returned which will resolve to:
|
||||
@@ -77,8 +75,6 @@ prettier.resolveConfigFile(filePath).then((configFile) => {
|
||||
});
|
||||
```
|
||||
|
||||
Use `prettier.resolveConfigFile.sync([filePath])` if you’d like to use sync version.
|
||||
|
||||
## `prettier.clearConfigCache()`
|
||||
|
||||
When Prettier loads configuration files and plugins, the file system structure is cached for performance. This function will clear the cache. Generally this is only needed for editor integrations that know that the file system has changed since the last format took place.
|
||||
@@ -104,8 +100,6 @@ Providing [plugin](plugins.md) paths in `options.plugins` (`string[]`) helps ext
|
||||
|
||||
When setting `options.resolveConfig` (`boolean`, default `false`), Prettier will resolve the configuration for the given `filePath`. This is useful, for example, when the `inferredParser` might be overridden for a subset of files.
|
||||
|
||||
Use `prettier.getFileInfo.sync(filePath [, options])` if you’d like to use sync version.
|
||||
|
||||
## `prettier.getSupportInfo()`
|
||||
|
||||
Returns an object representing the options, parsers, languages and file types Prettier supports.
|
||||
|
||||
@@ -11,25 +11,6 @@ async function createIgnorer(ignorePath, withNodeModules) {
|
||||
? await getFileContentOrNull(path.resolve(ignorePath))
|
||||
: null;
|
||||
|
||||
return _createIgnorer(ignoreContent, withNodeModules);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string?} ignorePath
|
||||
* @param {boolean?} withNodeModules
|
||||
*/
|
||||
createIgnorer.sync = function (ignorePath, withNodeModules) {
|
||||
const ignoreContent = !ignorePath
|
||||
? null
|
||||
: getFileContentOrNull.sync(path.resolve(ignorePath));
|
||||
return _createIgnorer(ignoreContent, withNodeModules);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {null | string} ignoreContent
|
||||
* @param {boolean?} withNodeModules
|
||||
*/
|
||||
function _createIgnorer(ignoreContent, withNodeModules) {
|
||||
const ignorer = ignore({ allowRelativePaths: true }).add(ignoreContent || "");
|
||||
if (!withNodeModules) {
|
||||
ignorer.add("node_modules");
|
||||
|
||||
@@ -31,33 +31,9 @@ async function getFileInfo(filePath, opts) {
|
||||
plugins: opts.plugins,
|
||||
resolveConfig: opts.resolveConfig,
|
||||
ignorePath: opts.ignorePath,
|
||||
sync: false,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {FileInfoOptions} opts
|
||||
* @returns {FileInfoResult}
|
||||
*/
|
||||
getFileInfo.sync = function (filePath, opts) {
|
||||
if (typeof filePath !== "string") {
|
||||
throw new TypeError(
|
||||
`expect \`filePath\` to be a string, got \`${typeof filePath}\``
|
||||
);
|
||||
}
|
||||
|
||||
const ignorer = createIgnorer.sync(opts.ignorePath, opts.withNodeModules);
|
||||
return _getFileInfo({
|
||||
ignorer,
|
||||
filePath,
|
||||
plugins: opts.plugins,
|
||||
resolveConfig: opts.resolveConfig,
|
||||
ignorePath: opts.ignorePath,
|
||||
sync: true,
|
||||
});
|
||||
};
|
||||
|
||||
function getFileParser(resolvedConfig, filePath, plugins) {
|
||||
if (resolvedConfig && resolvedConfig.parser) {
|
||||
return resolvedConfig.parser;
|
||||
@@ -72,13 +48,12 @@ function getFileParser(resolvedConfig, filePath, plugins) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function _getFileInfo({
|
||||
async function _getFileInfo({
|
||||
ignorer,
|
||||
filePath,
|
||||
plugins,
|
||||
resolveConfig = false,
|
||||
ignorePath,
|
||||
sync = false,
|
||||
}) {
|
||||
const normalizedFilePath = normalizeFilePath(filePath, ignorePath);
|
||||
|
||||
@@ -94,18 +69,9 @@ function _getFileInfo({
|
||||
let resolvedConfig;
|
||||
|
||||
if (resolveConfig) {
|
||||
if (sync) {
|
||||
resolvedConfig = config.resolveConfig.sync(filePath);
|
||||
} else {
|
||||
return config.resolveConfig(filePath).then((resolvedConfig) => {
|
||||
fileInfo.inferredParser = getFileParser(
|
||||
resolvedConfig,
|
||||
filePath,
|
||||
plugins
|
||||
);
|
||||
return fileInfo;
|
||||
});
|
||||
}
|
||||
const resolvedConfig = await config.resolveConfig(filePath);
|
||||
fileInfo.inferredParser = getFileParser(resolvedConfig, filePath, plugins);
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
fileInfo.inferredParser = getFileParser(resolvedConfig, filePath, plugins);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
module.exports = {
|
||||
cosmiconfig: require("cosmiconfig").cosmiconfig,
|
||||
cosmiconfigSync: require("cosmiconfig").cosmiconfigSync,
|
||||
findParentDir: require("find-parent-dir").sync,
|
||||
getStdin: require("get-stdin"),
|
||||
isCI: () => require("ci-info").isCI,
|
||||
|
||||
@@ -17,24 +17,15 @@ const editorconfigAsyncNoCache = async (filePath) =>
|
||||
editorConfigToPrettier(await maybeParse(filePath, editorconfig.parse));
|
||||
const editorconfigAsyncWithCache = jsonStringifyMem(editorconfigAsyncNoCache);
|
||||
|
||||
const editorconfigSyncNoCache = (filePath) =>
|
||||
editorConfigToPrettier(maybeParse(filePath, editorconfig.parseSync));
|
||||
const editorconfigSyncWithCache = jsonStringifyMem(editorconfigSyncNoCache);
|
||||
|
||||
function getLoadFunction(opts) {
|
||||
if (!opts.editorconfig) {
|
||||
return () => null;
|
||||
}
|
||||
|
||||
if (opts.sync) {
|
||||
return opts.cache ? editorconfigSyncWithCache : editorconfigSyncNoCache;
|
||||
}
|
||||
|
||||
return opts.cache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache;
|
||||
}
|
||||
|
||||
function clearCache() {
|
||||
memClear(editorconfigSyncWithCache);
|
||||
memClear(editorconfigAsyncWithCache);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,19 +11,18 @@ import * as resolveEditorConfig from "./resolve-config-editorconfig.js";
|
||||
|
||||
/**
|
||||
* @typedef {ReturnType<import("cosmiconfig").cosmiconfig>} Explorer
|
||||
* @typedef {ReturnType<import("cosmiconfig").cosmiconfigSync>} SyncExplorer
|
||||
* @typedef {{sync?: boolean; cache?: boolean }} Options
|
||||
* @typedef {{cache?: boolean }} Options
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {Options} Opts
|
||||
* @param {Opts} opts
|
||||
* @return {Opts["sync"] extends true ? SyncExplorer : Explorer}
|
||||
* @return {Explorer}
|
||||
*/
|
||||
const getExplorerMemoized = mem(
|
||||
(opts) => {
|
||||
const require = createRequire(import.meta.url);
|
||||
const cosmiconfig = thirdParty["cosmiconfig" + (opts.sync ? "Sync" : "")];
|
||||
const { cosmiconfig } = thirdParty;
|
||||
const explorer = cosmiconfig("prettier", {
|
||||
cache: opts.cache,
|
||||
transform: (result) => {
|
||||
@@ -70,84 +69,65 @@ const getExplorerMemoized = mem(
|
||||
);
|
||||
|
||||
/**
|
||||
* @template {Options} Opts
|
||||
* @param {Opts} opts
|
||||
* @return {Opts["sync"] extends true ? SyncExplorer : Explorer}
|
||||
* @param {Options} [options]
|
||||
* @return {Explorer}
|
||||
*/
|
||||
function getExplorer(opts) {
|
||||
// Normalize opts before passing to a memoized function
|
||||
opts = { sync: false, cache: false, ...opts };
|
||||
return getExplorerMemoized(opts);
|
||||
function getExplorer(options) {
|
||||
return getExplorerMemoized(
|
||||
// Normalize opts before passing to a memoized function
|
||||
{ cache: false, ...options }
|
||||
);
|
||||
}
|
||||
|
||||
function _resolveConfig(filePath, opts, sync) {
|
||||
async function resolveConfig(filePath, opts) {
|
||||
opts = { useCache: true, ...opts };
|
||||
const loadOpts = {
|
||||
cache: Boolean(opts.useCache),
|
||||
sync: Boolean(sync),
|
||||
editorconfig: Boolean(opts.editorconfig),
|
||||
};
|
||||
const { load, search } = getExplorer(loadOpts);
|
||||
const loadEditorConfig = resolveEditorConfig.getLoadFunction(loadOpts);
|
||||
/** @type {[any, any]} */
|
||||
const arr = [
|
||||
|
||||
const [result, editorConfigured] = await Promise.all([
|
||||
opts.config ? load(opts.config) : search(filePath),
|
||||
loadEditorConfig(filePath),
|
||||
];
|
||||
]);
|
||||
|
||||
const unwrapAndMerge = ([result, editorConfigured]) => {
|
||||
const merged = {
|
||||
...editorConfigured,
|
||||
...mergeOverrides(result, filePath),
|
||||
};
|
||||
|
||||
for (const optionName of ["plugins", "pluginSearchDirs"]) {
|
||||
if (Array.isArray(merged[optionName])) {
|
||||
merged[optionName] = merged[optionName].map((value) =>
|
||||
typeof value === "string" && value.startsWith(".") // relative path
|
||||
? path.resolve(path.dirname(result.filepath), value)
|
||||
: value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!result && !editorConfigured) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We are not using this option
|
||||
delete merged.insertFinalNewline;
|
||||
return merged;
|
||||
const merged = {
|
||||
...editorConfigured,
|
||||
...mergeOverrides(result, filePath),
|
||||
};
|
||||
|
||||
if (loadOpts.sync) {
|
||||
return unwrapAndMerge(arr);
|
||||
for (const optionName of ["plugins", "pluginSearchDirs"]) {
|
||||
if (Array.isArray(merged[optionName])) {
|
||||
merged[optionName] = merged[optionName].map((value) =>
|
||||
typeof value === "string" && value.startsWith(".") // relative path
|
||||
? path.resolve(path.dirname(result.filepath), value)
|
||||
: value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all(arr).then(unwrapAndMerge);
|
||||
if (!result && !editorConfigured) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We are not using this option
|
||||
delete merged.insertFinalNewline;
|
||||
return merged;
|
||||
}
|
||||
|
||||
const resolveConfig = (filePath, opts) => _resolveConfig(filePath, opts, false);
|
||||
|
||||
resolveConfig.sync = (filePath, opts) => _resolveConfig(filePath, opts, true);
|
||||
|
||||
function clearCache() {
|
||||
memClear(getExplorerMemoized);
|
||||
resolveEditorConfig.clearCache();
|
||||
}
|
||||
|
||||
async function resolveConfigFile(filePath) {
|
||||
const { search } = getExplorer({ sync: false });
|
||||
const { search } = getExplorer();
|
||||
const result = await search(filePath);
|
||||
return result ? result.filepath : null;
|
||||
}
|
||||
|
||||
resolveConfigFile.sync = (filePath) => {
|
||||
const { search } = getExplorer({ sync: true });
|
||||
const result = search(filePath);
|
||||
return result ? result.filepath : null;
|
||||
};
|
||||
|
||||
function mergeOverrides(configResult, filePath) {
|
||||
const { config, filepath: configPath } = configResult || {};
|
||||
const { overrides, ...options } = config || {};
|
||||
|
||||
+6
-15
@@ -30,7 +30,12 @@ const require = createRequire(import.meta.url);
|
||||
|
||||
const { version } = require("../package.json");
|
||||
|
||||
function _withPlugins(
|
||||
/**
|
||||
* @param {*} fn
|
||||
* @param {*} optsArgIdx
|
||||
* @returns {*}
|
||||
*/
|
||||
function withPlugins(
|
||||
fn,
|
||||
optsArgIdx = 1 // Usually `opts` is the 2nd argument
|
||||
) {
|
||||
@@ -47,20 +52,6 @@ function _withPlugins(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {*} fn
|
||||
* @param {*} optsArgIdx
|
||||
* @returns {*}
|
||||
*/
|
||||
function withPlugins(fn, optsArgIdx) {
|
||||
const resultingFn = _withPlugins(fn, optsArgIdx);
|
||||
if (fn.sync) {
|
||||
// @ts-expect-error
|
||||
resultingFn.sync = _withPlugins(fn.sync, optsArgIdx);
|
||||
}
|
||||
return resultingFn;
|
||||
}
|
||||
|
||||
const formatWithCursor = withPlugins(core.formatWithCursor);
|
||||
|
||||
async function format(text, opts) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import fs from "node:fs";
|
||||
const fsAsync = fs.promises;
|
||||
import { promises as fsPromises } from "node:fs";
|
||||
|
||||
/**
|
||||
* @param {string} filename
|
||||
@@ -7,30 +6,14 @@ const fsAsync = fs.promises;
|
||||
*/
|
||||
async function getFileContentOrNull(filename) {
|
||||
try {
|
||||
return await fsAsync.readFile(filename, "utf8");
|
||||
return await fsPromises.readFile(filename, "utf8");
|
||||
} catch (error) {
|
||||
return handleError(filename, error);
|
||||
}
|
||||
}
|
||||
if (error && error.code === "ENOENT") {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filename
|
||||
* @returns {null | string}
|
||||
*/
|
||||
getFileContentOrNull.sync = function (filename) {
|
||||
try {
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
} catch (error) {
|
||||
return handleError(filename, error);
|
||||
throw new Error(`Unable to read ${filename}: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
function handleError(filename, error) {
|
||||
if (error && error.code === "ENOENT") {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new Error(`Unable to read ${filename}: ${error.message}`);
|
||||
}
|
||||
|
||||
export default getFileContentOrNull;
|
||||
@@ -80,148 +80,84 @@ describe("CLI overrides take precedence", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig with no args", () =>
|
||||
prettier.resolveConfig().then((result) => {
|
||||
expect(result).toEqual({});
|
||||
}));
|
||||
|
||||
test("API resolveConfig.sync with no args", () => {
|
||||
expect(prettier.resolveConfig.sync()).toEqual({});
|
||||
test("API resolveConfig with no args", async () => {
|
||||
await expect(prettier.resolveConfig()).resolves.toEqual({});
|
||||
});
|
||||
|
||||
test("API resolveConfig with file arg", () => {
|
||||
test("API resolveConfig with file arg", async () => {
|
||||
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
|
||||
return prettier.resolveConfig(file).then((result) => {
|
||||
expect(result).toMatchObject({
|
||||
tabWidth: 8,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with file arg", () => {
|
||||
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
tabWidth: 8,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig with file arg and extension override", () => {
|
||||
test("API resolveConfig with file arg and extension override", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/no-config/file.ts")
|
||||
);
|
||||
return prettier.resolveConfig(file).then((result) => {
|
||||
expect(result).toMatchObject({
|
||||
semi: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with file arg and extension override", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/no-config/file.ts")
|
||||
);
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
semi: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig with file arg and .editorconfig", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/file.js")
|
||||
);
|
||||
return prettier.resolveConfig(file, { editorconfig: true }).then((result) => {
|
||||
expect(result).toMatchObject({
|
||||
useTabs: true,
|
||||
tabWidth: 8,
|
||||
printWidth: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with file arg and .editorconfig", () => {
|
||||
test("API resolveConfig with file arg and .editorconfig", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/file.js")
|
||||
);
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
semi: false,
|
||||
});
|
||||
|
||||
expect(
|
||||
prettier.resolveConfig.sync(file, { editorconfig: true })
|
||||
).toMatchObject({
|
||||
await expect(
|
||||
prettier.resolveConfig(file, { editorconfig: true })
|
||||
).resolves.toMatchObject({
|
||||
useTabs: true,
|
||||
tabWidth: 8,
|
||||
printWidth: 100,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with file arg and .editorconfig (key = unset)", () => {
|
||||
test("API resolveConfig with file arg and .editorconfig (key = unset)", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/tab_width=unset.js")
|
||||
);
|
||||
|
||||
expect(
|
||||
prettier.resolveConfig.sync(file, { editorconfig: true })
|
||||
).not.toMatchObject({ tabWidth: "unset" });
|
||||
await expect(
|
||||
prettier.resolveConfig(file, { editorconfig: true })
|
||||
).resolves.not.toMatchObject({ tabWidth: "unset" });
|
||||
});
|
||||
|
||||
test("API resolveConfig with nested file arg and .editorconfig", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/lib/file.js")
|
||||
);
|
||||
return prettier.resolveConfig(file, { editorconfig: true }).then((result) => {
|
||||
expect(result).toMatchObject({
|
||||
useTabs: false,
|
||||
tabWidth: 2,
|
||||
printWidth: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with nested file arg and .editorconfig", () => {
|
||||
test("API resolveConfig with nested file arg and .editorconfig", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/lib/file.js")
|
||||
);
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
semi: false,
|
||||
});
|
||||
|
||||
expect(
|
||||
prettier.resolveConfig.sync(file, { editorconfig: true })
|
||||
).toMatchObject({
|
||||
await expect(
|
||||
prettier.resolveConfig(file, { editorconfig: true })
|
||||
).resolves.toMatchObject({
|
||||
useTabs: false,
|
||||
tabWidth: 2,
|
||||
printWidth: 100,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig with nested file arg and .editorconfig and indent_size = tab", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js")
|
||||
);
|
||||
return prettier.resolveConfig(file, { editorconfig: true }).then((result) => {
|
||||
expect(result).toMatchObject({
|
||||
useTabs: false,
|
||||
tabWidth: 8,
|
||||
printWidth: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync with nested file arg and .editorconfig and indent_size = tab", () => {
|
||||
test("API resolveConfig with nested file arg and .editorconfig and indent_size = tab", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js")
|
||||
);
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
semi: false,
|
||||
});
|
||||
|
||||
expect(
|
||||
prettier.resolveConfig.sync(file, { editorconfig: true })
|
||||
).toMatchObject({
|
||||
await expect(
|
||||
prettier.resolveConfig(file, { editorconfig: true })
|
||||
).resolves.toMatchObject({
|
||||
useTabs: false,
|
||||
tabWidth: 8,
|
||||
printWidth: 100,
|
||||
@@ -241,20 +177,20 @@ test("API resolveConfig overrides work with dotfiles", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync overrides work with absolute paths", () => {
|
||||
test("API resolveConfig overrides work with absolute paths", async () => {
|
||||
// Absolute path
|
||||
const file = path.join(__dirname, "../cli/config/filepath/subfolder/file.js");
|
||||
expect(prettier.resolveConfig.sync(file)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
|
||||
tabWidth: 6,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync overrides excludeFiles", () => {
|
||||
test("API resolveConfig overrides excludeFiles", async () => {
|
||||
const notOverride = path.join(
|
||||
__dirname,
|
||||
"../cli/config/overrides-exclude-files/foo"
|
||||
);
|
||||
expect(prettier.resolveConfig.sync(notOverride)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(notOverride)).resolves.toMatchObject({
|
||||
singleQuote: true,
|
||||
trailingComma: "all",
|
||||
});
|
||||
@@ -263,7 +199,7 @@ test("API resolveConfig.sync overrides excludeFiles", () => {
|
||||
__dirname,
|
||||
"../cli/config/overrides-exclude-files/single-quote.js"
|
||||
);
|
||||
expect(prettier.resolveConfig.sync(singleQuote)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(singleQuote)).resolves.toMatchObject({
|
||||
singleQuote: true,
|
||||
trailingComma: "es5",
|
||||
});
|
||||
@@ -272,52 +208,45 @@ test("API resolveConfig.sync overrides excludeFiles", () => {
|
||||
__dirname,
|
||||
"../cli/config/overrides-exclude-files/double-quote.js"
|
||||
);
|
||||
expect(prettier.resolveConfig.sync(doubleQuote)).toMatchObject({
|
||||
await expect(prettier.resolveConfig(doubleQuote)).resolves.toMatchObject({
|
||||
singleQuote: false,
|
||||
trailingComma: "es5",
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig removes $schema option", () => {
|
||||
test("API resolveConfig removes $schema option", async () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/$schema/index.js")
|
||||
);
|
||||
return prettier.resolveConfig(file).then((result) => {
|
||||
expect(result).toEqual({
|
||||
tabWidth: 42,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig.sync removes $schema option", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/config/$schema/index.js")
|
||||
);
|
||||
expect(prettier.resolveConfig.sync(file)).toEqual({
|
||||
await expect(prettier.resolveConfig(file)).resolves.toEqual({
|
||||
tabWidth: 42,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig resolves relative path values based on config filepath", () => {
|
||||
test("API resolveConfig resolves relative path values based on config filepath", async () => {
|
||||
const currentDir = path.join(__dirname, "../cli/config/resolve-relative");
|
||||
const parentDir = path.resolve(currentDir, "..");
|
||||
expect(prettier.resolveConfig.sync(`${currentDir}/index.js`)).toMatchObject({
|
||||
await expect(
|
||||
prettier.resolveConfig(`${currentDir}/index.js`)
|
||||
).resolves.toMatchObject({
|
||||
plugins: [path.join(parentDir, "path-to-plugin")],
|
||||
pluginSearchDirs: [path.join(parentDir, "path-to-plugin-search-dir")],
|
||||
});
|
||||
|
||||
expect(
|
||||
prettier.resolveConfig.sync(
|
||||
await expect(
|
||||
prettier.resolveConfig(
|
||||
path.join(__dirname, "../cli/config/plugin-search-dirs/index.js")
|
||||
)
|
||||
).toMatchObject({
|
||||
).resolves.toMatchObject({
|
||||
pluginSearchDirs: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("API resolveConfig de-references to an external module", () => {
|
||||
test("API resolveConfig de-references to an external module", async () => {
|
||||
const currentDir = path.join(__dirname, "../cli/config/external-config");
|
||||
expect(prettier.resolveConfig.sync(`${currentDir}/index.js`)).toEqual({
|
||||
await expect(
|
||||
prettier.resolveConfig(`${currentDir}/index.js`)
|
||||
).resolves.toEqual({
|
||||
printWidth: 77,
|
||||
semi: false,
|
||||
});
|
||||
@@ -341,7 +270,6 @@ test(".cjs config file", async () => {
|
||||
]) {
|
||||
const file = path.join(parentDirectory, directoryName, "foo.js");
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toEqual(config);
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject(config);
|
||||
}
|
||||
});
|
||||
@@ -355,7 +283,6 @@ test(".json5 config file", async () => {
|
||||
};
|
||||
const file = path.join(parentDirectory, "json5/foo.js");
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toEqual(config);
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject(config);
|
||||
});
|
||||
|
||||
@@ -364,8 +291,5 @@ test(".json5 config file(invalid)", async () => {
|
||||
const file = path.join(parentDirectory, "invalid/foo.js");
|
||||
const error = /JSON5: invalid end of input at 2:1/;
|
||||
|
||||
expect(() => {
|
||||
prettier.resolveConfig.sync(file);
|
||||
}).toThrowError(error);
|
||||
await expect(prettier.resolveConfig(file)).rejects.toThrow(error);
|
||||
});
|
||||
@@ -140,12 +140,6 @@ test("API getFileInfo with no args", async () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("API getFileInfo.sync with no args", () => {
|
||||
expect(() => prettier.getFileInfo.sync()).toThrow(
|
||||
new TypeError("expect `filePath` to be a string, got `undefined`")
|
||||
);
|
||||
});
|
||||
|
||||
test("API getFileInfo with filepath only", async () => {
|
||||
await expect(prettier.getFileInfo("README")).resolves.toMatchObject({
|
||||
ignored: false,
|
||||
@@ -153,13 +147,6 @@ test("API getFileInfo with filepath only", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("API getFileInfo.sync with filepath only", () => {
|
||||
expect(prettier.getFileInfo.sync("README")).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "markdown",
|
||||
});
|
||||
});
|
||||
|
||||
describe("API getFileInfo resolveConfig", () => {
|
||||
const files = Object.fromEntries(
|
||||
["foo", "js", "bar", "css"].map((ext) => [
|
||||
@@ -213,58 +200,6 @@ describe("API getFileInfo resolveConfig", () => {
|
||||
inferredParser: "css",
|
||||
});
|
||||
});
|
||||
test("sync {resolveConfig: undefined}", () => {
|
||||
expect(prettier.getFileInfo.sync(files.foo)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
expect(prettier.getFileInfo.sync(files.js)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
expect(prettier.getFileInfo.sync(files.bar)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
expect(prettier.getFileInfo.sync(files.css)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "css",
|
||||
});
|
||||
});
|
||||
test("sync {resolveConfig: true}", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.foo, {
|
||||
resolveConfig: true,
|
||||
})
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "foo-parser",
|
||||
});
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.js, {
|
||||
resolveConfig: true,
|
||||
})
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "override-js-parser",
|
||||
});
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.bar, {
|
||||
resolveConfig: true,
|
||||
})
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.css, {
|
||||
resolveConfig: true,
|
||||
})
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "css",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("API getFileInfo resolveConfig when no config is present", () => {
|
||||
@@ -298,30 +233,6 @@ describe("API getFileInfo resolveConfig when no config is present", () => {
|
||||
inferredParser: "babel",
|
||||
});
|
||||
});
|
||||
test("sync {resolveConfig: undefined}", () => {
|
||||
expect(prettier.getFileInfo.sync(files.foo)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
expect(prettier.getFileInfo.sync(files.js)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
});
|
||||
test("sync {resolveConfig: true}", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.foo, { resolveConfig: true })
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
expect(
|
||||
prettier.getFileInfo.sync(files.js, { resolveConfig: true })
|
||||
).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("API getFileInfo with ignorePath", async () => {
|
||||
@@ -369,30 +280,7 @@ test("API getFileInfo with ignorePath containing relative paths", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("API getFileInfo.sync with ignorePath", () => {
|
||||
const file = path.resolve(
|
||||
path.join(__dirname, "../cli/ignore-path/regular-module.js")
|
||||
);
|
||||
const ignorePath = path.resolve(
|
||||
path.join(__dirname, "../cli/ignore-path/.prettierignore")
|
||||
);
|
||||
|
||||
expect(prettier.getFileInfo.sync(file)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
|
||||
expect(
|
||||
prettier.getFileInfo.sync(file, {
|
||||
ignorePath,
|
||||
})
|
||||
).toMatchObject({
|
||||
ignored: true,
|
||||
inferredParser: null,
|
||||
});
|
||||
});
|
||||
|
||||
describe("API getFileInfo.sync with ignorePath", () => {
|
||||
describe("API getFileInfo with ignorePath", () => {
|
||||
let cwd;
|
||||
let filePath;
|
||||
let options;
|
||||
@@ -409,20 +297,20 @@ describe("API getFileInfo.sync with ignorePath", () => {
|
||||
afterAll(() => {
|
||||
process.chdir(cwd);
|
||||
});
|
||||
test("with relative filePath", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync(filePath, options).ignored
|
||||
).toMatchInlineSnapshot("true");
|
||||
test("with relative filePath", async () => {
|
||||
const { ignored } = await prettier.getFileInfo(filePath, options);
|
||||
expect(ignored).toBe(true);
|
||||
});
|
||||
test("with relative filePath starts with dot", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync(`./${filePath}`, options).ignored
|
||||
).toMatchInlineSnapshot("true");
|
||||
test("with relative filePath starts with dot", async () => {
|
||||
const { ignored } = await prettier.getFileInfo(`./${filePath}`, options);
|
||||
expect(ignored).toBe(true);
|
||||
});
|
||||
test("with absolute filePath", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync(path.resolve(filePath), options).ignored
|
||||
).toMatchInlineSnapshot("true");
|
||||
test("with absolute filePath", async () => {
|
||||
const { ignored } = await prettier.getFileInfo(
|
||||
path.resolve(filePath),
|
||||
options
|
||||
);
|
||||
expect(ignored).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -444,28 +332,28 @@ test("API getFileInfo with withNodeModules", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("extracts file-info for a JS file with no extension but a standard shebang", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync("tests/integration/cli/shebang/node-shebang")
|
||||
).toMatchObject({
|
||||
test("extracts file-info for a JS file with no extension but a standard shebang", async () => {
|
||||
await expect(
|
||||
prettier.getFileInfo("tests/integration/cli/shebang/node-shebang")
|
||||
).resolves.toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
});
|
||||
|
||||
describe("extracts file-info for a JS file with no extension but an env-based shebang", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync("tests/integration/cli/shebang/env-node-shebang")
|
||||
).toMatchObject({
|
||||
test("extracts file-info for a JS file with no extension but an env-based shebang", async () => {
|
||||
await expect(
|
||||
prettier.getFileInfo("tests/integration/cli/shebang/env-node-shebang")
|
||||
).resolves.toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "babel",
|
||||
});
|
||||
});
|
||||
|
||||
describe("returns null parser for unknown shebang", () => {
|
||||
expect(
|
||||
prettier.getFileInfo.sync("tests/integration/cli/shebang/nonsense-shebang")
|
||||
).toMatchObject({
|
||||
test("returns null parser for unknown shebang", async () => {
|
||||
await expect(
|
||||
prettier.getFileInfo("tests/integration/cli/shebang/nonsense-shebang")
|
||||
).resolves.toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: null,
|
||||
});
|
||||
@@ -525,9 +413,4 @@ test("API getFileInfo with ignorePath and resolveConfig should infer parser with
|
||||
ignored: false,
|
||||
inferredParser: "parser-for-config-dir",
|
||||
});
|
||||
|
||||
expect(prettier.getFileInfo.sync(filePath, options)).toMatchObject({
|
||||
ignored: false,
|
||||
inferredParser: "parser-for-config-dir",
|
||||
});
|
||||
});
|
||||
@@ -8,8 +8,3 @@ test("API resolveConfigFile", async () => {
|
||||
const result = await prettier.resolveConfigFile();
|
||||
expect(result).toEqual(path.join(__dirname, "../../../.prettierrc"));
|
||||
});
|
||||
|
||||
test("API resolveConfigFile.sync", () => {
|
||||
const result = prettier.resolveConfigFile.sync();
|
||||
expect(result).toEqual(path.join(__dirname, "../../../.prettierrc"));
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import { thirdParty } from "../env.js";
|
||||
import jestPathSerializer from "../path-serializer.js";
|
||||
|
||||
const { require, __dirname } = createEsmUtils(import.meta);
|
||||
const { cosmiconfig, cosmiconfigSync, isCI } = require(thirdParty);
|
||||
const { cosmiconfig, isCI } = require(thirdParty);
|
||||
|
||||
expect.addSnapshotSerializer(jestPathSerializer);
|
||||
|
||||
@@ -47,21 +47,15 @@ describe("cosmiconfig", () => {
|
||||
expect(config).toEqual(value);
|
||||
expect(filepath).toBe(file);
|
||||
});
|
||||
|
||||
test(`sync version ${title}`, () => {
|
||||
const { config, filepath } = cosmiconfigSync("prettier").search(dirname);
|
||||
expect(config).toEqual(value);
|
||||
expect(filepath).toBe(file);
|
||||
});
|
||||
}
|
||||
|
||||
// #8815, please make sure this error contains code frame
|
||||
test("Invalid json file", () => {
|
||||
expect(() => {
|
||||
cosmiconfigSync("prettier").search(
|
||||
test("Invalid json file", async () => {
|
||||
await expect(
|
||||
cosmiconfig("prettier").search(
|
||||
path.join(__dirname, "../cli/config/invalid/broken-json")
|
||||
);
|
||||
}).toThrowErrorMatchingSnapshot();
|
||||
)
|
||||
).rejects.toThrowErrorMatchingSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -99,14 +99,6 @@ async function run(dir, args, options) {
|
||||
stopDir: path.join(__dirname, "cli"),
|
||||
})
|
||||
);
|
||||
jest
|
||||
.spyOn(require(thirdParty), "cosmiconfigSync")
|
||||
.mockImplementation((moduleName, options) =>
|
||||
require("cosmiconfig").cosmiconfigSync(moduleName, {
|
||||
...options,
|
||||
stopDir: path.join(__dirname, "cli"),
|
||||
})
|
||||
);
|
||||
jest
|
||||
.spyOn(require(thirdParty), "findParentDir")
|
||||
.mockImplementation(() => process.cwd());
|
||||
|
||||
Reference in new issue
Block a user