Skip to content

Commit c50b02c

Browse files
committed
Add prettier-eslint and fix formatting
1 parent b6b50b1 commit c50b02c

16 files changed

+288
-455
lines changed

index.js

Lines changed: 110 additions & 193 deletions
Large diffs are not rendered by default.

lib/join-media.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
module.exports = function(parentMedia, childMedia) {
2-
if (!parentMedia.length && childMedia.length) {
3-
return childMedia
4-
}
5-
if (parentMedia.length && !childMedia.length) {
6-
return parentMedia
7-
}
8-
if (!parentMedia.length && !childMedia.length) {
9-
return []
10-
}
2+
if (!parentMedia.length && childMedia.length) return childMedia
3+
if (parentMedia.length && !childMedia.length) return parentMedia
4+
if (!parentMedia.length && !childMedia.length) return []
115

126
var media = []
137

148
parentMedia.forEach(function(parentItem) {
159
childMedia.forEach(function(childItem) {
16-
if (parentItem !== childItem) {
17-
media.push(parentItem + " and " + childItem)
18-
}
10+
if (parentItem !== childItem) media.push(parentItem + " and " + childItem)
1911
})
2012
})
2113

lib/parse-statements.js

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ var stringify = valueParser.stringify
77
function split(params, start) {
88
var list = []
99
var last = params.reduce(function(item, node, index) {
10-
if (index < start) {
11-
return ""
12-
}
10+
if (index < start) return ""
1311
if (node.type === "div" && node.value === ",") {
1412
list.push(item)
1513
return ""
@@ -27,12 +25,8 @@ module.exports = function(result, styles) {
2725
styles.each(function(node) {
2826
var stmt
2927
if (node.type === "atrule") {
30-
if (node.name === "import") {
31-
stmt = parseImport(result, node)
32-
}
33-
else if (node.name === "media") {
34-
stmt = parseMedia(result, node)
35-
}
28+
if (node.name === "import") stmt = parseImport(result, node)
29+
else if (node.name === "media") stmt = parseMedia(result, node)
3630
}
3731

3832
if (stmt) {
@@ -45,10 +39,7 @@ module.exports = function(result, styles) {
4539
nodes = []
4640
}
4741
statements.push(stmt)
48-
}
49-
else {
50-
nodes.push(node)
51-
}
42+
} else nodes.push(node)
5243
})
5344

5445
if (nodes.length) {
@@ -74,27 +65,23 @@ function parseMedia(result, atRule) {
7465
function parseImport(result, atRule) {
7566
var prev = getPrev(atRule)
7667
if (prev) {
77-
do {
68+
do {
7869
if (
7970
prev.type !== "atrule" ||
80-
prev.name !== "import" &&
81-
prev.name !== "charset"
71+
(prev.name !== "import" && prev.name !== "charset")
8272
) {
8373
return result.warn(
8474
"@import must precede all other statements (besides @charset)",
8575
{ node: atRule }
8676
)
87-
}
88-
else {
89-
prev = getPrev(prev)
90-
}
77+
} else prev = getPrev(prev)
9178
} while (prev)
9279
}
9380

9481
if (atRule.nodes) {
9582
return result.warn(
9683
"It looks like you didn't end your @import statement correctly. " +
97-
"Child nodes are attached to it.",
84+
"Child nodes are attached to it.",
9885
{ node: atRule }
9986
)
10087
}
@@ -106,6 +93,7 @@ function parseImport(result, atRule) {
10693
media: [],
10794
}
10895

96+
// prettier-ignore
10997
if (
11098
!params.length ||
11199
(
@@ -119,26 +107,18 @@ function parseImport(result, atRule) {
119107
!params[0].nodes[0].value
120108
)
121109
) {
122-
return result.warn(
123-
"Unable to find uri in '" + atRule.toString() + "'",
124-
{ node: atRule }
125-
)
110+
return result.warn("Unable to find uri in '" + atRule.toString() + "'", {
111+
node: atRule,
112+
})
126113
}
127114

128-
if (params[0].type === "string") {
129-
stmt.uri = params[0].value
130-
}
131-
else {
132-
stmt.uri = params[0].nodes[0].value
133-
}
115+
if (params[0].type === "string") stmt.uri = params[0].value
116+
else stmt.uri = params[0].nodes[0].value
134117
stmt.fullUri = stringify(params[0])
135118

136119
if (params.length > 2) {
137120
if (params[1].type !== "space") {
138-
return result.warn(
139-
"Invalid import media statement",
140-
{ node: atRule }
141-
)
121+
return result.warn("Invalid import media statement", { node: atRule })
142122
}
143123
stmt.media = split(params, 2)
144124
}

lib/process-content.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ var postcss = require("postcss")
77
// placeholder tooling
88
var sugarss
99

10-
module.exports = function processContent(
11-
result,
12-
content,
13-
filename,
14-
options
15-
) {
10+
module.exports = function processContent(result, content, filename, options) {
1611
var plugins = options.plugins
1712
var ext = path.extname(filename)
1813

@@ -23,12 +18,11 @@ module.exports = function processContent(
2318
if (!sugarss) {
2419
try {
2520
sugarss = require("sugarss")
26-
}
27-
catch (e) {
21+
} catch (e) {
2822
// Ignore
2923
}
3024
}
31-
if (sugarss) return runPostcss(content, filename, plugins, [ sugarss ])
25+
if (sugarss) return runPostcss(content, filename, plugins, [sugarss])
3226
}
3327

3428
// Syntax support:
@@ -44,23 +38,18 @@ module.exports = function processContent(
4438
return runPostcss(content, filename, plugins, parserList)
4539
}
4640

47-
function runPostcss(
48-
content,
49-
filename,
50-
plugins,
51-
parsers,
52-
index
53-
) {
41+
function runPostcss(content, filename, plugins, parsers, index) {
5442
if (!index) index = 0
55-
return postcss(plugins).process(content, {
56-
from: filename,
57-
parser: parsers[index],
58-
})
59-
.catch(function(err) {
60-
// If there's an error, try the next parser
61-
index++
62-
// If there are no parsers left, throw it
63-
if (index === parsers.length) throw err
64-
return runPostcss(content, filename, plugins, parsers, index)
65-
})
43+
return postcss(plugins)
44+
.process(content, {
45+
from: filename,
46+
parser: parsers[index],
47+
})
48+
.catch(function(err) {
49+
// If there's an error, try the next parser
50+
index++
51+
// If there are no parsers left, throw it
52+
if (index === parsers.length) throw err
53+
return runPostcss(content, filename, plugins, parsers, index)
54+
})
6655
}

lib/resolve-id.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// external tooling
22
var resolve = require("resolve")
33

4-
var moduleDirectories = [
5-
"web_modules",
6-
"node_modules",
7-
]
4+
var moduleDirectories = ["web_modules", "node_modules"]
85

96
function resolveModule(id, opts) {
107
return new Promise(function(res, rej) {
@@ -24,32 +21,28 @@ module.exports = function(id, base, options) {
2421
basedir: base,
2522
moduleDirectory: moduleDirectories.concat(options.addModulesDirectories),
2623
paths: paths,
27-
extensions: [ ".css" ],
24+
extensions: [".css"],
2825
packageFilter: function processPackage(pkg) {
29-
if (pkg.style) {
30-
pkg.main = pkg.style
31-
}
32-
else if (!pkg.main || !/\.css$/.test(pkg.main)) {
33-
pkg.main = "index.css"
34-
}
26+
if (pkg.style) pkg.main = pkg.style
27+
else if (!pkg.main || !/\.css$/.test(pkg.main)) pkg.main = "index.css"
3528
return pkg
3629
},
3730
}
3831

3932
return resolveModule("./" + id, resolveOpts)
40-
.catch(function() {
41-
return resolveModule(id, resolveOpts)
42-
})
43-
.catch(function() {
44-
if (paths.indexOf(base) === -1) {
45-
paths.unshift(base)
46-
}
33+
.catch(function() {
34+
return resolveModule(id, resolveOpts)
35+
})
36+
.catch(function() {
37+
if (paths.indexOf(base) === -1) paths.unshift(base)
4738

48-
throw new Error([
49-
"Failed to find '" + id + "'",
50-
"in [ ",
51-
" " + paths.join(",\n "),
52-
"]",
53-
].join("\n "))
54-
})
39+
throw new Error(
40+
[
41+
"Failed to find '" + id + "'",
42+
"in [ ",
43+
" " + paths.join(",\n "),
44+
"]",
45+
].join("\n ")
46+
)
47+
})
5548
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
"eslint-plugin-import": "^2.2.0",
3232
"npmpub": "^3.0.1",
3333
"postcss-scss": "^1.0.0",
34+
"prettier-eslint-cli": "^3.4.3",
3435
"sugarss": "^1.0.0"
3536
},
3637
"scripts": {
37-
"lint": "eslint --fix .",
38+
"lint": "prettier-eslint \"**/*.js\" --write --prettier.semi false --prettier.trailing-comma es5",
3839
"pretest": "npm run lint",
3940
"test": "ava",
4041
"release": "npmpub"

test/custom-resolve.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import atImport from ".."
1313

1414
test.serial("should accept file", t => {
1515
return compareFixtures(t, "custom-resolve-file", {
16-
resolve: () => {
17-
return path.resolve("test/fixtures/imports/custom-resolve-1.css")
18-
},
16+
resolve: () => path.resolve("test/fixtures/imports/custom-resolve-1.css"),
1917
})
2018
})
2119

@@ -53,22 +51,23 @@ test.serial("should accept promised array of files", t => {
5351
})
5452
})
5553

56-
test(
57-
"should apply default resolver when custom doesn't return an absolute path",
58-
function(t) {
59-
return postcss()
60-
.use(atImport({
61-
resolve: path => {
62-
return path.replace("foo", "imports/bar")
63-
},
64-
load: p => {
65-
t.is(p, path.resolve("test/fixtures/imports", "bar.css"))
66-
return "/* comment */"
67-
},
68-
}))
69-
.process(`@import "foo.css";`, { from: "test/fixtures/custom-resolve-file" })
54+
test("should apply default resolver when custom doesn't return an absolute path", function(
55+
t
56+
) {
57+
return postcss()
58+
.use(
59+
atImport({
60+
resolve: path => path.replace("foo", "imports/bar"),
61+
load: p => {
62+
t.is(p, path.resolve("test/fixtures/imports", "bar.css"))
63+
return "/* comment */"
64+
},
65+
})
66+
)
67+
.process(`@import "foo.css";`, {
68+
from: "test/fixtures/custom-resolve-file",
69+
})
7070
.then(result => {
7171
t.is(result.css, "/* comment */")
7272
})
73-
}
74-
)
73+
})

test/custom-syntax-parser.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@ import compareFixtures from "./helpers/compare-fixtures"
88
import compareFixturesExt from "./helpers/compare-fixtures-ext"
99

1010
test("should process custom syntax", t => {
11-
return compareFixtures(t, "scss-syntax", null, {
12-
syntax: scss,
13-
})
11+
return compareFixtures(t, "scss-syntax", null, { syntax: scss })
1412
})
1513

1614
test("should process custom syntax by parser", t => {
17-
return compareFixtures(t, "scss-parser", null, {
18-
parser: scss,
19-
})
15+
return compareFixtures(t, "scss-parser", null, { parser: scss })
2016
})
2117

2218
test(".css importing .sss should work", t => {
2319
return compareFixtures(t, "import-sss")
2420
})
2521

2622
test(".sss importing .sss should work", t => {
27-
return compareFixturesExt(t, "sugar", ".sss", null, {
28-
parser: sugarss,
29-
})
23+
return compareFixturesExt(t, "sugar", ".sss", null, { parser: sugarss })
3024
})
3125

3226
test(".sss importing .css should work", t => {

test/helpers/compare-fixtures-ext.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ module.exports = function(t, name, ext, opts, postcssOpts, warnings) {
2323
// handy thing: checkout actual in the *.actual.css file
2424
fs.writeFile("test/fixtures/" + name + ".actual.css", actual)
2525
t.is(actual, expected)
26-
if (!warnings) {
27-
warnings = []
28-
}
26+
if (!warnings) warnings = []
2927
result.warnings().forEach(function(warning, index) {
3028
t.is(
3129
warning.text,
3230
warnings[index],
33-
"unexpected warning: \"" + warning.text + "\""
31+
'unexpected warning: "' + warning.text + '"'
3432
)
3533
})
3634
})

test/helpers/compare-fixtures.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ module.exports = function(t, name, opts, postcssOpts, warnings) {
2222
// handy thing: checkout actual in the *.actual.css file
2323
fs.writeFile("test/fixtures/" + name + ".actual.css", actual)
2424
t.is(actual, expected)
25-
if (!warnings) {
26-
warnings = []
27-
}
25+
if (!warnings) warnings = []
2826
result.warnings().forEach(function(warning, index) {
2927
t.is(
3028
warning.text,
3129
warnings[index],
32-
"unexpected warning: \"" + warning.text + "\""
30+
'unexpected warning: "' + warning.text + '"'
3331
)
3432
})
3533
})

0 commit comments

Comments
 (0)