From 55915aa6637bf807f347ce1293e9851eec82b2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Tue, 31 Mar 2020 19:58:24 +0200 Subject: [PATCH] feat(postcss-reporter): type definitiion for v6.0 (#43458) - type definition for plugin and for the formatter - tests https://github.com/postcss/postcss-reporter#readme Thanks! --- types/postcss-reporter/index.d.ts | 81 +++++++++++++++++++ types/postcss-reporter/lib/formatter.d.ts | 5 ++ types/postcss-reporter/package.json | 6 ++ .../postcss-reporter-tests.ts | 46 +++++++++++ types/postcss-reporter/tsconfig.json | 23 ++++++ types/postcss-reporter/tslint.json | 1 + 6 files changed, 162 insertions(+) create mode 100644 types/postcss-reporter/index.d.ts create mode 100644 types/postcss-reporter/lib/formatter.d.ts create mode 100644 types/postcss-reporter/package.json create mode 100644 types/postcss-reporter/postcss-reporter-tests.ts create mode 100644 types/postcss-reporter/tsconfig.json create mode 100644 types/postcss-reporter/tslint.json diff --git a/types/postcss-reporter/index.d.ts b/types/postcss-reporter/index.d.ts new file mode 100644 index 0000000000..1da38232ea --- /dev/null +++ b/types/postcss-reporter/index.d.ts @@ -0,0 +1,81 @@ +// Type definitions for postcss-reporter 6.0 +// Project: https://github.com/postcss/postcss-reporter#readme +// Definitions by: Piotr Błażejewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { Plugin, ResultMessage } from 'postcss'; + +declare namespace postcssReporter { + /** + * Additional options + */ + interface Options extends DefaultOptions { + /** + * If true, the plugin will clear the result's messages after it logs them. + * This prevents other plugins, or the whatever runner you use, from logging the same information again and causing confusion. + * @default false + */ + clearReportedMessages?: boolean; + /** + * By default, this reporter will format the messages for human legibility in the console. + * To use another formatter, pass a function that + * - accepts an object containing a messages array and a source string + * - returns the string to report + */ + formatter?: (input: { messages: ResultMessage[]; source: string }) => string; + /** + * If plugins is empty (as it is by default), + * the reporter will log messages from every PostCSS plugin. + * @default [] + */ + plugins?: string[]; + /** + * Provide a filter function. It receives the message object and returns a truthy or falsy value, + * indicating whether that particular message should be reported or not. + */ + filter?: (message: ResultMessage) => boolean; + /** + * If true, not pass any messages into other plugins, or the whatever runner you use, for logging. + * @default false + */ + clearAllMessages?: boolean; + /** + * If true, after the plugin logs your messages it will throw an error if it found any warnings. + * @default false + */ + throwError?: boolean; + /** + * By default, messages without line/column positions will be grouped at the beginning of the output. + * To put them at the end, instead, use "last". To not bother sorting these, use "any". + * @default 'first' + */ + positionless?: 'first' | 'last' | 'any'; + } + + /** + * Default plugin options + */ + interface DefaultOptions { + /** + * If false, messages will not be sorted by line/column position. + * @default true + */ + sortByPosition?: boolean; + /** + * If true, no exclamatory triangle icons will be printed next to warnings. + * @default false + */ + noIcon?: boolean; + /** + * If true, plugin names will not be printed in brackets after messages. + * @default false + */ + noPlugin?: boolean; + } + + type PostCSSReporter = Plugin; +} + +declare const postcssReporter: postcssReporter.PostCSSReporter; + +export = postcssReporter; diff --git a/types/postcss-reporter/lib/formatter.d.ts b/types/postcss-reporter/lib/formatter.d.ts new file mode 100644 index 0000000000..d4580e9eb9 --- /dev/null +++ b/types/postcss-reporter/lib/formatter.d.ts @@ -0,0 +1,5 @@ +import { ResultMessage } from 'postcss'; +import { DefaultOptions } from '../index'; + +declare function formatter(options?: DefaultOptions): (input?: { messages: ResultMessage[]; source: string }) => string; +export = formatter; diff --git a/types/postcss-reporter/package.json b/types/postcss-reporter/package.json new file mode 100644 index 0000000000..73f1a60b11 --- /dev/null +++ b/types/postcss-reporter/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "postcss": "^7.0.27" + } +} diff --git a/types/postcss-reporter/postcss-reporter-tests.ts b/types/postcss-reporter/postcss-reporter-tests.ts new file mode 100644 index 0000000000..f37446cdc7 --- /dev/null +++ b/types/postcss-reporter/postcss-reporter-tests.ts @@ -0,0 +1,46 @@ +/// + +import reporter = require('postcss-reporter'); +import formatter = require('postcss-reporter/lib/formatter'); +import { ResultMessage } from 'postcss'; +reporter({ + formatter: input => { + return `${input.source} produced ${input.messages.length} messages`; + }, +}); + +const basicMessages = [ + { + type: 'warning', + plugin: 'foo', + text: 'foo warning', + }, + { + type: 'warning', + plugin: 'bar', + text: 'bar warning', + }, + { + type: 'warning', + plugin: 'baz', + text: 'baz warning', + }, + { + type: 'error', + plugin: 'baz', + text: 'baz error', + }, +]; + +const myFormatter = formatter({ + noIcon: true, + noPlugin: true, +}); +// Defaults +myFormatter(); + +const warningLog = myFormatter({ + messages: basicMessages, + source: 'someSource', +}); +console.log(warningLog); diff --git a/types/postcss-reporter/tsconfig.json b/types/postcss-reporter/tsconfig.json new file mode 100644 index 0000000000..a2992e1a47 --- /dev/null +++ b/types/postcss-reporter/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "postcss-reporter-tests.ts" + ] +} diff --git a/types/postcss-reporter/tslint.json b/types/postcss-reporter/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/postcss-reporter/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }