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
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
shawnbot marked this conversation as resolved.
Show resolved Hide resolved
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