From a46bb9ff7355b8e7555c72265ca95692ddb797ef Mon Sep 17 00:00:00 2001 From: Ika Date: Sun, 16 Sep 2018 17:41:18 +0800 Subject: [PATCH] fix: specify config file correctly `overrides` field in config file did not work in the original implementation BREAKING CHANGE: - specify config file via filepath instead of directory --- README.md | 4 ++-- src/prettierRule.ts | 13 ++++++++++--- tests/prettier/specified-config/tslint.json | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9cd86034..a13b67c9 100644 --- a/README.md +++ b/README.md @@ -82,13 +82,13 @@ If there is no option provided, it'll try to load [config file](https://prettier } ``` -If you'd like to specify where to find the config file, just put the search path (relative to `process.cwd()`) in the second argument, the following example shows how to use the config file from `/configs/.prettierrc`: +If you'd like to specify which config file to use, just put its path (relative to `process.cwd()`) in the second argument, the following example shows how to load the config file from `/configs/.prettierrc`: ```json { "extends": ["tslint-plugin-prettier"], "rules": { - "prettier": [true, "configs"] + "prettier": [true, "configs/.prettierrc"] } } ``` diff --git a/src/prettierRule.ts b/src/prettierRule.ts index 9462f1b8..cdaa428e 100644 --- a/src/prettierRule.ts +++ b/src/prettierRule.ts @@ -23,12 +23,19 @@ class Walker extends tslint.AbstractWalker { options = ruleArgument1 as prettier.Options; break; case 'string': { - const filePath = path.resolve(process.cwd(), ruleArgument1 as string); - const resolvedConfig = prettier.resolveConfig.sync(filePath); + const configFilePath = path.resolve( + process.cwd(), + ruleArgument1 as string, + ); + + const resolvedConfig = prettier.resolveConfig.sync( + sourceFile.fileName, + { config: configFilePath }, + ); // istanbul ignore next if (resolvedConfig === null) { - throw new Error(`Config file not found: ${filePath}`); + throw new Error(`Config file not found: ${configFilePath}`); } options = resolvedConfig; diff --git a/tests/prettier/specified-config/tslint.json b/tests/prettier/specified-config/tslint.json index 78098054..ae3a31db 100644 --- a/tests/prettier/specified-config/tslint.json +++ b/tests/prettier/specified-config/tslint.json @@ -1,6 +1,6 @@ { "rulesDirectory": ["../../../rules"], "rules": { - "prettier": [true, "./fixtures/no-semi"] + "prettier": [true, "./fixtures/no-semi/.prettierrc"] } }