Skip to content

Commit 0b9216f

Browse files
committed
Hide global leak detection behind 'lint' option
1 parent 77a5950 commit 0b9216f

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][unreleased]
66
### Changed
7-
- Nothing yet.
7+
- Hide global leak detection behind undocumented `lint` option until it's more robust.
88

99
## [0.0.3] - 2015-05-22
1010
### Changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ var postcss = require('postcss');
22

33
var ESCAPED_DOT = ' ___LOCAL_SCOPE__ESCAPED_DOT___ ';
44

5-
module.exports = postcss.plugin('postcss-local-scope', function () {
5+
module.exports = postcss.plugin('postcss-local-scope', function (config) {
6+
var options = config || {};
67
return function(css, result) {
78
css.eachRule(function(rule) {
89
rule.selector = rule.selector
910
.split(',')
10-
.map(transformSelector.bind(null, rule))
11+
.map(transformSelector.bind(null, options, rule))
1112
.join(',');
1213
});
1314
};
1415
});
1516

16-
function transformSelector(rule, selector) {
17-
// Syntax warnings
17+
function transformSelector(options, rule, selector) {
1818
var trimmedSelector = selector.trim();
19-
if (!/^\./.test(trimmedSelector) && !/^\:local/.test(trimmedSelector)) {
20-
if (!/^\:global/.test(trimmedSelector)) {
21-
throw rule.error('Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global '+trimmedSelector+'"', { plugin: 'postcss-local-scope' });
19+
20+
if (options.lint) {
21+
if (!/^\./.test(trimmedSelector) && !/^\:local/.test(trimmedSelector)) {
22+
if (!/^\:global/.test(trimmedSelector)) {
23+
throw rule.error('Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global '+trimmedSelector+'"', { plugin: 'postcss-local-scope' });
24+
}
2225
}
2326
}
2427

test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ var tests = [
108108
should: 'convert psuedo elements that are already local using the old syntax into the new local syntax',
109109
input: '.local[foo]:after {}',
110110
expected: ':local(.foo):after {}'
111-
}
111+
},
112+
{
113+
should: 'not reject non-global element selectors when lint mode is not enabled',
114+
input: 'input {}',
115+
expected: 'input {}'
116+
},
112117
];
113118

114119
function process (css, options) {
@@ -127,32 +132,32 @@ test(name, function (t) {
127132

128133
var errorTests = [
129134
{
130-
should: 'reject naked element selectors',
135+
should: 'reject non-global element selectors',
131136
input: 'input {}',
132137
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global input"'
133138
},
134139
{
135-
should: 'reject naked element selectors in a collection',
140+
should: 'reject non-global element selectors in a collection',
136141
input: '.foo, input {}',
137142
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global input"'
138143
},
139144
{
140-
should: 'reject naked psuedo classes',
145+
should: 'reject non-global psuedo classes',
141146
input: ':focus {}',
142147
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global :focus"'
143148
},
144149
{
145-
should: 'reject naked psuedo classes in a collection',
150+
should: 'reject non-global psuedo classes in a collection',
146151
input: '.foo, :focus {}',
147152
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global :focus"'
148153
},
149154
{
150-
should: 'reject naked attribute selectors',
155+
should: 'reject non-global attribute selectors',
151156
input: '[data-foobar] {}',
152157
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global [data-foobar]"'
153158
},
154159
{
155-
should: 'reject naked attribute selectors in a collection',
160+
should: 'reject non-global attribute selectors in a collection',
156161
input: '.foo, [data-foobar] {}',
157162
reason: 'Global selector detected in local context. Does this selector really need to be global? If so, you need to explicitly export it into the global scope with ":global", e.g. ":global [data-foobar]"'
158163
}
@@ -170,7 +175,7 @@ test(name, function (t) {
170175
t.plan(errorTests.length);
171176

172177
errorTests.forEach(function (test) {
173-
var options = test.options || {};
178+
var options = { lint: true };
174179
var error = processError(test.input, options);
175180
t.equal(error.reason, test.reason, 'should ' + test.should);
176181
});

0 commit comments

Comments
 (0)