Skip to content

Commit c431523

Browse files
committed
Merge branch 'douglasduteil-style-enforce-single-quotes-eslint-rule'
2 parents 83e2deb + 7d0f578 commit c431523

File tree

4 files changed

+61
-57
lines changed

4 files changed

+61
-57
lines changed

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"env": {
33
"node": true
4+
},
5+
"rules": {
6+
"strict": [2, "never"],
7+
"quotes": [2, "single"]
48
}
59
}

index.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ function normalizeNodeArray(nodes) {
1212
array.push(x);
1313
}
1414
});
15-
if(array.length > 0 && array[array.length - 1].type === "spacing") {
15+
if(array.length > 0 && array[array.length - 1].type === 'spacing') {
1616
array.pop();
1717
}
1818
return array;
1919
}
2020

2121
function localizeNode(node, context) {
22-
if(context.ignoreNextSpacing && node.type !== "spacing") {
23-
throw new Error("Missing whitespace after :" + context.ignoreNextSpacing);
22+
if(context.ignoreNextSpacing && node.type !== 'spacing') {
23+
throw new Error('Missing whitespace after :' + context.ignoreNextSpacing);
2424
}
25-
if(context.enforceNoSpacing && node.type === "spacing") {
26-
throw new Error("Missing whitespace before :" + context.enforceNoSpacing);
25+
if(context.enforceNoSpacing && node.type === 'spacing') {
26+
throw new Error('Missing whitespace before :' + context.enforceNoSpacing);
2727
}
2828

2929
var newNodes;
3030
switch(node.type) {
31-
case "selectors":
31+
case 'selectors':
3232
var resultingGlobal;
3333
context.hasPureGlobals = false;
3434
newNodes = node.nodes.map(function(n) {
@@ -39,11 +39,11 @@ function localizeNode(node, context) {
3939
explicit: false
4040
};
4141
n = localizeNode(n, nContext);
42-
if(typeof resultingGlobal === "undefined") {
42+
if(typeof resultingGlobal === 'undefined') {
4343
resultingGlobal = nContext.global;
4444
} else if(resultingGlobal !== nContext.global) {
45-
throw new Error("Inconsistent rule global/local result in rule '" +
46-
Tokenizer.stringify(node) + "' (multiple selectors must result in the same mode for the rule)");
45+
throw new Error('Inconsistent rule global/local result in rule "' +
46+
Tokenizer.stringify(node) + '" (multiple selectors must result in the same mode for the rule)');
4747
}
4848
if(!nContext.hasLocals) {
4949
context.hasPureGlobals = true;
@@ -55,15 +55,15 @@ function localizeNode(node, context) {
5555
node.nodes = normalizeNodeArray(newNodes);
5656
break;
5757

58-
case "selector":
58+
case 'selector':
5959
newNodes = node.nodes.map(function(n) {
6060
return localizeNode(n, context);
6161
});
6262
node = Object.create(node);
6363
node.nodes = normalizeNodeArray(newNodes);
6464
break;
6565

66-
case "spacing":
66+
case 'spacing':
6767
if(context.ignoreNextSpacing) {
6868
context.ignoreNextSpacing = false;
6969
context.lastWasSpacing = false;
@@ -73,27 +73,27 @@ function localizeNode(node, context) {
7373
context.lastWasSpacing = true;
7474
return node;
7575

76-
case "pseudo-class":
77-
if(node.name === "local" || node.name === "global") {
76+
case 'pseudo-class':
77+
if(node.name === 'local' || node.name === 'global') {
7878
if(context.inside) {
79-
throw new Error("A :" + node.name + " is not allowed inside of a :" + context.inside + "(...)");
79+
throw new Error('A :' + node.name + ' is not allowed inside of a :' + context.inside + '(...)');
8080
}
8181
context.ignoreNextSpacing = context.lastWasSpacing ? node.name : false;
8282
context.enforceNoSpacing = context.lastWasSpacing ? false : node.name;
83-
context.global = (node.name === "global");
83+
context.global = (node.name === 'global');
8484
context.explicit = true;
8585
return null;
8686
}
8787
break;
8888

89-
case "nested-pseudo-class":
89+
case 'nested-pseudo-class':
9090
var subContext;
91-
if(node.name === "local" || node.name === "global") {
91+
if(node.name === 'local' || node.name === 'global') {
9292
if(context.inside) {
93-
throw new Error("A :" + node.name + "(...) is not allowed inside of a :" + context.inside + "(...)");
93+
throw new Error('A :' + node.name + '(...) is not allowed inside of a :' + context.inside + '(...)');
9494
}
9595
subContext = {
96-
global: (node.name === "global"),
96+
global: (node.name === 'global'),
9797
inside: node.name,
9898
hasLocals: false,
9999
explicit: true
@@ -123,12 +123,12 @@ function localizeNode(node, context) {
123123
}
124124
break;
125125

126-
case "id":
127-
case "class":
126+
case 'id':
127+
case 'class':
128128
if(!context.global) {
129129
node = {
130-
type: "nested-pseudo-class",
131-
name: "local",
130+
type: 'nested-pseudo-class',
131+
name: 'local',
132132
nodes: [node]
133133
};
134134
context.hasLocals = true;
@@ -146,15 +146,15 @@ function localizeNode(node, context) {
146146
function localizeDeclNode(node, context) {
147147
var newNode;
148148
switch(node.type) {
149-
case "item":
149+
case 'item':
150150
if(context.localizeNextItem) {
151151
newNode = Object.create(node);
152-
newNode.name = ":local(" + newNode.name + ")";
152+
newNode.name = ':local(' + newNode.name + ')';
153153
context.localizeNextItem = false;
154154
return newNode;
155155
}
156156
break;
157-
case "url":
157+
case 'url':
158158
if(context.options && context.options.rewriteUrl) {
159159
newNode = Object.create(node);
160160
newNode.url = context.options.rewriteUrl(context.global, node.url);
@@ -193,30 +193,30 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function (op
193193
options = {}; // If options is undefined or not an object the plugin fails
194194
}
195195
if(options && options.mode) {
196-
if(options.mode !== "global" && options.mode !== "local" && options.mode !== "pure") {
197-
throw new Error("options.mode must be either 'global', 'local' or 'pure' (default 'local')");
196+
if(options.mode !== 'global' && options.mode !== 'local' && options.mode !== 'pure') {
197+
throw new Error('options.mode must be either "global", "local" or "pure" (default "local")');
198198
}
199199
}
200-
var pureMode = options && options.mode === "pure";
201-
var globalMode = options && options.mode === "global";
200+
var pureMode = options && options.mode === 'pure';
201+
var globalMode = options && options.mode === 'global';
202202
return function(css) {
203203
css.walkAtRules(function(atrule) {
204204
if(/keyframes$/.test(atrule.name)) {
205205
var globalMatch = /^\s*:global\s*\((.+)\)\s*$/.exec(atrule.params);
206206
var localMatch = /^\s*:local\s*\((.+)\)\s*$/.exec(atrule.params);
207207
if(globalMatch) {
208208
if(pureMode) {
209-
throw atrule.error("@keyframes :global(...) is not allowed in pure mode");
209+
throw atrule.error('@keyframes :global(...) is not allowed in pure mode');
210210
}
211211
atrule.params = globalMatch[1];
212212
} else if(localMatch) {
213213
atrule.params = localMatch[0];
214214
} else if(!globalMode) {
215-
atrule.params = ":local(" + atrule.params + ")";
215+
atrule.params = ':local(' + atrule.params + ')';
216216
}
217217
} else if(atrule.nodes) {
218218
atrule.nodes.forEach(function(decl) {
219-
if(decl.type === "decl") {
219+
if(decl.type === 'decl') {
220220
localizeDecl(decl, {
221221
options: options,
222222
global: globalMode
@@ -226,7 +226,7 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function (op
226226
}
227227
});
228228
css.walkRules(function(rule) {
229-
if(rule.parent.type === "atrule" && /keyframes$/.test(rule.parent.name)) {
229+
if(rule.parent.type === 'atrule' && /keyframes$/.test(rule.parent.name)) {
230230
// ignore keyframe rules
231231
return;
232232
}
@@ -243,8 +243,8 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function (op
243243
throw rule.error(e.message);
244244
}
245245
if(pureMode && context.hasPureGlobals) {
246-
throw rule.error("Selector '" + Tokenizer.stringify(selector) + "' is not pure " +
247-
"(pure selectors must contain at least one local class or id)");
246+
throw rule.error('Selector "' + Tokenizer.stringify(selector) + '" is not pure ' +
247+
'(pure selectors must contain at least one local class or id)');
248248
}
249249
rule.nodes.forEach(function(decl) {
250250
localizeDecl(decl, context);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"tape": "^4.0.0"
2828
},
2929
"scripts": {
30-
"lint": "eslint index.js",
30+
"lint": "eslint index.js test.js",
3131
"pretest": "npm run lint",
3232
"test": "tape test.js",
3333
"autotest": "chokidar index.js test.js -c 'npm test'",

test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ var tests = [
177177
{
178178
should: 'default to global when mode provided',
179179
input: '.foo {}',
180-
options: { mode: "global" },
180+
options: { mode: 'global' },
181181
expected: '.foo {}'
182182
},
183183
{
184184
should: 'default to local when mode provided',
185185
input: '.foo {}',
186-
options: { mode: "local" },
186+
options: { mode: 'local' },
187187
expected: ':local(.foo) {}'
188188
},
189189
{
@@ -199,8 +199,8 @@ var tests = [
199199
':local( .a ).b {}',
200200
':local(.a) .b {}',
201201
':local( .a ) .b {}'
202-
].join("\n"),
203-
options: { mode: "global" },
202+
].join('\n'),
203+
options: { mode: 'global' },
204204
expected: [
205205
'.a :local(.b) {}',
206206
'.a:local(.b) {}',
@@ -212,7 +212,7 @@ var tests = [
212212
':local(.a).b {}',
213213
':local(.a) .b {}',
214214
':local(.a) .b {}'
215-
].join("\n")
215+
].join('\n')
216216
},
217217
{
218218
should: 'localize keyframes',
@@ -222,7 +222,7 @@ var tests = [
222222
{
223223
should: 'localize keyframes in global default mode',
224224
input: '@keyframes foo {}',
225-
options: { mode: "global" },
225+
options: { mode: 'global' },
226226
expected: '@keyframes foo {}'
227227
},
228228
{
@@ -243,7 +243,7 @@ var tests = [
243243
{
244244
should: 'compile in pure mode',
245245
input: ':global(.foo).bar, [type="radio"] ~ .label, :not(.foo), #bar {}',
246-
options: { mode: "pure" },
246+
options: { mode: 'pure' },
247247
expected: '.foo:local(.bar), [type="radio"] ~ :local(.label), :not(:local(.foo)), :local(#bar) {}'
248248
},
249249
{
@@ -260,8 +260,8 @@ var tests = [
260260
{
261261
should: 'throw on invalid mode',
262262
input: '',
263-
options: { mode: "???" },
264-
error: /'global', 'local' or 'pure'/
263+
options: { mode: '???' },
264+
error: /"global", "local" or "pure"/
265265
},
266266
{
267267
should: 'throw on inconsistent selector result',
@@ -301,37 +301,37 @@ var tests = [
301301
{
302302
should: 'throw on not pure selector (global class)',
303303
input: ':global(.foo) {}',
304-
options: { mode: "pure" },
305-
error: /':global\(\.foo\)' is not pure/
304+
options: { mode: 'pure' },
305+
error: /":global\(\.foo\)" is not pure/
306306
},
307307
{
308308
should: 'throw on not pure selector (with multiple 1)',
309309
input: '.foo, :global(.bar) {}',
310-
options: { mode: "pure" },
311-
error: /'.foo, :global\(\.bar\)' is not pure/
310+
options: { mode: 'pure' },
311+
error: /".foo, :global\(\.bar\)" is not pure/
312312
},
313313
{
314314
should: 'throw on not pure selector (with multiple 2)',
315315
input: ':global(.bar), .foo {}',
316-
options: { mode: "pure" },
317-
error: /':global\(\.bar\), .foo' is not pure/
316+
options: { mode: 'pure' },
317+
error: /":global\(\.bar\), .foo" is not pure/
318318
},
319319
{
320320
should: 'throw on not pure selector (element)',
321321
input: 'input {}',
322-
options: { mode: "pure" },
323-
error: /'input' is not pure/
322+
options: { mode: 'pure' },
323+
error: /"input" is not pure/
324324
},
325325
{
326326
should: 'throw on not pure selector (attribute)',
327327
input: '[type="radio"] {}',
328-
options: { mode: "pure" },
329-
error: /'\[type="radio"\]' is not pure/
328+
options: { mode: 'pure' },
329+
error: /"\[type="radio"\]" is not pure/
330330
},
331331
{
332332
should: 'throw on not pure keyframes',
333333
input: '@keyframes :global(foo) {}',
334-
options: { mode: "pure" },
334+
options: { mode: 'pure' },
335335
error: /@keyframes :global\(\.\.\.\) is not allowed in pure mode/
336336
},
337337
{

0 commit comments

Comments
 (0)