Skip to content

Commit

Permalink
Added: warning when a import statement has not been closed correctly …
Browse files Browse the repository at this point in the history
…(6.0.0)

Close #42
  • Loading branch information
MoOx committed Jun 17, 2015
1 parent 5e2169d commit 1987a5e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 6.0.0 - 2015-06-17

- Changed: warnings messages are now using postcss message api (4.1.x)
- Added: warning when a import statement has not been closed correctly
([#42](https://github.com/postcss/postcss-import/issues/42))

# 5.2.2 - 2015-04-19

- Fixed: globbed imports work for module directories ([#37](https://github.com/postcss/postcss-import/pull/37))
Expand Down
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var moduleDirectories = [
"node_modules",
]

var warnNodesMessage =
"It looks like you didn't end correctly your @import statement. " +
"Some children nodes are attached to it"

/**
* Inline `@import`ed files
*
Expand All @@ -35,7 +39,7 @@ function AtImport(options) {
(options.path || []) // fallback to empty array
)

return function(styles) {
return function(styles, result) {
// auto add from option if possible
if (
!options.from &&
Expand Down Expand Up @@ -68,6 +72,7 @@ function AtImport(options) {
var hashFiles = {}

parseStyles(
result,
styles,
options,
insertRules,
Expand All @@ -90,7 +95,9 @@ function AtImport(options) {
* @param {Object} styles
* @param {Object} options
*/
function parseStyles(styles,
function parseStyles(
result,
styles,
options,
cb,
importedFiles,
Expand All @@ -100,6 +107,9 @@ function parseStyles(styles,
) {
var imports = []
styles.eachAtRule("import", function checkAtRule(atRule) {
if (atRule.nodes) {
result.warn(warnNodesMessage, {node: atRule})
}
if (options.glob && glob.hasMagic(atRule.params)) {
imports = parseGlob(atRule, options, imports)
}
Expand All @@ -110,6 +120,7 @@ function parseStyles(styles,
imports.forEach(function(atRule) {
helpers.try(function transformAtImport() {
readAtImport(
result,
atRule,
options,
cb,
Expand Down Expand Up @@ -205,6 +216,7 @@ function addIgnoredAtRulesOnTop(styles, ignoredAtRules) {
* @param {Object} options
*/
function readAtImport(
result,
atRule,
options,
cb,
Expand Down Expand Up @@ -261,6 +273,7 @@ function readAtImport(
importedFiles[resolvedFilename][media] = true

readImportedContent(
result,
atRule,
parsedAtImport,
clone(options),
Expand All @@ -282,7 +295,9 @@ function readAtImport(
* @param {String} resolvedFilename
* @param {Function} cb
*/
function readImportedContent(atRule,
function readImportedContent(
result,
atRule,
parsedAtImport,
options,
resolvedFilename,
Expand Down Expand Up @@ -310,7 +325,7 @@ function readImportedContent(atRule,
)

if (fileContent.trim() === "") {
console.log(helpers.message(resolvedFilename + " is empty", atRule.source))
result.warn(resolvedFilename + " is empty", {node: atRule})
detach(atRule)
return
}
Expand All @@ -337,6 +352,7 @@ function readImportedContent(atRule,

// recursion: import @import from imported file
parseStyles(
result,
newStyles,
options,
cb,
Expand Down Expand Up @@ -506,3 +522,4 @@ module.exports = postcss.plugin(
"postcss-import",
AtImport
)
module.exports.warnNodesMessage = warnNodesMessage
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-import",
"version": "5.2.2",
"version": "6.0.0",
"description": "PostCSS plugin to import CSS files",
"keywords": [
"css",
Expand All @@ -24,8 +24,8 @@
"dependencies": {
"clone": "^0.1.17",
"glob": "^5.0.1",
"postcss": "^4.0.2",
"object-assign": "^3.0.0",
"postcss": "^4.1.4",
"postcss-message-helpers": "^2.0.0",
"resolve": "^1.0.0",
"string-hash": "^1.1.0"
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,25 @@ test("@import custom resolve", function(t) {

t.end()
})

test("warn when a import doesn't have ;", function(t) {
t.equal(
postcss()
.use(atImport())
.process("@import url('http://') :root{}")
.warnings()[0].text,
atImport.warnNodesMessage,
"should warn when a user didn't close an import with ;"
)

t.equal(
postcss()
.use(atImport())
.process("@import url('http://');")
.warnings().length,
0,
"should not warn when a user closed an import with ;"
)

t.end()
})

0 comments on commit 1987a5e

Please sign in to comment.