Skip to content

Commit

Permalink
feat: support .editorconfig
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- require prettier@1.9.0+
- load `.editorconfig` by default
  • Loading branch information
ikatyang committed Sep 16, 2018
1 parent a46bb9f commit 5afdfbc
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 33 deletions.
61 changes: 36 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ npm install --save-dev tslint-plugin-prettier prettier
yarn add --dev tslint-plugin-prettier prettier
```

(require `prettier@^1.7.0`)
(require `prettier@^1.9.0`)

## Usage

Expand Down Expand Up @@ -71,38 +71,49 @@ for `tslint@^5.2.0`

## Options

If there is no option provided, it'll try to load [config file](https://prettier.io/docs/en/configuration.html) if possible, uses Prettier's default option if not found.
- If there is no option provided, it'll try to load [config file](https://prettier.io/docs/en/configuration.html) and/or `.editorconfig` if possible, uses Prettier's default option if not found.

```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": true
```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": true
}
}
}
```
```

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 `<cwd>/configs/.prettierrc`:
If you don't want to load `.editorconfig`, disable it in the third argument.

```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, "configs/.prettierrc"]
```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, null, { "editorconfig": false }]
}
}
}
```
```

If you'd like to specify options manually, just put [Prettier Options](https://prettier.io/docs/en/options.html) in the second argument, for example:
- 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 `<cwd>/configs/.prettierrc`:

```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, { "singleQuote": true }]
```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, "configs/.prettierrc"]
}
}
}
```
```

- If you'd like to specify options manually, just put [Prettier Options](https://prettier.io/docs/en/options.html) in the second argument, for example:

```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, { "singleQuote": true }]
}
}
```

## Development

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"typescript": "3.0.3"
},
"peerDependencies": {
"prettier": "^1.7.0",
"prettier": "^1.9.0",
"tslint": "^5.0.0"
},
"engines": {
Expand Down
19 changes: 13 additions & 6 deletions src/prettierRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ export class Rule extends tslint.Rules.AbstractRule {

class Walker extends tslint.AbstractWalker<any[]> {
public walk(sourceFile: ts.SourceFile) {
const [ruleArgument1] = this.options;
const [ruleArgument1, ruleArgument2 = {}] = this.options;
const { editorconfig = true } = ruleArgument2;

let options: prettier.Options = {};

switch (typeof ruleArgument1) {
case 'object':
options = ruleArgument1 as prettier.Options;
break;
case 'string': {
const configFilePath = path.resolve(
process.cwd(),
Expand All @@ -30,7 +28,7 @@ class Walker extends tslint.AbstractWalker<any[]> {

const resolvedConfig = prettier.resolveConfig.sync(
sourceFile.fileName,
{ config: configFilePath },
{ config: configFilePath, editorconfig },
);

// istanbul ignore next
Expand All @@ -41,8 +39,17 @@ class Walker extends tslint.AbstractWalker<any[]> {
options = resolvedConfig;
break;
}
case 'object':
if (ruleArgument1) {
options = ruleArgument1 as prettier.Options;
break;
}
// falls through for null
default: {
const resolvedConfig = prettier.resolveConfig.sync(sourceFile.fileName);
const resolvedConfig = prettier.resolveConfig.sync(
sourceFile.fileName,
{ editorconfig },
);

if (resolvedConfig !== null) {
options = resolvedConfig;
Expand Down
4 changes: 4 additions & 0 deletions tests/prettier/editorconfig/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*]
indent_size = 8
4 changes: 4 additions & 0 deletions tests/prettier/editorconfig/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (condition) {
doSomething();
~ [Insert `······`]
}
6 changes: 6 additions & 0 deletions tests/prettier/editorconfig/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../../rules"],
"rules": {
"prettier": true
}
}
4 changes: 4 additions & 0 deletions tests/prettier/no-editorconfig/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*]
indent_size = 8
3 changes: 3 additions & 0 deletions tests/prettier/no-editorconfig/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (condition) {
doSomething();
}
6 changes: 6 additions & 0 deletions tests/prettier/no-editorconfig/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../../rules"],
"rules": {
"prettier": [true, null, { "editorconfig": false }]
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"prettier-config-ikatyang/tslint"
],
"rules": {
"max-classes-per-file": false
"max-classes-per-file": false,
"no-switch-case-fall-through": true
}
}

0 comments on commit 5afdfbc

Please sign in to comment.