-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to specify config file search path (#48)
- Loading branch information
Showing
6 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"semi": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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 | ||
|
@@ -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(); | ||
|
@@ -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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 `;`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rulesDirectory": ["../../../rules"], | ||
"rules": { | ||
"prettier": [true, "./fixtures/no-semi"] | ||
} | ||
} |