Skip to content

Commit

Permalink
feat: Only use "local" modules for formatting
Browse files Browse the repository at this point in the history
When we resolve configs for prettier and eslint we now use our own version of the respective packages. This way we don't need to check for backwards-compatibility becuase we know the latest compatible version is available, even if the project using prettier-eslint have an older version of prettier installed.

Because of this we can now lower the version requirements of prettier and eslint.
  • Loading branch information
zimme committed Dec 21, 2017
1 parent f9e993a commit 49b762b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"dependencies": {
"common-tags": "^1.4.0",
"dlv": "^1.1.0",
"eslint": "^4.5.0",
"eslint": "^4.0.0",
"indent-string": "^3.2.0",
"lodash.merge": "^4.6.0",
"loglevel-colored-level-prefix": "^1.0.0",
"prettier": "^1.9.0",
"prettier": "^1.7.0",
"pretty-format": "^22.0.3",
"require-relative": "^0.8.7",
"typescript": "^2.5.1",
Expand Down
18 changes: 0 additions & 18 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,24 +351,6 @@ test("calls prettier.resolveConfig.sync with the file path", () => {
expect(prettierMock.resolveConfig.sync).toHaveBeenCalledWith(filePath);
});

test("does not raise an error if prettier.resolveConfig.sync is not defined", () => {
const filePath = require.resolve("../../tests/fixtures/paths/foo.js");
const originalPrettierMockResolveConfigSync = prettierMock.resolveConfig.sync;
prettierMock.resolveConfig.sync = undefined;

function callingFormat() {
return format({
filePath,
text: defaultInputText(),
eslintConfig: getESLintConfigWithDefaultRules()
});
}

expect(callingFormat).not.toThrowError();

prettierMock.resolveConfig.sync = originalPrettierMockResolveConfigSync;
});

test("logs if there is a problem making the CLIEngine", () => {
const error = new Error("fake error");
eslintMock.CLIEngine.mockImplementation(() => {
Expand Down
21 changes: 8 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ function format(options) {
const eslintConfig = merge(
{},
options.eslintConfig,
getConfig(filePath, eslintPath)
getESLintConfig(filePath)
);

const prettierOptions = merge(
{},
filePath && { filepath: filePath },
getPrettierConfig(filePath, prettierPath),
getPrettierConfig(filePath),
options.prettierOptions
);

Expand Down Expand Up @@ -203,7 +203,7 @@ function getTextFromFilePath(filePath) {
}
}

function getConfig(filePath, eslintPath) {
function getESLintConfig(filePath) {
const eslintOptions = {};
if (filePath) {
eslintOptions.cwd = path.dirname(filePath);
Expand All @@ -214,10 +214,10 @@ function getConfig(filePath, eslintPath) {
"${filePath || process.cwd()}"
`
);
const configFinder = getESLintCLIEngine(eslintPath, eslintOptions);
const cliEngine = getESLintCLIEngine("eslint", eslintOptions);
try {
logger.debug(`getting eslint config for file at "${filePath}"`);
const config = configFinder.getConfigForFile(filePath);
const config = cliEngine.getConfigForFile(filePath);
logger.trace(
`eslint config for "${filePath}" received`,
prettyFormat(config)
Expand All @@ -230,14 +230,9 @@ function getConfig(filePath, eslintPath) {
}
}

function getPrettierConfig(filePath, prettierPath) {
const prettier = requireModule(prettierPath, "prettier");

// NOTE: Backward-compatibility with old prettier versions (<1.7)
// that don't have ``resolveConfig.sync` method.
return typeof prettier.resolveConfig.sync === "undefined"
? {}
: prettier.resolveConfig.sync(filePath);
function getPrettierConfig(filePath) {
const prettier = requireModule("prettier", "prettier");
return prettier.resolveConfig.sync(filePath);
}

function requireModule(modulePath, name) {
Expand Down

0 comments on commit 49b762b

Please sign in to comment.