forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(theming): fix broken sass expressions nested in theme classes
* Fixes certain theme selectors being broken due to uses of the `&` operator at the end of the selector. * Adds a custom Stylelint rule to catch future improper uses of the ampersand inside themes. Relates to angular#3928. Fixes angular#4077.
- Loading branch information
Showing
7 changed files
with
101 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const stylelint = require('stylelint'); | ||
const path = require('path'); | ||
const isStandardSyntaxRule = require('stylelint/lib/utils/isStandardSyntaxRule'); | ||
const isStandardSyntaxSelector = require('stylelint/lib/utils/isStandardSyntaxSelector'); | ||
|
||
const ruleName = 'material/selector-nested-pattern-scoped'; | ||
const messages = stylelint.utils.ruleMessages(ruleName, { | ||
expected: selector => `Expected nested selector '${selector}' to match specified pattern`, | ||
}); | ||
|
||
/** | ||
* Re-implementation of the `selector-nested-pattern` Stylelint rule, allowing us | ||
* to scope it to a particular set of files via the custom `filePattern` option. The | ||
* primary use-case is to be able to apply the rule only to theme files. | ||
* | ||
* Reference: https://stylelint.io/user-guide/rules/selector-nested-pattern/ | ||
* Source: https://github.com/stylelint/stylelint/blob/master/lib/rules/selector-nested-pattern/ | ||
*/ | ||
const plugin = stylelint.createPlugin(ruleName, (pattern, options) => { | ||
return (root, result) => { | ||
const selectorPattern = new RegExp(pattern); | ||
const filePattern = new RegExp(options.filePattern); | ||
const fileName = path.basename(root.source.input.file); | ||
|
||
if (!filePattern.test(fileName)) return; | ||
|
||
root.walkRules(rule => { | ||
if (rule.parent.type === 'rule' && | ||
isStandardSyntaxRule(rule) && | ||
isStandardSyntaxSelector(rule.selector) && | ||
!selectorPattern.test(rule.selector)) { | ||
|
||
stylelint.utils.report({ | ||
result, | ||
ruleName, | ||
message: messages.expected(rule.selector), | ||
node: rule | ||
}); | ||
} | ||
}); | ||
}; | ||
}); | ||
|
||
plugin.ruleName = ruleName; | ||
plugin.messages = messages; | ||
module.exports = plugin; |