-
Notifications
You must be signed in to change notification settings - Fork 115
/
lint.js
287 lines (269 loc) · 8.05 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"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 or empty @layer)",
)
})
})
})
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 or empty @layer)",
)
})
})
})
test("should warn if non-empty @layer before @import", t => {
return processor
.process(`@layer { a {} } @import "a.css";`, { from: undefined })
.then(result => {
t.plan(1)
result.warnings().forEach(warning => {
t.is(
warning.text,
"@import must precede all other statements (besides @charset or empty @layer)",
)
})
})
})
test("should warn when import statements are not consecutive", t => {
return processor
.process(
`
@import "bar.css";
@layer a;
@import "bar.css";
`,
{ from: "test/fixtures/imports/foo.css" },
)
.then(result => {
t.plan(1)
result.warnings().forEach(warning => {
t.is(
warning.text,
"@import must precede all other statements (besides @charset or empty @layer)",
)
})
})
})
test("should not warn if empty @layer before @import", t => {
return processor
.process(`@layer a; @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 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 @charset is not first", t => {
return Promise.all([
processor.process(`a {} @charset "utf-8";`, { from: undefined }),
processor.process(`@media {} @charset "utf-8";`, { from: undefined }),
processor.process(`/* foo */ @charset "utf-8";`, { from: undefined }),
processor.process(`@import "bar.css"; @charset "utf-8";`, {
from: "test/fixtures/imports/foo.css",
}),
]).then(results => {
results.forEach(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(warnings[0].text, "@charset must precede all other statements")
})
})
})
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("");
@import layer url("");
@import supports(foo: bar) url("");
`,
{ from: undefined },
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 9)
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("")'`)
t.is(warnings[7].text, `Unable to find uri in '@import layer url("")'`)
t.is(
warnings[8].text,
`Unable to find uri in '@import supports(foo: bar) url("")'`,
)
})
})
test("should warn on duplicate url's", t => {
return processor
.process(
`
@import 'foo' "bar";
@import "foo" url(bar);
@import url(foo) "bar";
@import url('foo') url(bar);
@import url('foo') layer url(bar);
@import url('foo') layer(foo) "bar";
@import url('foo') supports(foo: bar) url(bar);
`,
{ from: undefined },
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 7)
t.is(warnings[0].text, `Multiple url's in '@import 'foo' "bar"'`)
t.is(warnings[1].text, `Multiple url's in '@import "foo" url(bar)'`)
t.is(warnings[2].text, `Multiple url's in '@import url(foo) "bar"'`)
t.is(warnings[3].text, `Multiple url's in '@import url('foo') url(bar)'`)
t.is(
warnings[4].text,
`Multiple url's in '@import url('foo') layer url(bar)'`,
)
t.is(
warnings[5].text,
`Multiple url's in '@import url('foo') layer(foo) "bar"'`,
)
t.is(
warnings[6].text,
`Multiple url's in '@import url('foo') supports(foo: bar) url(bar)'`,
)
})
})
test("should warn on multiple layer clauses", t => {
return processor
.process(
`
@import url('foo') layer layer(bar);
`,
{ from: undefined },
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(
warnings[0].text,
`Multiple layers in '@import url('foo') layer layer(bar)'`,
)
})
})
test("should warn on when support conditions precede layer clauses", t => {
return processor
.process(
`
@import url('foo') supports(selector(&)) layer(bar);
`,
{ from: undefined },
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(
warnings[0].text,
`layers must be defined before support conditions in '@import url('foo') supports(selector(&)) layer(bar)'`,
)
})
})
test("should warn on multiple support conditions", t => {
return processor
.process(
`
@import url('foo') supports(selector(&)) supports((display: grid));
`,
{ from: undefined },
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 1)
t.is(
warnings[0].text,
`Multiple support conditions in '@import url('foo') supports(selector(&)) supports((display: grid))'`,
)
})
})
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)
})
})