Skip to content

Commit

Permalink
Add CLI option '--config-precedence' (prettier#2733)
Browse files Browse the repository at this point in the history
Add CLI option '--config-precedence'
  • Loading branch information
mitermayer authored and azz committed Sep 4, 2017
1 parent 7eaf5c7 commit c59a82c
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ prettier --single-quote --list-different "src/**/*.js"

Do not look for a configuration file. The default settings will be used.

#### `--config-precedence`

Defines how config file should be evaluated in combination of CLI options.

**cli-override (default)**

CLI options take precedence over config file

**file-override**

Config file take precedence over CLI options

**prefer-file**

If a config file is found will evaluate it and ignore other CLI options. If no config file is found CLI options will evaluate as normal.

This option adds support to editor integrations where users define their default configuration but want to respect project specific configuration.

#### `--with-node-modules`

Prettier CLI will ignore files located in `node_modules` directory. To opt-out from this behavior use `--with-node-modules` flag.
Expand Down
8 changes: 8 additions & 0 deletions src/cli-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const options = {
"range-end",
"stdin-filepath",
"config",
"config-precedence",
"find-config-path",
"ignore-path"
].concat(stringOptionNames),
Expand Down Expand Up @@ -66,6 +67,13 @@ Available options:
--write Edit the file in-place. (Beware!)
--list-different or -l Print filenames of files that are different from Prettier formatting.
--config Path to a prettier configuration file (.prettierrc, package.json, prettier.config.js).
--config-precedence <cli-override|file-override|prefer-file>
Defines how config file should be evaluated in combination of CLI options
cli-override | default config => config file => CLI options
file-override | default config => CLI options => config file
prefer-file | default config => config file (if config file is found) or
default config => CLI options (if no config file is found)
Defaults to cli-override
--no-config Do not look for a configuration file.
--find-config-path <path>
Finds and prints the path to a configuration file for a given input file.
Expand Down
19 changes: 17 additions & 2 deletions src/cli-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,34 @@ function getOptionsForFile(argv, filePath) {
argv["config"] === false ? null : resolver.resolveConfig.sync(filePath);

try {
const dashifiedConfig = dashifyObject(options);
const parsedArgs = minimist(argv.__args, {
boolean: constant.booleanOptionNames,
string: constant.stringOptionNames,
default: Object.assign(
{
semi: true,
"bracket-spacing": true,
"config-precedence": "cli-override",
parser: "babylon"
},
dashifyObject(options)
dashifiedConfig
)
});
return getOptions(Object.assign({}, argv, parsedArgs));

switch (parsedArgs["config-precedence"]) {
case "cli-override":
return getOptions(parsedArgs);
case "file-override":
return getOptions(Object.assign({}, parsedArgs, dashifiedConfig));
case "prefer-file":
if (!options) {
return getOptions(parsedArgs);
}
return getOptions(dashifiedConfig);
default:
throw new Error("Invalid option for --config-precedence");
}
} catch (error) {
console.error("Invalid configuration:", error.toString());
process.exit(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ exports[`throw error with invalid config option (trailingComma) 1`] = `
"
`;
exports[`throw error with invalid config precedence option (configPrecedence) 1`] = `
"Invalid configuration:
"
`;
exports[`throw error with invalid config target (directory) 1`] = `"EISDIR: illegal operation on a directory, read"`;
7 changes: 7 additions & 0 deletions tests_integration/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Available options:
--write Edit the file in-place. (Beware!)
--list-different or -l Print filenames of files that are different from Prettier formatting.
--config Path to a prettier configuration file (.prettierrc, package.json, prettier.config.js).
--config-precedence <cli-override|file-override|prefer-file>
Defines how config file should be evaluated in combination of CLI options
cli-override | default config => config file => CLI options
file-override | default config => CLI options => config file
prefer-file | default config => config file (if config file is found) or
default config => CLI options (if no config file is found)
Defaults to cli-override
--no-config Do not look for a configuration file.
--find-config-path <path>
Finds and prints the path to a configuration file for a given input file.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CLI overrides are still applied when no config is found with --config-precedence file-override 1`] = `
"function f() {
console.log(\\"should have no semicolons\\")
}
"
`;

exports[`CLI overrides gets applied when no config exists with --config-precedence prefer-file 1`] = `
"function f() {
console.log(
\\"should have no semicolons\\"
);
}
"
`;

exports[`CLI overrides gets ignored when config exists with --config-precedence prefer-file 1`] = `
"function f() {
console.log(\\"should have tab width 8\\");
}
\\"use strict\\";
module.exports = {
tabWidth: 8
};
"
`;

exports[`CLI overrides take lower precedence with --config-precedence file-override 1`] = `
"function f() {
console.log(\\"should have tab width 8\\");
}
\\"use strict\\";
module.exports = {
tabWidth: 8
};
"
`;

exports[`CLI overrides take precedence with --config-precedence cli-override 1`] = `
"console.log(
\\"should have semi\\"
);
console.log(
\\"should not have semi\\"
)
console.log(
\\"should have semi\\"
);
function f() {
console.log(
\\"should have tab width 8\\"
);
}
\\"use strict\\";
module.exports = {
tabWidth: 8
};
function f() {
console.log(
\\"should have no semicolons\\"
)
}
function f() {
console.log(
\\"should have tab width 3\\"
);
}
function f() {
console.log.apply(
null,
[
'this file',
'should have trailing comma',
'and single quotes',
],
);
}
"
`;

exports[`CLI overrides take precedence without --config-precedence 1`] = `
"console.log(
\\"should have semi\\"
);
console.log(
\\"should not have semi\\"
)
console.log(
\\"should have semi\\"
);
function f() {
console.log(
\\"should have tab width 8\\"
);
}
\\"use strict\\";
module.exports = {
tabWidth: 8
};
function f() {
console.log(
\\"should have no semicolons\\"
)
}
function f() {
console.log(
\\"should have tab width 3\\"
);
}
function f() {
console.log.apply(
null,
[
'this file',
'should have trailing comma',
'and single quotes',
],
);
}
"
`;
9 changes: 9 additions & 0 deletions tests_integration/__tests__/config-invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ test("throw error with invalid config option (trailingComma)", () => {
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});

test("throw error with invalid config precedence option (configPrecedence)", () => {
const output = runPrettier("cli/config/invalid", [
"--config-precedence",
"option/configPrecedence"
]);
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});
74 changes: 74 additions & 0 deletions tests_integration/__tests__/with-config-precedence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"use strict";

const runPrettier = require("../runPrettier");

test("CLI overrides take precedence without --config-precedence", () => {
const output = runPrettier("cli/config/", ["--print-width", "1", "**/*.js"]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});

test("CLI overrides take precedence with --config-precedence cli-override", () => {
const output = runPrettier("cli/config/", [
"--print-width",
"1",
"--config-precedence",
"cli-override",
"**/*.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});

test("CLI overrides take lower precedence with --config-precedence file-override", () => {
const output = runPrettier("cli/config/js/", [
"--tab-width",
"1",
"--config-precedence",
"file-override",
"**/*.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});

test("CLI overrides are still applied when no config is found with --config-precedence file-override", () => {
const output = runPrettier("cli/config/no-config/", [
"--tab-width",
"6",
"--config-precedence",
"file-override",
"**/*.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});

test("CLI overrides gets ignored when config exists with --config-precedence prefer-file", () => {
const output = runPrettier("cli/config/js/", [
"--print-width",
"1",
"--tab-width",
"1",
"--config-precedence",
"prefer-file",
"**/*.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});

test("CLI overrides gets applied when no config exists with --config-precedence prefer-file", () => {
const output = runPrettier("cli/config/no-config/", [
"--print-width",
"1",
"--tab-width",
"7",
"--no-config",
"--config-precedence",
"prefer-file",
"**/*.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});
3 changes: 3 additions & 0 deletions tests_integration/cli/config/invalid/option/configPrecedence
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"config-precedence": "invalidValue"
}

0 comments on commit c59a82c

Please sign in to comment.