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
Conversation
@@ -1,3 +1,3 @@ | |||
'use strict'; | |||
|
|||
module.exports = require('postcss-safe-parser/lib/safe-parse'); | |||
module.exports = require('postcss-safe-parser'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using internals.
module.exports = { | ||
parse: postcssScss.parse, | ||
stringify: postcssScss.stringify, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better represents custom syntax.
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'); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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. 👍🏼
There was a problem hiding this 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 theoverrides
property array.
And so postcss-less is used on the style.css
file.
|
Closes #4843.
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:
overrides
in config.For example, if we have this config:
And run stylelint like this:
File would be linted with
postcss-safe-parser
. If were removeoverrides
file would be linted withpostcss-scss
. And finally if we remove--custom-syntax
file would be linted withpostcss-sass
.Maybe we need to switch
overrides
and CLI/API order, so CLI/API has highest priority thanoverrides
.