Skip to content

Commit

Permalink
chore: require prettier@1.7.0+
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

require prettier@1.7.0+
  • Loading branch information
ikatyang committed Sep 16, 2018
1 parent 9349dd6 commit bd50395
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 70 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

Runs Prettier as a TSLint rule and reports differences as individual TSLint issues.

**NOTE**: This project uses official reporter from [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier).

[Changelog](https://github.com/ikatyang/tslint-plugin-prettier/blob/master/CHANGELOG.md)

## Sample
Expand Down Expand Up @@ -41,11 +39,13 @@ npm install --save-dev tslint-plugin-prettier prettier
yarn add --dev tslint-plugin-prettier prettier
```

(require `prettier@^1.7.0`)

## Usage

(tslint.json)

for `tslint@5.0.0+`
for `tslint@^5.0.0`

```json
{
Expand All @@ -56,7 +56,7 @@ for `tslint@5.0.0+`
}
```

for `tslint@5.2.0+`
for `tslint@^5.2.0`

```json
{
Expand All @@ -71,7 +71,7 @@ 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 (require `prettier@1.7.0+`), 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) if possible, uses Prettier's default option if not found.

```json
{
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.4.0",
"prettier": "^1.7.0",
"tslint": "^5.0.0"
},
"engines": {
Expand Down
112 changes: 48 additions & 64 deletions src/prettierRule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert = require('assert');
import * as utils from 'eslint-plugin-prettier';
import * as path from 'path';
import * as prettier from 'prettier';
Expand All @@ -24,17 +23,6 @@ class Walker extends tslint.AbstractWalker<any[]> {
options = ruleArgument1 as prettier.Options;
break;
case 'string': {
try {
assert_existence_of_resolve_config_sync();
} catch {
// istanbul ignore next
throw new Error(
`Require prettier@1.7.0+ to specify config file, but got prettier@${
prettier.version
}.`,
);
}

const filePath = path.resolve(process.cwd(), ruleArgument1 as string);
const resolvedConfig = prettier.resolveConfig.sync(filePath);

Expand All @@ -47,14 +35,6 @@ class Walker extends tslint.AbstractWalker<any[]> {
break;
}
default: {
try {
assert_existence_of_resolve_config_sync();
} catch {
// backward compatibility: use default options if no resolveConfig.sync()
// istanbul ignore next
break;
}

const resolvedConfig = prettier.resolveConfig.sync(sourceFile.fileName);

if (resolvedConfig !== null) {
Expand All @@ -75,51 +55,55 @@ class Walker extends tslint.AbstractWalker<any[]> {
return;
}

utils.generateDifferences(source, formatted).forEach(difference => {
const {
operation,
offset: start,
deleteText = '',
insertText = '',
} = difference;

const end = start + deleteText.length;
const deleteCode = utils.showInvisibles(deleteText);
const insertCode = utils.showInvisibles(insertText);

switch (operation) {
case 'insert':
this.addFailureAt(
start,
1,
`Insert \`${insertCode}\``,
tslint.Replacement.appendText(start, insertText),
);
break;
case 'delete':
this.addFailure(
start,
end,
`Delete \`${deleteCode}\``,
tslint.Replacement.deleteFromTo(start, end),
);
break;
case 'replace':
this.addFailure(
start,
end,
`Replace \`${deleteCode}\` with \`${insertCode}\``,
tslint.Replacement.replaceFromTo(start, end, insertText),
);
break;
// istanbul ignore next
default:
throw new Error(`Unexpected operation '${operation}'`);
}
});
reportDifferences(this, source, formatted);
}
}

function assert_existence_of_resolve_config_sync() {
assert(typeof prettier.resolveConfig.sync === 'function');
function reportDifferences(
walkContext: tslint.WalkContext<any>,
source: string,
formatted: string,
) {
utils.generateDifferences(source, formatted).forEach(difference => {
const {
operation,
offset: start,
deleteText = '',
insertText = '',
} = difference;

const end = start + deleteText.length;
const deleteCode = utils.showInvisibles(deleteText);
const insertCode = utils.showInvisibles(insertText);

switch (operation) {
case 'insert':
walkContext.addFailureAt(
start,
1,
`Insert \`${insertCode}\``,
tslint.Replacement.appendText(start, insertText),
);
break;
case 'delete':
walkContext.addFailure(
start,
end,
`Delete \`${deleteCode}\``,
tslint.Replacement.deleteFromTo(start, end),
);
break;
case 'replace':
walkContext.addFailure(
start,
end,
`Replace \`${deleteCode}\` with \`${insertCode}\``,
tslint.Replacement.replaceFromTo(start, end, insertText),
);
break;
// istanbul ignore next
default:
throw new Error(`Unexpected operation '${operation}'`);
}
});
}

0 comments on commit bd50395

Please sign in to comment.