Skip to content

Commit

Permalink
Fix report() error message responsibility for a missing node or lin…
Browse files Browse the repository at this point in the history
…e number (#7474)

Including a rule name in the error message helps us understand why an error happens.

Co-authored-by: Richard Hallows <jeddy3@users.noreply.github.com>
  • Loading branch information
ybiquitous and jeddy3 committed Jan 20, 2024
1 parent 34cdc97 commit eebb786
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-maps-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `report()` error message responsibility for a missing node or line number
11 changes: 11 additions & 0 deletions lib/utils/__tests__/report.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,14 @@ test('with custom message function', () => {
expect(spyArgs[0]).toBe('a=str, b=123');
expect(spyArgs[1].node).toBe(v.node);
});

test('without node nor line', () => {
const v = {
ruleName: 'foo',
result: {},
};

expect(() => report(v)).toThrow(
'The "foo" rule failed to pass either a node or a line number to the `report()` function.',
);
});
4 changes: 3 additions & 1 deletion lib/utils/report.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ function report(problem) {
const startLine = line || (start && start.line);

if (!startLine) {
throw new Error('You must pass either a node or a line number');
throw new Error(
`The "${ruleName}" rule failed to pass either a node or a line number to the \`report()\` function.`,
);
}

const { ignoreDisables } = result.stylelint.config || {};
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/report.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export default function report(problem) {
const startLine = line || (start && start.line);

if (!startLine) {
throw new Error('You must pass either a node or a line number');
throw new Error(
`The "${ruleName}" rule failed to pass either a node or a line number to the \`report()\` function.`,
);
}

const { ignoreDisables } = result.stylelint.config || {};
Expand Down

0 comments on commit eebb786

Please sign in to comment.