Support .prettierrc.json5 config file (#8957)
This commit is contained in:
1 parent
ff8ea78c08
commit
7c318cc677
9 files changed
+61
-10
No files matched your search
@@ -1,3 +1,7 @@
|
||||
#### Support `.prettierrc.cjs` and `prettier.config.cjs` config file ([#8890](https://github.com/prettier/prettier/pull/8890) by [@fisker](https://github.com/fisker))
|
||||
#### Support `.cjs` and `.json5` configuration files ([#8890](https://github.com/prettier/prettier/pull/8890), [#8957](https://github.com/prettier/prettier/pull/8957) by [@fisker](https://github.com/fisker))
|
||||
|
||||
New config files `.prettierrc.cjs` and `prettier.config.cjs` are supported.
|
||||
Added new format of configuration files:
|
||||
|
||||
- `.prettierrc.json5`
|
||||
- `.prettierrc.cjs`
|
||||
- `prettier.config.cjs`
|
||||
@@ -7,7 +7,7 @@ Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for co
|
||||
|
||||
- A `"prettier"` key in your `package.json` file.
|
||||
- A `.prettierrc` file written in JSON or YAML.
|
||||
- A `.prettierrc.json`, `.prettierrc.yml`, or `.prettierrc.yaml` file.
|
||||
- A `.prettierrc.json`, `.prettierrc.yml`, `.prettierrc.yaml`, or `.prettierrc.json5` file.
|
||||
- A `.prettierrc.js`, `.prettierrc.cjs`, `prettier.config.js`, or `prettier.config.cjs` file that exports an object using `module.exports`.
|
||||
- A `.prettierrc.toml` file.
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
"html-void-elements": "1.0.5",
|
||||
"ignore": "4.0.6",
|
||||
"jest-docblock": "26.0.0",
|
||||
"json5": "2.1.3",
|
||||
"leven": "3.1.0",
|
||||
"lines-and-columns": "1.1.6",
|
||||
"linguist-languages": "7.10.0",
|
||||
|
||||
@@ -6,6 +6,7 @@ const mem = require("mem");
|
||||
const thirdParty = require("../common/third-party");
|
||||
|
||||
const loadToml = require("../utils/load-toml");
|
||||
const loadJson5 = require("../utils/load-json5");
|
||||
const resolve = require("../common/resolve");
|
||||
const resolveEditorConfig = require("./resolve-config-editorconfig");
|
||||
|
||||
@@ -39,6 +40,7 @@ const getExplorerMemoized = mem(
|
||||
".prettierrc.json",
|
||||
".prettierrc.yaml",
|
||||
".prettierrc.yml",
|
||||
".prettierrc.json5",
|
||||
".prettierrc.js",
|
||||
".prettierrc.cjs",
|
||||
"prettier.config.js",
|
||||
@@ -47,6 +49,7 @@ const getExplorerMemoized = mem(
|
||||
],
|
||||
loaders: {
|
||||
".toml": loadToml,
|
||||
".json5": loadJson5,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
const { parse } = require("json5");
|
||||
|
||||
module.exports = function (filePath, content) {
|
||||
try {
|
||||
return parse(content);
|
||||
} catch (error) {
|
||||
error.message = `JSON5 Error in ${filePath}:\n${error.message}`;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@@ -308,3 +308,27 @@ test(".cjs config file", async () => {
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject(config);
|
||||
}
|
||||
});
|
||||
|
||||
test(".json5 config file", async () => {
|
||||
const parentDirectory = path.join(__dirname, "../cli/config/rc-json5");
|
||||
const config = {
|
||||
trailingComma: "all",
|
||||
printWidth: 81,
|
||||
tabWidth: 3,
|
||||
};
|
||||
const file = path.join(parentDirectory, "json5/foo.js");
|
||||
|
||||
expect(prettier.resolveConfig.sync(file)).toEqual(config);
|
||||
await expect(prettier.resolveConfig(file)).resolves.toMatchObject(config);
|
||||
});
|
||||
|
||||
test(".json5 config file(invalid)", async () => {
|
||||
const parentDirectory = path.join(__dirname, "../cli/config/rc-json5");
|
||||
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);
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
{
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
// comment
|
||||
trailingComma: 'all',
|
||||
printWidth: +81,
|
||||
tabWidth: 3.,
|
||||
}
|
||||
@@ -5309,6 +5309,13 @@ json-stringify-safe@~5.0.1:
|
||||
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
|
||||
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
|
||||
|
||||
json5@2.1.3, json5@^2.1.2:
|
||||
version "2.1.3"
|
||||
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
|
||||
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
|
||||
dependencies:
|
||||
minimist "^1.2.5"
|
||||
|
||||
json5@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
|
||||
@@ -5316,13 +5323,6 @@ json5@^1.0.1:
|
||||
dependencies:
|
||||
minimist "^1.2.0"
|
||||
|
||||
json5@^2.1.2:
|
||||
version "2.1.3"
|
||||
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
|
||||
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
|
||||
dependencies:
|
||||
minimist "^1.2.5"
|
||||
|
||||
jsonfile@^6.0.1:
|
||||
version "6.0.1"
|
||||
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
|
||||
|
||||
Reference in new issue
Block a user