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 customSyntax option to configuration object (#4843) #5538

Merged
merged 1 commit into from Sep 15, 2021

Conversation

hudochenkov
Copy link
Member

Which issue, if any, is this issue related to?

Closes #4843.

Is there anything in the PR that needs further explanation?

It was easier than I though :) Two previous PRs I made recently definitely helped :)


Something that we might need to document somewhere or changes. Probably I had to write this in overrides PR.

It is true for v13 (except overrides) and for all options which could be specified in config, API, and CLI.

Priority of options:

  1. overrides in config.
  2. Option specified in CLI/API.
  3. Option in config.

For example, if we have this config:

{
	"customSyntax": "postcss-sass",
	"rules": {
		"block-no-empty": true
	},
	"overrides": [{
		"files": "*.css",
		"customSyntax": "postcss-safe-parser"
	}]
}

And run stylelint like this:

stylelint --custom-syntax "postcss-scss" "style.css"

File would be linted with postcss-safe-parser. If were remove overrides file would be linted with postcss-scss. And finally if we remove --custom-syntax file would be linted with postcss-sass.

Maybe we need to switch overrides and CLI/API order, so CLI/API has highest priority than overrides.

@@ -1,3 +1,3 @@
'use strict';

module.exports = require('postcss-safe-parser/lib/safe-parse');
module.exports = require('postcss-safe-parser');
Copy link
Member Author

Choose a reason for hiding this comment

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

Avoid using internals.

Comment on lines +5 to +8
module.exports = {
parse: postcssScss.parse,
stringify: postcssScss.stringify,
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Better represents custom syntax.

Comment on lines +100 to +144
it('standalone with custom syntax as npm package name', () => {
const config = {
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
customSyntax: 'postcss-scss',
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('standalone with custom syntax as npm package', () => {
const config = {
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
customSyntax: require('postcss-scss'),
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});
Copy link
Member Author

Choose a reason for hiding this comment

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

We never had these tests, but should have, as it's a documented behavior.

Copy link
Member

@ybiquitous ybiquitous left a comment

Choose a reason for hiding this comment

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

The code changes and "Priority of options" look good to me. 👍🏼

Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

Excellent stuff, thanks!

Maybe we need to switch overrides and CLI/API order, so CLI/API has highest priority than overrides

It's a good point. I think the current order is OK as I see the CLI flags as setting their respective properties in the config file. For example, a hypothetical overrides flag would work like...

Given:

{
	"customSyntax": "postcss-sass",
	"rules": {
		"block-no-empty": true
	},
	"overrides": [{
		"files": "*.css",
		"customSyntax": "postcss-safe-parser"
	}]
}

And run stylelint like this:

stylelint --custom-syntax "postcss-scss" --overrides "files: ["*.css"], customSyntax: "postcss-less" "style.css"

The executed config would be:

{
	"customSyntax": "postcss-scss",
	"rules": {
		"block-no-empty": true
	},
	"overrides": [{
		"files": "*.css",
		"customSyntax": "postcss-safe-parser"
	},
        {
		"files": "*.css",
		"customSyntax": "postcss-less"
	}]
}

Where:

  • --custom-syntax "postcss-scss" sets the root "customSyntax" property
  • --overrides "files: ["*.css"], customSyntax: "postcss-less" adds an item to the overrides property array.

And so postcss-less is used on the style.css file.

@jeddy3 jeddy3 merged commit 6acf73f into v14 Sep 15, 2021
@jeddy3 jeddy3 deleted the add-customsyntax-to-config branch September 15, 2021 17:32
@jeddy3
Copy link
Member

jeddy3 commented Sep 15, 2021

  • Added: customSyntax option as a property in the configuration object (#5538).

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.

None yet

3 participants