feat: add definition for prettier-linter-helpers v1.0 (#43776)

This commit is contained in:
JounQin 2020-04-10 23:20:14 +08:00 committed by GitHub
parent 69f5a30b3c
commit 31d22b6594
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }