diff --git a/README.md b/README.md index a13b67c9..bddb4829 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 `/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 `/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 diff --git a/package.json b/package.json index 639e70be..83cbcc44 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "typescript": "3.0.3" }, "peerDependencies": { - "prettier": "^1.7.0", + "prettier": "^1.9.0", "tslint": "^5.0.0" }, "engines": { diff --git a/src/prettierRule.ts b/src/prettierRule.ts index cdaa428e..8306067b 100644 --- a/src/prettierRule.ts +++ b/src/prettierRule.ts @@ -14,14 +14,12 @@ export class Rule extends tslint.Rules.AbstractRule { class Walker extends tslint.AbstractWalker { 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(), @@ -30,7 +28,7 @@ class Walker extends tslint.AbstractWalker { const resolvedConfig = prettier.resolveConfig.sync( sourceFile.fileName, - { config: configFilePath }, + { config: configFilePath, editorconfig }, ); // istanbul ignore next @@ -41,8 +39,17 @@ class Walker extends tslint.AbstractWalker { 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; diff --git a/tests/prettier/editorconfig/.editorconfig b/tests/prettier/editorconfig/.editorconfig new file mode 100644 index 00000000..b537b720 --- /dev/null +++ b/tests/prettier/editorconfig/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +indent_size = 8 diff --git a/tests/prettier/editorconfig/test.ts.lint b/tests/prettier/editorconfig/test.ts.lint new file mode 100644 index 00000000..96c20b99 --- /dev/null +++ b/tests/prettier/editorconfig/test.ts.lint @@ -0,0 +1,4 @@ +if (condition) { + doSomething(); + ~ [Insert `······`] +} diff --git a/tests/prettier/editorconfig/tslint.json b/tests/prettier/editorconfig/tslint.json new file mode 100644 index 00000000..f5e10e85 --- /dev/null +++ b/tests/prettier/editorconfig/tslint.json @@ -0,0 +1,6 @@ +{ + "rulesDirectory": ["../../../rules"], + "rules": { + "prettier": true + } +} diff --git a/tests/prettier/no-editorconfig/.editorconfig b/tests/prettier/no-editorconfig/.editorconfig new file mode 100644 index 00000000..b537b720 --- /dev/null +++ b/tests/prettier/no-editorconfig/.editorconfig @@ -0,0 +1,4 @@ +root = true + +[*] +indent_size = 8 diff --git a/tests/prettier/no-editorconfig/test.ts.lint b/tests/prettier/no-editorconfig/test.ts.lint new file mode 100644 index 00000000..a77d1d31 --- /dev/null +++ b/tests/prettier/no-editorconfig/test.ts.lint @@ -0,0 +1,3 @@ +if (condition) { + doSomething(); +} diff --git a/tests/prettier/no-editorconfig/tslint.json b/tests/prettier/no-editorconfig/tslint.json new file mode 100644 index 00000000..009692bc --- /dev/null +++ b/tests/prettier/no-editorconfig/tslint.json @@ -0,0 +1,6 @@ +{ + "rulesDirectory": ["../../../rules"], + "rules": { + "prettier": [true, null, { "editorconfig": false }] + } +} diff --git a/tslint.json b/tslint.json index 20b5ecb1..02f46f54 100644 --- a/tslint.json +++ b/tslint.json @@ -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 } }