Skip to content

Commit

Permalink
Added partial support for the "--config-precedence" option
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiospampinato committed Apr 27, 2024
1 parent de93432 commit e148041
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ const makeBin = (): Bin => {
section: "Format",
})
/* CONFIG OPTIONS */
.option("--config-path <path>", "Path to a Prettier configuration file (.prettierrc, package.json, prettier.config.js)", { section: "Config" })
.option("--no-config", "Do not look for a configuration file", {
section: "Config",
default: true,
})
.option("--config-path <path>", "Path to a Prettier configuration file (.prettierrc, package.json, prettier.config.js)", { section: "Config" })
.option(
"--config-precedence <cli-override|file-override>",
'Define in which order config files and CLI options should be evaluated.\nDefaults to "cli-override"',
{
section: "Config",
enum: ["cli-override", "file-override"],
},
)
.option("--no-editorconfig", "Don't take .editorconfig into account when parsing configuration", {
section: "Config",
default: true,
Expand Down Expand Up @@ -230,6 +238,10 @@ const makeWarnedPluggableBin = async (): Promise<Bin> => {
exit('The "--find-config-path" is not currently supported, please open an issue on GitHub if you need it');
}

if (args["config-precedence"] === "prefer-file") {
exit('The "prefer-file" value for "--config-precedence" is not currently supported, please open an issue on GitHub if you need it');
}

const bin = await makePluggableBin();
return bin;
};
Expand Down
5 changes: 3 additions & 2 deletions src/prettier_serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ async function format(filePath: string, fileContent: string, formatOptions: Lazy
formatOptions = await resolve(formatOptions);
const pluginsBuiltin = await getPluginsBuiltin();
const plugins = await getPlugins(formatOptions.plugins || []);
const pluginsOverride = contextOptions.configPrecedence !== 'file-override';

const options = {
...pluginsDefaultOptions,
...formatOptions,
...pluginsCustomOptions,
...(pluginsOverride ? formatOptions : pluginsCustomOptions),
...(pluginsOverride ? pluginsCustomOptions : formatOptions),
...contextOptions,
filepath: filePath,
plugins: [
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type Bin = ReturnType<typeof import("tiny-bin").default>;

type ContextOptions = {
configPrecedence?: "cli-override" | "file-override";
cursorOffset?: number;
rangeEnd?: number;
rangeStart?: number;
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ function normalizeContextOptions(options: unknown): ContextOptions {

const contextOptions: ContextOptions = {};

if ("configPrecedence" in options) {
const value = options.configPrecedence;
if (isString(value) && (value === "cli-override" || value === "file-override")) {
contextOptions.configPrecedence = value;
}
}

if ("cursorOffset" in options) {
const value = Number(options.cursorOffset);
if (isInteger(value) && value >= 0) {
Expand Down

0 comments on commit e148041

Please sign in to comment.