Skip to content

Commit ad850ac

Browse files
committed
Throw if contains a class
1 parent 99a5fea commit ad850ac

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

__tests__/processPlugins.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,25 @@ test('when important is a selector it is used to scope utilities instead of addi
960960
`)
961961
})
962962

963+
test('when important contains a class an error is thrown', () => {
964+
expect(() => {
965+
processPlugins(
966+
[
967+
function({ addUtilities }) {
968+
addUtilities({
969+
'.rotate-90': {
970+
transform: 'rotate(90deg)',
971+
},
972+
})
973+
},
974+
],
975+
makeConfig({
976+
important: '#app .project',
977+
})
978+
)
979+
}).toThrow()
980+
})
981+
963982
test('when important is a selector it scopes all selectors in a rule, even though defining utilities like this is stupid', () => {
964983
const { utilities } = processPlugins(
965984
[

src/util/processPlugins.js

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import parseObjectStyles from '../util/parseObjectStyles'
99
import prefixSelector from '../util/prefixSelector'
1010
import wrapWithVariants from '../util/wrapWithVariants'
1111
import increaseSpecificity from '../util/increaseSpecificity'
12+
import selectorParser from 'postcss-selector-parser'
1213

1314
function parseStyles(styles) {
1415
if (!Array.isArray(styles)) {
@@ -18,6 +19,14 @@ function parseStyles(styles) {
1819
return _.flatMap(styles, style => (style instanceof Node ? style : parseObjectStyles(style)))
1920
}
2021

22+
function containsClass(value) {
23+
return selectorParser(selectors => {
24+
let classFound = false
25+
selectors.walkClasses(() => (classFound = true))
26+
return classFound
27+
}).transformSync(value)
28+
}
29+
2130
export default function(plugins, config) {
2231
const pluginBaseStyles = []
2332
const pluginComponents = []
@@ -86,6 +95,12 @@ export default function(plugins, config) {
8695
if (config.important === true) {
8796
rule.walkDecls(decl => (decl.important = true))
8897
} else if (typeof config.important === 'string') {
98+
if (containsClass(config.important)) {
99+
throw rule.error(
100+
`Classes are not allowed when using the \`important\` option with a string argument. Please use an ID instead.`
101+
)
102+
}
103+
89104
rule.selectors = rule.selectors.map(selector => {
90105
return increaseSpecificity(config.important, selector)
91106
})

0 commit comments

Comments
 (0)