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!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-03-31 19:58:24 +02:00 committed by GitHub
parent e21346be33
commit 55915aa663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 162 additions and 0 deletions

81
types/postcss-reporter/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/peterblazejewicz>
// 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<Options>;
}
declare const postcssReporter: postcssReporter.PostCSSReporter;
export = postcssReporter;

View File

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

View File

@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"postcss": "^7.0.27"
}
}

View File

@ -0,0 +1,46 @@
/// <reference types="node" />
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);

View File

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

View File

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