Skip to content

Commit

Permalink
feat: add ability to specify config file search path (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang authored Sep 28, 2017
1 parent 40c20da commit fe39a1d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<cwd>/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
Expand Down
3 changes: 3 additions & 0 deletions fixtures/no-semi/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": false
}
38 changes: 35 additions & 3 deletions src/prettierRule.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,10 +25,33 @@ class Walker extends tslint.AbstractWalker<any[]> {
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 [email protected]+ 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
Expand All @@ -37,10 +61,13 @@ class Walker extends tslint.AbstractWalker<any[]> {
const resolved_config = prettier.resolveConfig.sync(
source_file.fileName,
);

if (resolved_config !== null) {
options = resolved_config;
}

break;
}
}

const source = source_file.getFullText();
Expand Down Expand Up @@ -97,3 +124,8 @@ class Walker extends tslint.AbstractWalker<any[]> {
});
}
}

function assert_existence_of_resolve_config_sync() {
// tslint:disable-next-line:strict-type-predicates
assert(typeof prettier.resolveConfig.sync === 'function');
}
1 change: 1 addition & 0 deletions tests/prettier/specified-config/no-semi.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const x = "there should be no semi at the end of this line"
2 changes: 2 additions & 0 deletions tests/prettier/specified-config/no-semi.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const x = "there should be no semi at the end of this line";
~ [Delete `;`]
6 changes: 6 additions & 0 deletions tests/prettier/specified-config/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../../rules"],
"rules": {
"prettier": [true, "./fixtures/no-semi"]
}
}

0 comments on commit fe39a1d

Please sign in to comment.