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

Fix stylelint config usage #4498

Merged
merged 2 commits into from Dec 29, 2019
Merged

Conversation

webschik
Copy link
Contributor

There was a comment in lib/standalone.js:312 that stylelint._options.config might be undefined, but
invalidScopeDisables.js function always expected an object as 2nd parameter.

For me there were 2 ways to fix that:

  • adding a conditional expression to stylelint._options.config || {} for invalidScopeDisables call, or
  • changinginvalidScopeDisables interface to accept undefined as 2nd parameter.

I've chosen 2nd option, to prevent adding more conditions in the future in we need to call invalidScopeDisables somewhere else with the optional config param.

Closes #4479

@hudochenkov
Copy link
Member

I think test should look like this:

it('invalidScopeDisables without config', () => {
	return standalone({
		config: {},
		code: 'a {}',
		ignoreDisables: true,
	}).then((linted) => {
		expect(invalidScopeDisables(linted.results)).toEqual([]);
	});
});

Because we want to test when config is empty, and code doesn't have any stylelint-disable comments. (I'm not sure about expect part).

@webschik
Copy link
Contributor Author

Make sense, but then it should look like

return standalone({
    config: {
        rules: {}
    },
    code: 'a {}',
    ignoreDisables: true,
}).then((linted) => {
    expect(invalidScopeDisables(linted.results)).toEqual([]);
});

because there is a check for config.rules param in lib/augmentConfig.js:88

@@ -6,10 +6,10 @@

/**
* @param {import('stylelint').StylelintResult[]} results
* @param {import('stylelint').StylelintConfig} config
* @param {import('stylelint').StylelintConfig|undefined} config
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param {import('stylelint').StylelintConfig|undefined} config
* @param {import('stylelint').StylelintConfig} config

It works without undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case type annotation in JSDoc is not correct, it means the function accepts config only with StylelintConfig type, but it's not true. I think it's more correct to specify in type that function may accept undefined. Also, we can describe config as optional - https://jsdoc.app/tags-param.html#optional-parameters-and-default-values, but I think it's a worse approach.

Copy link
Member

Choose a reason for hiding this comment

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

By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.

http://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined

While explicitly specifying undefined is correct, it's not necessary.

If you want to keep, it's ok.

@hudochenkov hudochenkov merged commit 0e2439d into stylelint:master Dec 29, 2019
@hudochenkov
Copy link
Member

  • Fixed: --report-invalid-scope-disables crash when no rules specified (#4498).

@webschik webschik deleted the issue-4479 branch December 29, 2019 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

report-invalid-scope-disables Cannot read property 'rules' of undefined
3 participants