parent
7eaf5c7cb7
commit
c59a82cbae
18
README.md
18
README.md
|
@ -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.
|
||||
|
|
|
@ -39,6 +39,7 @@ const options = {
|
|||
"range-end",
|
||||
"stdin-filepath",
|
||||
"config",
|
||||
"config-precedence",
|
||||
"find-config-path",
|
||||
"ignore-path"
|
||||
].concat(stringOptionNames),
|
||||
|
@ -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.
|
||||
|
|
|
@ -200,6 +200,7 @@ 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,
|
||||
|
@ -207,12 +208,26 @@ function getOptionsForFile(argv, filePath) {
|
|||
{
|
||||
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);
|
||||
|
|
|
@ -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,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.
|
||||
|
|
|
@ -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',
|
||||
],
|
||||
);
|
||||
}
|
||||
"
|
||||
`;
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"config-precedence": "invalidValue"
|
||||
}
|
Loading…
Reference in New Issue