forked from postcss/postcss-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.js
128 lines (118 loc) · 3.53 KB
/
lint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict"
// external tooling
const test = require("ava")
const postcss = require("postcss")
// plugin
const atImport = require("..")
const processor = postcss().use(atImport())
test("should warn when not @charset and not @import statement before", t => {
return Promise.all([
processor.process(`a {} @import "";`, { from: undefined }),
processor.process(`@media {} @import "";`, { from: undefined }),
]).then(results => {
results.forEach(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(
warnings[0].text,
"@import must precede all other statements (besides @charset)"
)
})
})
})
test("should warn about all imports after some other CSS declaration", t => {
return processor
.process(
`
a {}
@import "a.css";
@import "b.css";
`,
{ from: undefined }
)
.then(result => {
t.plan(2)
result.warnings().forEach(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 "";`, { from: undefined })
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(warnings[0].text, `Unable to find uri in '@import ""'`)
})
})
test("should warn if something before comments", t => {
return processor
.process(`a{} /* skipped comment */ @import "";`, { from: undefined })
.then(result => {
t.is(result.warnings().length, 1)
})
})
test("should not warn when @charset or @import statement before", t => {
return Promise.all([
processor.process(`@import "bar.css"; @import "bar.css";`, {
from: "test/fixtures/imports/foo.css",
}),
processor.process(`@charset "bar.css"; @import "bar.css";`, {
from: "test/fixtures/imports/foo.css",
}),
]).then(results => {
results.forEach(result => {
t.is(result.warnings().length, 0)
})
})
})
test("should warn when a user didn't close an import with ;", t => {
return processor
.process(`@import url('http://') :root{}`, { from: undefined })
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(
warnings[0].text,
"It looks like you didn't end your @import statement correctly. " +
"Child nodes are attached to it."
)
})
})
test("should warn on invalid url", t => {
return processor
.process(
`
@import foo-bar;
@import ;
@import '';
@import "";
@import url();
@import url('');
@import url("");
`,
{ from: undefined }
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 7)
t.is(warnings[0].text, `Unable to find uri in '@import foo-bar'`)
t.is(warnings[1].text, `Unable to find uri in '@import '`)
t.is(warnings[2].text, `Unable to find uri in '@import '''`)
t.is(warnings[3].text, `Unable to find uri in '@import ""'`)
t.is(warnings[4].text, `Unable to find uri in '@import url()'`)
t.is(warnings[5].text, `Unable to find uri in '@import url('')'`)
t.is(warnings[6].text, `Unable to find uri in '@import url("")'`)
})
})
test("should not warn when a user closed an import with ;", t => {
return processor
.process(`@import url('http://');`, { from: undefined })
.then(result => {
t.is(result.warnings().length, 0)
})
})