From 31d22b6594e60f30bda3a0d74a7d211deac6fa4c Mon Sep 17 00:00:00 2001 From: JounQin Date: Fri, 10 Apr 2020 23:20:14 +0800 Subject: [PATCH] feat: add definition for prettier-linter-helpers v1.0 (#43776) --- types/prettier-linter-helpers/index.d.ts | 34 +++++++++++++++++++ .../prettier-linter-helpers-tests.ts | 27 +++++++++++++++ types/prettier-linter-helpers/tsconfig.json | 23 +++++++++++++ types/prettier-linter-helpers/tslint.json | 1 + 4 files changed, 85 insertions(+) create mode 100644 types/prettier-linter-helpers/index.d.ts create mode 100644 types/prettier-linter-helpers/prettier-linter-helpers-tests.ts create mode 100644 types/prettier-linter-helpers/tsconfig.json create mode 100644 types/prettier-linter-helpers/tslint.json diff --git a/types/prettier-linter-helpers/index.d.ts b/types/prettier-linter-helpers/index.d.ts new file mode 100644 index 0000000000..3710329a47 --- /dev/null +++ b/types/prettier-linter-helpers/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for prettier-linter-helpers 1.0 +// Project: https://github.com/prettier/prettier-linter-helpers +// Definitions by: 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; +} diff --git a/types/prettier-linter-helpers/prettier-linter-helpers-tests.ts b/types/prettier-linter-helpers/prettier-linter-helpers-tests.ts new file mode 100644 index 0000000000..e9e70b880a --- /dev/null +++ b/types/prettier-linter-helpers/prettier-linter-helpers-tests.ts @@ -0,0 +1,27 @@ +import { generateDifferences, showInvisibles } from 'prettier-linter-helpers'; + +declare function log(...args: Array): 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; diff --git a/types/prettier-linter-helpers/tsconfig.json b/types/prettier-linter-helpers/tsconfig.json new file mode 100644 index 0000000000..f06e4c9fd5 --- /dev/null +++ b/types/prettier-linter-helpers/tsconfig.json @@ -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" + ] +} diff --git a/types/prettier-linter-helpers/tslint.json b/types/prettier-linter-helpers/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/prettier-linter-helpers/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }