Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type Formatter for formatter functions #4823

Merged
merged 2 commits into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion lib/standalone.js
Expand Up @@ -25,6 +25,8 @@ 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 */
/** @typedef {import('stylelint').FormatterIdentifier} FormatterIdentifier */
m-allanson marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {StylelintOptions} options
Expand Down Expand Up @@ -77,7 +79,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,
Copy link
Member Author

@m-allanson m-allanson Jun 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second value returnValue isn't used in any of stylelint's built-in formatters (as far as I can tell).

However, it is mentioned in the docs, and there's some tests that ensure the value is passed through to the formatter.

So I've included it in the type definition here. But maybe it's an old argument that can be removed?

) => 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