Skip to content
This repository was archived by the owner on Feb 9, 2023. It is now read-only.

Commit 01cd00b

Browse files
authored
Merge pull request #139 from chuuddo/fix-eslint
Turn on standard ESLint rules and fix violations
2 parents aa66d1e + 203fede commit 01cd00b

13 files changed

+81
-104
lines changed

camel-case.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

33
function camelCase(str) {
4-
return str.replace(/[\w-]+/g, (s) =>
5-
/^-?[a-z]+(?:-[a-z]+)+$/.test(s)
4+
return str.replace(/[\w-]+/g, (s) => {
5+
return /^-?[a-z]+(?:-[a-z]+)+$/.test(s)
66
? s
77
.replace(/^-(ms|moz|khtml|epub|(\w+-?)*webkit)(?=-)/i, '$1')
88
.replace(/-\w/g, (uncasedStr) => uncasedStr[1].toUpperCase())
9-
: s,
10-
);
9+
: s;
10+
});
1111
}
1212

1313
module.exports = camelCase;

extract.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,11 @@ function literalParser(source, opts, styles) {
205205

206206
return path;
207207
}
208+
208209
// If this is not an object but a function returning an object, we want to parse the
209210
// object that is in the body of the function. We will only parse it if the body only
210211
// consist of an object and nothing else.
211-
else if (path.isArrowFunctionExpression()) {
212+
if (path.isArrowFunctionExpression()) {
212213
const body = path.get('body');
213214

214215
if (body) {

object-parser.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class objectParser {
229229
nodes: [],
230230
});
231231

232-
defineRaws(atRule, 'name', key.prefix + '@', params ? '' : key.suffix, {
232+
defineRaws(atRule, 'name', `${key.prefix}@`, params ? '' : key.suffix, {
233233
raw: 'camel',
234234
});
235235

@@ -269,34 +269,34 @@ class objectParser {
269269
raw(atRule);
270270

271271
return atRule;
272-
} else {
273-
let decl;
274-
275-
if (key.raw) {
276-
decl = postcss.decl({
277-
prop: key.value,
278-
value: value.value,
279-
raws: {
280-
prop: key,
281-
},
282-
});
283-
} else {
284-
decl = postcss.decl({
285-
prop: unCamelCase(key.value),
286-
value: value.value,
287-
});
272+
}
288273

289-
defineRaws(decl, 'prop', key.prefix, key.suffix, {
290-
raw: 'camel',
291-
});
292-
}
274+
let decl;
293275

294-
defineRaws(decl, 'value', value.prefix, value.suffix);
295-
raw(decl);
276+
if (key.raw) {
277+
decl = postcss.decl({
278+
prop: key.value,
279+
value: value.value,
280+
raws: {
281+
prop: key,
282+
},
283+
});
284+
} else {
285+
decl = postcss.decl({
286+
prop: unCamelCase(key.value),
287+
value: value.value,
288+
});
296289

297-
return decl;
290+
defineRaws(decl, 'prop', key.prefix, key.suffix, {
291+
raw: 'camel',
292+
});
298293
}
299294

295+
defineRaws(decl, 'value', value.prefix, value.suffix);
296+
raw(decl);
297+
298+
return decl;
299+
300300
function raw(postcssNode) {
301301
postcssNode.raws.between = between;
302302
postcssNode.raws.node = node;

object-stringifier.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ class ObjectStringifier extends Stringifier {
8787
if (node.raws.inline) {
8888
const text = node.raws.text || node.text;
8989

90-
this.builder('//' + left + text + right, node);
90+
this.builder(`//${left}${text}${right}`, node);
9191
} else {
92-
this.builder('/*' + left + node.text + right + '*/', node);
92+
this.builder(`/*${left}${node.text}${right}*/`, node);
9393
}
9494
}
9595
raw(node, own, detect) {
@@ -99,7 +99,7 @@ class ObjectStringifier extends Stringifier {
9999
(own === 'between' || (own === 'afterName' && node.type === 'atrule' && !node.nodes)) &&
100100
!/:/.test(value)
101101
) {
102-
value = ':' + value;
102+
value = `:${value}`;
103103
} else if (own === 'before' && /^(decl|rule)$/.test(node.type)) {
104104
value = value.replace(/\S+$/, '');
105105
}
@@ -133,7 +133,7 @@ class ObjectStringifier extends Stringifier {
133133
}
134134
} else if (node.type === 'atrule') {
135135
if (prop === 'name') {
136-
value = '@' + value;
136+
value = `@${value}`;
137137
} else if (node.nodes) {
138138
return;
139139
}

package.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,7 @@
5151
"eslintConfig": {
5252
"extends": [
5353
"stylelint"
54-
],
55-
"rules": {
56-
"array-callback-return": "off",
57-
"no-confusing-arrow": "off",
58-
"no-else-return": "off",
59-
"prefer--template": "off",
60-
"prefer-object-spread": "off",
61-
"prefer-rest-params": "off",
62-
"prefer-spread": "off",
63-
"prefer-template": "off",
64-
"jest/expect-expect": "off",
65-
"jest/valid-expect": "off"
66-
}
54+
]
6755
},
6856
"remarkConfig": {
6957
"plugins": [

template-parser-helper.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function parseTemplateLiteralStyles(styles, input, range) {
8787
const offset = input.quasis[0].start;
8888
const source = input.css;
8989

90-
const opts = Object.assign({}, input.parseOptions);
90+
const opts = { ...input.parseOptions };
9191

9292
delete opts.templateLiteralStyles;
9393
delete opts.expressions;
@@ -142,6 +142,8 @@ class LocalFixer {
142142

143143
return true;
144144
}
145+
146+
return false;
145147
});
146148

147149
this.line = line;
@@ -172,10 +174,7 @@ class LocalFixer {
172174
if (error && error.name === 'CssSyntaxError') {
173175
this.object(error);
174176
this.object(error.input);
175-
error.message = error.message.replace(
176-
/:\d+:\d+:/,
177-
':' + error.line + ':' + error.column + ':',
178-
);
177+
error.message = error.message.replace(/:\d+:\d+:/, `:${error.line}:${error.column}:`);
179178
}
180179

181180
return error;
@@ -186,21 +185,17 @@ class LocalFixer {
186185
let root = style.root;
187186

188187
try {
189-
root = this.templateParse(
190-
style.content,
191-
Object.assign(
192-
{},
193-
opts,
194-
{
195-
map: false,
196-
},
197-
style.opts,
198-
),
199-
);
188+
root = this.templateParse(style.content, {
189+
...opts,
190+
map: false,
191+
...style.opts,
192+
});
200193
} catch (error) {
201194
if (style.ignoreErrors) {
202195
return;
203-
} else if (!style.skipConvert) {
196+
}
197+
198+
if (!style.skipConvert) {
204199
this.error(error);
205200
}
206201

template-parser.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ class TemplateParser extends Parser {
88
createTokenizer() {
99
this.tokenizer = templateTokenize(this.input);
1010
}
11-
other() {
12-
const args = arguments;
13-
14-
return helper.literal.apply(this, args) || super.other.apply(this, args);
11+
other(start) {
12+
return helper.literal.call(this, start) || super.other.call(this, start);
1513
}
16-
freeSemicolon() {
17-
return helper.freeSemicolon.apply(this, arguments);
14+
freeSemicolon(token) {
15+
return helper.freeSemicolon.call(this, token);
1816
}
1917
}
2018
module.exports = TemplateParser;

template-safe-parser.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ class TemplateSafeParser extends SafeParser {
99
createTokenizer() {
1010
this.tokenizer = templateTokenize(this.input, { ignoreErrors: true });
1111
}
12-
other() {
13-
const args = arguments;
14-
15-
return helper.literal.apply(this, args) || super.other.apply(this, args);
12+
other(start) {
13+
return helper.literal.call(this, start) || super.other.call(this, start);
1614
}
17-
freeSemicolon() {
18-
return helper.freeSemicolon.apply(this, arguments);
15+
freeSemicolon(token) {
16+
return helper.freeSemicolon.call(this, token);
1917
}
2018
}
2119
module.exports = TemplateSafeParser;

template-tokenize.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const tokenize = require('postcss/lib/tokenize');
44

5-
function templateTokenize(input) {
5+
function templateTokenize(input, options = {}) {
66
let pos = input.quasis[0].start;
77
const quasis = input.quasis.filter((quasi) => quasi.start !== quasi.end);
8-
const tokenizer = tokenize.apply(this, arguments);
8+
const tokenizer = tokenize(input, options);
99

1010
function tokenInExpressions(token, returned) {
1111
const start = pos;
@@ -17,28 +17,26 @@ function templateTokenize(input) {
1717
(returned.length && token[0] === returned[0][0])
1818
) {
1919
return true;
20-
} else if (returned.length) {
20+
}
21+
22+
if (returned.length) {
2123
back(token);
2224
}
2325
}
2426

2527
function back(token) {
2628
pos -= token[1].length;
2729

28-
return tokenizer.back.apply(tokenizer, arguments);
30+
return tokenizer.back(token);
2931
}
3032

31-
function nextToken() {
32-
const args = arguments;
33+
function nextToken(opts) {
3334
const returned = [];
3435
let token;
3536
let line;
3637
let column;
3738

38-
while (
39-
(token = tokenizer.nextToken.apply(tokenizer, args)) &&
40-
tokenInExpressions(token, returned)
41-
) {
39+
while ((token = tokenizer.nextToken(opts)) && tokenInExpressions(token, returned)) {
4240
line = token[4] || token[2] || line;
4341
column = token[5] || token[3] || column;
4442
returned.push(token);
@@ -58,10 +56,7 @@ function templateTokenize(input) {
5856
return token;
5957
}
6058

61-
return Object.assign({}, tokenizer, {
62-
back,
63-
nextToken,
64-
});
59+
return { ...tokenizer, back, nextToken };
6560
}
6661

6762
module.exports = templateTokenize;

test/css-in-js.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ describe('CSS in JS', () => {
173173

174174
if (name === 'custom-properties.css') return;
175175

176-
it('objectStringifier ' + name, () => {
176+
it(`objectStringifier ${name}`, () => {
177177
const root = postcss.parse(css);
178178
const jsSource = root.toString(objectStringify).trim();
179-
const jsonSource = '{\n' + jsSource.replace(/,$/, '').replace(/[\s;]+$/gm, '') + '\n}';
179+
const jsonSource = `{\n${jsSource.replace(/,$/, '').replace(/[\s;]+$/gm, '')}\n}`;
180180

181181
expect(JSON5.parse(jsonSource)).toBeTruthy();
182182
});

test/literals.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,18 @@ describe('template literals', () => {
188188
'./fixtures/tpl-selector.mjs',
189189
'./fixtures/tpl-decl.mjs',
190190
'./fixtures/tpl-special.mjs',
191-
].map((file) => {
191+
].forEach((file) => {
192192
it(`${file}`, () => {
193193
file = require.resolve(file);
194194
const code = fs.readFileSync(file);
195195

196-
syntax({
197-
css: 'safe-parser',
198-
}).parse(code, {
199-
from: 'styled-safe-parse.js',
200-
});
196+
expect(() =>
197+
syntax({
198+
css: 'safe-parser',
199+
}).parse(code, {
200+
from: 'styled-safe-parse.js',
201+
}),
202+
).not.toThrow();
201203
});
202204
});
203205
});

test/supports.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('should support for each CSS in JS package', () => {
5757
'material-ui.jsx',
5858
].forEach((file) => {
5959
it(`${file}`, () => {
60-
file = require.resolve('./fixtures/' + file);
60+
file = require.resolve(`./fixtures/${file}`);
6161
const code = fs.readFileSync(file);
6262
const document = syntax.parse(code, {
6363
from: file,
@@ -69,7 +69,7 @@ describe('should support for each CSS in JS package', () => {
6969
const parsed = JSON.stringify(clean(document), 0, '\t');
7070

7171
// fs.writeFileSync(file + ".json", parsed + "\n");
72-
expect(parsed).toBe(fs.readFileSync(file + '.json', 'utf8').trim());
72+
expect(parsed).toBe(fs.readFileSync(`${file}.json`, 'utf8').trim());
7373
});
7474
});
7575
});

un-camel-case.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

33
function unCamelCase(str) {
4-
return str.replace(/[\w-]+/g, (s) =>
5-
/^[A-Z]?[a-z]*(?:[A-Z][a-z]*)+$/.test(s)
4+
return str.replace(/[\w-]+/g, (s) => {
5+
return /^[A-Z]?[a-z]*(?:[A-Z][a-z]*)+$/.test(s)
66
? s
7-
.replace(/[A-Z]/g, (casedStr) => '-' + casedStr.toLowerCase())
7+
.replace(/[A-Z]/g, (casedStr) => `-${casedStr.toLowerCase()}`)
88
.replace(/^(o|ms|moz|khtml|epub|(\w+-?)*webkit)(?=-)/i, '-$1')
9-
: s,
10-
);
9+
: s;
10+
});
1111
}
1212

1313
module.exports = unCamelCase;

0 commit comments

Comments
 (0)