Skip to content

Commit

Permalink
feat: respect .prettierignore (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhallett authored and ikatyang committed Dec 21, 2019
1 parent 0f0b819 commit 0420071
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ for `tslint@^5.0.0`
}
```

## Ignoring files

- It will respect your .prettierignore file in your project root ( process.cwd() ) but if you would like to use a different file you can provide it in the third argument, for example:

```json
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": [true, null, { "ignorePath": "otherDirectory/.prettierignore" }]
}
}

```

## Development

```sh
Expand Down
17 changes: 17 additions & 0 deletions src/prettierRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@ import * as ts from 'typescript';

export class Rule extends tslint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): tslint.RuleFailure[] {
if (this.sourceFileIsIgnored(sourceFile, this.ruleArguments)) {
return [];
}
return this.applyWithWalker(
new Walker(sourceFile, this.ruleName, this.ruleArguments),
);
}
private getIgnorePath(ruleArguments: any[]) {
let ignorePath = '.prettierignore';
if (ruleArguments.length === 2 && ruleArguments[1].ignorePath) {
ignorePath = ruleArguments[1].ignorePath;
}
return ignorePath;
}
private sourceFileIsIgnored(sourceFile: ts.SourceFile, ruleArguments: any[]) {
const ignorePath = this.getIgnorePath(ruleArguments);
const fileInfo = prettier.getFileInfo.sync(sourceFile!.fileName, {
ignorePath,
});
return fileInfo.ignored;
}
}

class Walker extends tslint.AbstractWalker<any[]> {
Expand Down
1 change: 1 addition & 0 deletions tests/prettier/prettier-ignored/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prettierIgnoredSyntaxError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
6 changes: 6 additions & 0 deletions tests/prettier/prettier-ignored/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rulesDirectory": ["../../../rules"],
"rules": {
"prettier": [true, null, { "ignorePath": "tests/prettier/prettier-ignored/.prettierignore"}]
}
}

0 comments on commit 0420071

Please sign in to comment.