Skip to content

Commit 3a2e231

Browse files
romainmenkeRyanZim
andauthored
use node.error for CSS relevant errors (#540)
* use node.error for CSS relevant errors * Apply suggestions from code review Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com> --------- Co-authored-by: Ryan Zimmerman <opensrc@ryanzim.com>
1 parent 6757bae commit 3a2e231

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Type: `Function`
164164
Default: `null`
165165

166166
You can provide a custom path resolver with this option. This function gets
167-
`(id, basedir, importOptions)` arguments and should return a path, an array of
167+
`(id, basedir, importOptions, astNode)` arguments and should return a path, an array of
168168
paths or a promise resolving to the path(s). If you do not return an absolute
169169
path, your path will be resolved to an absolute path using the default
170170
resolver.

lib/parse-styles.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ async function parseStyles(
4949
else if (
5050
stmt.node.params.toLowerCase() !== charset.node.params.toLowerCase()
5151
) {
52-
throw new Error(
53-
`Incompatable @charset statements:
52+
throw stmt.node.error(
53+
`Incompatible @charset statements:
5454
${stmt.node.params} specified in ${stmt.node.source.input.file}
5555
${charset.node.params} specified in ${charset.node.source.input.file}`
5656
)
@@ -107,12 +107,14 @@ async function resolveImportId(result, stmt, options, state, postcss) {
107107
? path.dirname(atRule.source.input.file)
108108
: options.root
109109

110-
const paths = [await options.resolve(stmt.uri, base, options)].flat()
110+
const paths = [await options.resolve(stmt.uri, base, options, atRule)].flat()
111111

112112
// Ensure that each path is absolute:
113113
const resolved = await Promise.all(
114114
paths.map(file => {
115-
return !path.isAbsolute(file) ? resolveId(file, base, options) : file
115+
return !path.isAbsolute(file)
116+
? resolveId(file, base, options, atRule)
117+
: file
116118
})
117119
)
118120

lib/resolve-id.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function resolveModule(id, opts) {
1111
})
1212
}
1313

14-
module.exports = function resolveId(id, base, options) {
14+
module.exports = function resolveId(id, base, options, node) {
1515
const paths = options.path
1616

1717
const resolveOpts = {
@@ -32,10 +32,10 @@ module.exports = function resolveId(id, base, options) {
3232
.catch(() => {
3333
if (paths.indexOf(base) === -1) paths.unshift(base)
3434

35-
throw new Error(
35+
throw node.error(
3636
`Failed to find '${id}'
3737
in [
38-
${paths.join(",\n ")}
38+
${paths.join(",\n ")}
3939
]`
4040
)
4141
})

test/import.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test(
6363
"charset-import"
6464
)
6565

66-
test("should error if incompatable @charset statements", t => {
66+
test("should error if incompatible @charset statements", t => {
6767
t.plan(2)
6868
const file = "test/fixtures/charset-error.css"
6969
return postcss()
@@ -73,7 +73,7 @@ test("should error if incompatable @charset statements", t => {
7373
t.truthy(err)
7474
t.regex(
7575
err.message,
76-
/Incompatable @charset statements:.+specified in.+specified in.+/s
76+
/Incompatible @charset statements:.+specified in.+specified in.+/s
7777
)
7878
})
7979
})

test/layer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ test("should error when value is not a function", t => {
4949
return postcss()
5050
.use(atImport({ nameLayer: "not a function" }))
5151
.process("", { from: undefined })
52-
.catch(error => t.is(error.message, "nameLayer option must be a function"))
52+
.catch(error =>
53+
t.regex(error.message, /nameLayer option must be a function/)
54+
)
5355
})
5456

5557
test("should throw when using anonymous layers without the nameLayer plugin option", t => {

test/plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test("should error when value is not an array", t => {
3535
return postcss()
3636
.use(atImport({ plugins: "foo" }))
3737
.process("", { from: undefined })
38-
.catch(error => t.is(error.message, "plugins option must be an array"))
38+
.catch(error => t.regex(error.message, /plugins option must be an array/))
3939
})
4040

4141
test("should remain silent when value is an empty array", t => {

0 commit comments

Comments
 (0)