Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about all @imports after other CSS declarations #240

Merged
merged 1 commit into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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