Skip to content

Commit

Permalink
Add type Formatter for formatter functions (#4823)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-allanson committed Jun 9, 2020
1 parent 769f917 commit 5b6b996
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
7 changes: 5 additions & 2 deletions docs/developer-guide/formatters.md
Expand Up @@ -3,12 +3,15 @@
A formatter is a function with the following signature:

```js
/**
* @type {import('stylelint').Formatter}
*/
function formatter(results, returnValue) {
return "a string of formatted results";
}
```

Where the first argument (`results`) is an array of stylelint result objects in the form:
Where the first argument (`results`) is an array of stylelint result objects (type `Array<StylelintResult>`) in the form:

```js
// A stylelint result object
Expand Down Expand Up @@ -42,7 +45,7 @@ Where the first argument (`results`) is an array of stylelint result objects in
}
```

And the second argument (`returnValue`) is an object with one or more of the following keys:
And the second argument (`returnValue`) is an object (type `StylelintStandaloneReturnValue`) with one or more of the following keys:

```js
{
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters/compactFormatter.js
Expand Up @@ -3,8 +3,7 @@
const _ = require('lodash');

/**
* @param {import('stylelint').StylelintResult[]} results
* @returns {string}
* @type {import('stylelint').Formatter}
*/
const formatter = (results) =>
_.flatMap(results, (result) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters/jsonFormatter.js
Expand Up @@ -3,8 +3,7 @@
/**
* Omit any properties starting with `_`, which are fake-private
*
* @param {import('stylelint').StylelintResult[]} results
* @returns {string}
* @type {import('stylelint').Formatter}
*/
module.exports = function jsonFormatter(results) {
const cleanedResults = results.map((result) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters/stringFormatter.js
Expand Up @@ -189,8 +189,7 @@ function formatter(messages, source) {
}

/**
* @param {import('stylelint').StylelintResult[]} results
* @returns {string}
* @type {import('stylelint').Formatter}
*/
module.exports = function (results) {
let output = invalidOptionsFormatter(results);
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters/unixFormatter.js
Expand Up @@ -3,8 +3,7 @@
const _ = require('lodash');

/**
* @param {import('stylelint').StylelintResult[]} results
* @returns {string}
* @type {import('stylelint').Formatter}
*/
const unixFormatter = (results) => {
const lines = _.flatMap(results, (result) =>
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters/verboseFormatter.js
Expand Up @@ -5,8 +5,7 @@ const chalk = require('chalk');
const stringFormatter = require('./stringFormatter');

/**
* @param {import('stylelint').StylelintResult[]} results
* @returns {string}
* @type {import('stylelint').Formatter}
*/
module.exports = function (results) {
let output = stringFormatter(results);
Expand Down
3 changes: 2 additions & 1 deletion lib/standalone.js
Expand Up @@ -25,6 +25,7 @@ const writeFileAtomic = require('write-file-atomic');
/** @typedef {import('stylelint').StylelintStandaloneOptions} StylelintOptions */
/** @typedef {import('stylelint').StylelintStandaloneReturnValue} StylelintStandaloneReturnValue */
/** @typedef {import('stylelint').StylelintResult} StylelintResult */
/** @typedef {import('stylelint').Formatter} Formatter */

/**
* @param {StylelintOptions} options
Expand Down Expand Up @@ -77,7 +78,7 @@ module.exports = function (options) {
throw new Error('You must pass stylelint a `files` glob or a `code` string, though not both');
}

/** @type {Function} */
/** @type {Formatter} */
let formatterFunction;

if (typeof formatter === 'string') {
Expand Down
11 changes: 9 additions & 2 deletions types/stylelint/index.d.ts
Expand Up @@ -83,6 +83,13 @@ declare module 'stylelint' {
warn(message: string, options?: StylelintWarningOptions): void;
};

export type Formatter = (
results: Array<StylelintResult>,
returnValue?: StylelintStandaloneReturnValue,
) => string;

export type FormatterIdentifier = 'compact' | 'json' | 'string' | 'unix' | 'verbose' | Formatter;

export type StylelintOptions = {
config?: StylelintConfig;
configFile?: string;
Expand Down Expand Up @@ -152,7 +159,7 @@ declare module 'stylelint' {
maxWarnings?: number;
syntax?: string;
customSyntax?: string;
formatter?: 'compact' | 'json' | 'string' | 'unix' | 'verbose' | Function;
formatter?: FormatterIdentifier;
disableDefaultIgnores?: boolean;
fix?: boolean;
allowEmptyInput?: boolean;
Expand Down Expand Up @@ -231,7 +238,7 @@ declare module 'stylelint' {
export type StylelintPublicAPI = {
lint: Function;
rules: { [k: string]: any };
formatters: { [k: string]: Function };
formatters: { [k: string]: Formatter };
createPlugin: Function;
createLinter: Function;
utils: {
Expand Down

0 comments on commit 5b6b996

Please sign in to comment.