From fe39a1d0c62f1c060e9e2d663ae2962f0279fe89 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 27 Sep 2017 23:34:26 -0500 Subject: [PATCH] feat: add ability to specify config file search path (#48) --- README.md | 11 ++++++ fixtures/no-semi/.prettierrc | 3 ++ src/prettierRule.ts | 38 +++++++++++++++++-- .../prettier/specified-config/no-semi.ts.fix | 1 + .../prettier/specified-config/no-semi.ts.lint | 2 + tests/prettier/specified-config/tslint.json | 6 +++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 fixtures/no-semi/.prettierrc create mode 100644 tests/prettier/specified-config/no-semi.ts.fix create mode 100644 tests/prettier/specified-config/no-semi.ts.lint create mode 100644 tests/prettier/specified-config/tslint.json diff --git a/README.md b/README.md index 89485445..e1f2b685 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,17 @@ If there is no option provided, it'll try to load [config file](https://github.c } ``` +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`: + +```json +{ + "extends": ["tslint-plugin-prettier"], + "rules": { + "prettier": [true, "configs"] + } +} +``` + If you'd like to specify options manually, just put [Prettier Options](https://github.com/prettier/prettier#options) in the second argument, for example: ```json diff --git a/fixtures/no-semi/.prettierrc b/fixtures/no-semi/.prettierrc new file mode 100644 index 00000000..cce9d3c0 --- /dev/null +++ b/fixtures/no-semi/.prettierrc @@ -0,0 +1,3 @@ +{ + "semi": false +} diff --git a/src/prettierRule.ts b/src/prettierRule.ts index f690c86f..aa380249 100644 --- a/src/prettierRule.ts +++ b/src/prettierRule.ts @@ -1,5 +1,6 @@ import assert = require('assert'); import * as utils from 'eslint-plugin-prettier'; +import * as path from 'path'; import * as prettier from 'prettier'; import * as tslint from 'tslint'; import * as ts from 'typescript'; @@ -24,10 +25,33 @@ class Walker extends tslint.AbstractWalker { case 'object': options = rule_argument_1 as prettier.Options; break; - default: + case 'string': { try { - // tslint:disable-next-line:strict-type-predicates - assert(typeof prettier.resolveConfig.sync === 'function'); + assert_existence_of_resolve_config_sync(); + } catch { + // istanbul ignore next + throw new Error( + `Require prettier@1.7.0+ to specify config file, but got prettier@${prettier.version}.`, + ); + } + + const file_path = path.resolve( + process.cwd(), + rule_argument_1 as string, + ); + const resolved_config = prettier.resolveConfig.sync(file_path); + + // istanbul ignore next + if (resolved_config === null) { + throw new Error(`Config file not found: ${file_path}`); + } + + options = resolved_config; + break; + } + default: { + try { + assert_existence_of_resolve_config_sync(); } catch { // backward compatibility: use default options if no resolveConfig.sync() // istanbul ignore next @@ -37,10 +61,13 @@ class Walker extends tslint.AbstractWalker { const resolved_config = prettier.resolveConfig.sync( source_file.fileName, ); + if (resolved_config !== null) { options = resolved_config; } + break; + } } const source = source_file.getFullText(); @@ -97,3 +124,8 @@ class Walker extends tslint.AbstractWalker { }); } } + +function assert_existence_of_resolve_config_sync() { + // tslint:disable-next-line:strict-type-predicates + assert(typeof prettier.resolveConfig.sync === 'function'); +} diff --git a/tests/prettier/specified-config/no-semi.ts.fix b/tests/prettier/specified-config/no-semi.ts.fix new file mode 100644 index 00000000..d1cdef75 --- /dev/null +++ b/tests/prettier/specified-config/no-semi.ts.fix @@ -0,0 +1 @@ +const x = "there should be no semi at the end of this line" diff --git a/tests/prettier/specified-config/no-semi.ts.lint b/tests/prettier/specified-config/no-semi.ts.lint new file mode 100644 index 00000000..5797cb79 --- /dev/null +++ b/tests/prettier/specified-config/no-semi.ts.lint @@ -0,0 +1,2 @@ +const x = "there should be no semi at the end of this line"; + ~ [Delete `;`] diff --git a/tests/prettier/specified-config/tslint.json b/tests/prettier/specified-config/tslint.json new file mode 100644 index 00000000..78098054 --- /dev/null +++ b/tests/prettier/specified-config/tslint.json @@ -0,0 +1,6 @@ +{ + "rulesDirectory": ["../../../rules"], + "rules": { + "prettier": [true, "./fixtures/no-semi"] + } +}