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

use node.error for CSS relevant errors #540

Merged
merged 2 commits into from
Aug 1, 2023
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Type: `Function`
Default: `null`

You can provide a custom path resolver with this option. This function gets
`(id, basedir, importOptions)` arguments and should return a path, an array of
`(id, basedir, importOptions, astNode)` arguments and should return a path, an array of
paths or a promise resolving to the path(s). If you do not return an absolute
path, your path will be resolved to an absolute path using the default
resolver.
Expand Down
10 changes: 6 additions & 4 deletions lib/parse-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async function parseStyles(
else if (
stmt.node.params.toLowerCase() !== charset.node.params.toLowerCase()
) {
throw new Error(
`Incompatable @charset statements:
throw stmt.node.error(
`Incompatible @charset statements:
${stmt.node.params} specified in ${stmt.node.source.input.file}
${charset.node.params} specified in ${charset.node.source.input.file}`
)
Expand Down Expand Up @@ -108,12 +108,14 @@ async function resolveImportId(result, stmt, options, state, postcss) {
? path.dirname(atRule.source.input.file)
: options.root

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

// Ensure that each path is absolute:
const resolved = await Promise.all(
paths.map(file => {
return !path.isAbsolute(file) ? resolveId(file, base, options) : file
return !path.isAbsolute(file)
? resolveId(file, base, options, atRule)
: file
})
)

Expand Down
6 changes: 3 additions & 3 deletions lib/resolve-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function resolveModule(id, opts) {
})
}

module.exports = function resolveId(id, base, options) {
module.exports = function resolveId(id, base, options, node) {
const paths = options.path

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

throw new Error(
throw node.error(
`Failed to find '${id}'
in [
${paths.join(",\n ")}
${paths.join(",\n ")}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent before :

    message: `postcss-import: <css input>:3:1: Failed to find 'layer\`\`\`.css'␊
      in [␊
        /projects/postcss-import,␊
            /projects/postcss-import/test/fixtures/imports␊
      ]`,

Indent now :

    message: `postcss-import: <css input>:3:1: Failed to find 'layer\`\`\`.css'␊
      in [␊
        /projects/postcss-import,␊
        /projects/postcss-import/test/fixtures/imports␊
      ]`,

]`
)
})
Expand Down
4 changes: 2 additions & 2 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test(
"charset-import"
)

test("should error if incompatable @charset statements", t => {
test("should error if incompatible @charset statements", t => {
t.plan(2)
const file = "test/fixtures/charset-error.css"
return postcss()
Expand All @@ -73,7 +73,7 @@ test("should error if incompatable @charset statements", t => {
t.truthy(err)
t.regex(
err.message,
/Incompatable @charset statements:.+specified in.+specified in.+/s
/Incompatible @charset statements:.+specified in.+specified in.+/s
)
})
})
Expand Down
4 changes: 3 additions & 1 deletion test/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ test("should error when value is not a function", t => {
return postcss()
.use(atImport({ nameLayer: "not a function" }))
.process("", { from: undefined })
.catch(error => t.is(error.message, "nameLayer option must be a function"))
.catch(error =>
t.regex(error.message, /nameLayer option must be a function/)
)
})

test("should throw when using anonymous layers without the nameLayer plugin option", t => {
Expand Down
2 changes: 1 addition & 1 deletion test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("should error when value is not an array", t => {
return postcss()
.use(atImport({ plugins: "foo" }))
.process("", { from: undefined })
.catch(error => t.is(error.message, "plugins option must be an array"))
.catch(error => t.regex(error.message, /plugins option must be an array/))
})

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