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

Pass returnValue to formatter with needlessDisables + invalidScopeDisables #4393

Merged
merged 8 commits into from Nov 5, 2019
44 changes: 43 additions & 1 deletion docs/developer-guide/formatters.md
@@ -1,6 +1,14 @@
# Writing formatters

A formatter is a function that accepts *an array of these stylelint result objects* and outputs a string:
A formatter is a function with the following signature:

```js
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:

```js
// A stylelint result object
Expand Down Expand Up @@ -32,6 +40,40 @@ A formatter is a function that accepts *an array of these stylelint result objec
}
```

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

```js
{
errored: false, // `true` if there were any warnings with "error" severity
needlessDisables: [ // Present if stylelint was configured with `reportNeedlessDisables: true`
{
source: "path/to/file.css",
ranges: [
{
start: 10,
unusedRule: "indentation"
}
]
}
],
invalidScopeDisables: [ // Present if stylelint was configured with `reportInvalidScopeDisables: true`
{
source: "path/to/file.css",
ranges: [
{
start: 1,
unusedRule: "color-named"
}
]
}
],
maxWarningsExceeded: { // Present if stylelint was configured with a `maxWarnings` count
maxWarnings: 10,
foundWarnings: 15
}
}
```

## `stylelint.formatters`

stylelint's internal formatters are exposed publicly in `stylelint.formatters`.
50 changes: 50 additions & 0 deletions lib/__tests__/standalone-formatter.test.js
Expand Up @@ -50,3 +50,53 @@ it('standalone with invalid formatter option', () => {
);
});
});

it('standalone formatter receives {needlessDisables} as second argument', () => {
const formatter = jest.fn((results, { needlessDisables }) => {
return JSON.stringify({ results, needlessDisables }, null, 2);
});

return standalone({
code: `
/* stylelint-disable yo */
a {}
`,
config: configBlockNoEmpty,
reportNeedlessDisables: true,
formatter,
}).then((linted) => {
const { output, results, needlessDisables } = linted;

expect(needlessDisables).not.toBeUndefined();
expect(needlessDisables).toHaveLength(1);

expect(typeof output).toBe('string');
expect(formatter).toHaveBeenCalledTimes(1);
expect(output).toBe(JSON.stringify({ results, needlessDisables }, null, 2));
});
});

it('standalone formatter receives {invalidScopeDisables} as second argument', () => {
const formatter = jest.fn((results, { invalidScopeDisables }) => {
return JSON.stringify({ results, invalidScopeDisables }, null, 2);
});

return standalone({
code: `
/* stylelint-disable indentation */
a { color: red; }
`,
config: configBlockNoEmpty,
reportInvalidScopeDisables: true,
formatter,
}).then((linted) => {
const { output, results, invalidScopeDisables } = linted;

expect(invalidScopeDisables).not.toBeUndefined();
expect(invalidScopeDisables).toHaveLength(1);

expect(typeof output).toBe('string');
expect(formatter).toHaveBeenCalledTimes(1);
expect(output).toBe(JSON.stringify({ results, invalidScopeDisables }, null, 2));
});
});
8 changes: 6 additions & 2 deletions lib/standalone.js
Expand Up @@ -293,11 +293,12 @@ module.exports = function(
const errored = stylelintResults.some(
(result) => result.errored || result.parseErrors.length > 0,
);

/** @type {StylelintStandaloneReturnValue} */
const returnValue /*: stylelint$standaloneReturnValue*/ = {
errored,
output: formatterFunction(stylelintResults),
results: stylelintResults,
results: [],
output: '',
Copy link
Contributor Author

@shawnbot shawnbot Oct 31, 2019

Choose a reason for hiding this comment

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

Initializing these with empty values appears to have been necessary to appease the lint:types script (tsc). I'm not familiar with TypeScript, but I imagine there's a way to do this in the @type comment instead?

Copy link
Member

Choose a reason for hiding this comment

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

I thinks it is fine to initialize with empty fields

};

if (reportNeedlessDisables) {
Expand All @@ -322,6 +323,9 @@ module.exports = function(
}
}

returnValue.output = formatterFunction(stylelintResults, returnValue);
returnValue.results = stylelintResults;

debug(`Linting complete in ${Date.now() - startTime}ms`);

return returnValue;
Expand Down