Skip to content

Commit 1987a5e

Browse files
committed
Added: warning when a import statement has not been closed correctly (6.0.0)
Close #42
1 parent 5e2169d commit 1987a5e

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 6.0.0 - 2015-06-17
2+
3+
- Changed: warnings messages are now using postcss message api (4.1.x)
4+
- Added: warning when a import statement has not been closed correctly
5+
([#42](https://github.com/postcss/postcss-import/issues/42))
6+
17
# 5.2.2 - 2015-04-19
28

39
- Fixed: globbed imports work for module directories ([#37](https://github.com/postcss/postcss-import/pull/37))

index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ var moduleDirectories = [
2020
"node_modules",
2121
]
2222

23+
var warnNodesMessage =
24+
"It looks like you didn't end correctly your @import statement. " +
25+
"Some children nodes are attached to it"
26+
2327
/**
2428
* Inline `@import`ed files
2529
*
@@ -35,7 +39,7 @@ function AtImport(options) {
3539
(options.path || []) // fallback to empty array
3640
)
3741

38-
return function(styles) {
42+
return function(styles, result) {
3943
// auto add from option if possible
4044
if (
4145
!options.from &&
@@ -68,6 +72,7 @@ function AtImport(options) {
6872
var hashFiles = {}
6973

7074
parseStyles(
75+
result,
7176
styles,
7277
options,
7378
insertRules,
@@ -90,7 +95,9 @@ function AtImport(options) {
9095
* @param {Object} styles
9196
* @param {Object} options
9297
*/
93-
function parseStyles(styles,
98+
function parseStyles(
99+
result,
100+
styles,
94101
options,
95102
cb,
96103
importedFiles,
@@ -100,6 +107,9 @@ function parseStyles(styles,
100107
) {
101108
var imports = []
102109
styles.eachAtRule("import", function checkAtRule(atRule) {
110+
if (atRule.nodes) {
111+
result.warn(warnNodesMessage, {node: atRule})
112+
}
103113
if (options.glob && glob.hasMagic(atRule.params)) {
104114
imports = parseGlob(atRule, options, imports)
105115
}
@@ -110,6 +120,7 @@ function parseStyles(styles,
110120
imports.forEach(function(atRule) {
111121
helpers.try(function transformAtImport() {
112122
readAtImport(
123+
result,
113124
atRule,
114125
options,
115126
cb,
@@ -205,6 +216,7 @@ function addIgnoredAtRulesOnTop(styles, ignoredAtRules) {
205216
* @param {Object} options
206217
*/
207218
function readAtImport(
219+
result,
208220
atRule,
209221
options,
210222
cb,
@@ -261,6 +273,7 @@ function readAtImport(
261273
importedFiles[resolvedFilename][media] = true
262274

263275
readImportedContent(
276+
result,
264277
atRule,
265278
parsedAtImport,
266279
clone(options),
@@ -282,7 +295,9 @@ function readAtImport(
282295
* @param {String} resolvedFilename
283296
* @param {Function} cb
284297
*/
285-
function readImportedContent(atRule,
298+
function readImportedContent(
299+
result,
300+
atRule,
286301
parsedAtImport,
287302
options,
288303
resolvedFilename,
@@ -310,7 +325,7 @@ function readImportedContent(atRule,
310325
)
311326

312327
if (fileContent.trim() === "") {
313-
console.log(helpers.message(resolvedFilename + " is empty", atRule.source))
328+
result.warn(resolvedFilename + " is empty", {node: atRule})
314329
detach(atRule)
315330
return
316331
}
@@ -337,6 +352,7 @@ function readImportedContent(atRule,
337352

338353
// recursion: import @import from imported file
339354
parseStyles(
355+
result,
340356
newStyles,
341357
options,
342358
cb,
@@ -506,3 +522,4 @@ module.exports = postcss.plugin(
506522
"postcss-import",
507523
AtImport
508524
)
525+
module.exports.warnNodesMessage = warnNodesMessage

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-import",
3-
"version": "5.2.2",
3+
"version": "6.0.0",
44
"description": "PostCSS plugin to import CSS files",
55
"keywords": [
66
"css",
@@ -24,8 +24,8 @@
2424
"dependencies": {
2525
"clone": "^0.1.17",
2626
"glob": "^5.0.1",
27-
"postcss": "^4.0.2",
2827
"object-assign": "^3.0.0",
28+
"postcss": "^4.1.4",
2929
"postcss-message-helpers": "^2.0.0",
3030
"resolve": "^1.0.0",
3131
"string-hash": "^1.1.0"

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,25 @@ test("@import custom resolve", function(t) {
230230

231231
t.end()
232232
})
233+
234+
test("warn when a import doesn't have ;", function(t) {
235+
t.equal(
236+
postcss()
237+
.use(atImport())
238+
.process("@import url('http://') :root{}")
239+
.warnings()[0].text,
240+
atImport.warnNodesMessage,
241+
"should warn when a user didn't close an import with ;"
242+
)
243+
244+
t.equal(
245+
postcss()
246+
.use(atImport())
247+
.process("@import url('http://');")
248+
.warnings().length,
249+
0,
250+
"should not warn when a user closed an import with ;"
251+
)
252+
253+
t.end()
254+
})

0 commit comments

Comments
 (0)