Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use official reporter #13

Merged
merged 3 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

tslint plugin for prettier formatting

**NOTE**: This project is modified from [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier), ported to TSLint.
**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)

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
},
"license": "MIT",
"scripts": {
"prepublish": "yarn run build -- --inlineSourceMap false",
"prepublish": "yarn run link && yarn run build -- --inlineSourceMap false",
"link": "ln -sf $PWD/types/* $PWD/node_modules/@types",
"lint": "tslint -p ./tsconfig.json --type-check",
"test": "tslint --test ./tests/*/*",
"test-coverage": "nyc yarn run test",
"build": "rm -rf ./rules && tsc -p tsconfig.build.json",
"release": "standard-version"
},
"dependencies": {
"fast-diff": "^1.1.1",
"eslint-plugin-prettier": "^2.2.0",
"tslib": "^1.7.1"
},
"devDependencies": {
"@types/fast-diff": "1.1.0",
"@types/node": "8.0.22",
"@types/prettier": "1.5.0",
"nyc": "11.1.0",
Expand Down
52 changes: 48 additions & 4 deletions src/prettierRule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as utils from 'eslint-plugin-prettier';
import * as prettier from 'prettier';
import * as tslint from 'tslint';
import * as ts from 'typescript';
import { report_differences } from './utils/report-differences';

// tslint:disable:max-classes-per-file no-use-before-declare
// tslint:disable:max-classes-per-file no-use-before-declare restrict-plus-operands

export class Rule extends tslint.Rules.AbstractRule {
public apply(source_file: ts.SourceFile): tslint.RuleFailure[] {
Expand All @@ -22,8 +22,52 @@ class Walker extends tslint.AbstractWalker<prettier.Options> {
public walk(source_file: ts.SourceFile) {
const source = source_file.getFullText();
const formatted = prettier.format(source, this.options);
if (source !== formatted) {
report_differences(this, formatted);

if (source === formatted) {
return;
}

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

const end = start + delete_text.length;
const delete_code = utils.showInvisibles(delete_text);
const insert_code = utils.showInvisibles(insert_text);

switch (operation) {
case utils.DifferenceOperation.Insert:
this.addFailureAt(
start,
1,
`Insert \`${insert_code}\``,
tslint.Replacement.appendText(start, insert_text),
);
break;
case utils.DifferenceOperation.Delete:
this.addFailure(
start,
end,
`Delete \`${delete_code}\``,
tslint.Replacement.deleteFromTo(start, end),
);
break;
case utils.DifferenceOperation.Replace:
this.addFailure(
start,
end,
`Replace \`${delete_code}\` with \`${insert_code}\``,
tslint.Replacement.replaceFromTo(start, end, insert_text),
);
break;
// istanbul ignore next
default:
throw new Error(`Unexpected operation '${operation}'`);
}
});
}
}
20 changes: 0 additions & 20 deletions src/utils/report-delete.ts

This file was deleted.

86 changes: 0 additions & 86 deletions src/utils/report-differences.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/utils/report-insert.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/utils/report-replace.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/utils/show-invisibles.ts

This file was deleted.

33 changes: 33 additions & 0 deletions types/eslint-plugin-prettier/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Type definitions for eslint-plugin-prettier 2.2
// Project: https://github.com/prettier/eslint-plugin-prettier
// Definitions by: Ika <https://github.com/ikatyang>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/**
* Converts invisible characters to a commonly recognizable visible form.
* @param str The string with invisibles to convert.
*/
export function showInvisibles(str: string): string;

/**
* Generate results for differences between source code and formatted version.
* @param source The original source.
* @param formatted The formatted source.
*/
export function generateDifferences(
source: string,
formatted: string,
): Difference[];

export interface Difference {
operation: DifferenceOperation;
offset: number;
insertText?: string;
deleteText?: string;
}

export const enum DifferenceOperation {
Insert = 'insert',
Delete = 'delete',
Replace = 'replace',
}
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
version "6.25.1"
resolved "https://registry.yarnpkg.com/@types/babel-types/-/babel-types-6.25.1.tgz#ce8f126a4403e11e1b0033a424f11638afac7889"

"@types/fast-diff@1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@types/fast-diff/-/fast-diff-1.1.0.tgz#68c7f476025740b0b6756e51e38b1188dd528b0e"

"@types/node@8.0.22":
version "8.0.22"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.22.tgz#9c6bfee1f45f5e9952ff6b487e657ecca48c7777"
Expand Down Expand Up @@ -532,6 +528,13 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

eslint-plugin-prettier@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-2.2.0.tgz#f2837ad063903d73c621e7188fb3d41486434088"
dependencies:
fast-diff "^1.1.1"
jest-docblock "^20.0.1"

esutils@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-1.1.6.tgz#c01ccaa9ae4b897c6d0c3e210ae52f3c7a844375"
Expand Down Expand Up @@ -951,6 +954,10 @@ istanbul-reports@^1.1.1:
dependencies:
handlebars "^4.0.3"

jest-docblock@^20.0.1:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-20.0.3.tgz#17bea984342cc33d83c50fbe1545ea0efaa44712"

js-tokens@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
Expand Down