Skip to content

Commit eebb786

Browse files
ybiquitousjeddy3
andauthored
Fix report() error message responsibility for a missing node or line 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>
1 parent 34cdc97 commit eebb786

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

.changeset/weak-maps-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": patch
3+
---
4+
5+
Fixed: `report()` error message responsibility for a missing node or line number

lib/utils/__tests__/report.test.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,14 @@ test('with custom message function', () => {
419419
expect(spyArgs[0]).toBe('a=str, b=123');
420420
expect(spyArgs[1].node).toBe(v.node);
421421
});
422+
423+
test('without node nor line', () => {
424+
const v = {
425+
ruleName: 'foo',
426+
result: {},
427+
};
428+
429+
expect(() => report(v)).toThrow(
430+
'The "foo" rule failed to pass either a node or a line number to the `report()` function.',
431+
);
432+
});

lib/utils/report.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ function report(problem) {
4747
const startLine = line || (start && start.line);
4848

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

5355
const { ignoreDisables } = result.stylelint.config || {};

lib/utils/report.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export default function report(problem) {
4343
const startLine = line || (start && start.line);
4444

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

4951
const { ignoreDisables } = result.stylelint.config || {};

0 commit comments

Comments
 (0)