Skip to content

Commit e148041

Browse files
Added partial support for the "--config-precedence" option
1 parent de93432 commit e148041

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/bin.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ const makeBin = (): Bin => {
7878
section: "Format",
7979
})
8080
/* CONFIG OPTIONS */
81-
.option("--config-path <path>", "Path to a Prettier configuration file (.prettierrc, package.json, prettier.config.js)", { section: "Config" })
8281
.option("--no-config", "Do not look for a configuration file", {
8382
section: "Config",
8483
default: true,
8584
})
85+
.option("--config-path <path>", "Path to a Prettier configuration file (.prettierrc, package.json, prettier.config.js)", { section: "Config" })
86+
.option(
87+
"--config-precedence <cli-override|file-override>",
88+
'Define in which order config files and CLI options should be evaluated.\nDefaults to "cli-override"',
89+
{
90+
section: "Config",
91+
enum: ["cli-override", "file-override"],
92+
},
93+
)
8694
.option("--no-editorconfig", "Don't take .editorconfig into account when parsing configuration", {
8795
section: "Config",
8896
default: true,
@@ -230,6 +238,10 @@ const makeWarnedPluggableBin = async (): Promise<Bin> => {
230238
exit('The "--find-config-path" is not currently supported, please open an issue on GitHub if you need it');
231239
}
232240

241+
if (args["config-precedence"] === "prefer-file") {
242+
exit('The "prefer-file" value for "--config-precedence" is not currently supported, please open an issue on GitHub if you need it');
243+
}
244+
233245
const bin = await makePluggableBin();
234246
return bin;
235247
};

src/prettier_serial.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ async function format(filePath: string, fileContent: string, formatOptions: Lazy
1818
formatOptions = await resolve(formatOptions);
1919
const pluginsBuiltin = await getPluginsBuiltin();
2020
const plugins = await getPlugins(formatOptions.plugins || []);
21+
const pluginsOverride = contextOptions.configPrecedence !== 'file-override';
2122

2223
const options = {
2324
...pluginsDefaultOptions,
24-
...formatOptions,
25-
...pluginsCustomOptions,
25+
...(pluginsOverride ? formatOptions : pluginsCustomOptions),
26+
...(pluginsOverride ? pluginsCustomOptions : formatOptions),
2627
...contextOptions,
2728
filepath: filePath,
2829
plugins: [

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type Bin = ReturnType<typeof import("tiny-bin").default>;
22

33
type ContextOptions = {
4+
configPrecedence?: "cli-override" | "file-override";
45
cursorOffset?: number;
56
rangeEnd?: number;
67
rangeStart?: number;

src/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ function normalizeContextOptions(options: unknown): ContextOptions {
345345

346346
const contextOptions: ContextOptions = {};
347347

348+
if ("configPrecedence" in options) {
349+
const value = options.configPrecedence;
350+
if (isString(value) && (value === "cli-override" || value === "file-override")) {
351+
contextOptions.configPrecedence = value;
352+
}
353+
}
354+
348355
if ("cursorOffset" in options) {
349356
const value = Number(options.cursorOffset);
350357
if (isInteger(value) && value >= 0) {

0 commit comments

Comments
 (0)