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

Add more sensitive plugins tests #157

Merged
merged 3 commits into from
Jan 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ postcssImport({
([#147](https://github.com/postcss/postcss-import/pull/147))
- Added: detect css extension in package.json `main` field
([153](https://github.com/postcss/postcss-import/pull/153))
- Changed: `options.plugins` are applied to unprocessed ast before imports detecting
([157](https://github.com/postcss/postcss-import/pull/157))

# 7.1.3 - 2015-11-05

Expand Down
54 changes: 25 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,41 +310,37 @@ function loadImportContent(
return
}

var newStyles = postcss().process(content, {
return processor.process(content, {
from: filename,
syntax: result.opts.syntax,
parser: result.opts.parser,
}).root

if (options.skipDuplicates) {
var hasImport = newStyles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) {
state.hashFiles[content] = {}
})
.then(function(importedResult) {
var styles = importedResult.root
result.messages = result.messages.concat(importedResult.messages)

if (options.skipDuplicates) {
var hasImport = styles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) {
state.hashFiles[content] = {}
}
state.hashFiles[content][media] = true
}
state.hashFiles[content][media] = true
}
}

// recursion: import @import from imported file
return parseStyles(
result,
newStyles,
options,
state,
media,
processor
)
.then(function(statements) {
return processor.process(newStyles)
.then(function(newResult) {
result.messages = result.messages.concat(newResult.messages)

return statements
})
// recursion: import @import from imported file
return parseStyles(
result,
styles,
options,
state,
media,
processor
)
})
})
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/plugins.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
@import "foo-decl";
@import "bar-decl";
@import 'foo/index.css';
@import 'bar.css';
@level-1-1 {}
@level-1-2 {}
9 changes: 3 additions & 6 deletions test/fixtures/plugins.expected.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
body {
baz: baz;
}
body {
qux: qux;
}
foo-converted {}
@level-1-1 {}
@level-1-2 {}
38 changes: 23 additions & 15 deletions test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ import scss from "postcss-scss"
import atImport from ".."
import compareFixtures from "./lib/compare-fixtures"

test("should apply plugins", t => {
test("should apply plugins to root", t => {
const atRules = []
const rules = []
return compareFixtures(t, "plugins", {
plugins: [
postcss.plugin("postcss-no-foo", () => {
return css => {
css.walkDecls("foo", decl => {
decl.remove()
})
}
}),
postcss.plugin("postcss-no-bar", () => {
return css => {
css.walkDecls("bar", decl => {
decl.remove()
})
}
}),
css => {
css.walk(node => {
if (node.type === "rule") {
rules.push(node.selector)
if (node.selector === "bar") {
node.remove()
}
else {
node.selector += "-converted"
}
}
if (node.type === "atrule") {
atRules.push(node.name)
}
})
},
],
})
.then(() => {
t.same(atRules, [ "import" ])
t.same(rules, [ "bar", "foo" ])
})
})

test("should error when value is not an array", t => {
Expand Down