Skip to content

Commit

Permalink
Warn about all @imports after other CSS declarations
Browse files Browse the repository at this point in the history
Fixes #176
  • Loading branch information
RyanZim committed Nov 8, 2016
1 parent 95aa38d commit b64342e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/parse-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,23 @@ function parseMedia(result, atRule) {
}

function parseImport(result, atRule) {
var prev = atRule.prev()
while (prev && prev.type === "comment") {
prev = prev.prev()
}
var prev = getPrev(atRule)
if (prev) {
if (
prev.type !== "atrule" ||
prev.name !== "import" &&
prev.name !== "charset"
) {
return result.warn(
"@import must precede all other statements (besides @charset)",
{ node: atRule }
)
}
do {
if (
prev.type !== "atrule" ||
prev.name !== "import" &&
prev.name !== "charset"
) {
return result.warn(
"@import must precede all other statements (besides @charset)",
{ node: atRule }
)
}
else {
prev = getPrev(prev)
}
} while (prev)
}

if (atRule.nodes) {
Expand Down Expand Up @@ -140,3 +142,11 @@ function parseImport(result, atRule) {

return stmt
}

function getPrev(item) {
var prev = item.prev()
while (prev && prev.type === "comment") {
prev = prev.prev()
}
return prev
}
17 changes: 17 additions & 0 deletions test/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ test("should warn when not @charset and not @import statement before", t => {
})
})

test("should warn about all imports after some other CSS declaration", t => {
return processor.process(
`a {}
@import "a.css";
@import "b.css";`
)
.then(function(result) {
t.plan(2)
result.warnings().forEach(function(warning) {
t.is(
warning.text,
"@import must precede all other statements (besides @charset)"
)
})
})
})

test("should not warn if comments before @import", t => {
return processor.process(`/* skipped comment */ @import "";`)
.then(function(result) {
Expand Down

0 comments on commit b64342e

Please sign in to comment.