Skip to content

Commit

Permalink
feat: add definition for prettier-linter-helpers v1.0 (DefinitelyType…
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Apr 10, 2020
1 parent 69f5a30 commit 31d22b6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
34 changes: 34 additions & 0 deletions types/prettier-linter-helpers/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Type definitions for prettier-linter-helpers 1.0
// Project: https://github.com/prettier/prettier-linter-helpers
// Definitions by: JounQin <https://github.com/JounQin>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

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

export interface GenerateDifferences {
/**
* Generate results for differences between source code and formatted version.
*
* @param source - The original source.
* @param prettierSource - The Prettier formatted source.
* @returns An array containing { operation, offset, insertText, deleteText }
*/
(source: string, prettierSource: string): Difference[];
INSERT: 'insert';
DELETE: 'delete';
REPLACE: 'replace';
}

export const generateDifferences: GenerateDifferences;

export interface Difference {
operation: 'insert' | 'delete' | 'replace';
offset: number;
insertText?: string;
deleteText?: string;
}
27 changes: 27 additions & 0 deletions types/prettier-linter-helpers/prettier-linter-helpers-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { generateDifferences, showInvisibles } from 'prettier-linter-helpers';

declare function log(...args: Array<string | number>): void;

showInvisibles(' some string ');

generateDifferences('abc', 'def').forEach(difference => {
const { operation, offset, deleteText = '', insertText = '' } = difference;

switch (operation) {
case 'delete':
log('delete', offset, deleteText);
break;
case 'insert':
log('insert', offset, insertText);
break;
case 'replace':
log('replace', offset, insertText, deleteText);
break;
default:
throw new Error(`Unexpected operation: '${operation}'`);
}
});

generateDifferences.INSERT;
generateDifferences.DELETE;
generateDifferences.REPLACE;
23 changes: 23 additions & 0 deletions types/prettier-linter-helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"prettier-linter-helpers-tests.ts"
]
}
1 change: 1 addition & 0 deletions types/prettier-linter-helpers/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

0 comments on commit 31d22b6

Please sign in to comment.