Skip to content

Commit cf61eaf

Browse files
authored
Warn about all @imports after other CSS declarations (#240)
Fixes #176
1 parent 95aa38d commit cf61eaf

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

lib/parse-statements.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,23 @@ function parseMedia(result, atRule) {
6969
}
7070

7171
function parseImport(result, atRule) {
72-
var prev = atRule.prev()
73-
while (prev && prev.type === "comment") {
74-
prev = prev.prev()
75-
}
72+
var prev = getPrev(atRule)
7673
if (prev) {
77-
if (
78-
prev.type !== "atrule" ||
79-
prev.name !== "import" &&
80-
prev.name !== "charset"
81-
) {
82-
return result.warn(
83-
"@import must precede all other statements (besides @charset)",
84-
{ node: atRule }
85-
)
86-
}
74+
do {
75+
if (
76+
prev.type !== "atrule" ||
77+
prev.name !== "import" &&
78+
prev.name !== "charset"
79+
) {
80+
return result.warn(
81+
"@import must precede all other statements (besides @charset)",
82+
{ node: atRule }
83+
)
84+
}
85+
else {
86+
prev = getPrev(prev)
87+
}
88+
} while (prev)
8789
}
8890

8991
if (atRule.nodes) {
@@ -140,3 +142,11 @@ function parseImport(result, atRule) {
140142

141143
return stmt
142144
}
145+
146+
function getPrev(item) {
147+
var prev = item.prev()
148+
while (prev && prev.type === "comment") {
149+
prev = prev.prev()
150+
}
151+
return prev
152+
}

test/lint.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ test("should warn when not @charset and not @import statement before", t => {
2121
})
2222
})
2323

24+
test("should warn about all imports after some other CSS declaration", t => {
25+
return processor.process(
26+
`a {}
27+
@import "a.css";
28+
@import "b.css";`
29+
)
30+
.then(function(result) {
31+
t.plan(2)
32+
result.warnings().forEach(function(warning) {
33+
t.is(
34+
warning.text,
35+
"@import must precede all other statements (besides @charset)"
36+
)
37+
})
38+
})
39+
})
40+
2441
test("should not warn if comments before @import", t => {
2542
return processor.process(`/* skipped comment */ @import "";`)
2643
.then(function(result) {

0 commit comments

Comments
 (0)