Enable cosmiconfig rcExtensions (#2749)

* Enable cosmiconfig rcExtensions; add two tests

* Remove default value; make integration input files self descripting; update all integration snapshots

* Add engines field to package.json
master
Nick Ribal 2017-09-16 19:47:53 +03:00 committed by Lucas Azzola
parent ed67690925
commit 592f99fde6
18 changed files with 161 additions and 76 deletions

View File

@ -699,7 +699,7 @@ Default | CLI Override | API Override
Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support.
This means you can configure prettier via:
* A `.prettierrc` file, written in YAML or JSON.
* A `.prettierrc` file, written in YAML or JSON, with optional extensions: `.yaml/.yml/.json/.js`.
* A `prettier.config.js` file that exports an object.
* A `"prettier"` key in your `package.json` file.

View File

@ -10,6 +10,9 @@
"author": "James Long",
"license": "MIT",
"main": "./index.js",
"engines": {
"node": ">=4"
},
"dependencies": {
"babel-code-frame": "7.0.0-alpha.12",
"babylon": "7.0.0-beta.23",

View File

@ -5,7 +5,11 @@ const minimatch = require("minimatch");
const mem = require("mem");
const getExplorerMemoized = mem(opts =>
cosmiconfig("prettier", { sync: opts.sync, cache: opts.cache })
cosmiconfig("prettier", {
sync: opts.sync,
cache: opts.cache,
rcExtensions: true
})
);
/** @param {{ cache: boolean, sync: boolean }} opts */

View File

@ -2,17 +2,17 @@
exports[`CLI overrides take precedence 1`] = `
"console.log(
\\"should have semi\\"
\\"jest/__best-tests__/file.js should have semi\\"
);
console.log(
\\"should not have semi\\"
\\"jest/Component.js should not have semi\\"
)
console.log(
\\"should have semi\\"
\\"jest/Component.test.js should have semi\\"
);
function f() {
function js() {
console.log(
\\"should have tab width 8\\"
\\"js/file.js should have tab width 8\\"
);
}
\\"use strict\\";
@ -20,21 +20,31 @@ function f() {
module.exports = {
tabWidth: 8
};
function f() {
function noConfigJs() {
console.log(
\\"should have no semicolons\\"
\\"no-config/file.js should have no semicolons\\"
)
}
function f() {
function packageJs() {
console.log(
\\"should have tab width 3\\"
\\"package/file.js should have tab width 3\\"
);
}
function f() {
function rcJson() {
console.log.apply(
null,
[
'this file',
'rc-json/file.js',
'should have trailing comma',
'and single quotes',
],
);
}
function rcYaml() {
console.log.apply(
null,
[
'rc-yaml/file.js',
'should have trailing comma',
'and single quotes',
],
@ -44,8 +54,8 @@ function f() {
`;
exports[`accepts configuration from --config 1`] = `
"function f() {
console.log(\\"should have tab width 8\\")
"function js() {
console.log(\\"js/file.js should have tab width 8\\")
}
"
`;
@ -56,26 +66,33 @@ exports[`resolves configuration file with --find-config-path file 1`] = `
`;
exports[`resolves configuration from external files 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\\");
"console.log(\\"jest/__best-tests__/file.js should have semi\\");
console.log(\\"jest/Component.js should not have semi\\")
console.log(\\"jest/Component.test.js should have semi\\");
function js() {
console.log(\\"js/file.js should have tab width 8\\");
}
\\"use strict\\";
module.exports = {
tabWidth: 8
};
function f() {
console.log(\\"should have no semicolons\\")
function noConfigJs() {
console.log(\\"no-config/file.js should have no semicolons\\")
}
function f() {
console.log(\\"should have tab width 3\\");
function packageJs() {
console.log(\\"package/file.js should have tab width 3\\");
}
function f() {
function rcJson() {
console.log.apply(null, [
'this file',
'rc-json/file.js',
'should have trailing comma',
'and single quotes',
]);
}
function rcYaml() {
console.log.apply(null, [
'rc-yaml/file.js',
'should have trailing comma',
'and single quotes',
]);
@ -84,11 +101,25 @@ function f() {
`;
exports[`resolves configuration from external files and overrides by extname 1`] = `
"function g() {
console.log(\\"should have semicolons because it has a .ts extension\\");
"function noConfigTs() {
console.log(
\\"no-config/file.ts should have semicolons because it has a .ts extension\\"
);
}
function g() {
console.log(\\"should have tab width 5 because it has .ts extension\\");
function packageTs() {
console.log(
\\"package/file.ts should have tab width 5 because it has .ts extension\\"
);
}
"
`;
exports[`resolves json configuration file with --find-config-path file 1`] = `
"rc-json/.prettierrc.json
"
`;
exports[`resolves yaml configuration file with --find-config-path file 1`] = `
"rc-yaml/.prettierrc.yaml
"
`;

View File

@ -1,24 +1,24 @@
// 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\\")
"function noConfigJs() {
console.log(\\"no-config/file.js should have no semicolons\\")
}
"
`;
exports[`CLI overrides gets applied when no config exists with --config-precedence prefer-file 1`] = `
"function f() {
"function noConfigJs() {
console.log(
\\"should have no semicolons\\"
\\"no-config/file.js 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\\");
"function js() {
console.log(\\"js/file.js should have tab width 8\\");
}
\\"use strict\\";
@ -29,8 +29,8 @@ module.exports = {
`;
exports[`CLI overrides take lower precedence with --config-precedence file-override 1`] = `
"function f() {
console.log(\\"should have tab width 8\\");
"function js() {
console.log(\\"js/file.js should have tab width 8\\");
}
\\"use strict\\";
@ -42,17 +42,17 @@ module.exports = {
exports[`CLI overrides take precedence with --config-precedence cli-override 1`] = `
"console.log(
\\"should have semi\\"
\\"jest/__best-tests__/file.js should have semi\\"
);
console.log(
\\"should not have semi\\"
\\"jest/Component.js should not have semi\\"
)
console.log(
\\"should have semi\\"
\\"jest/Component.test.js should have semi\\"
);
function f() {
function js() {
console.log(
\\"should have tab width 8\\"
\\"js/file.js should have tab width 8\\"
);
}
\\"use strict\\";
@ -60,21 +60,31 @@ function f() {
module.exports = {
tabWidth: 8
};
function f() {
function noConfigJs() {
console.log(
\\"should have no semicolons\\"
\\"no-config/file.js should have no semicolons\\"
)
}
function f() {
function packageJs() {
console.log(
\\"should have tab width 3\\"
\\"package/file.js should have tab width 3\\"
);
}
function f() {
function rcJson() {
console.log.apply(
null,
[
'this file',
'rc-json/file.js',
'should have trailing comma',
'and single quotes',
],
);
}
function rcYaml() {
console.log.apply(
null,
[
'rc-yaml/file.js',
'should have trailing comma',
'and single quotes',
],
@ -85,17 +95,17 @@ function f() {
exports[`CLI overrides take precedence without --config-precedence 1`] = `
"console.log(
\\"should have semi\\"
\\"jest/__best-tests__/file.js should have semi\\"
);
console.log(
\\"should not have semi\\"
\\"jest/Component.js should not have semi\\"
)
console.log(
\\"should have semi\\"
\\"jest/Component.test.js should have semi\\"
);
function f() {
function js() {
console.log(
\\"should have tab width 8\\"
\\"js/file.js should have tab width 8\\"
);
}
\\"use strict\\";
@ -103,21 +113,31 @@ function f() {
module.exports = {
tabWidth: 8
};
function f() {
function noConfigJs() {
console.log(
\\"should have no semicolons\\"
\\"no-config/file.js should have no semicolons\\"
)
}
function f() {
function packageJs() {
console.log(
\\"should have tab width 3\\"
\\"package/file.js should have tab width 3\\"
);
}
function f() {
function rcJson() {
console.log.apply(
null,
[
'this file',
'rc-json/file.js',
'should have trailing comma',
'and single quotes',
],
);
}
function rcYaml() {
console.log.apply(
null,
[
'rc-yaml/file.js',
'should have trailing comma',
'and single quotes',
],

View File

@ -36,6 +36,24 @@ test("resolves configuration file with --find-config-path file", () => {
expect(output.status).toEqual(0);
});
test("resolves json configuration file with --find-config-path file", () => {
const output = runPrettier("cli/config/", [
"--find-config-path",
"rc-json/file.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});
test("resolves yaml configuration file with --find-config-path file", () => {
const output = runPrettier("cli/config/", [
"--find-config-path",
"rc-yaml/file.js"
]);
expect(output.stdout).toMatchSnapshot();
expect(output.status).toEqual(0);
});
test("prints nothing when no file found with --find-config-path", () => {
const output = runPrettier("cli/config/", ["--find-config-path", ".."]);
expect(output.stdout).toEqual("");

View File

@ -1 +1 @@
console.log("should not have semi")
console.log("jest/Component.js should not have semi")

View File

@ -1 +1 @@
console.log("should have semi");
console.log("jest/Component.test.js should have semi");

View File

@ -1 +1 @@
console.log("should have semi");
console.log("jest/__best-tests__/file.js should have semi");

View File

@ -1,3 +1,3 @@
function f() {
console.log("should have tab width 8");
function js() {
console.log("js/file.js should have tab width 8");
}

View File

@ -1,3 +1,3 @@
function f() {
console.log("should have no semicolons");
function noConfigJs() {
console.log("no-config/file.js should have no semicolons");
}

View File

@ -1,3 +1,3 @@
function g() {
console.log("should have semicolons because it has a .ts extension");
function noConfigTs() {
console.log("no-config/file.ts should have semicolons because it has a .ts extension");
}

View File

@ -1,3 +1,3 @@
function f() {
console.log("should have tab width 3");
function packageJs() {
console.log("package/file.js should have tab width 3");
}

View File

@ -1,3 +1,3 @@
function g() {
console.log("should have tab width 5 because it has .ts extension");
function packageTs() {
console.log("package/file.ts should have tab width 5 because it has .ts extension");
}

View File

@ -1,7 +1,7 @@
function f() {
function rcJson() {
console.log.apply(null, [
"this file",
"rc-json/file.js",
"should have trailing comma",
"and single quotes",
"and single quotes"
]);
}

View File

@ -0,0 +1,2 @@
trailingComma: all
singleQuote: true

View File

@ -0,0 +1,7 @@
function rcYaml() {
console.log.apply(null, [
"rc-yaml/file.js",
"should have trailing comma",
"and single quotes"
]);
}